Skip to content

Commit

Permalink
🏗♻️ Consolidate all babel configs into babel.config.js (#27576)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha committed Apr 9, 2020
1 parent 570d041 commit f801a93
Show file tree
Hide file tree
Showing 26 changed files with 680 additions and 439 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@
"module": false,
"process": false,
"require": false
},
"rules": {
"local/no-module-exports": 0
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions OWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
owners: [{name: 'mrjoro'}, {name: 'ampproject/wg-outreach'}],
},
{
pattern: '{.*,babel.config.js,gulpfile.js}',
pattern: '{.*,gulpfile.js}',
owners: [{name: 'ampproject/wg-infra'}],
},
{
pattern: '{package.json,yarn.lock}',
pattern: '{babel.config.js,package.json,yarn.lock}',
owners: [
{name: 'ampproject/wg-infra'},
{name: 'ampproject/wg-runtime'},
Expand Down
125 changes: 34 additions & 91 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,99 +25,42 @@

'use strict';

const minimist = require('minimist');
const {isTravisBuild} = require('./build-system/common/travis');
const argv = minimist(process.argv.slice(2));
const {
getDepCheckConfig,
getPostClosureConfig,
getPreClosureConfig,
getSinglePassDepsConfig,
getSinglePassPostConfig,
getTestConfig,
getUnminifiedConfig,
} = require('./build-system/babel-config');

const isClosureCompiler =
argv._.includes('dist') ||
argv._.includes('check-types') ||
(argv._.length == 0 && argv.compiled);
const {esm} = argv;

const targets = (esm) => {
if (esm) {
return {'esmodules': true};
}

if (isTravisBuild()) {
return {'browsers': ['Last 2 versions', 'safari >= 9']};
}

return {'browsers': ['Last 2 versions']};
};

const plugins = (esm) => {
const leadingComments =
'./build-system/babel-plugins/babel-plugin-transform-fix-leading-comments';
const reactConstantElements =
'@babel/plugin-transform-react-constant-elements';
const transformJSX = [
'@babel/plugin-transform-react-jsx',
{
pragma: 'Preact.createElement',
pragmaFrag: 'Preact.Fragment',
useSpread: true,
},
];
const transformClasses = [
'@babel/plugin-transform-classes',
{
loose: false,
},
];

if (esm) {
return [leadingComments, reactConstantElements, transformJSX];
}

return [
leadingComments,
reactConstantElements,
transformClasses,
transformJSX,
];
};

const presets = (esm) => {
if (esm) {
return [
[
'@babel/preset-env',
{
'modules': false,
'targets': targets(esm),
'bugfixes': true,
},
],
];
}

return [
[
'@babel/preset-env',
{
'modules': isClosureCompiler ? false : 'commonjs',
'loose': true,
'targets': targets(esm),
},
],
];
};
/**
* Mapping of babel transform callers to their corresponding babel configs.
*/
const babelTransforms = new Map([
['dep-check', getDepCheckConfig()],
['post-closure', getPostClosureConfig()],
['pre-closure', getPreClosureConfig()],
['single-pass-deps', getSinglePassDepsConfig()],
['single-pass-post', getSinglePassPostConfig()],
['test', getTestConfig()],
['unminified', getUnminifiedConfig()],
]);

// eslint-disable-next-line local/no-module-exports
/**
* Main entry point. Returns babel config corresponding to the caller.
*
* @param {!Object} api
* @return {!Object}
*/
module.exports = function (api) {
api.cache(true);
// Closure Compiler builds do not use any of the default settings below until its
// an esm build. (Both Multipass and Singlepass)
if (isClosureCompiler && !esm) {
return {};
const caller = api.caller((caller) => caller.name);
if (babelTransforms.has(caller)) {
return babelTransforms.get(caller);
} else {
const err = new Error('Unrecognized Babel caller (see babel.config.js).');
err.showStack = false;
throw err;
}

return {
'plugins': plugins(esm),
'presets': presets(esm),
'compact': false,
'sourceType': 'module',
};
};
5 changes: 5 additions & 0 deletions build-system/babel-config/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"local/prefer-spread-props": 0
}
}
14 changes: 14 additions & 0 deletions build-system/babel-config/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// For an explanation of the OWNERS rules and syntax, see:
// https://github.com/ampproject/amp-github-apps/blob/master/owners/OWNERS.example

{
rules: [
{
owners: [
{name: 'ampproject/wg-infra'},
{name: 'ampproject/wg-runtime'},
{name: 'ampproject/wg-performance'},
],
},
],
}
61 changes: 61 additions & 0 deletions build-system/babel-config/dep-check-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

const {getDevDependencies} = require('./dev-dependencies');

/**
* Gets the config for babel transforms run during `gulp dep-check`.
*
* @return {!Object}
*/
function getDepCheckConfig() {
const reactJsxPlugin = [
'@babel/plugin-transform-react-jsx',
{
pragma: 'Preact.createElement',
pragmaFrag: 'Preact.Fragment',
useSpread: true,
},
];
const presetEnv = [
'@babel/preset-env',
{
bugfixes: true,
modules: 'commonjs',
loose: true,
targets: {'browsers': ['Last 2 versions']},
},
];
const depCheckPlugins = [
'./build-system/babel-plugins/babel-plugin-transform-fix-leading-comments',
'@babel/plugin-transform-react-constant-elements',
'@babel/plugin-transform-classes',
reactJsxPlugin,
];
const depCheckPresets = [presetEnv];
const devDependencies = getDevDependencies();
return {
compact: false,
ignore: devDependencies,
plugins: depCheckPlugins,
presets: depCheckPresets,
};
}

module.exports = {
getDepCheckConfig,
};
33 changes: 33 additions & 0 deletions build-system/babel-config/dev-dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

const packageJson = require('../../package.json');

/**
* Gets relative paths to all the devDependencies defined in package.json.
*
* @return {!Array<string>}
*/
function getDevDependencies() {
return Object.keys(packageJson.devDependencies).map(
(dependency) => `./node_modules/${dependency}`
);
}

module.exports = {
getDevDependencies,
};
34 changes: 34 additions & 0 deletions build-system/babel-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

const fs = require('fs');

/**
* Populates a single object with the babel configs from all the *-config.js
* files in this directory.
*
* @return {!Object}
*/
function getAllBabelConfigs() {
const babelConfigFiles = fs
.readdirSync(__dirname)
.filter((file) => file.includes('-config.js'));
const babelConfigs = babelConfigFiles.map((file) => require(`./${file}`));
return Object.assign({}, ...babelConfigs);
}

module.exports = getAllBabelConfigs();
69 changes: 69 additions & 0 deletions build-system/babel-config/post-closure-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

const argv = require('minimist')(process.argv.slice(2));

/**
* Gets the config for post-closure babel transforms run during `gulp dist --esm`.
*
* @return {!Object}
*/
function getPostClosureConfig() {
if (!argv.esm) {
return {};
}

const reactJsxPlugin = [
'@babel/plugin-transform-react-jsx',
{
pragma: 'Preact.createElement',
pragmaFrag: 'Preact.Fragment',
useSpread: true,
},
];
const postClosurePlugins = [
'./build-system/babel-plugins/babel-plugin-transform-minified-comments',
'./build-system/babel-plugins/babel-plugin-const-transformer',
'./build-system/babel-plugins/babel-plugin-transform-remove-directives',
'./build-system/babel-plugins/babel-plugin-transform-function-declarations',
'./build-system/babel-plugins/babel-plugin-transform-stringish-literals',
'./build-system/babel-plugins/babel-plugin-transform-fix-leading-comments',
'@babel/plugin-transform-react-constant-elements',
reactJsxPlugin,
];
const presetEnv = [
'@babel/preset-env',
{
bugfixes: true,
modules: false,
targets: {'esmodules': true},
},
];
const postClosurePresets = [presetEnv];
return {
compact: false,
inputSourceMap: false,
plugins: postClosurePlugins,
presets: postClosurePresets,
retainLines: false,
sourceMaps: true,
};
}

module.exports = {
getPostClosureConfig,
};

0 comments on commit f801a93

Please sign in to comment.