Comparing version 0.1.6 to 0.2.0
@@ -33,2 +33,6 @@ #!/usr/bin/env node | ||
.boolean("colors") | ||
.describe("colors", "Output Stats with colors") | ||
.default("colors", false) | ||
.demand(1) | ||
@@ -96,4 +100,4 @@ .argv; | ||
} | ||
console.log(stats); | ||
console.log(require("util").inspect(stats, false, 10, argv.colors)); | ||
}); | ||
} |
@@ -24,2 +24,3 @@ /* | ||
var depTree = { | ||
warnings: [], | ||
modules: {}, | ||
@@ -75,3 +76,9 @@ modulesById: {}, | ||
} | ||
var deps = parse(source); | ||
var deps; | ||
try { | ||
deps = parse(source); | ||
} catch(e) { | ||
callback("File \"" + filename + "\" parsing failed: " + e); | ||
return; | ||
} | ||
module.requires = deps.requires || []; | ||
@@ -134,3 +141,7 @@ module.asyncs = deps.asyncs || []; | ||
endOne(); | ||
}) | ||
}); | ||
if(context.warn) { | ||
depTree.warnings.push(filename + " (line " + context.line + ", column " + context.column + "): " + | ||
"implicit use of require.context(\".\") is not recommended."); | ||
} | ||
}); | ||
@@ -171,2 +182,3 @@ } | ||
depTree.modulesById[contextModule.id] = contextModule; | ||
var extensions = (options.resolve && options.resolve.extensions) || [".web.js", ".js"]; | ||
function doDir(dirname, moduleName, done) { | ||
@@ -201,2 +213,11 @@ fs.readdir(dirname, function(err, list) { | ||
} else { | ||
var hasExt = false; | ||
extensions.forEach(function(ext) { | ||
if(file.substr(file.length - ext.length) === ext) | ||
hasExt = true; | ||
}); | ||
if(!hasExt) { | ||
endOne(); | ||
return; | ||
} | ||
addModule(depTree, null, filename, options, function(err, moduleId) { | ||
@@ -225,3 +246,2 @@ if(err) { | ||
var extensionsAccess = []; | ||
var extensions = (options.resolve && options.resolve.extensions) || [".web.js", ".js"]; | ||
extensions.forEach(function(ext) { | ||
@@ -228,0 +248,0 @@ extensionsAccess.push("||map[name+\""); |
@@ -81,2 +81,7 @@ /* | ||
case "FunctionDeclaration": | ||
var req = functionParamsContainsRequire(statement.params); | ||
if(req) { | ||
var old = context.requireOverwrite; | ||
context.requireOverwrite = true; | ||
} | ||
if(statement.body.type === "BlockStatement") | ||
@@ -86,2 +91,4 @@ walkStatement(context, statement.body); | ||
walkExpression(context, statement.body); | ||
if(req) | ||
context.requireOverwrite = old; | ||
break; | ||
@@ -140,2 +147,11 @@ case "VariableDeclaration": | ||
case "FunctionExpression": | ||
var req = functionParamsContainsRequire(expression.params); | ||
if(context.paramMustRequire) { | ||
req = false; | ||
context.paramMustRequire = false; | ||
} | ||
if(req) { | ||
var old = context.requireOverwrite; | ||
context.requireOverwrite = true; | ||
} | ||
if(expression.body.type === "BlockStatement") | ||
@@ -145,2 +161,4 @@ walkStatement(context, expression.body); | ||
walkExpression(context, expression.body); | ||
if(req) | ||
context.requireOverwrite = old; | ||
break; | ||
@@ -172,3 +190,5 @@ case "SequenceExpression": | ||
case "CallExpression": | ||
if(expression.callee && expression.arguments && | ||
var noCallee = false; | ||
if(!context.requireOverwrite && | ||
expression.callee && expression.arguments && | ||
expression.arguments.length == 1 && | ||
@@ -181,4 +201,12 @@ expression.callee.type === "Identifier" && | ||
var pos = param.value.indexOf("/"); | ||
context.contexts = context.contexts || []; | ||
if(pos === -1) { | ||
throw new Error("require a module by variable is not supported"); | ||
var newContext = { | ||
name: ".", | ||
require: true, | ||
calleeRange: expression.callee.range, | ||
line: expression.loc.start.line, | ||
column: expression.loc.start.column | ||
}; | ||
context.contexts.push(newContext); | ||
} else { | ||
@@ -188,3 +216,2 @@ var match = /\/[^\/]*$/.exec(param.value); | ||
var remainder = "." + param.value.substring(match.index); | ||
context.contexts = context.contexts || []; | ||
var newContext = { | ||
@@ -210,4 +237,6 @@ name: dirname, | ||
} | ||
noCallee = true; | ||
} | ||
if(expression.callee && expression.arguments && | ||
if(!context.requireOverwrite && | ||
expression.callee && expression.arguments && | ||
expression.arguments.length >= 1 && | ||
@@ -225,3 +254,4 @@ expression.callee.type === "MemberExpression" && | ||
line: expression.loc.start.line, | ||
column: expression.loc.start.column | ||
column: expression.loc.start.column, | ||
paramMustRequire: true | ||
}; | ||
@@ -233,4 +263,6 @@ param.forEach(function(r) { | ||
context = newContext; | ||
noCallee = true; | ||
} | ||
if(expression.callee && expression.arguments && | ||
if(!context.requireOverwrite && | ||
expression.callee && expression.arguments && | ||
expression.arguments.length == 1 && | ||
@@ -251,5 +283,6 @@ expression.callee.type === "MemberExpression" && | ||
context.contexts.push(newContext); | ||
noCallee = true; | ||
} | ||
if(expression.callee) | ||
if(expression.callee && !noCallee) | ||
walkExpression(context, expression.callee); | ||
@@ -264,5 +297,31 @@ if(expression.arguments) | ||
break; | ||
case "Identifier": | ||
if(!context.requireOverwrite && | ||
expression.name === "require") { | ||
context.contexts = context.contexts || []; | ||
var newContext = { | ||
name: ".", | ||
warn: "Identifier", | ||
require: true, | ||
calleeRange: [expression.range[0], expression.range[1]], | ||
line: expression.loc.start.line, | ||
column: expression.loc.start.column, | ||
}; | ||
context.contexts.push(newContext); | ||
} | ||
break; | ||
} | ||
} | ||
function functionParamsContainsRequire(params) { | ||
if(!params) return false; | ||
var found = false; | ||
params.forEach(function(param) { | ||
if(param.type === "Identifier" && | ||
param.name === "require") | ||
found = true; | ||
}); | ||
return found; | ||
} | ||
function parseString(expression) { | ||
@@ -269,0 +328,0 @@ switch(expression.type) { |
@@ -75,2 +75,3 @@ /* | ||
var fileSizeMap = {}; | ||
var fileModulesMap = {}; | ||
var chunksCount = 0; | ||
@@ -119,2 +120,8 @@ for(var chunkId in depTree.chunks) { | ||
fileSizeMap[path.basename(filename)] = buffer.length; | ||
var modulesArray = []; | ||
for(var moduleId in chunk.modules) { | ||
if(chunk.modules[moduleId] === "include") | ||
modulesArray.push({id: moduleId, filename: depTree.modulesById[moduleId].filename}); | ||
} | ||
fileModulesMap[path.basename(filename)] = modulesArray; | ||
} | ||
@@ -140,2 +147,4 @@ buffer = {}; | ||
buffer.fileSizes = fileSizeMap; | ||
buffer.warnings = depTree.warnings; | ||
buffer.fileModules = fileModulesMap; | ||
callback(null, buffer); | ||
@@ -142,0 +151,0 @@ } else { |
@@ -34,7 +34,8 @@ /* | ||
}); | ||
replaces.push({ | ||
from: contextItem.replace[0][0], | ||
to: contextItem.replace[0][1], | ||
value: stringify(contextItem.replace[1]) | ||
}); | ||
if(contextItem.replace) | ||
replaces.push({ | ||
from: contextItem.replace[0][0], | ||
to: contextItem.replace[0][1], | ||
value: stringify(contextItem.replace[1]) | ||
}); | ||
} else { | ||
@@ -41,0 +42,0 @@ replaces.push({ |
{ | ||
"name": "webpack", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"author": "Tobias Koppers @sokra", | ||
@@ -5,0 +5,0 @@ "description": "Packs CommonJs Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand.", |
@@ -122,2 +122,5 @@ # modules-webpack | ||
When try to store the `require` function in another variable or try to pass it as parameter, | ||
`webpack` convert it to a `require.context(".")` to be combatible. | ||
There is a warning emitted in this case. | ||
@@ -132,9 +135,16 @@ *Warning: The complete code in the directory are included. So use it carefully.* | ||
* `require` should not be overwritten | ||
* `require` should not be called indirect as `var r = require; r("./a");`. Use `require.context`? | ||
* `require` should not be overwritten by variable declaration (`var require = ...`), by function parameter is allowed `function(require) {...}`. | ||
* `require.ensure` should not be overwritten or called indirect | ||
* the function passed to `require.ensure` should be inlined in the call. | ||
* `require.context` should not be overwritten or called indirect | ||
* the argument to `require.context` should be a literal or addition of multiple literals | ||
* An indirect call of `require` should access a file in current directory: This throws an exception: `var r = require; r("../file");` | ||
The following cases could result in too much code in result file if used wrong: | ||
* indirect call of `require`: `var r = require; r("./file");` | ||
* `require.context`. It includes the whole directory. | ||
* expressions in require arguments: `require(variable)`, `require(condition ? "a" : "b")` (TODO) | ||
* the function passed to `require.ensure` is not inlined in the call. | ||
### node.js specific modules | ||
@@ -152,2 +162,4 @@ | ||
TODO provide some replacements | ||
## Usage | ||
@@ -173,2 +185,3 @@ | ||
--libary Stores the exports into this variable [string] | ||
--colors Output Stats with colors [boolean] [default: false] | ||
``` | ||
@@ -227,3 +240,3 @@ | ||
`webpack` resolves requires in compile time and have no resolve code on client side. This results in smaller bundles. | ||
Variables as argments will be handled different and with more limitations in `webpack`. | ||
Variables as arguments will be handled different and with more limitations in `webpack`. | ||
@@ -230,0 +243,0 @@ Another limitation in `webmake` which are based on the previous one is that modules must be in the current package scope. |
@@ -12,3 +12,3 @@ /* | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --single --libary libary1 node_modules/libary1 js/libary1.js", function (error, stdout, stderr) { | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --colors --single --libary libary1 node_modules/libary1 js/libary1.js", function (error, stdout, stderr) { | ||
console.log('libary1 stdout:\n' + stdout); | ||
@@ -20,3 +20,3 @@ console.log('libary1 stderr:\n ' + stderr); | ||
}); | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --script-src-prefix js/ --libary libary2 node_modules/libary2 js/libary2.js", function (error, stdout, stderr) { | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --colors --script-src-prefix js/ --libary libary2 node_modules/libary2 js/libary2.js", function (error, stdout, stderr) { | ||
console.log('libary2 stdout:\n' + stdout); | ||
@@ -28,3 +28,3 @@ console.log('libary2 stderr:\n ' + stderr); | ||
}); | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --script-src-prefix js/ lib/index js/web.js", function (error, stdout, stderr) { | ||
cp.exec("node ../../bin/webpack.js "+extraArgs+" --colors --script-src-prefix js/ lib/index js/web.js", function (error, stdout, stderr) { | ||
console.log('web stdout:\n' + stdout); | ||
@@ -31,0 +31,0 @@ console.log('web stderr:\n ' + stderr); |
@@ -21,5 +21,13 @@ window.test(true, "index.js should be replaced with index.web.js"); | ||
window.test(require . context ( "." + "." + "/" + "templ" + "ates" ) ( "./subdir/tmpl.js" ) === "subdir test template", "Context should work with subdirectories and splitted"); | ||
var template = "tmpl"; | ||
var template = "tmpl", templateFull = "./tmpl.js"; | ||
window.test(require("../templates/" + template) === "test template", "Automatical context should work"); | ||
window.test(require("../templates/templateLoader")(templateFull) === "test template", "Automatical context without prefix should work"); | ||
window.test(require("../templates/templateLoaderIndirect")(templateFull) === "test template", "Automatical context should work with require identifier"); | ||
window.test(function(require) { return require; }(1234) === 1234, "require overwrite in anonymos function"); | ||
function testFunc(abc, require) { | ||
return require; | ||
} | ||
window.test(testFunc(333, 678) === 678, "require overwrite in named function"); | ||
require.ensure([], function(require) { | ||
@@ -26,0 +34,0 @@ var contextRequire = require.context("."); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
74356
81
1686
277
35