Skip to content

Commit

Permalink
v5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bplok20010 committed Mar 1, 2020
1 parent c1d135a commit ecb7298
Show file tree
Hide file tree
Showing 12 changed files with 9,951 additions and 10,967 deletions.
18 changes: 18 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "auto",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"printWidth": 100,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": false,
"tabWidth": 4,
"trailingComma": "es5",
"useTabs": true
}
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@

```js
module.exports = function(options, state) {
return {
babel: {
//...babel options
decoratorsBeforeExport: true,
strictMode: true,
useFlow: true,
loose: true,
runtimeOptions: {},
presets: [],
plugins: []
},
eslint: {},
postcss: {},
//compilerOptions
typescript: {}
};
return {
babel: {
//...babel options
decoratorsBeforeExport: true,
strictMode: true,
useFlow: true,
loose: true,
runtimeOptions: {},
presets: [],
plugins: [],
},
eslint: {},
postcss: {},
//compilerOptions
typescript: {},
};
};
```

Expand Down Expand Up @@ -76,8 +76,6 @@ transformEs( src, dest, options );
},
eslint: {},
postcss: {},
//compilerOptions
typescript: {}
}
```

Expand Down
149 changes: 68 additions & 81 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,61 @@ const log = require("./logger");
const success = chalk.keyword("green");

program
.version(pkg.version)
.option("-w, --watch", "是否监控文件改变", false)
.option("-d, --outDir [outDir]", "输出到指定目录,默认为 lib", "lib")
.option("-c, --clean", "转换前清空输出目录", false)
.option(
"-t, --target [target]",
"转换目标格式:node|web|esm|cjs|commonjs|amd|umd|systemjs|auto 默认为 web",
/^node|web|esm|cjs|commonjs|amd|umd|systemjs|auto$/,
"web"
)
.option("--config [config]", "配置文件路径", "./transform-es.config.js")
.option("--state [state]", "配置文件路径", "")
.parse(process.argv);
.version(pkg.version)
.option("-w, --watch", "是否监控文件改变", false)
.option("-d, --outDir [outDir]", "输出到指定目录,默认为 lib", "lib")
.option("-c, --clean", "转换前清空输出目录", false)
.option(
"-t, --target [target]",
"转换目标格式:node|web|esm|cjs|commonjs|amd|umd|systemjs|auto 默认为 web",
/^node|web|esm|cjs|commonjs|amd|umd|systemjs|auto$/,
"web"
)
.option("--config [config]", "配置文件路径", "./transform-es.config.js")
.option("--state [state]", "配置文件路径", "")
.parse(process.argv);

const options = {
configFile: program.config,
mode: program.mode,
state: program.state,
watch: !!program.watch,
clean: !!program.clean
configFile: program.config,
mode: program.mode,
state: program.state,
watch: !!program.watch,
clean: !!program.clean,
};

if (program.target === "node") {
options.babel = {
targets: {
node: "current"
}
};
options.babel = {
targets: {
node: "current",
},
};
} else if (program.target === "esm") {
options.babel = {
modules: false
};
options.babel = {
modules: false,
};
} else {
options.babel = {
modules: program.target === "web" ? "commonjs" : program.target
};
options.babel = {
modules: program.target === "web" ? "commonjs" : program.target,
};
}

const exec = program.args[0];

if (exec === "init") {
const pkgFile = process.cwd() + "/package.json";
let userPkg = {};
if (fs.existsSync(pkgFile)) {
userPkg = require(pkgFile);
}
const pkgFile = process.cwd() + "/package.json";
let userPkg = {};
if (fs.existsSync(pkgFile)) {
userPkg = require(pkgFile);
}

const scripts = userPkg.scripts || {};
const scripts = userPkg.scripts || {};

userPkg.scripts = scripts;
userPkg.scripts = scripts;

if (!fs.existsSync(process.cwd() + "/transform-es.config.js")) {
fs.writeFileSync(
path.resolve(process.cwd(), "transform-es.config.js"),
`
if (!fs.existsSync(process.cwd() + "/transform-es.config.js")) {
fs.writeFileSync(
path.resolve(process.cwd(), "transform-es.config.js"),
`
module.exports = function(options, state){
return {
babel: {
Expand All @@ -76,50 +76,37 @@ module.exports = function(options, state){
plugins: []
},
postcss: {},
typescript: {}
};
}
`
);

log(success(`add transform-es.config.js successfully.`));
}

if (!scripts["dest"]) {
scripts["dest"] = "transform-es ./src -t web -d lib -w -c";
log(success(`add scripts.dest = ${scripts["dest"]} successfully.`));
}

if (!scripts["dest:esm"]) {
scripts["dest:esm"] = "transform-es ./src -t esm -d esm -c";
log(
success(
`add scripts["dest:esm"] = ${scripts["dest:esm"]} successfully.`
)
);
}

