Skip to content
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

Serialized compile to address #299 #517

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,15 @@ if you are trying to override the entry in webpack.config.js with other unsuppor
The individual packaging needs more time at the packaging phase, but you'll
get that paid back twice at runtime.

#### Individual packaging serializedCompile
```yaml
# serverless.yml
custom:
webpack:
serializedCompile: true
```
Will run each webpack build one at a time which helps reduce memory usage and in some cases impoves overall build performance.

## Usage

### Automatic bundling
Expand Down
7 changes: 6 additions & 1 deletion lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const DefaultConfig = {
packager: 'npm',
packagerOptions: {},
keepOutputDirectory: false,
config: null
config: null,
serializedCompile: false
};

class Configuration {
Expand Down Expand Up @@ -73,6 +74,10 @@ class Configuration {
return this._config.keepOutputDirectory;
}

get serializedCompile() {
return this._config.serializedCompile;
}

toJSON() {
return _.omitBy(this._config, _.isNil);
}
Expand Down
12 changes: 8 additions & 4 deletions lib/Configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe('Configuration', () => {
packager: 'npm',
packagerOptions: {},
keepOutputDirectory: false,
config: null
config: null,
serializedCompile: false
};
});

Expand Down Expand Up @@ -78,7 +79,8 @@ describe('Configuration', () => {
packager: 'npm',
packagerOptions: {},
keepOutputDirectory: false,
config: null
config: null,
serializedCompile: false
});
});
});
Expand All @@ -98,7 +100,8 @@ describe('Configuration', () => {
packager: 'npm',
packagerOptions: {},
keepOutputDirectory: false,
config: null
config: null,
serializedCompile: false
});
});

Expand All @@ -117,7 +120,8 @@ describe('Configuration', () => {
packager: 'npm',
packagerOptions: {},
keepOutputDirectory: false,
config: null
config: null,
serializedCompile: false
});
});
});
Expand Down
79 changes: 46 additions & 33 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,57 @@ const BbPromise = require('bluebird');
const webpack = require('webpack');
const tty = require('tty');

