Skip to content

Added forceInclude setting to add dynamically required modules explicitly #226

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

Merged
merged 3 commits into from
Sep 19, 2017
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ custom:
```
> Note that only relative path is supported at the moment.


Sometimes it might happen that you use dynamic requires in your code, i.e. you
require modules that are only known at runtime. Webpack is not able to detect
such externals and the compiled package will miss the needed dependencies.
In such cases you can force the plugin to include certain modules by setting
them in the `forceInclude` array property. However the module must appear in
your service's production dependencies in `package.json`.

```yaml
# serverless.yml
custom:
webpackIncludeModules:
forceInclude:
- module1
- module2
```


You can find an example setups in the [`examples`][link-examples] folder.

#### Service level packaging
Expand Down
12 changes: 10 additions & 2 deletions lib/packExternalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ module.exports = {
return BbPromise.resolve(stats);
}

const packageForceIncludes = _.get(includes, 'forceInclude', []);
const packagePath = includes.packagePath || './package.json';
const packageJsonPath = path.join(process.cwd(), packagePath);

Expand Down Expand Up @@ -158,7 +159,10 @@ module.exports = {

// (1) Generate dependency composition
const compositeModules = _.uniq(_.flatMap(stats.stats, compileStats => {
const externalModules = getExternalModules.call(this, compileStats);
const externalModules = _.concat(
getExternalModules.call(this, compileStats),
_.map(packageForceIncludes, whitelistedPackage => ({ external: whitelistedPackage }))
);
return getProdModules.call(this, externalModules, packagePath, dependencyGraph);
}));

Expand Down Expand Up @@ -196,7 +200,11 @@ module.exports = {
const modulePackage = {
dependencies: {}
};
const prodModules = getProdModules.call(this, getExternalModules.call(this, compileStats), packagePath, dependencyGraph);
const prodModules = getProdModules.call(this,
_.concat(
getExternalModules.call(this, compileStats),
_.map(packageForceIncludes, whitelistedPackage => ({ external: whitelistedPackage }))
), packagePath, dependencyGraph);
_.forEach(prodModules, prodModule => {
const splitModule = _.split(prodModule, '@');
// If we have a scoped module we have to re-add the @
Expand Down
3 changes: 2 additions & 1 deletion tests/mocks/package.mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"npm-programmatic": "0.0.5",
"uuid": "^5.4.1",
"ts-node": "^3.2.0",
"@scoped/vendor": "1.0.0"
"@scoped/vendor": "1.0.0",
"pg": "^4.3.5"
},
"devDependencies": {
"babel-eslint": "^7.2.3",
Expand Down
102 changes: 102 additions & 0 deletions tests/packExternalModules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,107 @@ describe('packExternalModules', () => {
expect(childProcessMock.exec).to.have.been.calledOnce,
]));
});

it('should install external modules when forced', () => {
const expectedPackageJSON = {
dependencies: {
'@scoped/vendor': '1.0.0',
uuid: '^5.4.1',
bluebird: '^3.4.0',
pg: '^4.3.5'
}
};
serverless.service.custom = {
webpackIncludeModules: {
forceInclude: ['pg']
}
};
module.webpackOutputPath = 'outputPath';
npmMock.install.returns(BbPromise.resolve());
fsExtraMock.copy.yields();
childProcessMock.exec.onFirstCall().yields(null, '{}', '');
childProcessMock.exec.onSecondCall().yields();
return expect(module.packExternalModules(stats)).to.be.fulfilled
.then(() => BbPromise.all([
// npm install should have been called with all externals from the package mock
expect(npmMock.install).to.have.been.calledOnce,
expect(npmMock.install).to.have.been.calledWithExactly([
'@scoped/vendor@1.0.0',
'uuid@^5.4.1',
'bluebird@^3.4.0',
'pg@^4.3.5'
],
{
cwd: path.join('outputPath', 'dependencies'),
maxBuffer: 204800,
save: true
}),
// The module package JSON and the composite one should have been stored
expect(writeFileSyncStub).to.have.been.calledTwice,
expect(writeFileSyncStub.firstCall.args[1]).to.equal('{}'),
expect(writeFileSyncStub.secondCall.args[1]).to.equal(JSON.stringify(expectedPackageJSON, null, 2)),
// The modules should have been copied
expect(fsExtraMock.copy).to.have.been.calledOnce,
// npm ls and npm prune should have been called
expect(childProcessMock.exec).to.have.been.calledTwice,
expect(childProcessMock.exec.firstCall).to.have.been.calledWith(
'npm ls -prod -json -depth=1'
),
expect(childProcessMock.exec.secondCall).to.have.been.calledWith(
'npm prune'
)
]));
});

it('should add forced external modules without version when not in production dependencies', () => {
const expectedPackageJSON = {
dependencies: {
'@scoped/vendor': '1.0.0',
uuid: '^5.4.1',
bluebird: '^3.4.0',
'not-in-prod-deps': ''
}
};
serverless.service.custom = {
webpackIncludeModules: {
forceInclude: ['not-in-prod-deps']
}
};
module.webpackOutputPath = 'outputPath';
npmMock.install.returns(BbPromise.resolve());
fsExtraMock.copy.yields();
childProcessMock.exec.onFirstCall().yields(null, '{}', '');
childProcessMock.exec.onSecondCall().yields();
return expect(module.packExternalModules(stats)).to.be.fulfilled
.then(() => BbPromise.all([
// npm install should have been called with all externals from the package mock
expect(npmMock.install).to.have.been.calledOnce,
expect(npmMock.install).to.have.been.calledWithExactly([
'@scoped/vendor@1.0.0',
'uuid@^5.4.1',
'bluebird@^3.4.0',
'not-in-prod-deps'
],
{
cwd: path.join('outputPath', 'dependencies'),
maxBuffer: 204800,
save: true
}),
// The module package JSON and the composite one should have been stored
expect(writeFileSyncStub).to.have.been.calledTwice,
expect(writeFileSyncStub.firstCall.args[1]).to.equal('{}'),
expect(writeFileSyncStub.secondCall.args[1]).to.equal(JSON.stringify(expectedPackageJSON, null, 2)),
// The modules should have been copied
expect(fsExtraMock.copy).to.have.been.calledOnce,
// npm ls and npm prune should have been called
expect(childProcessMock.exec).to.have.been.calledTwice,
expect(childProcessMock.exec.firstCall).to.have.been.calledWith(
'npm ls -prod -json -depth=1'
),
expect(childProcessMock.exec.secondCall).to.have.been.calledWith(
'npm prune'
)
]));
});
});
});