if (!scripts["dest:cjs"]) {
scripts["dest:cjs"] = "transform-es ./src -t web -d cjs -c";
log(
success(
`add scripts["dest:cjs"] = ${scripts["dest:cjs"]} successfully.`
)
);
}

if (!scripts["dest:node"]) {
scripts["dest:node"] = "transform-es ./src -t node -d lib -c";
log(
success(
`add scripts["dest:node"] = ${scripts["dest:node"]} successfully.`
)
);
}

fs.writeFileSync(pkgFile, JSON.stringify(userPkg, null, 2) + os.EOL);

process.exit(1);
);

log(success(`add transform-es.config.js successfully.`));
}

if (!scripts["dest"]) {
scripts["dest"] = "transform-es ./src -t web -d lib -w -c";
log(success(`add scripts.dest = ${scripts["dest"]} successfully.`));
}

if (!scripts["dest:esm"]) {
scripts["dest:esm"] = "transform-es ./src -t esm -d esm -c";
log(success(`add scripts["dest:esm"] = ${scripts["dest:esm"]} successfully.`));
}

if (!scripts["dest:cjs"]) {
scripts["dest:cjs"] = "transform-es ./src -t web -d cjs -c";
log(success(`add scripts["dest:cjs"] = ${scripts["dest:cjs"]} successfully.`));
}

if (!scripts["dest:node"]) {
scripts["dest:node"] = "transform-es ./src -t node -d lib -c";
log(success(`add scripts["dest:node"] = ${scripts["dest:node"]} successfully.`));
}

fs.writeFileSync(pkgFile, JSON.stringify(userPkg, null, 2) + os.EOL);

process.exit(1);
}

let src = "src";
Expand Down
116 changes: 78 additions & 38 deletions lib/compile.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,88 @@
const gulp = require("gulp");
const path = require("path");
const execSync = require("child_process").execSync;
const fs = require("fs-extra");
const { isDir } = require("./utils");
const babelLoader = require("./loaders/babel");
const eslintLoader = require("./loaders/eslint");
const tsLoader = require("./loaders/typescript");
const cssLoader = require("./loaders/css");
const log = require("./logger");

let hasInstall = false;

function hasInstallDeps() {
if (hasInstall) return true;

const pkgFile = process.cwd() + "/package.json";
let pkg = {};
if (fs.existsSync(pkgFile)) {
pkg = require(pkgFile);
}
const allDeps = Object.assign({}, pkg.dependencies, pkg.devDependencies);

hasInstall = "gulp-sass" in allDeps;

return hasInstall;
}

function installDeps() {
if (hasInstall) return;
hasInstall = true;

log("安装依赖 gulp-sass ...");
cmd = `npm install --save-dev gulp-sass@4.0.2`;
try {
execSync(cmd);
} catch (e) {
log("依赖安装失败,请自行安装 gulp-sass");
process.exit(0);
}
log("安装完成");
}

function checkFileIfNeedInstall(file) {
return /\.scss$/.test(file);
}

module.exports = async function compile(file, options) {
const ignore =
options.ignore ||
function() {
return false;
};
const absPath = path.resolve(options.appSrc, file);
const outFile = path.resolve(options.outDir, file);
let outDir = options.outDir;

if (isDir(absPath)) {
await fs.ensureDir(outFile);
return Promise.resolve(file);
}

outDir = path.dirname(outFile);

return new Promise(resolve => {
let stream = gulp.src(absPath, {
cwd: options.appSrc,
allowEmpty: true
});

if (!ignore(file)) {
stream = eslintLoader(stream, options);
stream = cssLoader(stream, options);
stream = tsLoader(stream, options);
stream = babelLoader(stream, options);
}
stream = stream.pipe(gulp.dest(outDir));

stream.on("finish", () => {
resolve(file);
});
stream.on("error", () => {
resolve(file);
});
});
const ignore =
options.ignore ||
function() {
return false;
};
const absPath = path.resolve(options.appSrc, file);
const outFile = path.resolve(options.outDir, file);
let outDir = options.outDir;

if (isDir(absPath)) {
await fs.ensureDir(outFile);
return Promise.resolve(file);
}

if (checkFileIfNeedInstall(file) && !hasInstallDeps()) {
installDeps();
}

outDir = path.dirname(outFile);

return new Promise(resolve => {
let stream = gulp.src(absPath, {
cwd: options.appSrc,
allowEmpty: true,
});

if (!ignore(file)) {
stream = eslintLoader(stream, options);
stream = cssLoader(stream, options);
stream = babelLoader(stream, options);
}
stream = stream.pipe(gulp.dest(outDir));

stream.on("finish", () => {
resolve(file);
});
stream.on("error", () => {
resolve(file);
});
});
};
Loading

0 comments on commit ecb7298

Please sign in to comment.