-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Bring in terser-webpack-plugin (backport #6231 to master) * Use correct path for ignore * Comment out schema
- Loading branch information
1 parent
ab46c45
commit 155423f
Showing
9 changed files
with
937 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright JS Foundation and other contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
106 changes: 106 additions & 0 deletions
106
build/webpack/plugins/terser-webpack-plugin/src/TaskRunner.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import os from 'os'; | ||
|
||
import cacache from 'cacache'; | ||
import findCacheDir from 'find-cache-dir'; | ||
import workerFarm from 'worker-farm'; | ||
import serialize from 'serialize-javascript'; | ||
|
||
import minify from './minify'; | ||
|
||
const worker = require.resolve('./worker'); | ||
|
||
export default class TaskRunner { | ||
constructor(options = {}) { | ||
const { cache, parallel } = options; | ||
this.cacheDir = | ||
cache === true ? findCacheDir({ name: 'terser-webpack-plugin' }) : cache; | ||
// In some cases cpus() returns undefined | ||
// https://github.com/nodejs/node/issues/19022 | ||
const cpus = os.cpus() || { length: 1 }; | ||
this.maxConcurrentWorkers = | ||
parallel === true | ||
? cpus.length - 1 | ||
: Math.min(Number(parallel) || 0, cpus.length - 1); | ||
} | ||
|
||
run(tasks, callback) { | ||
/* istanbul ignore if */ | ||
if (!tasks.length) { | ||
callback(null, []); | ||
return; | ||
} | ||
|
||
if (this.maxConcurrentWorkers > 1) { | ||
const workerOptions = | ||
process.platform === 'win32' | ||
? { | ||
maxConcurrentWorkers: this.maxConcurrentWorkers, | ||
maxConcurrentCallsPerWorker: 1, | ||
} | ||
: { maxConcurrentWorkers: this.maxConcurrentWorkers }; | ||
this.workers = workerFarm(workerOptions, worker); | ||
this.boundWorkers = (options, cb) => { | ||
try { | ||
this.workers(serialize(options), cb); | ||
} catch (error) { | ||
// worker-farm can fail with ENOMEM or something else | ||
cb(error); | ||
} | ||
}; | ||
} else { | ||
this.boundWorkers = (options, cb) => { | ||
try { | ||
cb(null, minify(options)); | ||
} catch (error) { | ||
cb(error); | ||
} | ||
}; | ||
} | ||
|
||
let toRun = tasks.length; | ||
const results = []; | ||
const step = (index, data) => { | ||
toRun -= 1; | ||
results[index] = data; | ||
|
||
if (!toRun) { | ||
callback(null, results); | ||
} | ||
}; | ||
|
||
tasks.forEach((task, index) => { | ||
const enqueue = () => { | ||
this.boundWorkers(task, (error, data) => { | ||
const result = error ? { error } : data; | ||
const done = () => step(index, result); | ||
|
||
if (this.cacheDir && !result.error) { | ||
cacache | ||
.put( | ||
this.cacheDir, | ||
serialize(task.cacheKeys), | ||
JSON.stringify(data) | ||
) | ||
.then(done, done); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
}; | ||
|
||
if (this.cacheDir) { | ||
cacache | ||
.get(this.cacheDir, serialize(task.cacheKeys)) | ||
.then(({ data }) => step(index, JSON.parse(data)), enqueue); | ||
} else { | ||
enqueue(); | ||
} | ||
}); | ||
} | ||
|
||
exit() { | ||
if (this.workers) { | ||
workerFarm.end(this.workers); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const plugin = require('./index'); | ||
|
||
module.exports = plugin.default; |
Oops, something went wrong.