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

Skip compile & packaging if --no-build is set #560

Merged
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
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ServerlessWebpack {
},
package: {
type: 'entrypoint',
lifecycleEvents: [ 'packExternalModules', 'packageModules' ]
lifecycleEvents: [ 'packExternalModules', 'packageModules', 'copyExistingArtifacts' ]
}
}
}
Expand All @@ -87,7 +87,7 @@ class ServerlessWebpack {
'before:package:createDeploymentArtifacts': () =>
BbPromise.bind(this)
.then(() => this.serverless.pluginManager.spawn('webpack:validate'))
.then(() => this.serverless.pluginManager.spawn('webpack:compile'))
.then(() => (this.skipCompile ? BbPromise.resolve() : this.serverless.pluginManager.spawn('webpack:compile')))
.then(() => this.serverless.pluginManager.spawn('webpack:package')),

'after:package:createDeploymentArtifacts': () => BbPromise.bind(this).then(this.cleanup),
Expand All @@ -102,10 +102,6 @@ class ServerlessWebpack {
BbPromise.bind(this)
.then(() => {
lib.webpack.isLocal = true;
// --no-build override
if (this.options.build === false) {
this.skipCompile = true;
}

return this.serverless.pluginManager.spawn('webpack:validate');
})
Expand Down Expand Up @@ -155,6 +151,8 @@ class ServerlessWebpack {

'webpack:package:packageModules': () => BbPromise.bind(this).then(this.packageModules),

'webpack:package:copyExistingArtifacts': () => BbPromise.bind(this).then(this.copyExistingArtifacts),

'before:offline:start': () =>
BbPromise.bind(this)
.tap(() => {
Expand Down
29 changes: 28 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('ServerlessWebpack', () => {
sandbox.stub(slsw, 'watch').returns(BbPromise.resolve());
sandbox.stub(slsw, 'wpwatch').returns(BbPromise.resolve());
sandbox.stub(slsw, 'packExternalModules').returns(BbPromise.resolve());
sandbox.stub(slsw, 'copyExistingArtifacts').returns(BbPromise.resolve());
sandbox.stub(slsw, 'prepareRun').returns(BbPromise.resolve());
sandbox.stub(slsw, 'watchRun').returns(BbPromise.resolve());
sandbox.stub(slsw, 'validate').returns(BbPromise.resolve());
Expand All @@ -130,6 +131,7 @@ describe('ServerlessWebpack', () => {

beforeEach(() => {
ServerlessWebpack.lib.webpack.isLocal = false;
slsw.skipCompile = false;
});

after(() => {
Expand All @@ -154,6 +156,20 @@ describe('ServerlessWebpack', () => {
return null;
});
});

it('should skip compile if requested', () => {
slsw.skipCompile = true;
return expect(slsw.hooks['before:package:createDeploymentArtifacts']()).to.be.fulfilled.then(() => {
expect(slsw.serverless.pluginManager.spawn).to.have.been.calledTwice;
expect(slsw.serverless.pluginManager.spawn.firstCall).to.have.been.calledWithExactly(
'webpack:validate'
);
expect(slsw.serverless.pluginManager.spawn.secondCall).to.have.been.calledWithExactly(
'webpack:package'
);
return null;
});
});
}
},
{
Expand Down Expand Up @@ -222,7 +238,7 @@ describe('ServerlessWebpack', () => {
});

it('should skip compile if requested', () => {
slsw.options.build = false;
slsw.skipCompile = true;
return expect(slsw.hooks['before:invoke:local:invoke']()).to.be.fulfilled.then(() => {
expect(slsw.serverless.pluginManager.spawn).to.have.been.calledOnce;
expect(slsw.serverless.pluginManager.spawn).to.have.been.calledWithExactly('webpack:validate');
Expand Down Expand Up @@ -346,6 +362,17 @@ describe('ServerlessWebpack', () => {
});
}
},
{
name: 'webpack:package:copyExistingArtifacts',
test: () => {
it('should call copyExistingArtifacts', () => {
return expect(slsw.hooks['webpack:package:copyExistingArtifacts']()).to.be.fulfilled.then(() => {
expect(slsw.copyExistingArtifacts).to.have.been.calledOnce;
return null;
});
});
}
},
{
name: 'before:offline:start',
test: () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
cleanup() {
const webpackOutputPath = this.webpackOutputPath;

const keepOutputDirectory = this.configuration.keepOutputDirectory;
const keepOutputDirectory = this.keepOutputDirectory;
if (!keepOutputDirectory) {
this.options.verbose && this.serverless.cli.log(`Remove ${webpackOutputPath}`);
if (this.serverless.utils.dirExistsSync(webpackOutputPath)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/packExternalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ module.exports = {
* and performance.
*/
packExternalModules() {
if (this.skipCompile) {
return BbPromise.resolve();
}

const stats = this.compileStats;

const includes = this.configuration.includeModules;
Expand Down
116 changes: 84 additions & 32 deletions lib/packageModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const semver = require('semver');
function setArtifactPath(funcName, func, artifactPath) {
const version = this.serverless.getVersion();

this.options.verbose && this.serverless.cli.log(`Setting artifact for function '${funcName}' to '${artifactPath}'`);

// Serverless changed the artifact path location in version 1.18
if (semver.lt(version, '1.18.0')) {
func.artifact = artifactPath;
Expand All @@ -26,7 +28,8 @@ function setArtifactPath(funcName, func, artifactPath) {
function zip(directory, name) {
const zip = archiver.create('zip');
// Create artifact in temp path and move it to the package path (if any) later
const artifactFilePath = path.join(this.serverless.config.servicePath, '.serverless', name);
// This allows us to persist the webpackOutputPath and re-use the compiled output
const artifactFilePath = path.join(this.webpackOutputPath, name);
this.serverless.utils.writeFileDir(artifactFilePath);

const output = fs.createWriteStream(artifactFilePath);
Expand Down Expand Up @@ -69,13 +72,47 @@ function zip(directory, name) {
});
}

function getArtifactLocations(name) {
const archiveName = `${name}.zip`;

const webpackArtifact = path.join(this.webpackOutputPath, archiveName);
const serverlessArtifact = path.join('.serverless', archiveName);

return { webpackArtifact, serverlessArtifact };
}

function copyArtifactByName(artifactName) {
const { webpackArtifact, serverlessArtifact } = getArtifactLocations.call(this, artifactName);

// Make sure the destination dir exists
this.serverless.utils.writeFileDir(serverlessArtifact);

fs.copyFileSync(webpackArtifact, serverlessArtifact);
}

function setServiceArtifactPath(artifactPath) {
_.set(this.serverless, 'service.package.artifact', artifactPath);
}

function isIndividialPackaging() {
return _.get(this.serverless, 'service.package.individually');
}

function getArtifactName(entryFunction) {
return `${entryFunction.funcName || this.serverless.service.getServiceObject().name}.zip`;
}

module.exports = {
packageModules() {
if (this.skipCompile) {
return BbPromise.resolve();
}

const stats = this.compileStats;

return BbPromise.mapSeries(stats.stats, (compileStats, index) => {
const entryFunction = _.get(this.entryFunctions, index, {});
const filename = `${entryFunction.funcName || this.serverless.service.getServiceObject().name}.zip`;
const filename = getArtifactName.call(this, entryFunction);
const modulePath = compileStats.compilation.compiler.outputPath;

const startZip = _.now();
Expand All @@ -87,37 +124,52 @@ module.exports = {
this.serverless.cli.log(
`Zip ${_.isEmpty(entryFunction) ? 'service' : 'function'}: ${modulePath} [${_.now() - startZip} ms]`
)
)
.then(artifactPath => {
if (_.get(this.serverless, 'service.package.individually')) {
setArtifactPath.call(
this,
entryFunction.funcName,
entryFunction.func,
path.relative(this.serverless.config.servicePath, artifactPath)
);
}
return artifactPath;
});
}).then(artifacts => {
if (!_.get(this.serverless, 'service.package.individually') && !_.isEmpty(artifacts)) {
// Set the service artifact to all functions
const allFunctionNames = this.serverless.service.getAllFunctions();
_.forEach(allFunctionNames, funcName => {
const func = this.serverless.service.getFunction(funcName);
setArtifactPath.call(this, funcName, func, path.relative(this.serverless.config.servicePath, artifacts[0]));
});
// For Google set the service artifact path
if (_.get(this.serverless, 'service.provider.name') === 'google') {
_.set(
this.serverless,
'service.package.artifact',
path.relative(this.serverless.config.servicePath, artifacts[0])
);
}
}
);
});
},

copyExistingArtifacts() {
this.serverless.cli.log('Copying existing artifacts...');
const allFunctionNames = this.serverless.service.getAllFunctions();

// Copy artifacts to package location
if (isIndividialPackaging.call(this)) {
_.forEach(allFunctionNames, funcName => copyArtifactByName.call(this, funcName));
} else {
// Copy service packaged artifact
const serviceName = this.serverless.service.getServiceObject().name;
copyArtifactByName.call(this, serviceName);
}

_.forEach(allFunctionNames, funcName => {
const func = this.serverless.service.getFunction(funcName);

const archiveName = isIndividialPackaging.call(this) ? funcName : this.serverless.service.getServiceObject().name;

return null;
const { serverlessArtifact } = getArtifactLocations.call(this, archiveName);
setArtifactPath.call(this, funcName, func, serverlessArtifact);
});

// Set artifact locations
if (isIndividialPackaging.call(this)) {
_.forEach(allFunctionNames, funcName => {
const func = this.serverless.service.getFunction(funcName);

const archiveName = funcName;

const { serverlessArtifact } = getArtifactLocations.call(this, archiveName);
setArtifactPath.call(this, funcName, func, serverlessArtifact);
});
} else {
const archiveName = this.serverless.service.getServiceObject().name;

const { serverlessArtifact } = getArtifactLocations.call(this, archiveName);

if (_.get(this.serverless, 'service.provider.name') === 'google') {
setServiceArtifactPath.call(this, serverlessArtifact);
}
}

return BbPromise.resolve();
}
};
4 changes: 3 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ module.exports = {
this.webpackConfig.output.path = path.join(this.serverless.config.servicePath, this.options.out);
}

if (this.skipCompile) {
// Skip compilation with --no-build
if (this.options.build === false) {
this.skipCompile = true;
this.serverless.cli.log('Skipping build and using existing compiled output');
if (!fse.pathExistsSync(this.webpackConfig.output.path)) {
return BbPromise.reject(new this.serverless.classes.Error('No compiled output found'));
Expand Down
5 changes: 2 additions & 3 deletions tests/cleanup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ describe('cleanup', () => {
{
serverless,
options: {},
webpackOutputPath: 'my/Output/Path',
configuration: {}
webpackOutputPath: 'my/Output/Path'
},
baseModule
);
Expand Down Expand Up @@ -96,7 +95,7 @@ describe('cleanup', () => {
fseMock.removeSync.reset();

const configuredModule = _.assign({}, module, {
configuration: { keepOutputDirectory: true }
keepOutputDirectory: true
});
return expect(configuredModule.cleanup()).to.be.fulfilled.then(() => {
expect(dirExistsSyncStub).to.not.have.been.calledOnce;
Expand Down
1 change: 1 addition & 0 deletions tests/mocks/fs.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports.create = sandbox => {
readFileSync: sandbox.stub(),
statSync: sinon.stub().returns(statMock), // Persistent stub
writeFileSync: sandbox.stub(),
copyFileSync: sandbox.stub(),

_streamMock: streamMock,
_statMock: statMock
Expand Down
18 changes: 18 additions & 0 deletions tests/packExternalModules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,24 @@ describe('packExternalModules', () => {
);
});

it('should do nothing if skipCompile is true', () => {
module.configuration = new Configuration({
webpack: {
includeModules: {
packagePath: path.join('locals', 'package.json')
}
}
});
module.skipCompile = true;
return expect(module.packExternalModules()).to.be.fulfilled.then(() =>
BbPromise.all([
expect(fsExtraMock.copy).to.not.have.been.called,
expect(packagerFactoryMock.get).to.not.have.been.called,
expect(writeFileSyncStub).to.not.have.been.called
])
);
});

it('should copy needed package sections if available', () => {
const originalPackageJSON = {
name: 'test-service',
Expand Down
Loading