Skip to content
This repository
tag: v1.1.6
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 251 lines (241 sloc) 10.942 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
#!/usr/bin/env node

var optimist = require('optimist'),
    commandLineOptions = optimist
        .usage('$0 --root <inputRootDirectory> --outroot <dir> [options] <htmlFile(s)>')
        .options('h', {
            alias: 'help',
            describe: 'Show this help',
            type: 'boolean',
            default: false
        })
        .options('root', {
            describe: 'Path to your web root (will be deduced from your input files if not specified)',
            type: 'string',
            demand: false
        })
        .options('outroot', {
            describe: 'Path to the output folder. Will be generated if non-existing',
            type: 'string',
            demand: true
        })
        .options('cdnroot', {
            describe: 'URI root where the static assets will be deployed. Must be either an absolute or a protocol-relative url.',
            type: 'string',
            demand: false
        })
        .options('cdnoutroot', {
            describe: 'Path to the CDN output folder. Will be generated if non-existing',
            type: 'string',
            demand: false
        })
        .options('exclude', {
            describe: 'Url pattern to exclude from the build. Supports * wildcards. You can create multiple of these: --exclude *.php --exclude http://example.com/*.gif',
            type: 'string',
            demand: false
        })
        .options('label', {
            describe: 'Registers labels as custom protocols for path resolving. You can create multiple of these: --label <labelName>=<dir> --label <otherLabelName>=<otherDir>',
            type: 'string',
            demand: false
        })
        .options('parentdir', {
            describe: 'If an unknown label (scheme) is found, look for at parent dir of that name before failing (breaks custom protocols)',
            type: 'boolean',
            demand: false
        })
        .options('locales', {
            describe: 'Comma-separated list of locales to build seperate versions for',
            type: 'string',
            demand: false
        })
        .options('defaultlocale', {
            describe: 'The locale of the default value in TR statements and tags with a data-i18n attribute',
            type: 'string',
            default: 'en'
        })
        .options('localecookiename', {
            describe: 'The name of your locale cookie (exposed as LOCALECOOKIENAME)',
            type: 'string',
            default: 'en'
        })
        .options('optimizepngs', {
            describe: 'Tries optimal palette reduction, removes ancillary chunks and tries for better compression',
            type: 'boolean',
            default: false
        })
        .options('inlinesize', {
            describe: 'Inline CSS backgrounds below this threshold as data-uris',
            default: 8192
        })
        .options('deferscripts', {
            describe: 'Sets the "defer" attribute on all script tags',
            type: 'boolean',
            default: false
        })
        .options('asyncscripts', {
            describe: 'Sets the "async" attribute on all script tags',
            type: 'boolean',
            default: false
        })
        .options('define', {
            alias: 'd',
            describe: '--define SYMBOL[=value] will be passed to UglifyJS as is (see the docs at https://github.com/mishoo/UglifyJS#usage). Remember to protect quotes from the shell, eg. --define foo=\\"bar\\". Does not work with --nocompress',
            type: 'string'
        })
        .options('nocompress', {
            describe: 'Prettifies HTML, CSS and Javascript for easier debugging',
            type: 'boolean',
            default: false
        })
        .options('mangletoplevel', {
            describe: 'Wraps your javascript code in a function literal that pulls global variables into local variables for better minification. WARNING: This may break your JS',
            type: 'boolean',
            default: false
        })
        .options('manifest', {
            describe: 'Generates an appcache manifest file with all static assets included',
            type: 'boolean',
            default: false
        })
        .options('negotiatemanifest', {
            describe: 'Removes the locale id from the <html manifest="..."> references so all manifests are assumed to be accessible from the same url. Useful if you want the browser to pick up the right cache manifest and HTML after a locale change (your static file server needs to support content negotiation). Only makes sense when both --manifest and --locale have been specified',
            type: 'boolean',
            default: false
        })
        .options('less', {
            describe: 'Translates .less files to CSS',
            type: 'boolean',
            default: false
        })
        .options('stoponwarning', {
            describe: 'Whether to stop with a non-zero exit code when a warning is encountered',
            type: 'boolean',
            default: false
        })
        .options('version', {
            describe: 'Adds or updates <meta http-equiv="Content-Version" content="..."> to the specified value. Use {0} to refer to the current value, eg. --version {0}/production or --version `git describe --long --tags --always --dirty 2>/dev/null || echo unknown`',
            type: 'string'
        })
        .check(function (argv) {
            return typeof argv.inlinesize === 'number';
        })
        .wrap(72)
        .argv;

if (commandLineOptions.h) {
    optimist.showHelp();
    process.exit(1);
}

var _ = require('underscore'),
    uglifyJs = require('uglify-js-papandreou'),
    AssetGraph = require('assetgraph'),
    i18nTools = require('../lib/i18nTools'),
    query = AssetGraph.query,
    urlTools = require('assetgraph/lib/util/urlTools'),
    outRoot = urlTools.fsDirToFileUrl(commandLineOptions.outroot),
    cdnRoot = commandLineOptions.cdnroot && urlTools.ensureTrailingSlash(commandLineOptions.cdnroot),
    cdnOutRoot = commandLineOptions.cdnoutroot && urlTools.fsDirToFileUrl(commandLineOptions.cdnoutroot),
    blacklistUrlRegExp = /^$/,
    rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true),
    defines = {},
    inputUrls;

