Skip to content

Commit

Permalink
Fix volofile to build and optimize libraries loaded by static script …
Browse files Browse the repository at this point in the history
…tags and workaround r.js bug that attempts to read commented-out modules, r=paired with jrburke
  • Loading branch information
Dan Mosedale committed May 10, 2012
1 parent fce2dc4 commit 373bd60
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
64 changes: 49 additions & 15 deletions volofile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ define(function (require) {
}, },


run: function (d, v, namedArgs) { run: function (d, v, namedArgs) {
var optimize = namedArgs.optimize || 'uglify';

q.call(function () { q.call(function () {
//Remove the old dir //Remove the old dir
v.rm(buildDir); v.rm(buildDir);
Expand All @@ -58,8 +60,6 @@ define(function (require) {
}) })
.then(function () { .then(function () {
//JS go time //JS go time
var optimize = namedArgs.optimize || 'uglify';

if (namedArgs.dynamic) { if (namedArgs.dynamic) {
//Still use require.js to load the app.js file. //Still use require.js to load the app.js file.
return v.spawn('node', ['tools/r.js', '-o', return v.spawn('node', ['tools/r.js', '-o',
Expand Down Expand Up @@ -94,21 +94,55 @@ define(function (require) {
} }
}); });


//If almond is in use, it is built into app.js, so need var indexName = buildDir + '/index.html';
//to update the script tag to just load app.js instead. var indexContent = v.read(indexName);
if (!namedArgs.dynamic) { var scriptBuffer = "";
var indexName = buildDir + '/index.html',
contents = v.read(indexName), indexContent = indexContent.replace(/<!--[\s\S]*?-->/g, "")
scriptRegExp = /(<script[^>]+data-main="[^"]+"[^>]+)(src="[^"]+")([^>]+>\s*<\/script>)/; .replace(/<script.*?src="([^"]*)".*?>\s*<\/script>/g,

function(fullMatch, src){
contents = contents.replace(scriptRegExp,
function (match, pre, script, post) { // treat require.js specially, depending on link mode
return pre + 'src="js/app.js"' + post; 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); v.write(indexName, indexContent);
}
return buildOutput; 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) { .then(function (buildOutput) {
d.resolve(buildOutput); d.resolve(buildOutput);
Expand Down
12 changes: 11 additions & 1 deletion www/index.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
<title>Template Game</title> <title>Template Game</title>
</head> </head>
<body> <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 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> </body>
</html> </html>
21 changes: 11 additions & 10 deletions www/js/app/main.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
// time you need it. <http://requirejs.org/> has detailed documentation. // 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) { 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');


}); });

0 comments on commit 373bd60

Please sign in to comment.