From 1ace4188eaf748a4b5dba4537c4b4dee2e6b861d Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 11 Feb 2020 19:32:30 +0800 Subject: [PATCH] fix: executing midway-bin build at arbitrary directory (#384) --- packages/midway-bin/lib/cmd/build.js | 50 ++++++++++++++++------------ packages/midway-bin/package.json | 2 +- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/packages/midway-bin/lib/cmd/build.js b/packages/midway-bin/lib/cmd/build.js index 699e0a5efd39..84797ad23791 100644 --- a/packages/midway-bin/lib/cmd/build.js +++ b/packages/midway-bin/lib/cmd/build.js @@ -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]/; @@ -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; @@ -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, @@ -118,7 +127,6 @@ class BuildCommand extends Command { return; } - outDir = outDir || path.resolve('dist'); fse.mkdirpSync(outDir); let basename = path.basename(entry, '.ts'); @@ -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 ); } } @@ -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]; } } } diff --git a/packages/midway-bin/package.json b/packages/midway-bin/package.json index fb8668d09dc7..c27a8c662073 100644 --- a/packages/midway-bin/package.json +++ b/packages/midway-bin/package.json @@ -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",