Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix volofile to build and optimize libraries loaded by static script …

…tags and workaround r.js bug that attempts to read commented-out modules, r=paired with jrburke
  • Loading branch information...
commit 373bd6016e5f486de84c5f9268c707adf93d00e5 1 parent fce2dc4
@dmose dmose authored
Showing with 71 additions and 26 deletions.
  1. +49 −15 volofile
  2. +11 −1 www/index.html
  3. +11 −10 www/js/app/main.js
View
64 volofile
@@ -35,6 +35,8 @@ define(function (require) {
},
run: function (d, v, namedArgs) {
+ var optimize = namedArgs.optimize || 'uglify';
+
q.call(function () {
//Remove the old dir
v.rm(buildDir);
@@ -58,8 +60,6 @@ define(function (require) {
})
.then(function () {
//JS go time
- var optimize = namedArgs.optimize || 'uglify';
-
if (namedArgs.dynamic) {
//Still use require.js to load the app.js file.
return v.spawn('node', ['tools/r.js', '-o',
@@ -94,21 +94,55 @@ define(function (require) {
}
});
- //If almond is in use, it is built into app.js, so need
- //to update the script tag to just load app.js instead.
- if (!namedArgs.dynamic) {
- var indexName = buildDir + '/index.html',
- contents = v.read(indexName),
- scriptRegExp = /(<script[^>]+data-main="[^"]+"[^>]+)(src="[^"]+")([^>]+>\s*<\/script>)/;
-
- contents = contents.replace(scriptRegExp,
- function (match, pre, script, post) {
- return pre + 'src="js/app.js"' + post;
+ var indexName = buildDir + '/index.html';
+ var indexContent = v.read(indexName);
+ var scriptBuffer = "";
+
+ indexContent = indexContent.replace(/<!--[\s\S]*?-->/g, "")
+ .replace(/<script.*?src="([^"]*)".*?>\s*<\/script>/g,
+ function(fullMatch, src){
+
+ // treat require.js specially, depending on link mode
+ if (/require\.js$/.test(src)) {
+ if (namedArgs.dynamic) {
+ return fullMatch; // keep it so the app can call require
+ } else {
+ return ""; // optimize it away for static builds
+ }
+ }
+
+ if (src === "js/app.js" || src.indexOf(':') !== -1) {
+ // This is either our app's entry point, or
+ // something we don't control, so leave it alone.
+ return fullMatch;
+ }
+
+ // otherwise, we've got a script file that we want
+ // to add to the head of app.js. we copy from
+ // the source dir, since this won't exist in the
+ // the built dir in the static case
+ scriptBuffer += v.read('www/' + src) + ";";
+
+ // and remove the <script> tag from the HTML
+ return "";
});
- v.write(indexName, contents);
- }
- return buildOutput;
+ v.write(indexName, indexContent);
+
+ var appJs = buildDir + "/js/app.js";
+ v.write(appJs, scriptBuffer + v.read(appJs));
+
+ // Re-optimize to capture the prepended scripts
+ return v.spawn('node', ['tools/r.js', '-o',
+ 'baseUrl=' + buildDir + '/js',
+ 'name=app',
+ 'out=' + buildDir + '/js/app-temp.js',
+ 'optimize=' + optimize], {
+ useConsole: !namedArgs.quiet
+ });
+ })
+ .then(function() {
+ v.mv(buildDir + '/js/app-temp.js', buildDir + '/js/app.js');
})
.then(function (buildOutput) {
d.resolve(buildOutput);
View
12 www/index.html
@@ -7,7 +7,17 @@
<title>Template Game</title>
</head>
<body>
+ <!-- Insert web content here -->
+
+ <!-- If you need/want to load libraries via <script> rather than
+ require.js, it's safest to insert those script tags here before
+ require loads -->
+
<script src="js/lib/require.js" charset="utf-8"></script>
- <script async src="js/app.js" charset="utf-8"></script>
+
+ <!-- volofile has special knowledge of the name app.js and uses
+ it during the build process so, so look over there too if you want
+ to change this -->
+ <script src="js/app.js" charset="utf-8"></script>
</body>
</html>
View
21 www/js/app/main.js
@@ -5,17 +5,18 @@
// time you need it. <http://requirejs.org/> has detailed documentation.
//
//
-// Toji's game-shim module is included with this template; if you wish to use
-// it, replace the define() below this comment with this one:
-//
-// define(['game-shim'], function (gameshim) {
-//
-// The reason that this isn't done by default is that the replacement
-// versions of the various APIs that it includes could potentially cause
-// complications or performance issues with other libraries that use these APIs
-// and may have their own strategies for dealing with cross-browser issues.
-// We suggest reviewing the code (in lib/game-shim.js) before enabling it.
//
define(function(require) {
+
+ // Toji's game-shim module is included with this template; if you wish to use
+ // it, uncomment the require() line below.
+ //
+ // The reason that this isn't done by default is that the replacement
+ // versions of the various APIs that it includes could potentially cause
+ // complications or performance issues with other libraries that use these APIs
+ // and may have their own strategies for dealing with cross-browser issues.
+ // We suggest reviewing the code (in lib/game-shim.js) before enabling it.
+ //
+ //require('game-shim');
});
Please sign in to comment.
Something went wrong with that request. Please try again.