(commandLineOptions.define ? _.flatten(_.flatten([commandLineOptions.define])) : []).forEach(function (define) {
    var matchDefine = define.match(/^(\w+)(?:=(.*))?$/);
    if (matchDefine) {
        var valueAst;
        if (matchDefine[2]) {
            var parsedValue;
            try {
                parsedValue = uglifyJs.parser.parse(matchDefine[2]);
            } catch (e) {
                throw new Error('Invalid --define ' + matchDefine[1] + ': Could not parse ' + matchDefine[2] + ' as a JavaScript expression. Missing shell escapes?');
            }
            if (parsedValue[1].length !== 1 || !/^(?:stat|directive)$/.test(parsedValue[1][0][0])) {
                throw new Error('Invalid --define ' + matchDefine[1] + ': Value parses as ' + JSON.stringify(parsedValue) + ', must be an expression');
            }
            if (parsedValue[1][0][0] === 'directive') {
                valueAst = ['string', parsedValue[1][0][1]];
            } else {
                valueAst = parsedValue[1][0][1];
            }
        } else {
            valueAst = ['name', 'true'];
        }
        defines[matchDefine[1]] = valueAst;
    }
});


if (commandLineOptions._.length > 0) {
    inputUrls = commandLineOptions._.map(function (urlOrFsPath) {
        return urlTools.urlOrFsPathToUrl(urlOrFsPath, false);
    });
    if (!rootUrl) {
        rootUrl = urlTools.findCommonUrlPrefix(inputUrls.filter(function (inputUrl) {
            return /^file:/.test(inputUrl);
        }));
        if (rootUrl) {
            console.warn("Guessing --root from input files: " + rootUrl);
        }
    }
} else if (rootUrl && /^file:/.test(rootUrl)) {
    inputUrls = [rootUrl + '**/*.html'];
    console.warn('No input files specified, defaulting to ' + inputUrls[0]);
} else {
    throw new Error("No input files and no --root specified (or it isn't file:), cannot proceed");
}

if (commandLineOptions.exclude) {
    blacklistUrlRegExp = new RegExp('(?:' +
        _.flatten(_.flatten([commandLineOptions.exclude])).map(function (wildcard) {
            return wildcard.replace(/[\.\+\{\}\[\]\(\)\?\^\$]/g, '\\$&').replace(/\*/g, '.*?');
        }).join('|') +
        ')');
}

require('../lib/registerTransforms');

new AssetGraph({root: rootUrl})
    .on('afterTransform', function (transform, elapsedTime) {
        console.log((elapsedTime / 1000).toFixed(3) + " secs: " + transform.name);
    })
    .on('warn', function (err) {
        // These are way too noisy
        if (err.relationType !== 'JavaScriptCommonJsRequire') {
            console.warn((err.asset ? err.asset.urlOrDescription + ': ' : '') + err.message);
            if (commandLineOptions.stoponwarning) {
                process.exit(1);
            }
        }
    })
    .on('error', function (err) {
        console.error((err.asset ? err.asset.urlOrDescription + ': ' : '') + err.stack);
        process.exit(1);
    })
    .registerRequireJsConfig()
    .registerLabelsAsCustomProtocols(commandLineOptions.label, {installFindParentDirectoryAsDefault: commandLineOptions.parentdir})
    .loadAssets(inputUrls)
    .populate({
        followRelations: query.or({to: {type: 'I18n'}},
                                  {type: query.not(['JavaScriptInclude', 'JavaScriptExtJsRequire', 'JavaScriptCommonJsRequire', 'HtmlAnchor']),
                                   to: query.and({url: query.not(/^https?:/)}, {url: query.not(blacklistUrlRegExp)})})
    })
    .buildProduction({
        version: commandLineOptions.version,
        less: commandLineOptions.less,
        optimizePngs: commandLineOptions.optimizepngs,
        inlineSize: commandLineOptions.inlinesize,
        mangleTopLevel: commandLineOptions.mangletoplevel,
        defines: defines,
        localeIds: commandLineOptions.locales && _.flatten(_.flatten([commandLineOptions.locales]).map(function (localeId) {
            return localeId.split(",");
        })).map(i18nTools.normalizeLocaleId),
        localeCookieName: commandLineOptions.localecookiename,
        defaultLocaleId: commandLineOptions.defaultlocaleid && i18nTools.normalizeLocaleId(commandLineOptions.defaultlocaleid),
        manifest: commandLineOptions.manifest,
        negotiateManifest: commandLineOptions.negotiatemanifest,
        asyncScripts: commandLineOptions.asyncscripts,
        deferScripts: commandLineOptions.deferscripts,
        cdnRoot: cdnRoot,
        noCompress: commandLineOptions.nocompress
    })
    .writeAssetsToDisc({url: /^file:/}, outRoot)
    .if(cdnRoot)
        .writeAssetsToDisc({url: query.createPrefixMatcher(cdnRoot)}, cdnOutRoot || outRoot, cdnRoot)
    .endif()
    .writeStatsToStderr()
    .run();
Something went wrong with that request. Please try again.