-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Use woker-farm to handle webpack multi compile
This improves memory footprint of compilations using package individually. By only extracting the properties we need from webpack stats/result we can clean up a lot more memory every compile. This also uses seperate processes to compile each function which can clean up all the memory related to compilations and build multiple functions at same time.
- Loading branch information
Showing
19 changed files
with
4,019 additions
and
1,046 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,27 @@ | ||
'use strict'; | ||
|
||
const BbPromise = require('bluebird'); | ||
const webpack = require('webpack'); | ||
|
||
const { processWebpackStats } = require('./processWebpackStats'); | ||
const { setOptionsOnConfig } = require('./processConfig'); | ||
|
||
module.exports = { | ||
compiler(options) { | ||
const webpackConfig = options.webpackConfig; | ||
const configOptions = options.configOptions; | ||
const consoleStats = options.consoleStats; | ||
|
||
const config = setOptionsOnConfig(webpackConfig, configOptions); | ||
|
||
const compiler = webpack(config); | ||
|
||
return BbPromise.fromCallback(cb => compiler.run(cb)).then(stats => { | ||
const result = processWebpackStats(stats, consoleStats); | ||
|
||
return { | ||
stats: [result] | ||
}; | ||
}); | ||
} | ||
}; |
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,45 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const BbPromise = require('bluebird'); | ||
const workerFarm = require('worker-farm'); | ||
|
||
module.exports = { | ||
multiCompiler(options) { | ||
const workerOptions = { | ||
maxCallsPerWorker: 1 | ||
}; | ||
const workerThreadSourcePath = require.resolve('./workerThread'); | ||
const methods = ['runWebpack']; | ||
|
||
const workers = workerFarm(workerOptions, workerThreadSourcePath, methods); | ||
|
||
const configOptions = options.configOptions; | ||
|
||
const threads = _.map(options.entryFunctions, entryFunc => { | ||
const webpackConfigFilePath = options.webpackConfigFilePath; | ||
const workerOptions = { | ||
webpackConfigFilePath, | ||
configOptions: { | ||
...configOptions, | ||
entryFunc | ||
}, | ||
consoleStats: options.consoleStats | ||
}; | ||
|
||
if (!webpackConfigFilePath) { | ||
workerOptions.webpackConfig = options.webpackConfig; | ||
} | ||
|
||
return BbPromise.fromCallback(cb => { | ||
workers.runWebpack(workerOptions, cb); | ||
}); | ||
}); | ||
|
||
return Promise.all(threads).then(stats => { | ||
return { | ||
stats | ||
}; | ||
}); | ||
} | ||
}; |
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,43 @@ | ||
'use strict'; | ||
|
||
const webpack = require('webpack'); | ||
const { setOptionsOnConfig } = require('../processConfig'); | ||
const { processWebpackStats } = require('../processWebpackStats'); | ||
|
||
module.exports = { | ||
runWebpack(options, callback) { | ||
const webpackConfigFilePath = options.webpackConfigFilePath; | ||
const configOptions = options.configOptions; | ||
const consoleStats = options.consoleStats; | ||
|
||
let webpackConfig = null; | ||
try { | ||
if (webpackConfigFilePath) { | ||
webpackConfig = require(webpackConfigFilePath); | ||
} else if (options.webpackConfig) { | ||
webpackConfig = options.webpackConfig; | ||
} else { | ||
throw new Error('Missing config'); | ||
} | ||
} catch (error) { | ||
callback(new Error('Failed to load config')); | ||
return; | ||
} | ||
|
||
const entryConfig = setOptionsOnConfig(webpackConfig, configOptions); | ||
|
||
const compiler = webpack(entryConfig); | ||
|
||
compiler.run((error, stats) => { | ||
if (error) { | ||
callback(new Error('Failed to compile')); | ||
|
||
return; | ||
} | ||
|
||
const result = processWebpackStats(stats, consoleStats); | ||
|
||
callback(null, result); | ||
}); | ||
} | ||
}; |
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,47 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
setOptionsOnConfig(webpackConfig, options) { | ||
// Default context | ||
if (!webpackConfig.context) { | ||
webpackConfig.context = options.servicePath; | ||
} | ||
|
||
// Default target | ||
if (!webpackConfig.target) { | ||
webpackConfig.target = 'node'; | ||
} | ||
|
||
// Default output | ||
if (!webpackConfig.output || _.isEmpty(webpackConfig.output)) { | ||
const outputPath = path.join(options.servicePath, '.webpack'); | ||
webpackConfig.output = { | ||
libraryTarget: 'commonjs', | ||
path: outputPath, | ||
filename: '[name].js' | ||
}; | ||
} | ||
|
||
// Custom output path | ||
if (options.out) { | ||
webpackConfig.output.path = path.join(options.servicePath, options.out); | ||
} | ||
|
||
// In case of individual packaging we have to create a separate config for each function | ||
if (options.entryFunc) { | ||
const entryFunc = options.entryFunc; | ||
webpackConfig.entry = { | ||
[entryFunc.entry.key]: entryFunc.entry.value | ||
}; | ||
const compileName = entryFunc.funcName || _.camelCase(entryFunc.entry.key); | ||
webpackConfig.output.path = path.join(webpackConfig.output.path, compileName); | ||
} else { | ||
webpackConfig.output.path = path.join(webpackConfig.output.path, 'service'); | ||
} | ||
|
||
return webpackConfig; | ||
} | ||
}; |
Oops, something went wrong.