From f6fcfe7caf12e5fd5375e012f1c014d7e2efd079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 7 Jun 2019 14:16:58 +0200 Subject: [PATCH] feat(build): remove support for multi-dist compilation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify `lb-tsc` to always use `outDir` and `target` as configured via `tsconfig.json` or CLI arguments. Simplify `lb-clean` to always require list of file globs to delete. Simplify `lb-mocha`, remove code converting `DIST` to the outDir based on compilation target. Remove already deprecated `lb-dist` command. BREAKING CHANGE: We are no longer choosing outDir for you, you have to specify it explicitly. It is no longer possible to specify compilation target via non-option argument like `lb-tsc es2017`. Migration guide: - Modify your `tsconfig.json` file and configure `dist` via `compilerOptions.outDir` - If you are using target different from `es2017`, then configure it via `compilerOptions.target`. - Remove `es2017` and `--outDir dist` from lb-tsc arguments. - Ensure that the output directory is listed in `lb-clean` arguments, e.g. call `lb-clean dist`. - When calling `lb-mocha`, replace `DIST` with the actual outDir value, typically `dist`. Signed-off-by: Miroslav Bajtoš --- packages/build/README.md | 3 -- packages/build/bin/compile-package.js | 49 ++++++------------- packages/build/bin/run-clean.js | 6 +-- packages/build/bin/run-mocha.js | 4 +- packages/build/bin/select-dist.js | 39 --------------- packages/build/bin/utils.js | 42 ---------------- packages/build/docs.json | 1 - packages/build/index.js | 1 - packages/build/package.json | 1 - .../test/integration/scripts.integration.js | 33 ++----------- 10 files changed, 22 insertions(+), 157 deletions(-) delete mode 100755 packages/build/bin/select-dist.js diff --git a/packages/build/README.md b/packages/build/README.md index 08852b17b574..acd0a2243076 100644 --- a/packages/build/README.md +++ b/packages/build/README.md @@ -11,9 +11,6 @@ LoopBack 4 or other TypeScript modules, including: - lb-prettier: Run [`prettier`](https://github.com/prettier/prettier) - lb-mocha: Run [`mocha`](https://mochajs.org/) to execute test cases - lb-nyc: Run [`nyc`](https://github.com/istanbuljs/nyc) -- lb-dist: Detect the correct distribution target: `dist` => ES2017, `dist6` => - ES2015. The command is deprecated as `lb-mocha` detects the distribution - target now. These scripts first try to locate the CLI from target project dependencies and fall back to bundled ones in `@loopback/build`. diff --git a/packages/build/bin/compile-package.js b/packages/build/bin/compile-package.js index dabd0d22a634..fb4aa598dec2 100755 --- a/packages/build/bin/compile-package.js +++ b/packages/build/bin/compile-package.js @@ -43,47 +43,24 @@ function run(argv, options) { '--copy-resources', ); - let target; - // --copy-resources is not a TS Compiler option so we remove it from the // list of compiler options to avoid compiler errors. if (isCopyResourcesSet) { compilerOpts.splice(compilerOpts.indexOf('--copy-resources'), 1); } - if (!isTargetSet) { - // Find the last non-option argument as the `target` - // For example `-p tsconfig.json es2017` or `es2017 -p tsconfig.json` - for (let i = compilerOpts.length - 1; i >= 0; i--) { - target = compilerOpts[i]; - // It's an option - if (target.indexOf('-') === 0) { - target = undefined; - continue; - } - // It's the value of an option - if (i >= 1 && compilerOpts[i - 1].indexOf('-') === 0) { - target = undefined; - continue; - } - // Remove the non-option - compilerOpts.splice(i, 1); - break; - } - - if (!target) { - target = utils.getCompilationTarget(); - } + let target; + if (isTargetSet) { + const targetIx = compilerOpts.indexOf('--target'); + target = compilerOpts[targetIx + 1]; + compilerOpts.splice(targetIx, 2); } let outDir; - if (isOutDirSet) { const outDirIx = compilerOpts.indexOf('--outDir'); outDir = path.resolve(process.cwd(), compilerOpts[outDirIx + 1]); compilerOpts.splice(outDirIx, 2); - } else { - outDir = path.join(packageDir, utils.getDistribution(target)); } let tsConfigFile; @@ -109,13 +86,11 @@ function run(argv, options) { JSON.stringify( { extends: baseConfigFile, - include: ['src', 'test'], - exclude: [ - 'node_modules/**', - 'packages/*/node_modules/**', - 'examples/*/node_modules/**', - '**/*.d.ts', - ], + compilerOptions: { + outDir: 'dist', + rootDir: 'src', + }, + include: ['src'], }, null, ' ', @@ -136,6 +111,10 @@ function run(argv, options) { args.push('--outDir', path.relative(cwd, outDir)); } + if (target) { + args.push('--target', target); + } + if (isCopyResourcesSet) { // Since outDir is set, ts files are compiled into that directory. // If copy-resources flag is passed, copy resources (non-ts files) diff --git a/packages/build/bin/run-clean.js b/packages/build/bin/run-clean.js index 6f06a9522ff8..92688e7c8aef 100755 --- a/packages/build/bin/run-clean.js +++ b/packages/build/bin/run-clean.js @@ -26,11 +26,11 @@ Example usage: function run(argv, options) { const rimraf = require('rimraf'); const path = require('path'); - const utils = require('./utils'); - let globPatterns = argv.slice(2); + const globPatterns = argv.slice(2); const removed = []; if (!globPatterns.length) { - globPatterns = [utils.getDistribution()]; + console.error('Please specify file patterns to remove.'); + process.exit(1); } // Keep it backward compatible as dryRun if (typeof options === 'boolean') options = {dryRun: options}; diff --git a/packages/build/bin/run-mocha.js b/packages/build/bin/run-mocha.js index a1061dc25b06..f1deaf6b3661 100755 --- a/packages/build/bin/run-mocha.js +++ b/packages/build/bin/run-mocha.js @@ -18,9 +18,7 @@ Usage: function run(argv, options) { const utils = require('./utils'); - // Substitute the dist variable with the dist folder - const dist = utils.getDistribution(); - const mochaOpts = argv.slice(2).map(a => a.replace(/\bDIST\b/g, dist)); + const mochaOpts = argv.slice(2); const setMochaOpts = !utils.isOptionSet( diff --git a/packages/build/bin/select-dist.js b/packages/build/bin/select-dist.js deleted file mode 100755 index 3f5d502cfeac..000000000000 --- a/packages/build/bin/select-dist.js +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env node -// Copyright IBM Corp. 2017,2019. All Rights Reserved. -// Node module: @loopback/build -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -/* -======== - -Usage: - node ./bin/select-dist command [arguments...] - -The script will scan all arguments (including the command) and replace -the string dist with either dist or dist6, depending on the current -Node.js version. - -Then the provided command is executed with the modified arguments. - -Example usage: - - node ./bin/select-dist mocha dist/__tests__ - -======== -*/ - -'use strict'; - -function run(argv, options) { - const utils = require('./utils'); - const dist = utils.getDistribution(); - - const args = argv.slice(2).map(a => a.replace(/\bDIST\b/g, dist)); - const command = args.shift(); - - return utils.runShell(command, args, options); -} - -module.exports = run; -if (require.main === module) run(process.argv); diff --git a/packages/build/bin/utils.js b/packages/build/bin/utils.js index 5b946ac288f0..ed66271e3e0c 100644 --- a/packages/build/bin/utils.js +++ b/packages/build/bin/utils.js @@ -11,46 +11,6 @@ const path = require('path'); const spawn = require('cross-spawn'); const debug = require('debug')('loopback:build'); -/** - * Get the Node.js compilation target - es2015, es2017 or es2018 - */ -function getCompilationTarget() { - const nodeMajorVersion = +process.versions.node.split('.')[0]; - return nodeMajorVersion >= 10 - ? 'es2018' - : nodeMajorVersion >= 7 - ? 'es2017' - : 'es2015'; -} - -/** - * Get the distribution name - * @param {*} target - */ -function getDistribution(target) { - if (!target) { - target = getCompilationTarget(); - } - let dist; - switch (target) { - case 'es2018': - dist = 'dist10'; - break; - case 'es2017': - dist = 'dist8'; - break; - case 'es2015': - dist = 'dist6'; - break; - default: - console.error( - 'Unknown build target %s. Supported values: es2015, es2017, es2018', - ); - process.exit(1); - } - return dist; -} - /** * Get the root directory of this module */ @@ -228,8 +188,6 @@ function mochaConfiguredForProject() { }); } -exports.getCompilationTarget = getCompilationTarget; -exports.getDistribution = getDistribution; exports.getRootDir = getRootDir; exports.getPackageDir = getPackageDir; exports.getConfigFile = getConfigFile; diff --git a/packages/build/docs.json b/packages/build/docs.json index a2e6cdbe7fab..248878672d85 100644 --- a/packages/build/docs.json +++ b/packages/build/docs.json @@ -7,7 +7,6 @@ "bin/run-mocha.js", "bin/run-prettier.js", "bin/run-eslint.js", - "bin/select-dist.js", "bin/utils.js" ], "codeSectionDepth": 4 diff --git a/packages/build/index.js b/packages/build/index.js index 8c1c4abd4af6..8b6133f224a0 100644 --- a/packages/build/index.js +++ b/packages/build/index.js @@ -8,7 +8,6 @@ exports.tsc = require('./bin/compile-package'); exports.prettier = require('./bin/run-prettier'); exports.nyc = require('./bin/run-nyc'); -exports.dist = require('./bin/select-dist'); exports.mocha = require('./bin/run-mocha'); exports.clean = require('./bin/run-clean'); diff --git a/packages/build/package.json b/packages/build/package.json index 232e458292f0..aff12f7a3689 100644 --- a/packages/build/package.json +++ b/packages/build/package.json @@ -40,7 +40,6 @@ "lb-prettier": "./bin/run-prettier.js", "lb-mocha": "./bin/run-mocha.js", "lb-nyc": "./bin/run-nyc.js", - "lb-dist": "./bin/select-dist.js", "lb-clean": "./bin/run-clean.js" }, "scripts": { diff --git a/packages/build/test/integration/scripts.integration.js b/packages/build/test/integration/scripts.integration.js index 6be675fb5641..c619c6e68ac8 100644 --- a/packages/build/test/integration/scripts.integration.js +++ b/packages/build/test/integration/scripts.integration.js @@ -38,12 +38,12 @@ describe('build', function() { it('compiles ts files', done => { const run = require('../../bin/compile-package'); - const childProcess = run(['node', 'bin/compile-package', 'es2015']); + const childProcess = run(['node', 'bin/compile-package']); childProcess.on('close', code => { assert.equal(code, 0); assert( - fs.existsSync(path.join(projectDir, 'dist6')), - 'dist6 should have been created', + fs.existsSync(path.join(projectDir, 'dist')), + 'dist should have been created', ); assert( fs.existsSync(path.join(projectDir, 'tsconfig.json')), @@ -120,32 +120,7 @@ describe('build', function() { ); assert( command.indexOf('--target es2015') !== -1, - '--target should be honored', - ); - }); - - it('honors no-option as target for tsc', () => { - const run = require('../../bin/compile-package'); - const command = run(['node', 'bin/compile-package', 'es2015'], true); - assert( - command.indexOf('--target es2015') !== -1, - '--target should be honored', - ); - }); - - it('honors no-option as target with -p for tsc', () => { - const run = require('../../bin/compile-package'); - const command = run( - ['node', 'bin/compile-package', 'es2015', '-p', 'tsconfig.my.json'], - true, - ); - assert( - command.indexOf('--target es2015') !== -1, - '--target should be honored', - ); - assert( - command.indexOf('-p tsconfig.my.json') !== -1, - '-p should be honored', + '--target should be honored (actual command: ' + command + ')', ); });