webpack-node-externals
Advanced tools
Comparing version 2.3.0 to 2.4.0
36
index.js
var utils = require('./utils'); | ||
var scopedModuleRegex = new RegExp('@[a-zA-Z0-9][\\w-.]+\/[a-zA-Z0-9][\\w-.]+([a-zA-Z0-9.\/]+)?', 'g'); | ||
var scopedModuleRegex = new RegExp( | ||
'@[a-zA-Z0-9][\\w-.]+/[a-zA-Z0-9][\\w-.]+([a-zA-Z0-9./]+)?', | ||
'g' | ||
); | ||
@@ -23,2 +26,8 @@ function getModuleName(request, includeAbsolutePaths) { | ||
options = options || {}; | ||
var mistakes = utils.validateOptions(options) || []; | ||
if (mistakes.length) { | ||
mistakes.forEach(function (mistake) { | ||
utils.log(mistake.message); | ||
}); | ||
} | ||
var allowlist = [].concat(options.allowlist || []); | ||
@@ -38,11 +47,18 @@ var binaryDirs = [].concat(options.binaryDirs || ['.bin']); | ||
// create the node modules list | ||
var nodeModules = modulesFromFile ? utils.readFromPackageJson(options.modulesFromFile) : utils.readDir(modulesDir).filter(isNotBinary); | ||
additionalModuleDirs.forEach(function(additionalDirectory){ | ||
nodeModules = nodeModules.concat(utils.readDir(additionalDirectory).filter(isNotBinary)); | ||
}) | ||
var nodeModules = modulesFromFile | ||
? utils.readFromPackageJson(options.modulesFromFile) | ||
: utils.readDir(modulesDir).filter(isNotBinary); | ||
additionalModuleDirs.forEach(function (additionalDirectory) { | ||
nodeModules = nodeModules.concat( | ||
utils.readDir(additionalDirectory).filter(isNotBinary) | ||
); | ||
}); | ||
// return an externals function | ||
return function(context, request, callback){ | ||
return function (context, request, callback) { | ||
var moduleName = getModuleName(request, includeAbsolutePaths); | ||
if (utils.contains(nodeModules, moduleName) && !utils.containsPattern(allowlist, request)) { | ||
if ( | ||
utils.contains(nodeModules, moduleName) && | ||
!utils.containsPattern(allowlist, request) | ||
) { | ||
if (typeof importType === 'function') { | ||
@@ -53,6 +69,6 @@ return callback(null, importType(request)); | ||
// https://webpack.js.org/configuration/externals/ | ||
return callback(null, importType + " " + request); | ||
}; | ||
return callback(null, importType + ' ' + request); | ||
} | ||
callback(); | ||
} | ||
}; | ||
}; |
{ | ||
"name": "webpack-node-externals", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Easily exclude node_modules in Webpack bundle", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
121
utils.js
@@ -6,3 +6,3 @@ var fs = require('fs'); | ||
return arr && arr.indexOf(val) !== -1; | ||
} | ||
}; | ||
@@ -16,25 +16,30 @@ var atPrefix = new RegExp('^@', 'g'); | ||
try { | ||
return fs.readdirSync(dirName).map(function(module) { | ||
if (atPrefix.test(module)) { | ||
// reset regexp | ||
atPrefix.lastIndex = 0; | ||
try { | ||
return fs.readdirSync(path.join(dirName, module)).map(function(scopedMod) { | ||
return module + '/' + scopedMod; | ||
}); | ||
} catch (e) { | ||
return [module]; | ||
return fs | ||
.readdirSync(dirName) | ||
.map(function (module) { | ||
if (atPrefix.test(module)) { | ||
// reset regexp | ||
atPrefix.lastIndex = 0; | ||
try { | ||
return fs | ||
.readdirSync(path.join(dirName, module)) | ||
.map(function (scopedMod) { | ||
return module + '/' + scopedMod; | ||
}); | ||
} catch (e) { | ||
return [module]; | ||
} | ||
} | ||
} | ||
return module | ||
}).reduce(function(prev, next) { | ||
return prev.concat(next); | ||
}, []); | ||
return module; | ||
}) | ||
.reduce(function (prev, next) { | ||
return prev.concat(next); | ||
}, []); | ||
} catch (e) { | ||
return []; | ||
} | ||
} | ||
}; | ||
exports.readFromPackageJson = function readFromPackageJson(options) { | ||
if(typeof options !== 'object') { | ||
if (typeof options !== 'object') { | ||
options = {}; | ||
@@ -49,14 +54,22 @@ } | ||
var fileName = options.fileName || 'package.json'; | ||
var packageJsonString = fs.readFileSync(path.resolve(process.cwd(), fileName), 'utf8'); | ||
var packageJsonString = fs.readFileSync( | ||
path.resolve(process.cwd(), fileName), | ||
'utf8' | ||
); | ||
packageJson = JSON.parse(packageJsonString); | ||
} catch (e){ | ||
} catch (e) { | ||
return []; | ||
} | ||
// sections to search in package.json | ||
var sections = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']; | ||
if(excludeFromBundle) { | ||
var sections = [ | ||
'dependencies', | ||
'devDependencies', | ||
'peerDependencies', | ||
'optionalDependencies', | ||
]; | ||
if (excludeFromBundle) { | ||
sections = [].concat(excludeFromBundle); | ||
} | ||
if(includeInBundle) { | ||
sections = sections.filter(function(section){ | ||
if (includeInBundle) { | ||
sections = sections.filter(function (section) { | ||
return [].concat(includeInBundle).indexOf(section) === -1; | ||
@@ -67,4 +80,4 @@ }); | ||
var deps = {}; | ||
sections.forEach(function(section){ | ||
Object.keys(packageJson[section] || {}).forEach(function(dep){ | ||
sections.forEach(function (section) { | ||
Object.keys(packageJson[section] || {}).forEach(function (dep) { | ||
deps[dep] = true; | ||
@@ -74,14 +87,54 @@ }); | ||
return Object.keys(deps); | ||
} | ||
}; | ||
exports.containsPattern = function containsPattern(arr, val) { | ||
return arr && arr.some(function(pattern){ | ||
if(pattern instanceof RegExp){ | ||
return pattern.test(val); | ||
} else if (typeof pattern === 'function') { | ||
return pattern(val); | ||
} else { | ||
return pattern == val; | ||
return ( | ||
arr && | ||
arr.some(function (pattern) { | ||
if (pattern instanceof RegExp) { | ||
return pattern.test(val); | ||
} else if (typeof pattern === 'function') { | ||
return pattern(val); | ||
} else { | ||
return pattern == val; | ||
} | ||
}) | ||
); | ||
}; | ||
exports.validateOptions = function (options) { | ||
var results = []; | ||
var mistakes = { | ||
allowlist: ['allowslist', 'whitelist', 'allow'], | ||
importType: ['import', 'importype', 'importtype'], | ||
modulesDir: ['moduleDir', 'moduledir', 'moduledirs'], | ||
modulesFromFile: ['modulesfile'], | ||
includeAbsolutePaths: ['includeAbsolutePaths'], | ||
additionalModuleDirs: ['additionalModulesDirs', 'additionalModulesDir'], | ||
}; | ||
var optionsKeys = Object.keys(options); | ||
var optionsKeysLower = optionsKeys.map(function (optionName) { | ||
return optionName && optionName.toLowerCase(); | ||
}); | ||
Object.keys(mistakes).forEach(function (correctTerm) { | ||
if (options[correctTerm] === undefined) { | ||
mistakes[correctTerm] | ||
.concat(correctTerm.toLowerCase()) | ||
.forEach(function (mistake) { | ||
var ind = optionsKeysLower.indexOf(mistake.toLowerCase()); | ||
if (ind > -1) { | ||
results.push({ | ||
message: `Option '${optionsKeys[ind]}' is not supported. Did you mean '${correctTerm}'?`, | ||
wrongTerm: optionsKeys[ind], | ||
correctTerm: correctTerm, | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
return results; | ||
}; | ||
exports.log = function(message) { | ||
console.log(`[webpack-node-externals] : ${message}`) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15290
190