|
|
@ -5,8 +5,69 @@ const fs = require('fs'); |
|
|
|
const path = require('path'); |
|
|
|
const npm = require('npm-programmatic'); |
|
|
|
|
|
|
|
function getProdModules(externalModules, packagePath) { |
|
|
|
|
|
|
|
const packageJson = require(path.join(process.cwd(), packagePath)); |
|
|
|
|
|
|
|
const prodModules = []; |
|
|
|
|
|
|
|
// only process the module stated in dependencies section
|
|
|
|
if (!packageJson.dependencies) { |
|
|
|
return [] |
|
|
|
} |
|
|
|
|
|
|
|
externalModules.forEach(module => { |
|
|
|
|
|
|
|
const moduleVersion = packageJson.dependencies[module]; |
|
|
|
|
|
|
|
if (moduleVersion) { |
|
|
|
prodModules.push(`${module}@${moduleVersion}`); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return prodModules; |
|
|
|
} |
|
|
|
|
|
|
|
function getExternalModuleName(module) { |
|
|
|
|
|
|
|
const path = /^external "(.*)"$/.exec(module.identifier())[1]; |
|
|
|
|
|
|
|
|
|
|
|
const pathComponents = path.split('/'); |
|
|
|
|
|
|
|
const main = pathComponents[0]; |
|
|
|
|
|
|
|
// this is a package within a namespace
|
|
|
|
if (main.charAt(0) == '@') { |
|
|
|
return `${main}/${pathComponents[1]}` |
|
|
|
} |
|
|
|
|
|
|
|
return main |
|
|
|
} |
|
|
|
|
|
|
|
function isExternalModule(module) { |
|
|
|
return module.identifier().indexOf('external ') === 0; |
|
|
|
} |
|
|
|
|
|
|
|
function getExternalModules(stats) { |
|
|
|
|
|
|
|
const externals = new Set(); |
|
|
|
|
|
|
|
stats.compilation.chunks.forEach(function(chunk) { |
|
|
|
// Explore each module within the chunk (built inputs):
|
|
|
|
chunk.modules.forEach(function(module) { |
|
|
|
// Explore each source file path that was included into the module:
|
|
|
|
if (isExternalModule(module)) { |
|
|
|
externals.add(getExternalModuleName(module)); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
return Array.from(externals); |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
packExternalModules() { |
|
|
|
packExternalModules(stats) { |
|
|
|
|
|
|
|
const includes = ( |
|
|
|
this.serverless.service.custom && |
|
|
@ -14,11 +75,23 @@ module.exports = { |
|
|
|
); |
|
|
|
|
|
|
|
return BbPromise.resolve().then(() => { |
|
|
|
if (!includes || includes.length === 0) { |
|
|
|
|
|
|
|
if (!includes) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.serverless.cli.log('Packing external modules: ' + includes.join(",")); |
|
|
|
const packagePath = includes.packagePath || './package.json'; |
|
|
|
|
|
|
|
const externalModules = getExternalModules(stats); |
|
|
|
|
|
|
|
// this plugin will only install modules stated in dependencies section of package.json
|
|
|
|
const prodModules = getProdModules(externalModules, packagePath); |
|
|
|
|
|
|
|
if (prodModules.length === 0) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.serverless.cli.log('Packing external modules: ' + prodModules.join(", ")); |
|
|
|
|
|
|
|
const tmpPackageJson = path.join(this.serverless.config.servicePath, 'package.json'); |
|
|
|
|
|
|
@ -26,17 +99,17 @@ module.exports = { |
|
|
|
fs.writeFileSync(tmpPackageJson, "{}"); |
|
|
|
|
|
|
|
return new BbPromise((resolve, reject) => { |
|
|
|
npm.install(includes, { |
|
|
|
npm.install(prodModules, { |
|
|
|
cwd: this.serverless.config.servicePath, |
|
|
|
save: false |
|
|
|
save: true |
|
|
|
}).then(() => { |
|
|
|
fs.unlink(tmpPackageJson); |
|
|
|
// fs.unlink(tmpPackageJson);
|
|
|
|
resolve() |
|
|
|
}).catch(e => { |
|
|
|
fs.unlink(tmpPackageJson); |
|
|
|
// fs.unlink(tmpPackageJson);
|
|
|
|
reject(e); |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|