-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run uglify in parallel, using a workerpool #63
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,77 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('broccoli-uglify-sourcemap'); | ||
var defaults = require('lodash.defaultsdeep'); | ||
var fs = require('fs'); | ||
var mkdirp = require('mkdirp'); | ||
var path = require('path'); | ||
var srcURL = require('source-map-url'); | ||
|
||
var Promise = require('rsvp').Promise; | ||
var UglifyJS = require('uglify-es'); | ||
|
||
|
||
module.exports = function processFile(inFile, outFile, relativePath, outDir, silent, _options) { | ||
var src = fs.readFileSync(inFile, 'utf-8'); | ||
var mapName = path.basename(outFile).replace(/\.js$/,'') + '.map'; | ||
|
||
var mapDir; | ||
if (_options.sourceMapDir) { | ||
mapDir = path.join(outDir, _options.sourceMapDir); | ||
} else { | ||
mapDir = path.dirname(path.join(outDir, relativePath)); | ||
} | ||
|
||
let options = defaults({}, _options.uglify); | ||
if (options.sourceMap) { | ||
let filename = path.basename(inFile); | ||
let url = _options.sourceMapDir ? '/' + path.join(_options.sourceMapDir, mapName) : mapName; | ||
|
||
let sourceMap = { filename, url }; | ||
|
||
if (srcURL.existsIn(src)) { | ||
let url = srcURL.getFrom(src); | ||
let sourceMapPath = path.join(path.dirname(inFile), url); | ||
if (fs.existsSync(sourceMapPath)) { | ||
sourceMap.content = JSON.parse(fs.readFileSync(sourceMapPath)); | ||
} else if (!silent) { | ||
console.warn(`[WARN] (broccoli-uglify-sourcemap) "${url}" referenced in "${relativePath}" could not be found`); | ||
} | ||
} | ||
|
||
options = defaults(options, { sourceMap }); | ||
} | ||
|
||
var start = new Date(); | ||
debug('[starting]: %s %dKB', relativePath, (src.length / 1000)); | ||
var result = UglifyJS.minify(src, options); | ||
var end = new Date(); | ||
var total = end - start; | ||
if (total > 20000 && !silent) { | ||
console.warn('[WARN] (broccoli-uglify-sourcemap) Minifying: `' + relativePath + '` took: ' + total + 'ms (more than 20,000ms)'); | ||
} | ||
|
||
if (result.error) { | ||
result.error.filename = relativePath; | ||
throw result.error; | ||
} | ||
|
||
debug('[finished]: %s %dKB in %dms', relativePath, (result.code.length / 1000), total); | ||
|
||
if (options.sourceMap) { | ||
var newSourceMap = JSON.parse(result.map); | ||
|
||
newSourceMap.sources = newSourceMap.sources.map(function(path){ | ||
// If out output file has the same name as one of our original | ||
// sources, they will shadow eachother in Dev Tools. So instead we | ||
// alter the reference to the upstream file. | ||
if (path === relativePath) { | ||
path = path.replace(/\.js$/, '-orig.js'); | ||
} | ||
return path; | ||
}); | ||
mkdirp.sync(mapDir); | ||
fs.writeFileSync(path.join(mapDir, mapName), JSON.stringify(newSourceMap)); | ||
} | ||
fs.writeFileSync(outFile, result.code); | ||
}; |
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,15 @@ | ||
'use strict'; | ||
|
||
var workerpool = require('workerpool'); | ||
|
||
var processFile = require('./process-file'); | ||
|
||
// TODO - with an option to disable this parallelism | ||
function processFileParallel(inFile, outFile, relativePath, outDir, silent, _options) { | ||
return processFile(inFile, outFile, relativePath, outDir, silent, _options); | ||
} | ||
|
||
// create worker and register public functions | ||
workerpool.worker({ | ||
processFileParallel | ||
}); |
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
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 @@ | ||
/* This file has an error. */ | ||
var i = | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we may want to do this even if the promise fails. So in the
finally
clause (if this is a new promise, or a rsvp promise) otherwise we can use thecatch
clause.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm testing this now and it looks like it doesn't support
finally
, so I'll usecatch