Skip to content

Commit b449bdf

Browse files
committed
Add in a write.asModule function to assist text transform plugins.
1 parent 71d47e6 commit b449bdf

6 files changed

Lines changed: 83 additions & 37 deletions

File tree

build/jslib/build.js

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -592,14 +592,9 @@ function (lang, logger, file, parse, optimize, pragma,
592592
build.flattenModule = function (module, layer, config) {
593593
var buildFileContents = "", requireContents = "",
594594
context = require.s.contexts._,
595-
//This regexp is not bullet-proof, and it has one optional part to
596-
//avoid issues with some Dojo transition modules that use a
597-
//define(\n//begin v1.x content
598-
//for a comment.
599-
anonDefRegExp = /(require\s*\.\s*def|define)\s*\(\s*(\/\/[^\n\r]*[\r\n])?(\[|f|\{)/,
600-
prop, path, reqIndex, fileContents, currContents,
601-
i, moduleName, specified, deps, includeRequire,
602-
parts, builder;
595+
path, reqIndex, fileContents, currContents,
596+
i, moduleName, includeRequire,
597+
parts, builder, writeApi;
603598

604599
//Use override settings, particularly for pragmas
605600
if (module.override) {
@@ -643,37 +638,19 @@ function (lang, logger, file, parse, optimize, pragma,
643638
builder = parts.prefix && require.pluginBuilders[parts.prefix];
644639
if (builder) {
645640
if (builder.write) {
646-
builder.write(parts.prefix, parts.name, function (input) {
641+
writeApi = function (input) {
647642
fileContents += input;
648-
});
643+
};
644+
writeApi.asModule = function (moduleName, input) {
645+
fileContents += build.toTransport(moduleName, path, input, layer);
646+
};
647+
builder.write(parts.prefix, parts.name, writeApi);
649648
}
650649
} else {
651650
//Add the contents but remove any pragmas.
652651
currContents = pragma.process(path, file.readFile(path), config);
653652

654-
//If anonymous module, insert the module name.
655-
currContents = currContents.replace(anonDefRegExp, function (match, callName, possibleComment, suffix) {
656-
layer.modulesWithNames[moduleName] = true;
657-
658-
//Look for CommonJS require calls inside the function if this is
659-
//an anonymous define/require.def call that just has a function registered.
660-
deps = null;
661-
if (suffix.indexOf('f') !== -1) {
662-
deps = parse.getAnonDeps(path, currContents);
663-
664-
if (deps.length) {
665-
deps = deps.map(function (dep) {
666-
return "'" + dep + "'";
667-
});
668-
} else {
669-
deps = null;
670-
}
671-
}
672-
673-
return "define('" + moduleName + "'," +
674-
(deps ? ('[' + deps.toString() + '],') : '') +
675-
suffix;
676-
});
653+
currContents = build.toTransport(moduleName, path, currContents, layer);
677654

678655
fileContents += currContents;
679656
}
@@ -698,5 +675,38 @@ function (lang, logger, file, parse, optimize, pragma,
698675
};
699676
};
700677

678+
//This regexp is not bullet-proof, and it has one optional part to
679+
//avoid issues with some Dojo transition modules that use a
680+
//define(\n//begin v1.x content
681+
//for a comment.
682+
build.anonDefRegExp = /(require\s*\.\s*def|define)\s*\(\s*(\/\/[^\n\r]*[\r\n])?(\[|f|\{)/;
683+
684+
build.toTransport = function (moduleName, path, contents, layer) {
685+
//If anonymous module, insert the module name.
686+
return contents.replace(build.anonDefRegExp, function (match, callName, possibleComment, suffix) {
687+
layer.modulesWithNames[moduleName] = true;
688+
689+
//Look for CommonJS require calls inside the function if this is
690+
//an anonymous define/require.def call that just has a function registered.
691+
var deps = null;
692+
if (suffix.indexOf('f') !== -1) {
693+
deps = parse.getAnonDeps(path, contents);
694+
695+
if (deps.length) {
696+
deps = deps.map(function (dep) {
697+
return "'" + dep + "'";
698+
});
699+
} else {
700+
deps = null;
701+
}
702+
}
703+
704+
return "define('" + moduleName + "'," +
705+
(deps ? ('[' + deps.toString() + '],') : '') +
706+
suffix;
707+
});
708+
709+
};
710+
701711
return build;
702712
});

build/tests/builds.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,24 @@ define(['build', 'env!env/file'], function (build, file) {
120120
]
121121
);
122122
doh.run();
123+
124+
doh.register("buildPluginAsModule",
125+
[
126+
function buildPluginAsModule(t) {
127+
build(["..", "name=refine!a", "out=builds/refineATest.js",
128+
"baseUrl=../../tests/plugins/fromText",
129+
"excludeShallow=require/text,refine",
130+
"paths.require=../../../require", "optimize=none"]);
131+
132+
t.is(nol(nol(c("../../tests/plugins/fromText/a.refine")
133+
.replace(/refine/g, 'define')))
134+
.replace(/define\(\{/, "define('refine!a',{"),
135+
nol(c("builds/refineATest.js")));
136+
137+
require._buildReset();
138+
}
139+
140+
]
141+
);
142+
doh.run();
123143
});

docs/plugins.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ <h3>
227227
<ul>
228228
<li><b>pluginName</b>: String. The <b>normalized</b> name for the plugin. Most plugins will not be authored with a name (they will be anonymous plugins) so it is useful to know the normalized name for the plugin module for use in the optimized file.</li>
229229
<li><b>name</b>: String. The <b>normalized</b> resource name.</li>
230-
<li><b>write</b>: Function. A function to be called with a string of output to write to the optimized file.</li>
230+
<li><b>write</b>: Function. A function to be called with a string of output to write to the optimized file. This function also contains a property function, <b>write.asModule(moduleName, text)</b>. asModule can be used to write out a module that may have an anonymous define call in there that needs name insertion or/and contains implicit require("") dependencies that need to be pulled out for the optimized file. asModule is useful for text transform plugins, like a CoffeeScript plugin.</li>
231231
</ul>
232232

233233
<p>The text! plugin implements write, to write out a string value for the text file that it loaded. A snippet from that file:</p>

tasks.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Next release
22
--------------
33
- update to jQuery 1.5.1 when released
44
- Consider putting in the version detection?
5+
- Make sure node execution uses directory of the top js file as the baseUrl, not the current working directory.
56
- Allow specifying the use of a plugin in the data-main property.
67
- Move up plugins, or move them to their own repo?
78
- build tool to rename require and define calls to other names?
@@ -13,6 +14,11 @@ flag was set to false when calling ast_squeeze. If I left this flag set to true
1314
- localeList for i18n.js support, for better Dojo support.
1415
- Point to Miller's gist for ant integration: https://gist.github.com/825117
1516
- Put in error message with URL refs to faq page?
17+
- one candidate: mismatched require.def calls: happens when loading some modules/scripts with inline script tags that include a named module,
18+
but later the anonymous version is loaded: so the named one is in a built file.
19+
- Doc that r.js finds relative path entries if not in the config explicitly.
20+
- Doc require.toUrl and anything else expected on parentRequire in plugins.
21+
- use setimeout for the data-main require? to assist cases where require.js contains all the code for the page?
1622

1723
Bugs to work on
1824
------------------

tests/all.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ doh.registerUrl("allplugins-text", "../layers/allplugins-text.html");
5656
doh.registerUrl("afterload", "../afterload.html", 10000);
5757

5858
doh.registerUrl("pluginsSync", "../plugins/sync.html");
59-
doh.registerUrl("pluginsFromText", "../plugins/fromText.html");
59+
doh.registerUrl("pluginsFromText", "../plugins/fromText/fromText.html");
6060
doh.registerUrl("text", "../text/text.html");
6161
doh.registerUrl("textOnly", "../text/textOnly.html");
6262
doh.registerUrl("jsonp", "../jsonp/jsonp.html");

tests/plugins/fromText/refine.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
/*global define: false, require: false */
44

55
(function () {
6-
7-
86
//Load the text plugin, so that the XHR calls can be made.
7+
var buildMap = {};
98

109
define(['require/text'], function (text) {
1110
return {
@@ -14,6 +13,10 @@
1413
require.fetchText(url, function (text) {
1514
text = text.replace(/refine/g, 'define');
1615

16+
if (config.isBuild) {
17+
buildMap[name] = text;
18+
}
19+
1720
//Add in helpful debug line
1821
text += "\r\n//@ sourceURL=" + url;
1922

@@ -23,6 +26,13 @@
2326
load(value);
2427
});
2528
});
29+
},
30+
31+
write: function (pluginName, name, write) {
32+
if (name in buildMap) {
33+
var text = buildMap[name];
34+
write.asModule(pluginName + "!" + name, text);
35+
}
2636
}
2737
};
2838
});

0 commit comments

Comments
 (0)