Skip to content

Commit

Permalink
Adding Babel filter import to remove unwanted fetch polyfill (ampproj…
Browse files Browse the repository at this point in the history
…ect#18494)

* adding plugin to remove fetch polyfill

* adding babel plugins

* fix lint

* passing options through and through

* fixing path for esm build

* consolidating plugin logic

* fixing shas

* readding plugin

* removing all polyfills

* fixing package lock

* fixing yarn lock
  • Loading branch information
prateekbh authored Oct 5, 2018
1 parent affbe96 commit 9fd5aa2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
25 changes: 21 additions & 4 deletions build-system/build.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,27 @@
* limitations under the License.
*/

const defaultPlugins = [
require.resolve(
'./babel-plugins/babel-plugin-transform-parenthesize-expression'),
];

module.exports = {
plugins: [
require.resolve(
'./babel-plugins/babel-plugin-transform-parenthesize-expression'),
],
plugins: isEsmBuild => {
if (isEsmBuild) {
return defaultPlugins.concat([
[require.resolve('babel-plugin-filter-imports'), {
'imports': {
'./polyfills/fetch': ['installFetch'],
'./polyfills/domtokenlist-toggle': ['installDOMTokenListToggle'],
'./polyfills/document-contains': ['installDocContains'],
'./polyfills/math-sign': ['installMathSign'],
'./polyfills/object-assign': ['installObjectAssign'],
'./polyfills/promise': ['installPromise'],
},
}],
]);
}
return defaultPlugins;
},
};
9 changes: 5 additions & 4 deletions build-system/get-dep-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ exports.getFlags = function(config) {
],
//new_type_inf: true,
language_in: 'ES6',
language_out: 'ES5',
language_out: config.language_out || 'ES5',
module_output_path_prefix: config.writeTo || 'out/',
externs: config.externs,
define: config.define,
Expand Down Expand Up @@ -305,7 +305,7 @@ exports.getGraph = function(entryModules, config) {
graph.sorted = Array.from(topo.sort().keys()).reverse();

setupBundles(graph);
transformPathsToTempDir(graph);
transformPathsToTempDir(graph, config);
resolve(graph);
fs.writeFileSync('deps.txt', JSON.stringify(graph, null, 2));
}).on('error', reject).pipe(devnull());
Expand Down Expand Up @@ -377,8 +377,9 @@ function setupBundles(graph) {
* to a temporary directory where we can run babel transformations.
*
* @param {!Object} graph
* @param {!Object} config
*/
function transformPathsToTempDir(graph) {
function transformPathsToTempDir(graph, config) {
console/*OK*/.log(colors.green(`temp directory ${graph.tmp}`));
// `sorted` will always have the files that we need.
graph.sorted.forEach(f => {
Expand All @@ -387,7 +388,7 @@ function transformPathsToTempDir(graph) {
fs.copySync(f, `${graph.tmp}/${f}`);
} else {
const {code} = babel.transformFileSync(f, {
plugins: conf.plugins,
plugins: conf.plugins(config.define.indexOf['ESM_BUILD=true'] !== -1),
babelrc: false,
retainLines: true,
});
Expand Down
17 changes: 14 additions & 3 deletions build-system/tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,23 @@ function compile(entryModuleFilenames, outputDir,
define.push('FORTESTING=true');
}
if (options.singlePassCompilation) {
// TODO(@cramforce): Run the post processing step
return singlePassCompile(entryModuleFilenames, {
const compilationOptions = {
define,
externs: baseExterns,
hideWarningsFor,
}).then(() => {
};

// Add babel plugin to remove unwanted polyfills in esm build
if (options.esmPassCompilation) {
compilationOptions['dest'] = './dist/esm/';
define.push('ESM_BUILD=true');
}

// TODO(@cramforce): Run the post processing step
return singlePassCompile(
entryModuleFilenames,
compilationOptions
).then(() => {
return new Promise((resolve, reject) => {
const stream = gulp.src(outputDir + '/**/*.js');
stream.on('end', resolve);
Expand Down
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ function compile(watch, shouldMinify, opt_preventRemoveAndMakeDir,
minify: shouldMinify,
wrapper: wrappers.mainBinary,
singlePassCompilation: argv.single_pass,
esmPassCompilation: argv.esm,
}),
compileJs('./extensions/amp-viewer-integration/0.1/examples/',
'amp-viewer-host.js', './dist/v0/examples', {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"ava": "0.25.0",
"babel-core": "6.26.3",
"babel-eslint": "10.0.1",
"babel-plugin-filter-imports": "^2.0.3",
"babel-plugin-istanbul": "5.0.1",
"babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
"babel-plugin-transform-remove-strict-mode": "0.0.2",
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,14 @@ babel-plugin-espower@^2.3.2:
espurify "^1.6.0"
estraverse "^4.1.1"

babel-plugin-filter-imports@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/babel-plugin-filter-imports/-/babel-plugin-filter-imports-2.0.3.tgz#21f2773d0d74558986dd7af89f6f753d5fa92da3"
integrity sha512-s2vbul45NHOI665jcJvvIdzZLMIKgUdhofGEbxXn7GxfEB6SePCUbj2qBPjV9oqHuYBQucih/tRhO/GJu8mPkg==
dependencies:
"@babel/types" "^7.0.0"
lodash "^4.17.11"

[email protected]:
version "5.0.1"
resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-5.0.1.tgz#2ce7bf211f0d9480ff7fd294bd05e2fa555e31ea"
Expand Down Expand Up @@ -10034,6 +10042,11 @@ lodash@^4.17.10, lodash@^4.17.5:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
integrity sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==

lodash@^4.17.11:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==

lodash@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-1.0.2.tgz#8f57560c83b59fc270bd3d561b690043430e2551"
Expand Down

0 comments on commit 9fd5aa2

Please sign in to comment.