module.exports = {
compile() {
this.serverless.cli.log('Bundling with Webpack...');
const defaultStatsConfig = {
colors: tty.isatty(process.stdout.fd),
hash: false,
version: false,
chunks: false,
children: false
};

const compiler = webpack(this.webpackConfig);

return BbPromise.fromCallback(cb => compiler.run(cb)).then(stats => {
if (!this.multiCompile) {
stats = { stats: [stats] };
}

const compileOutputPaths = [];
const consoleStats = this.webpackConfig.stats ||
_.get(this, 'webpackConfig[0].stats') || {
colors: tty.isatty(process.stdout.fd),
hash: false,
version: false,
chunks: false,
children: false
};

_.forEach(stats.stats, compileStats => {
const statsOutput = compileStats.toString(consoleStats);
if (statsOutput) {
this.serverless.cli.consoleLog(statsOutput);
function ensureArray(obj) {
return _.isArray(obj) ? obj : [obj];
}

function getStatsLogger(statsConfig, consoleLog) {
return stats => consoleLog(stats.toString(statsConfig || defaultStatsConfig));
}

function webpackCompile(config, logStats) {
return BbPromise
.fromCallback(cb => webpack(config).run(cb))
.then(stats => {
// ensure stats in any array in the case of multiCompile
stats = stats.stats ? stats.stats : [stats];

_.forEach(stats, compileStats => {
logStats(compileStats);
if (compileStats.hasErrors()) {
throw new Error('Webpack compilation error, see stats above');
}
});

if (compileStats.compilation.errors.length) {
throw new Error('Webpack compilation error, see above');
}
return stats;
});
}

compileOutputPaths.push(compileStats.compilation.compiler.outputPath);
});
function webpackCompileSerial(configs, logStats) {
return BbPromise
.mapSeries(configs, config => webpackCompile(config, logStats))
.then(stats => _.flatten(stats));
}

this.compileOutputPaths = compileOutputPaths;
this.compileStats = stats;
module.exports = {
compile() {
this.serverless.cli.log('Bundling with Webpack...');

return BbPromise.resolve();
});
const configs = ensureArray(this.webpackConfig);
const logStats = getStatsLogger(configs[0].stats, this.serverless.cli.consoleLog);

return (this.serializedCompile ? webpackCompileSerial : webpackCompile)(configs, logStats)
.then(stats => {
this.compileStats = { stats };
return BbPromise.resolve();
});
}
};
3 changes: 2 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ module.exports = {

// In case of individual packaging we have to create a separate config for each function
if (_.has(this.serverless, 'service.package') && this.serverless.service.package.individually) {
this.options.verbose && this.serverless.cli.log('Using multi-compile (individual packaging)');
this.multiCompile = true;
this.serializedCompile = this.configuration.serializedCompile;
this.options.verbose && this.serverless.cli.log(`Using ${this.serializedCompile ? 'serialized' : 'multi'}-compile (individual packaging)`);

if (this.webpackConfig.entry && !_.isEqual(this.webpackConfig.entry, entries)) {
return BbPromise.reject(
Expand Down
90 changes: 59 additions & 31 deletions tests/compile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ describe('compile', () => {
consoleLog: sandbox.stub()
};

module = _.assign(
{
serverless,
options: {}
},
baseModule
);
module = _.assign({
serverless,
options: {},
}, baseModule);
});

afterEach(() => {
Expand All @@ -65,8 +62,9 @@ describe('compile', () => {
it('should compile with webpack from a context configuration', () => {
const testWebpackConfig = 'testconfig';
module.webpackConfig = testWebpackConfig;
return expect(module.compile()).to.be.fulfilled.then(() => {
expect(webpackMock).to.have.been.calledWith(testWebpackConfig);
return expect(module.compile()).to.be.fulfilled
.then(() => {
expect(webpackMock).to.have.been.calledWith([testWebpackConfig]);
expect(webpackMock.compilerMock.run).to.have.been.calledOnce;
return null;
});
Expand All @@ -80,29 +78,58 @@ describe('compile', () => {
});

it('should work with multi compile', () => {
const testWebpackConfig = 'testconfig';
const multiStats = [
{
const testWebpackConfig = ['testconfig'];
const multiStats = {
stats: [{
compilation: {
errors: [],
compiler: {
outputPath: 'statsMock-outputPath'
}
outputPath: 'statsMock-outputPath',
},
},
toString: sandbox.stub().returns('testStats')
}
];
toString: sandbox.stub().returns('testStats'),
hasErrors: _.constant(false)
}]
};
module.webpackConfig = testWebpackConfig;
module.multiCompile = true;
webpackMock.compilerMock.run.reset();
webpackMock.compilerMock.run.yields(null, multiStats);
return expect(module.compile()).to.be.fulfilled.then(() => {
return expect(module.compile()).to.be.fulfilled
.then(() => {
expect(webpackMock).to.have.been.calledWith(testWebpackConfig);
expect(webpackMock.compilerMock.run).to.have.been.calledOnce;
return null;
});
});

it('should work with serialized compile', () => {
const testWebpackConfig = ['testconfig'];
const multiStats = {
stats: [{
compilation: {
errors: [],
compiler: {
outputPath: 'statsMock-outputPath',
},
},
toString: sandbox.stub().returns('testStats'),
hasErrors: _.constant(false)
}]
};
module.webpackConfig = testWebpackConfig;
module.multiCompile = true;
module.serializedCompile = true;
webpackMock.compilerMock.run.reset();
webpackMock.compilerMock.run.yields(null, multiStats);
return expect(module.compile()).to.be.fulfilled
.then(() => {
expect(webpackMock).to.have.been.calledWith(testWebpackConfig);
expect(webpackMock.compilerMock.run).to.have.been.calledOnce;
return null;
});
});

it('should use correct stats option', () => {
const testWebpackConfig = {
stats: 'minimal'
Expand All @@ -114,23 +141,24 @@ describe('compile', () => {
outputPath: 'statsMock-outputPath'
}
},
toString: sandbox.stub().returns('testStats')
toString: sandbox.stub().returns('testStats'),
hasErrors: _.constant(false)
};

module.webpackConfig = testWebpackConfig;
webpackMock.compilerMock.run.reset();
webpackMock.compilerMock.run.yields(null, mockStats);
return expect(module.compile())
.to.be.fulfilled.then(() => {
expect(webpackMock).to.have.been.calledWith(testWebpackConfig);
expect(mockStats.toString.firstCall.args).to.eql([testWebpackConfig.stats]);
module.webpackConfig = [testWebpackConfig];
return expect(module.compile()).to.be.fulfilled;
})
.then(() => {
expect(webpackMock).to.have.been.calledWith([testWebpackConfig]);
expect(mockStats.toString.args).to.eql([ [testWebpackConfig.stats], [testWebpackConfig.stats] ]);
return null;
});
return (expect(module.compile()).to.be.fulfilled)
.then(() => {
expect(webpackMock).to.have.been.calledWith([testWebpackConfig]);
expect(mockStats.toString.firstCall.args).to.eql([testWebpackConfig.stats]);
module.webpackConfig = [testWebpackConfig];
return (expect(module.compile()).to.be.fulfilled);
})
.then(() => {
expect(webpackMock).to.have.been.calledWith([testWebpackConfig]);
expect(mockStats.toString.args).to.eql([ [testWebpackConfig.stats], [testWebpackConfig.stats] ]);
return null;
});
});
});
12 changes: 8 additions & 4 deletions tests/webpack.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ const StatsMock = () => ({
compilation: {
errors: [],
compiler: {
outputPath: 'statsMock-outputPath'
}
outputPath: 'statsMock-outputPath',
},
},
toString: sinon.stub().returns('testStats'),
hasErrors() {
return Boolean(this.compilation.errors.length);
},
toString: sinon.stub().returns('testStats')
});


const CompilerMock = (sandbox, statsMock) => ({
run: sandbox.stub().yields(null, statsMock),
watch: sandbox.stub().yields(null, statsMock),
hooks: {
beforeCompile: {
tapPromise: sandbox.stub()
}
}
},
});

const webpackMock = sandbox => {
Expand Down