Skip to content

Commit

Permalink
fix: executing midway-bin build at arbitrary directory (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas authored Feb 11, 2020
1 parent 4b9461d commit 1ace418
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
50 changes: 29 additions & 21 deletions packages/midway-bin/lib/cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require('fs');
const rimraf = require('mz-modules/rimraf');
const fse = require('fs-extra');
const globby = require('globby');
const ncc = require('@zeit/ncc');
const ncc = require('@midwayjs/ncc');

const shebangRegEx = /^#![^\n\r]*[\r\n]/;

Expand Down Expand Up @@ -53,26 +53,35 @@ class BuildCommand extends Command {
const { cwd, argv } = context;

const tscCli = require.resolve('typescript/bin/tsc');
if (!fs.existsSync(path.join(cwd, argv.project))) {
const projectFile = path.join(cwd, argv.project);
if (!fs.existsSync(projectFile)) {
console.log(`[midway-bin] tsconfig.json not found in ${cwd}\n`);
return;
}
const tsConfig = require(path.join(cwd, argv.project));
const outDir = this.inferCompilerOptions(tsConfig, 'outDir');
const tsConfig = require(projectFile);
const projectDir = this.projectDir = path.dirname(projectFile);
let outDir = this.inferCompilerOptions(tsConfig, 'outDir');
let outDirAbsolute;
if (outDir) {
outDir = path.resolve(projectDir, outDir);
outDir = path.relative(projectDir, outDir);
outDirAbsolute = path.join(projectDir, outDir);
}

if (argv.clean) {
await this.cleanDir(outDir);
await this.cleanDir(outDirAbsolute);
}

await this.copyFiles(cwd, outDir, argv);
await this.copyFiles(projectDir, outDir, argv);

if (argv.mode !== 'release') {
argv.mode = 'debug';
}
if (argv.entrypoint) {
outDir = outDirAbsolute || path.resolve(projectDir, 'dist');
await this.bundle(path.resolve(cwd, argv.entrypoint), outDir,
{
sourceMap: this.inferCompilerOptions(tsConfig, 'sourceMap'),
sourceMap: this.inferCompilerOptions(tsConfig, 'sourceMap', { projectDir }),
mode: argv.mode,
});
return;
Expand Down Expand Up @@ -100,7 +109,7 @@ class BuildCommand extends Command {
minify: false,
sourceMap,
// default treats sources as output-relative
sourceMapBasePrefix: path.relative(outDir, path.dirname(entry)) + '/',
sourceMapBasePrefix: path.relative(outDir, process.cwd()) + '/',
// Node.js v12 comes with builtin source map support.
sourceMapRegister: false,
watch: false,
Expand All @@ -118,7 +127,6 @@ class BuildCommand extends Command {
return;
}

outDir = outDir || path.resolve('dist');
fse.mkdirpSync(outDir);

let basename = path.basename(entry, '.ts');
Expand Down Expand Up @@ -146,26 +154,26 @@ class BuildCommand extends Command {
}
}

async copyFiles(cwd, outDir, argv) {
if (outDir && fs.existsSync(path.join(cwd, 'package.json'))) {
const pkg = require(path.join(cwd, 'package.json'));
async copyFiles(from, outDir, argv) {
if (outDir && fs.existsSync(path.join(from, 'package.json'))) {
const pkg = require(path.join(from, 'package.json'));
if (pkg['midway-bin-build'] && pkg['midway-bin-build'].include) {
for (const file of pkg['midway-bin-build'].include) {
if (typeof file === 'string' && !/\*/.test(file)) {
const srcDir = path.join(argv.srcDir, file);
const targetDir = path.join(outDir, file);
// 目录,或者不含通配符的普通文件
this.copyFile(srcDir, targetDir, cwd);
this.copyFile(srcDir, targetDir, from);
} else {
// 通配符的情况
const paths = await globby([].concat(file), {
cwd: path.join(cwd, argv.srcDir),
cwd: path.join(from, argv.srcDir),
});
for (const p of paths) {
this.copyFile(
path.join(argv.srcDir, p),
path.join(outDir, p),
cwd
from
);
}
}
Expand All @@ -174,29 +182,29 @@ class BuildCommand extends Command {
}
}

copyFile(srcFile, targetFile, cwd) {
copyFile(srcFile, targetFile, from) {
if (!fs.existsSync(srcFile)) {
console.warn(`[midway-bin] can't find ${srcFile} and skip it`);
} else {
fse.copySync(path.join(cwd, srcFile), path.join(cwd, targetFile));
fse.copySync(path.join(from, srcFile), path.join(from, targetFile));
console.log(`[midway-bin] copy ${srcFile} to ${targetFile} success!`);
}
}

inferCompilerOptions(tsConfig, option) {
inferCompilerOptions(tsConfig, optionKeyPath, { projectDir = process.cwd() } = {}) {
// if projectFile extended and without the option,
// get setting from its parent
if (tsConfig && tsConfig.extends) {
if (
!tsConfig.compilerOptions ||
(tsConfig.compilerOptions && !tsConfig.compilerOptions[option])
(tsConfig.compilerOptions && !tsConfig.compilerOptions[optionKeyPath])
) {
return this.inferCompilerOptions(require(path.join(process.cwd(), tsConfig.extends)), option);
return this.inferCompilerOptions(require(path.join(projectDir, tsConfig.extends)), optionKeyPath, { projectDir });
}
}

if (tsConfig && tsConfig.compilerOptions) {
return tsConfig.compilerOptions[option];
return tsConfig.compilerOptions[optionKeyPath];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/midway-bin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"license": "MIT",
"dependencies": {
"@zeit/ncc": "^0.20.5",
"@midwayjs/ncc": "^0.22.0",
"co": "^4.6.0",
"egg-bin": "^4.11.1",
"fs-extra": "^8.1.0",
Expand Down

0 comments on commit 1ace418

Please sign in to comment.