Skip to content

Commit

Permalink
Added option to customise name of 'require' function.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jul 2, 2014
1 parent 3d0a3fb commit 49de9dd
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 57 deletions.
105 changes: 56 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,55 +107,62 @@ Example:
webant --entry src/js/main.js --dest build/js/main.js
Options:
--entry, -e Path to entry script relative to current directory.
[Required, unless --useConfig flag is set].
--dest, -d Path to where compiled output will be written.
--urlDest, -u URL at which the path specified for '--dest' can be
reached. Can be absolute or relative. Example:
--urlDest js/main.js
--postProcess, -p Post-processing to apply to compiled javascript. Can
be either 'compress' (compresses output), 'debug'
(adds file and line numbers to output) or 'none' (no
post-processing).
--requireBase, -r Resolve require paths relative to the supplied
directory instead of relative to the file containing
the require.
--defaultExtension, -D Default extension to append to a require path when no
extension is provided. Include the starting period
(e.g. '.ts'). Example: setting `--defaultExtension
.coffee` would mean that `require('./path/to/file')`
would look for `./path/to/file.coffee`. Defaults to
'.js'.
--handlers, -H Additional handlers to use. Can be set multiple
times. Example: -H coffee -H scss -H json
--includeBootstrap, -i The path to the file where the bootstrap code will be
included. This should be set to one of the source
files to be compiled. Webant will then insert the
bootstrap code into the compiled version of the
specified source file. Alternatively set this to the
empty string for no bootstrap code to be included.
Leave undefined for the bootstrap code to be included
in the entry file specified via the `--entry` key.
--browserGlobalVar, -b Name of the global variable used in the compiled
javascript. Defaults to '__MODULES__'.
--aliases Parameters that can be used as part of `require`
calls. Example: passing `--aliases.foo=jquery
--aliases.baz=lib` would mean you could do
`require('../{baz}/{foo}.js')` in your scripts.
--entryModules, -E Path to entry modules. These are the modules that are
automatically loaded when the compiled javascript is
included in a webpage. This can be set multiple
times. Leave blank to default to the entry script
passed via the --entry key. Example usage: -E
path/to/src/1.js -E path/to/src/2.js
--useConfig, -c Path to a JSON configuration file which sets default
options. If this option is set but no path is
provided, the path is assumed to be
'./webant-config.json'. [Required, unless --entry
option is provided]. [Additional options override
those found in the configuration file].
--version, -v Show version.
--help, -h Show help.
--entry, -e Path to entry script relative to current
directory. [Required, unless --useConfig flag is
set].
--dest, -d Path to where compiled output will be written.
--urlDest, -u URL at which the path specified for '--dest' can
be reached. Can be absolute or relative.
Example: --urlDest js/main.js
--postProcess, -p Post-processing to apply to compiled javascript.
Can be either 'compress' (compresses output),
'debug' (adds file and line numbers to output)
or 'none' (no post-processing).
--requireBase, -r Resolve require paths relative to the supplied
directory instead of relative to the file
containing the require.
--defaultExtension, -D Default extension to append to a require path
when no extension is provided. Include the
starting period (e.g. '.ts'). Example: setting
`--defaultExtension .coffee` would mean that
`require('./path/to/file')` would look for
`./path/to/file.coffee`. Defaults to '.js'.
--handlers, -H Additional handlers to use. Can be set multiple
times. Example: -H coffee -H scss -H json
--includeBootstrap, -i The path to the file where the bootstrap code
will be included. This should be set to one of
the source files to be compiled. Webant will
then insert the bootstrap code into the compiled
version of the specified source file.
Alternatively set this to the empty string for
no bootstrap code to be included. Leave
undefined for the bootstrap code to be included
in the entry file specified via the `--entry`
key.
--browserGlobalVar, -b Name of the global variable used in the compiled
javascript. Defaults to '__MODULES__'.
--aliases Parameters that can be used as part of `require`
calls. Example: passing `--aliases.foo=jquery
--aliases.baz=lib` would mean you could do
`require('../{baz}/{foo}.js')` in your scripts.
--entryModules, -E Path to entry modules. These are the modules
that are automatically loaded when the compiled
javascript is included in a webpage. This can be
set multiple times. Leave blank to default to
the entry script passed via the --entry key.
Example usage: -E path/to/src/1.js -E
path/to/src/2.js
--requireExpressionName, -R Name of the function used for requiring
javascript files. Defaults to 'require'.
--useConfig, -c Path to a JSON configuration file which sets
default options. If this option is set but no
path is provided, the path is assumed to be
'./webant-config.json'. [Required, unless
--entry option is provided]. [Additional options
override those found in the configuration file].
--version, -v Show version.
--help, -h Show help.
```

### Configuration file settings
Expand Down
8 changes: 7 additions & 1 deletion lib/ConfigParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ ConfigParser.prototype = {
includeBootstrap:args.includeBootstrap,
browserGlobalVar:args.browserGlobalVar,
entryModules:args.entryModules,
aliases:args.aliases
aliases:args.aliases,
requireExpressionName:args.requireExpressionName
};
} else if (typeof config === "string") {
config = {
Expand Down Expand Up @@ -73,6 +74,7 @@ ConfigParser.prototype = {
settings.includeBootstrap = config.includeBootstrap;
settings.browserGlobalVar = config.browserGlobalVar || "__MODULES__";
settings.entryModules = config.entryModules;
settings.requireExpressionName = config.requireExpressionName || "require";
settings.aliases = {};
settings.handlers = {};

Expand Down Expand Up @@ -140,6 +142,10 @@ ConfigParser.prototype = {
throw new Error("'browserGlobalVar' must be a string.");
}

if (typeof settings.requireExpressionName !== "string") {
throw new Error("'requireExpressionName' must be a string.");
}

if (
(settings.postProcess !== "none")
&& (settings.postProcess !== "debug")
Expand Down
8 changes: 6 additions & 2 deletions lib/RequireFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ RequireFinder.prototype = {
};
context.push(reqData);
},
find:function(ast,filePath) {
find:function(ast,filePath,requireExpressionName) {
requireExpressionName = requireExpressionName || "require";
var _this = this;
var requires = [];
ast.walk(new uglify.TreeWalker(function(node){
if ((node instanceof uglify.AST_Call) && (node.expression.name === "require")) {
if (
(node instanceof uglify.AST_Call)
&& (node.expression.name === requireExpressionName)
) {
var loc = "file: " + node.start.file
+ "; line: " + node.start.line
+ "; col: " + node.start.col;
Expand Down
6 changes: 5 additions & 1 deletion lib/RequireTreeParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ RequireTreeParser.prototype = {
if (!_this.requireFinder) {
_this.requireFinder = new RequireTreeParser.RequireFinder(_this);
}
var requires = _this.requireFinder.find(parnt.ast,parnt.filePath);
var requires = _this.requireFinder.find(
parnt.ast,
parnt.filePath,
opts.requireExpressionName
);
if (requires.length) {
async.eachSeries(requires,function(req,done){
parnt.requires.push(req);
Expand Down
3 changes: 2 additions & 1 deletion lib/Stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ Stringifier.prototype = {
}) ]
}));
}
ret += '\n"' + i + '":function(require,module,exports) {\n';
var n = jsStringEscape(opts.requireExpressionName);
ret += '\n"' + i + '":function(' + n + ',module,exports) {\n';
ret += this._stringifyNode(chunk.modules[i]);
ret += "\n";
ret += "}";
Expand Down
3 changes: 3 additions & 0 deletions lib/util/yargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = require("yargs")
.describe("browserGlobalVar","Name of the global variable used in the compiled javascript. Defaults to '__MODULES__'.")
.describe("aliases","Parameters that can be used as part of `require` calls. Example: passing `--aliases.foo=jquery --aliases.baz=lib` would mean you could do `require('../{baz}/{foo}.js')` in your scripts.")
.describe("entryModules","Path to entry modules. These are the modules that are automatically loaded when the compiled javascript is included in a webpage. This can be set multiple times. Leave blank to default to the entry script passed via the --entry key. Example usage: -E path/to/src/1.js -E path/to/src/2.js")
.describe("requireExpressionName","Name of the function used for requiring javascript files. Defaults to 'require'.")
.describe("useConfig","Path to a JSON configuration file which sets default options. If this option is set but no path is provided, the path is assumed to be './webant-config.json'. [Required, unless --entry option is provided]. [Additional options override those found in the configuration file].")
.describe("version","Show version.")
.describe("help","Show help.")
Expand All @@ -32,6 +33,7 @@ module.exports = require("yargs")
.alias("includeBootstrap","i")
.alias("browserGlobalVar","b")
.alias("entryModules","E")
.alias("requireExpressionName","R")
.alias("useConfig","c")
.alias("version","v")
.alias("help","h")
Expand All @@ -41,6 +43,7 @@ module.exports = require("yargs")
.string("postProcess")
.string("includeBootstrap")
.string("browserGlobalVar")
.string("requireExpressionName")
.string("useConfig")
.boolean("version")
.boolean("help")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webant",
"version": "5.0.1",
"version": "5.1.0",
"description": "Bringing CommonJS-style requires to the browser and more.",
"keywords": ["ant", "browser", "browserify", "build", "builder", "bundle", "bundler", "cjs", "commonjs", "deploy", "generator", "make", "modules", "package", "packager", "require", "requirejs", "web", "webant", "webmake"],
"author": "A Kazim",
Expand Down
16 changes: 14 additions & 2 deletions test/tests/ConfigParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ test("parseConfig 2",function(t){
aliases:{},
includeBootstrap:path.resolve("/path/to/src/js/main.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.resolve("/path/to/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand Down Expand Up @@ -96,6 +97,7 @@ test("parseConfig 3",function(t){
aliases:{},
includeBootstrap:path.resolve("/path/to/src/js/main.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.resolve("/path/to/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand Down Expand Up @@ -160,6 +162,7 @@ test("parseConfig 4",function(t){
aliases:{},
includeBootstrap:path.resolve("/path/to/src/js/main.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.resolve("/path/to/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand Down Expand Up @@ -245,6 +248,7 @@ test("parseConfig 5",function(t){
},
includeBootstrap:path.resolve("/path/to/src/js/main.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.resolve("/path/to/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand Down Expand Up @@ -319,6 +323,7 @@ test("parseConfig 7",function(t) {
aliases:{},
includeBootstrap:path.resolve("/path/to/src/js/main.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.resolve("/path/to/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand Down Expand Up @@ -349,6 +354,7 @@ test("parseConfig 8",function(t) {
aliases:{},
includeBootstrap:path.resolve("/src/js/main.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.resolve("/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand All @@ -367,7 +373,8 @@ test("parseConfig 9",function(t){
"--no-includeBootstrap",
"--aliases.foo",
"bar",
"--aliases.baz=val"
"--aliases.baz=val",
"--requireExpressionName=bar"
];

var settings = parseConfig(rawConfig);
Expand All @@ -390,6 +397,7 @@ test("parseConfig 9",function(t){
},
includeBootstrap:false,
browserGlobalVar:"__MODULES__",
requireExpressionName:"bar",
entryModules:[path.resolve("/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand Down Expand Up @@ -425,6 +433,7 @@ test("parseConfig 10",function(t){
aliases:{},
includeBootstrap:path.join(__dirname,"nested","foo","bar","baz.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.join(__dirname,"nested","foo","bar","baz.js")]
},
"Defaults should have been merged in properly."
Expand Down Expand Up @@ -452,6 +461,7 @@ test("parseConfig 11",function(t){
aliases:{},
includeBootstrap:path.resolve("/path/to/src/js/main.js"),
browserGlobalVar:"__MODULES__",
requireExpressionName:"require",
entryModules:[path.resolve("/path/to/src/js/main.js")]
},
"Defaults should have been merged in properly."
Expand All @@ -469,7 +479,8 @@ test("parseConfig 12",function(t) {
entryModules:["foo/bar.js","baz/boosh.js"],
files:[{
entry:"/src/js/contact.js"
}]
}],
requireExpressionName:"foobar",
};

var settings = parseConfig(rawConfig);
Expand All @@ -492,6 +503,7 @@ test("parseConfig 12",function(t) {
aliases:{},
includeBootstrap:false,
browserGlobalVar:"foo",
requireExpressionName:"foobar",
entryModules:[path.resolve("foo/bar.js"),path.resolve("baz/boosh.js")]
},
"Defaults should have been merged in properly."
Expand Down
6 changes: 6 additions & 0 deletions test/tests/webant-requireexpressionname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var webantTester = require("../lib/webant.js");

webantTester("requireexpressionname",1,{requireExpressionName:"_abc_"},function(obj,data,done){
data.t.equivalent(obj,{msg:"MWAHAHAH!",a:"I'm from a.js"});
done();
});
11 changes: 11 additions & 0 deletions test/webant/requireexpressionname/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>

</body>
</html>
1 change: 1 addition & 0 deletions test/webant/requireexpressionname/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "I'm from a.js";
1 change: 1 addition & 0 deletions test/webant/requireexpressionname/src/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.msg = "MWAHAHAH!";
6 changes: 6 additions & 0 deletions test/webant/requireexpressionname/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var g = {};

g.msg = _abc_("./b.js").msg;
g.a = _abc_("./a.js");

window.__global = g;

0 comments on commit 49de9dd

Please sign in to comment.