Skip to content

Commit

Permalink
Merge pull request #243 from ember-cli/refactor-to-class
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Sep 4, 2020
2 parents d7ee089 + bb7634c commit 8a94c05
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 115 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ npm install --save broccoli-uglify-sourcemap
### usage

```js
var uglify = require('broccoli-uglify-sourcemap');
const Uglify = require('broccoli-uglify-sourcemap');

// basic usage
var uglified = uglify(input);
let uglified = new Uglify(input);

// advanced usage
var uglified = uglify(input, {
let uglified = new Uglify(input, {
exclude: [..], // array of globs, to not minify

uglify: {
Expand All @@ -31,7 +31,6 @@ var uglified = uglify(input, {
publicUrl: 'https://myamazingapp.com/', // value to be prepended to sourceMappingURL, defaults to ''
hiddenSourceMap: false, // skips adding the reference to sourcemap in the minified JS, defaults to false

async: true, // run uglify in parallel, defaults to false
concurrency: 3 // number of parallel workers, defaults to number of CPUs - 1
});
```
Expand Down
156 changes: 73 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ const workerpool = require('workerpool');

const processFile = require('./lib/process-file');

module.exports = UglifyWriter;

UglifyWriter.prototype = Object.create(Plugin.prototype);
UglifyWriter.prototype.constructor = UglifyWriter;

const silent = process.argv.indexOf('--silent') !== -1;

const worker = queue.async.asyncify(doWork => doWork());
Expand All @@ -28,97 +23,92 @@ const MatchNothing = {
},
};

