Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v0.3.33
Fetching contributors…

Cannot retrieve contributors at this time

executable file 110 lines (103 sloc) 5.862 kb
#!/usr/bin/env node
var _ = require('underscore'),
AssetGraph = require('assetgraph'),
oneBootstrapper = require('../lib/util/oneBootstrapper'),
query = AssetGraph.query,
urlTools = require('assetgraph/lib/util/urlTools'),
commandLineOptions = require('optimist').
usage('$0 --root <inputRootDirectory> [--label <labelName>=<dir> ...] --outroot <dir> [--less] [--deferscripts] [--asyncscripts] [--locale <localeId>,...] [--cdnroot <url>] [--cdnoutroot <dir>] [--manifest] [--nocompress] [--mangletoplevel] [--optimizepngs] <htmlFile>...').
boolean('less', 'deferscripts', 'asyncscripts', 'manifest', 'nocompress', 'mangletoplevel', 'optimizepngs').
demand(['root', 'outroot']).
argv,
outroot = urlTools.fsDirToFileUrl(commandLineOptions.outroot),
cdnroot = commandLineOptions.cdnroot && urlTools.ensureTrailingSlash(commandLineOptions.cdnroot),
cdnoutroot = urlTools.fsDirToFileUrl(commandLineOptions.cdnoutroot),
localeIds = commandLineOptions.locale && _.flatten(_.flatten([commandLineOptions.locale]).map(function (localeId) {return localeId.split(",");}));
require('../lib/registerTransforms');
new AssetGraph({root: commandLineOptions.root})
.on('afterTransform', function (transform, elapsedTime) {
console.log((elapsedTime / 1000).toFixed(3) + " secs: " + transform.name);
})
.on('error', function (err) {
console.error(err.stack);
process.exit(1);
})
.registerRequireJsConfig()
.registerLabelsAsCustomProtocols(commandLineOptions.label)
.loadAssets(commandLineOptions._.map(urlTools.fsFilePathToFileUrl))
.populate({
followRelations: query.or({to: {type: 'I18n'}},
{type: query.not(['JavaScriptOneInclude', 'JavaScriptExtJsRequire', 'JavaScriptCommonJsRequire', 'HtmlAnchor']), to: {url: query.not(/^https?:/)}})
})
.stripDevelopmentModeFromOneBootstrapper({type: 'Html', isInitial: true})
.if(cdnroot)
.queue(function addCdnRootToOneBootstrapper(assetGraph) {
oneBootstrapper.findOneBootstrappersInGraph(assetGraph).forEach(function (javaScript) {
javaScript.parseTree[1].splice(1, 0, ['stat', ['assign', true, ['dot', ['name', 'one'], 'cdnRoot'], ['string', cdnroot]]]);
});
})
.endif()
.addContentVersionMetaElement({type: 'Html'}, '{0}/production', true)
.if(commandLineOptions.less)
// Replace Less assets with their Css counterparts:
.compileLessToCss({type: 'Less'})
// Kill in-browser less compiler and remove its incoming relations:
.removeAssets({url: /\/less(?:-\d+\.\d+\.\d+)?(?:\.min)?\.js$/}, true)
// Find and populate CssImage relations from the compiled Less assets:
.populate({from: {type: 'Css'}})
.endif()
.removeRelations({type: 'JavaScriptOneInclude', to: {type: ['Css', 'JavaScript']}}, {detach: true, unresolved: true})
.convertCssImportsToHtmlStyles()
.removeAssets({isEmpty: true, type: ['Css', 'JavaScript']}, true)
.externalizeRelations({from: {type: query.not('Htc')}, type: ['HtmlStyle', 'HtmlScript'], node: function (node) {return !node.hasAttribute('nobundle');}})
.mergeIdenticalAssets(query.or({isImage: true}, {type: ['JavaScript', 'Css']}))
.spriteBackgroundImages()
.postProcessCssImages()
.if(commandLineOptions.optimizepngs)
.optimizePngs()
.endif()
.bundleRequireJs({type: 'Html'})
.bundleRelations({type: ['HtmlStyle', 'HtmlScript'], node: function (node) {return !node.hasAttribute('nobundle');}})
.removeNobundleAttribute({type: ['HtmlScript', 'HtmlStyle']})
.inlineCssImagesWithLegacyFallback({type: 'Html', isInline: false}, 8192)
.if(commandLineOptions.mangletoplevel)
.pullGlobalsIntoVariables({type: 'JavaScript'})
.endif()
.if(!commandLineOptions.nocompress)
.compressJavaScript({type: 'JavaScript'}, 'uglifyJs', {toplevel: commandLineOptions.mangletoplevel})
.endif()
.if(localeIds)
.cloneForEachLocale({type: 'Html', isInitial: true}, localeIds)
.runJavaScriptConditionalBlocks({isInitial: true}, 'localize', true)
.endif()
.removeAssets({type: 'I18n'}, true)
.minifyAssets()
.inlineRelations({
type: ['HtmlStyle', 'HtmlScript'],
from: {isInline: false}, // Excludes relations occurring in conditional comments
to: function (asset) {return asset.isAsset && asset.rawSrc.length < 4096;}
})
.if(commandLineOptions.manifest)
.addCacheManifest({isInitial: true})
.endif()
.if(commandLineOptions.nocompress)
.prettyPrintAssets({type: ['Html', 'JavaScript', 'Css']})
.endif()
.setAsyncOrDeferOnHtmlScripts({to: {isInline: false, url: /^file:/}}, commandLineOptions.asyncscripts, commandLineOptions.deferscripts)
.omitGetStaticUrlFunctionCall()
.inlineRelations({type: 'JavaScriptOneGetText'})
.moveAssetsInOrder({isInitial: query.not(true), type: query.not('CacheManifest')}, function (asset, assetGraph) {
var targetUrl = "/static/";
// Conservatively assume that all one.getStaticUrl relations pointing at non-images are intended to be fetched via XHR
// and thus cannot be put on a CDN because of same origin restrictions:
if (cdnroot && (asset.isImage || assetGraph.findRelations({to: asset, type: 'StaticUrlMapEntry'}).length === 0)) {
targetUrl = cdnroot;
}
return targetUrl + asset.md5Hex.substr(0, 10) + asset.extension + asset.url.replace(/^[^#\?]*/, ''); // Preserve query string and fragment identifier
})
.writeAssetsToDisc({url: /^file:/}, outroot)
.if(cdnroot)
.writeAssetsToDisc({url: query.createPrefixMatcher(cdnroot)}, cdnoutroot || outroot, cdnroot)
.endif()
.writeStatsToStderr()
.run();
Jump to Line
Something went wrong with that request. Please try again.