Skip to content

Commit

Permalink
Fix for static root being an external CDN/URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhughes committed Mar 23, 2016
1 parent dddcdac commit ce86dd6
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions lib/optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,19 @@ VersionedFile.prototype.getIntegrity = function() {

return HASH_FUNCTION + '-' + this.hash_;
};
VersionedFile.prototype.getVersionedFilename = function() {
VersionedFile.prototype.getVersionedFilename = function(staticPrefix, publicStaticRoot) {
if (!this.hash_) {
throw new Error('Hash not computed yet');
}

var parts = this.filename_.split('.');
parts[0] += '-v' + this.hash_.replace(/\+/g, '-').replace(/\//g, '_').substring(0, 24);
return parts.join('.');
var filename = parts.join('.');

if (publicStaticRoot) {
return filename.replace(staticPrefix, publicStaticRoot);
}
return filename;
};


Expand All @@ -87,10 +92,9 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {
var options = opt_options || {};

var aliasMapFilename = options.aliasMap;
var publicStaticRoot = options.staticRoot || '';

// COMBINE MODULES FOR PUBLIC SERVING
var STATIC_PREFIX = '/' + server.staticPrefix;
var staticPrefix = '/' + server.staticPrefix;
var publicStaticRoot = options.staticRoot || '';

var addFile;
var forEachFile;
Expand All @@ -117,7 +121,7 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {

console.log('Building bundle "' + bundle.name + '"...');

var cssFile = new VersionedFile(path.join(STATIC_PREFIX, bundle.name + '.css'));
var cssFile = new VersionedFile(path.join(staticPrefix, bundle.name + '.css'));
bundle.allStyles().forEach(function(css) {
var body = css.bodyContentType().body;
bundleStylesheets.push(body);
Expand All @@ -128,7 +132,7 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {
cssFile.setContent(bundleStylesheets.join('\n'));
addFile(cssFile);

var jsFile = new VersionedFile(path.join(STATIC_PREFIX, bundle.name + '.js'));
var jsFile = new VersionedFile(path.join(staticPrefix, bundle.name + '.js'));
bundle.allScripts().forEach(function(script) {
if (!script.includeInEnvironment(TargetEnvironment.BROWSER)) {
return;
Expand Down Expand Up @@ -208,7 +212,7 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {
extension = '.css';
}
var relativeFilename = res.resourcePath.replace(/[:\.]/g, '_') + extension;
var staticFilename = path.join(STATIC_PREFIX, '__resource__', relativeFilename);
var staticFilename = path.join(staticPrefix, '__resource__', relativeFilename);

var file = new VersionedFile(staticFilename);
file.setContent(bodyContentType.body);
Expand All @@ -225,7 +229,7 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {
forEachFile(function(file) {
var filename = file.getFilename();

if (/\.js$/.test(filename) && filename.indexOf(STATIC_PREFIX) == 0) {
if (/\.js$/.test(filename) && filename.indexOf(staticPrefix) == 0) {
console.log('Minifying:', filename, '...');

var uglifyOptions = {fromString: true};
Expand Down Expand Up @@ -258,7 +262,7 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {
forEachFile(function(file) {
var filename = file.getFilename();
fileByName[filename] = file;
if (filename.indexOf(STATIC_PREFIX) == 0) {
if (filename.indexOf(staticPrefix) == 0) {
staticBasenames[path.basename(filename)] = 1;
}
});
Expand All @@ -269,42 +273,36 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {
"(['\"(])(/?(?:[\\w.-]+/)*(?:" + escapedBasenames.join('|') + "))(\\\\?['\")])", 'g');

function updateFileAndReferences(file) {
if (file.getHash()) {
return file.getVersionedFilename();
}

if (file.visiting) {
throw new Error('Cyclical dependency! ' + file.getFilename());
}
file.visiting = true;

var content = file.getContent();
if (content.replace) {
content = content.replace(buildFilenamesRegex, function(_, quote1, filenameMatch, quote2) {
if (filenameMatch.indexOf('/') != 0 && filenameMatch.indexOf('://') == -1) {
filenameMatch = path.join(path.dirname(filename), filenameMatch);
}
if (!file.getHash()) {
if (file.visiting) {
throw new Error('Cyclical dependency! ' + file.getFilename());
}
file.visiting = true;

var referencedFile = fileByName[filenameMatch];
if (referencedFile) {
filenameMatch = updateFileAndReferences(referencedFile);
var content = file.getContent();
if (content.replace) {
content = content.replace(buildFilenamesRegex, function(_, quote1, filenameMatch, quote2) {
if (filenameMatch.indexOf('/') != 0 && filenameMatch.indexOf('://') == -1) {
filenameMatch = path.join(path.dirname(filename), filenameMatch);
}

if (publicStaticRoot) {
filenameMatch = publicStaticRoot + filenameMatch.replace(STATIC_PREFIX, '');
var referencedFile = fileByName[filenameMatch];
if (referencedFile) {
filenameMatch = updateFileAndReferences(referencedFile);
}
}
return quote1 + filenameMatch + quote2;
});
return quote1 + filenameMatch + quote2;
});

// this will fail if the node has already been visited.
file.setContent(content);
}
// this will fail if the node has already been visited.
file.setContent(content);
}

// After all the replacements are done, actually update the file.
file.computeHash();
delete file.visiting;
// After all the replacements are done, actually update the file.
file.computeHash();
delete file.visiting;
}

return file.getVersionedFilename();
return file.getVersionedFilename(staticPrefix, publicStaticRoot);
}

forEachFile(updateFileAndReferences);
Expand All @@ -317,7 +315,7 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {
var paths = file.getResourcePaths();
paths.forEach(function(resourcePath) {
moduleToStaticAliasMap[resourcePath] = {
path: file.getVersionedFilename(),
path: file.getVersionedFilename(staticPrefix, publicStaticRoot),
integrity: file.getIntegrity(),
};
});
Expand All @@ -341,7 +339,8 @@ function optimizeServer(server, optimizedPackagePath, opt_options) {

var filename = file.getFilename();
var outfiles = [filename];
if (filename.indexOf(STATIC_PREFIX) === 0) {
if (filename.indexOf(staticPrefix) === 0) {
// NOTE: Leave staticPrefix here because these are local filenames.
outfiles.push(file.getVersionedFilename());
}

Expand Down

0 comments on commit ce86dd6

Please sign in to comment.