function UglifyWriter(inputNodes, options) {
if (!(this instanceof UglifyWriter)) {
return new UglifyWriter(inputNodes, options);
}
module.exports = class UglifyWriter extends Plugin {
constructor(_inputNodes, options = {}) {
let inputNodes = Array.isArray(_inputNodes) ? _inputNodes : [_inputNodes];

inputNodes = Array.isArray(inputNodes) ? inputNodes : [inputNodes];
super(inputNodes, {
name: options.name,
annotation: options.annotation,
needsCache: false,
});

Plugin.call(this, inputNodes, options);
this.options = defaults(options, {
uglify: {
sourceMap: {},
},
});

this.options = defaults(options, {
uglify: {
sourceMap: {},
},
});
// async prop is deprecated since terser.minify() is async by default
if ('async' in this.options) {
throw new Error('\n Passing `async` property inside `options` is deprecated.');
}

// async prop is deprecated since terser.minify() is async by default
if ('async' in this.options) {
throw new Error('\n Passing `async` property inside `options` is deprecated.');
}
this.concurrency = Number(process.env.JOBS) || this.options.concurrency || Math.max(require('os').cpus().length - 1, 1);

this.concurrency = Number(process.env.JOBS) || this.options.concurrency || Math.max(require('os').cpus().length - 1, 1);
// create a worker pool using an external worker script
this.pool = workerpool.pool(path.join(__dirname, 'lib', 'worker.js'), {
maxWorkers: this.concurrency,
workerType: 'auto',
});

// create a worker pool using an external worker script
this.pool = workerpool.pool(path.join(__dirname, 'lib', 'worker.js'), {
maxWorkers: this.concurrency,
workerType: 'auto',
});
let exclude = this.options.exclude;
if (Array.isArray(exclude)) {
this.excludes = new MatcherCollection(exclude);
} else {
this.excludes = MatchNothing;
}
}

this.inputNodes = inputNodes;
async build() {
let pendingWork = [];

let exclude = this.options.exclude;
if (Array.isArray(exclude)) {
this.excludes = new MatcherCollection(exclude);
} else {
this.excludes = MatchNothing;
}
}

UglifyWriter.prototype.build = async function() {
let writer = this;

let pendingWork = [];

this.inputPaths.forEach(inputPath => {
walkSync(inputPath).forEach(relativePath => {
if (relativePath.slice(-1) === '/') {
return;
}
let inFile = path.join(inputPath, relativePath);
let outFile = path.join(writer.outputPath, relativePath);

mkdirp.sync(path.dirname(outFile));

if (this._isJSExt(relativePath) && !writer.excludes.match(relativePath)) {
// wrap this in a function so it doesn't actually run yet, and can be throttled
let uglifyOperation = function() {
return writer.processFile(inFile, outFile, relativePath, writer.outputPath);
};
pendingWork.push(uglifyOperation);
} else if (relativePath.slice(-4) === '.map') {
if (writer.excludes.match(`${relativePath.slice(0, -4)}.{js,mjs}`)) {
// ensure .map files for excluded JS paths are also copied forward
this.inputPaths.forEach(inputPath => {
walkSync(inputPath).forEach(relativePath => {
if (relativePath.slice(-1) === '/') {
return;
}
let inFile = path.join(inputPath, relativePath);
let outFile = path.join(this.outputPath, relativePath);

mkdirp.sync(path.dirname(outFile));

if (this._isJSExt(relativePath) && !this.excludes.match(relativePath)) {
// wrap this in a function so it doesn't actually run yet, and can be throttled
let uglifyOperation = () => this.processFile(inFile, outFile, relativePath, this.outputPath);
pendingWork.push(uglifyOperation);
} else if (relativePath.slice(-4) === '.map') {
if (this.excludes.match(`${relativePath.slice(0, -4)}.{js,mjs}`)) {
// ensure .map files for excluded JS paths are also copied forward
symlinkOrCopy.sync(inFile, outFile);
}
// skip, because it will get handled when its corresponding JS does
} else {
symlinkOrCopy.sync(inFile, outFile);
}
// skip, because it will get handled when its corresponding JS does
} else {
symlinkOrCopy.sync(inFile, outFile);
}
});
});
});

try {
await queue(worker, pendingWork, writer.concurrency);
return writer.outputPath;
} finally {
// make sure to shut down the workers on both success and error case
writer.pool.terminate();

try {
await queue(worker, pendingWork, this.concurrency);
} finally {
// make sure to shut down the workers on both success and error case
this.pool.terminate();
}
}
};

UglifyWriter.prototype._isJSExt = function(relativePath) {
return relativePath.slice(-3) === '.js' || relativePath.slice(-4) === '.mjs';
};
_isJSExt(relativePath) {
return relativePath.slice(-3) === '.js' || relativePath.slice(-4) === '.mjs';
}

UglifyWriter.prototype.processFile = function(inFile, outFile, relativePath, outDir) {
// don't run this in the workerpool if concurrency is disabled (can set JOBS <= 1)
if (this.concurrency > 1) {
debug('running in workerpool, concurrency=%d', this.concurrency);
// each of these arguments is a string, which can be sent to the worker process as-is
return this.pool.exec('processFileParallel', [inFile, outFile, relativePath, outDir, silent, this.options]);
processFile(inFile, outFile, relativePath, outDir) {
// don't run this in the workerpool if concurrency is disabled (can set JOBS <= 1)
if (this.concurrency > 1) {
debug('running in workerpool, concurrency=%d', this.concurrency);
// each of these arguments is a string, which can be sent to the worker process as-is
return this.pool.exec('processFileParallel', [inFile, outFile, relativePath, outDir, silent, this.options]);
}
debug('not running in workerpool');
return processFile(inFile, outFile, relativePath, outDir, silent, this.options);
}
debug('not running in workerpool');
return processFile(inFile, outFile, relativePath, outDir, silent, this.options);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"async-promise-queue": "^1.0.5",
"broccoli-plugin": "^1.2.1",
"broccoli-plugin": "^4.0.3",
"debug": "^4.1.0",
"lodash.defaultsdeep": "^4.6.1",
"matcher-collection": "^2.0.1",
Expand Down
5 changes: 1 addition & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

const Uglify = require('..');
const path = require('path');
const helpers = require('broccoli-test-helper');
const { createTempDir, createBuilder } = require('broccoli-test-helper');

const fixtures = path.join(__dirname, 'fixtures');
const fixturesError = path.join(__dirname, 'fixtures-error');

const createTempDir = helpers.createTempDir;
const createBuilder = helpers.createBuilder;

describe('broccoli-uglify-sourcemap', function() {
let input, builder;

Expand Down
Loading

0 comments on commit 8a94c05

Please sign in to comment.