Skip to content
This repository
tag: v0.2.33
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 80 lines (76 sloc) 5.024 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#!/usr/bin/env node

var _ = require('underscore'),
    AssetGraph = require('assetgraph'),
    transforms = require('../lib/transforms'),
    query = AssetGraph.query,
    urlTools = require('assetgraph/lib/util/urlTools'),
    commandLineOptions = require('optimist').
        usage('$0 --root <inputRootDirectory> [--label <labelName>=<dir> ...] --outroot <dir> [--locale <localeId>,...] [--cdnroot <url>] [--cdnoutroot <dir>] [--manifest] [--nocompress] <htmlFile>...').
        boolean('manifest').
        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(",");}));

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);
}).queue(
    transforms.registerLabelsAsCustomProtocols(commandLineOptions.label),
    transforms.loadAssets(commandLineOptions._.map(urlTools.fsFilePathToFileUrl)),
    transforms.populate({
        followRelations: query.or({to: {type: 'I18n'}},
                                  {type: 'HtmlAnchor', to: {type: query.not('Html'), url: query.not(/^https?:/)}},
                                  {type: query.not(['JavaScriptOneInclude', 'JavaScriptExtJsRequire', 'JavaScriptCommonJsRequire', 'HtmlAnchor']), to: {url: query.not(/^https?:/)}})
    }),
    localeIds && transforms.stripDevelopmentModeFromOneBootstrapper({type: 'Html', isInitial: true}),
    transforms.removeRelations({type: 'JavaScriptOneInclude', to: {type: ['Css', 'JavaScript']}}, {detach: true, unresolved: true}),
    transforms.inlineRelations({type: 'JavaScriptOneGetText'}),
    transforms.removeRelations({type: 'JavaScriptOneGetText'}, {removeOrphan: true}),
    transforms.convertCssImportsToHtmlStyles(),
    transforms.removeAssets({isEmpty: true, type: ['Css', 'JavaScript']}, true),
    transforms.externalizeRelations({from: {type: query.not('Htc')}, type: ['HtmlStyle', 'HtmlScript'], node: function (node) {return !node.getAttribute('nobundle');}}),
    transforms.mergeIdenticalAssets({type: ['JavaScript', 'Css']}),
    require('assetgraph-sprite')(),
    transforms.postProcessCssImages(),
    transforms.bundleAssets({type: 'Css', incoming: {type: 'HtmlStyle', node: function (node) {return !node.getAttribute('nobundle') && !node.getAttribute('media');}}}),
    transforms.bundleAssets({type: 'JavaScript', incoming: {type: 'HtmlScript', node: function (node) {return !node.getAttribute('nobundle');}}}),
    function removeNobundleAttribute(assetGraph) {
        assetGraph.findRelations({type: ['HtmlScript', 'HtmlStyle']}).forEach(function (relation) {
            if (relation.node.hasAttribute('nobundle')) {
                relation.node.removeAttribute('nobundle');
                relation.from.markDirty();
            }
        });
    },
    transforms.inlineCssImagesWithLegacyFallback({type: 'Html', isInline: false}, 8192),
    !commandLineOptions.nocompress && transforms.compressJavaScript({type: 'JavaScript'}),
    localeIds && transforms.cloneForEachLocale({type: 'Html', isInitial: true}, localeIds),
    localeIds && transforms.runJavaScriptConditionalBlocks({isInitial: true}, 'localize', true),
    transforms.removeAssets({type: 'I18n'}, true),
    transforms.minifyAssets(),
    transforms.inlineRelations({
        type: ['HtmlStyle', 'HtmlScript'],
        from: {isInline: false}, // Excludes relations occurring in conditional comments
        to: function (asset) {return asset.isAsset && asset.rawSrc.length < 4096;}
    }),
    commandLineOptions.manifest && transforms.addCacheManifest({isInitial: true}),
    commandLineOptions.nocompress && transforms.prettyPrintAssets({type: ['Html', 'JavaScript', 'Css']}),
    transforms.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;

    }),
    transforms.parallel(
        transforms.writeAssetsToDisc({url: /^file:/}, outroot),
        cdnroot && transforms.writeAssetsToDisc({url: query.createPrefixMatcher(cdnroot)}, cdnoutroot || outroot, cdnroot)
    ),
    transforms.stats()
).run();
Something went wrong with that request. Please try again.