Skip to content

Commit

Permalink
Support --extensions=minimal_set & bug fix (ampproject#13249)
Browse files Browse the repository at this point in the history
* fix dist with alias

* change minimal_set usage

* fix documentation

* refactor print logic

* rename

* update function description
  • Loading branch information
zhouyx authored and protonate committed Feb 26, 2018
1 parent e153e60 commit e6d14b3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 36 deletions.
6 changes: 6 additions & 0 deletions contributing/DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,27 @@ The Quick Start Guide's [One-time setup](getting-started-quick.md#one-time-setu
| ----------------------------------------------------------------------- | --------------------------------------------------------------------- |
| **`gulp`**<sup>[[1]](#footnote-1)</sup> | Runs "watch" and "serve". Use this for standard local dev. |
| `gulp --extensions=<amp-foo,amp-bar>` | Runs "watch" and "serve", after building only the listed extensions.
| `gulp --extensions=minimal_set` | Runs "watch" and "serve", after building the extensions needed to load `article.amp.html`.
| `gulp --noextensions` | Runs "watch" and "serve" without building any extensions.
| `gulp dist`<sup>[[1]](#footnote-1)</sup> | Builds production binaries. |
| `gulp dist --extensions=<amp-foo,amp-bar>` | Builds production binaries, with only the listed extensions.
| `gulp dist --extensions=minimal_set` | Builds production binaries, with only the extensions needed to load `article.amp.html`.
| `gulp dist --noextensions` | Builds production binaries without building any extensions.
| `gulp dist --fortesting`<sup>[[1]](#footnote-1)</sup> | Builds production binaries for local testing. (Allows use cases like ads, tweets, etc. to work with minified sources. Overrides `TESTING_HOST` if specified. Uses the production `AMP_CONFIG` by default.) |
| `gulp dist --fortesting --config=<config>`<sup>[[1]](#footnote-1)</sup> | Builds production binaries for local testing, with the specified `AMP_CONFIG`. `config` can be `prod` or `canary`. (Defaults to `prod`.) |
| `gulp lint` | Validates against Google Closure Linter. |
| `gulp lint --watch` | Watches for changes in files, Validates against Google Closure Linter.|
| `gulp lint --fix` | Fixes simple lint warnings/errors automatically. |
| `gulp build`<sup>[[1]](#footnote-1)</sup> | Builds the AMP library. |
| `gulp build --extensions=<amp-foo,amp-bar>` | Builds the AMP library, with only the listed extensions.
| `gulp build --extensions=minimal_set` | Builds the AMP library, with only the extensions needed to load `article.amp.html`.
| `gulp build --noextensions` | Builds the AMP library with no extensions.
| `gulp check-links --files foo.md,bar.md` | Reports dead links in `.md` files. |
| `gulp clean` | Removes build output. |
| `gulp css`<sup>[[1]](#footnote-1)</sup> | Recompiles css to build directory and builds the embedded css into js files for the AMP library. |
| `gulp watch`<sup>[[1]](#footnote-1)</sup> | Watches for changes in files, re-builds. |
| `gulp watch --extensions=<amp-foo,amp-bar>` | Watches for changes in files, re-builds only the listed extensions.
| `gulp watch --extensions=minimal_set` | Watches for changes in files, re-builds only the extensions needed to load `article.amp.html`.
| `gulp watch --noextensions` | Watches for changes in files, re-builds with no extensions.
| `gulp pr-check`<sup>[[1]](#footnote-1)</sup> | Runs all the Travis CI checks locally. |
| `gulp pr-check --nobuild`<sup>[[1]](#footnote-1)</sup> | Runs all the Travis CI checks locally, but skips the `gulp build` step. |
Expand Down
108 changes: 72 additions & 36 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,15 @@ function declareExtension(name, version, options) {
* @param {boolean} hasCss
*/
function declareExtensionVersionAlias(name, version, lastestVersion, hasCss) {
extensionAliasFilePath[name + '-' + version + '.js'] =
name + '-' + lastestVersion + '.js';
extensionAliasFilePath[name + '-' + version + '.js'] = {
'name': name,
'file': name + '-' + lastestVersion + '.js',
};
if (hasCss) {
extensionAliasFilePath[name + '-' + version + '.css'] =
name + '-' + lastestVersion + '.css';
extensionAliasFilePath[name + '-' + version + '.css'] = {
'name': name,
'file': name + '-' + lastestVersion + '.css',
};
}
}

Expand Down Expand Up @@ -246,7 +250,7 @@ function buildExtensions(options) {
}

var extensionsToBuild = [];
if (!!argv.extensions && argv.extensions !== true) {
if (!!argv.extensions) {
extensionsToBuild = argv.extensions.split(',');
}

Expand Down Expand Up @@ -602,6 +606,53 @@ function printConfigHelp(command) {
}
}

/**
* Parse the --extensions or the --noextensions flag and
* prints a helpful message that lets the developer know how to build
* a list of extensions or without any extensions.
*/
function parseExtensionFlags() {
if (!process.env.TRAVIS) {
if (argv.extensions) {
if (typeof(argv.extensions) !== 'string') {
log(red('ERROR:'), 'Missing list of extensions. Expected format:',
cyan('--extensions=amp-foo,amp-bar'),
'to choose which ones to build, or',
cyan('--extensions=minimal_set'),
'to build the ones needed to load',
cyan('article.amp.html') + '.');
process.exit(1);
}
if (argv.extensions === 'minimal_set') {
argv.extensions =
'amp-ad,amp-ad-network-adsense-impl,amp-audio,amp-video,' +
'amp-image-lightbox,amp-lightbox,amp-sidebar,' +
'amp-analytics,amp-app-banner';
}
log(green('Building extension(s):'),
cyan(argv.extensions.split(',').join(', ')));
log(green('⤷ Use'), cyan('--noextensions'),
green('to skip building extensions.'));
} else if (argv.noextensions) {
log(green('Not building any AMP extensions.'));
log(green('⤷ Use'), cyan('--extensions=amp-foo,amp-bar'),
green('to choose which ones to build, or'),
cyan('--extensions=minimal_set'),
green('to build the ones needed to load'),
cyan('article.amp.html') + green('.'));
} else {
log(green('Building all AMP extensions.'));
log(green('⤷ Use'), cyan('--noextensions'),
green('to skip building extensions, or'),
cyan('--extensions=amp-foo,amp-bar'),
green('to choose which ones to build, or'),
cyan('--extensions=minimal_set'),
green('to build the ones needed to load'),
cyan('article.amp.html') + green('.'));
}
}
}

/**
* Enables runtime to be used for local testing by writing AMP_CONFIG to file.
* Called at the end of "gulp build" and "gulp dist --fortesting".
Expand All @@ -626,29 +677,7 @@ function enableLocalTesting(targetFile) {
function performBuild(watch) {
process.env.NODE_ENV = 'development';
printConfigHelp(watch ? 'gulp watch' : 'gulp build');
if (!process.env.TRAVIS) {
if (argv.extensions) {
if (typeof(argv.extensions) !== 'string') {
log(red('ERROR:'), 'Missing list of extensions. Expected format:',
cyan('--extensions=amp-foo,amp-bar'));
process.exit(1);
}
log(green('Building extension(s):'),
cyan(argv.extensions.split(',').join(', ')));
log(green('⤷ Use'), cyan('--noextensions'),
green('to skip building extensions.'));
} else if (argv.noextensions) {
log(green('Not building any AMP extensions.'));
log(green('⤷ Use'), cyan('--extensions=amp-foo,amp-bar'),
green('to choose which ones to build.'));
} else {
log(green('Building all AMP extensions.'));
log(green('⤷ Use'), cyan('--noextensions'),
green('to skip building extensions, or'),
cyan('--extensions=amp-foo,amp-bar'),
green('to choose which ones to build.'));
}
}
parseExtensionFlags();
return compileCss(watch).then(() => {
return Promise.all([
polyfillsForTests(),
Expand Down Expand Up @@ -691,6 +720,7 @@ function dist() {
if (argv.fortesting) {
printConfigHelp('gulp dist --fortesting')
}
parseExtensionFlags();
return compileCss().then(() => {
return Promise.all([
compile(false, true, true),
Expand Down Expand Up @@ -724,8 +754,21 @@ function dist() {
* Copy built extension to alias extension
*/
function copyAliasExtensions() {
if (argv.noextensions) {
return;
}
var extensionsToBuild = [];
if (!!argv.extensions) {
extensionsToBuild = argv.extensions.split(',');
}

for (var key in extensionAliasFilePath) {
fs.copySync('dist/v0/' + extensionAliasFilePath[key], 'dist/v0/' + key);
if (extensionsToBuild.length > 0 &&
extensionsToBuild.indexOf(extensionAliasFilePath[key]['name']) == -1) {
continue;
}
fs.copySync('dist/v0/' + extensionAliasFilePath[key]['file'],
'dist/v0/' + key);
}
}

Expand Down Expand Up @@ -891,12 +934,6 @@ function concatFilesToString(files) {
function compileJs(srcDir, srcFilename, destDir, options) {
options = options || {};
if (options.minify) {
if (argv.minimal_set
&& !(/integration|babel|amp-ad|lightbox|sidebar|analytics|app-banner/
.test(srcFilename))) {
log('Skipping', cyan(srcFilename), 'because of --minimal_set');
return Promise.resolve();
}
const startTime = Date.now();
return closureCompile(
srcDir + srcFilename, destDir, options.minifiedName, options)
Expand Down Expand Up @@ -1392,7 +1429,6 @@ gulp.task('dist', 'Build production binaries',
'Great for profiling and debugging production code.',
fortesting: ' Compiles production binaries for local testing',
config: ' Sets the runtime\'s AMP_CONFIG to one of "prod" or "canary"',
minimal_set: ' Only compile files needed to load article.amp.html',
}
});
gulp.task('watch', 'Watches for changes in files, re-builds when detected',
Expand Down

0 comments on commit e6d14b3

Please sign in to comment.