Browse Source

pack external packages to distribution for deploy command and --out option

master
Tony Yuen 8 years ago
parent
commit
994bcec2ee
  1. 11
      index.js
  2. 44
      lib/packExternalModules.js
  3. 1
      package.json

11
index.js

@ -7,6 +7,7 @@ const compile = require('./lib/compile');
const cleanup = require('./lib/cleanup');
const run = require('./lib/run');
const serve = require('./lib/serve');
const packExternalModules = require('./lib/packExternalModules')
class ServerlessWebpack {
constructor(serverless, options) {
@ -19,7 +20,8 @@ class ServerlessWebpack {
compile,
cleanup,
run,
serve
serve,
packExternalModules
);
this.commands = {
@ -28,6 +30,7 @@ class ServerlessWebpack {
lifecycleEvents: [
'validate',
'compile',
'packExternalModules',
],
options: {
out: {
@ -89,7 +92,8 @@ class ServerlessWebpack {
this.hooks = {
'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.validate)
.then(this.compile),
.then(this.compile)
.then(this.packExternalModules),
'after:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.cleanup),
@ -100,6 +104,9 @@ class ServerlessWebpack {
'webpack:compile': () => BbPromise.bind(this)
.then(this.compile),
'webpack:packExternalModules': () => BbPromise.bind(this)
.then(this.packExternalModules),
'webpack:invoke:invoke': () => BbPromise.bind(this)
.then(this.validate)
.then(this.compile)

44
lib/packExternalModules.js

@ -0,0 +1,44 @@
'use strict';
const BbPromise = require('bluebird');
const webpack = require('webpack');
const fs = require('fs');
const path = require('path');
const npm = require('npm-programmatic');
module.exports = {
packExternalModules() {
this.serverless.cli.log(this.webpackOutputPath);
this.serverless.cli.log(JSON.stringify(this.webpackConfig.externals));
const externals = this.webpackConfig.externals;
return BbPromise.resolve().then(() => {
if (!externals || externals.length === 0) {
return;
}
this.serverless.cli.log('packing external modules');
const tmpPackageJson = path.join(this.webpackOutputPath, 'package.json');
// create a temp package.json in dist directory so that we can install the dependencies later.
fs.writeFileSync(tmpPackageJson, "{}");
return new BbPromise((resolve, reject) => {
npm.install(externals, {
cwd: this.webpackOutputPath,
save: false
}).then(() => {
fs.unlink(tmpPackageJson);
resolve()
}).catch(e => {
fs.unlink(tmpPackageJson);
reject(e);
})
})
})
},
};

1
package.json

@ -31,6 +31,7 @@
"body-parser": "^1.15.2",
"express": "^4.14.0",
"fs-extra": "^0.26.7",
"npm-programmatic": "0.0.5",
"webpack": "^1.13.1"
},
"devDependencies": {

Loading…
Cancel
Save