Skip to content

Commit

Permalink
feat(build): remove support for multi-dist compilation
Browse files Browse the repository at this point in the history
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š <mbajtoss@gmail.com>
  • Loading branch information
bajtos committed Jun 10, 2019
1 parent f8f078f commit f6fcfe7
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 157 deletions.
3 changes: 0 additions & 3 deletions packages/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
49 changes: 14 additions & 35 deletions packages/build/bin/compile-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
' ',
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions packages/build/bin/run-clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 1 addition & 3 deletions packages/build/bin/run-mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
39 changes: 0 additions & 39 deletions packages/build/bin/select-dist.js

This file was deleted.

42 changes: 0 additions & 42 deletions packages/build/bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -228,8 +188,6 @@ function mochaConfiguredForProject() {
});
}

exports.getCompilationTarget = getCompilationTarget;
exports.getDistribution = getDistribution;
exports.getRootDir = getRootDir;
exports.getPackageDir = getPackageDir;
exports.getConfigFile = getConfigFile;
Expand Down
1 change: 0 additions & 1 deletion packages/build/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion packages/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
1 change: 0 additions & 1 deletion packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
33 changes: 4 additions & 29 deletions packages/build/test/integration/scripts.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down Expand Up @@ -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 + ')',
);
});

Expand Down

0 comments on commit f6fcfe7

Please sign in to comment.