Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-system: consolidate all build time constants #34327

Merged
merged 14 commits into from
Jul 9, 2021
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ module.exports = {
...getExperimentGlobals(),
'IS_ESM': 'readonly',
'IS_SXG': 'readonly',
'IS_MINIFIED': 'readonly',
'IS_FORTESTING': 'readonly',
'INTERNAL_RUNTIME_VERSION': 'readonly',
'AMP': 'readonly',
'context': 'readonly',
'global': 'readonly',
Expand Down
23 changes: 10 additions & 13 deletions build-system/babel-config/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const argv = require('minimist')(process.argv.slice(2));
const experimentsConfig = require('../global-configs/experiments-config.json');
const experimentsConstantBackup = require('../global-configs/experiments-const.json');
const {BUILD_CONSTANTS} = require('../compile/build-constants');

/**
* Get experiment constant to define from command line arguments, if any
Expand Down Expand Up @@ -46,24 +47,20 @@ function getExperimentConstant() {
function getReplacePlugin() {
/**
* @param {string} identifierName the identifier name to replace
* @param {boolean} value the value to replace with
* @param {boolean|string} value the value to replace with
* @return {!Object} replacement options used by minify-replace plugin
*/
function createReplacement(identifierName, value) {
return {
identifierName,
replacement: {type: 'booleanLiteral', value: !!value},
};
const replacement =
typeof value === 'boolean'
? {type: 'booleanLiteral', value}
: {type: 'stringLiteral', value};
Comment on lines +54 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your consideration; typechecking guarantees the possible results from typeof

Suggested change
const replacement =
typeof value === 'boolean'
? {type: 'booleanLiteral', value}
: {type: 'stringLiteral', value};
const replacement = {value, type: `${typeof value}Literal`};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever! I don't know how I feel about this 🤔

return {identifierName, replacement};
}

// We build on the idea that SxG is an upgrade to the ESM build.
// Therefore, all conditions set by ESM will also hold for SxG.
// However, we will also need to introduce a separate IS_SxG flag
// for conditions only true for SxG.
const replacements = [
createReplacement('IS_ESM', argv.esm || argv.sxg),
createReplacement('IS_SXG', argv.sxg),
];
const replacements = Object.entries(BUILD_CONSTANTS).map(([ident, val]) =>
createReplacement(ident, val)
);

const experimentConstant = getExperimentConstant();
if (experimentConstant) {
Expand Down
5 changes: 0 additions & 5 deletions build-system/babel-config/minified-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ function getMinifiedConfig() {
'./build-system/babel-plugins/babel-plugin-transform-json-import',
{freeze: false},
],
'./build-system/babel-plugins/babel-plugin-is_minified-constant-transformer',
'./build-system/babel-plugins/babel-plugin-transform-html-template',
'./build-system/babel-plugins/babel-plugin-transform-jss',
'./build-system/babel-plugins/babel-plugin-transform-version-call',
'./build-system/babel-plugins/babel-plugin-transform-simple-array-destructure',
'./build-system/babel-plugins/babel-plugin-transform-default-assignment',
replacePlugin,
Expand All @@ -75,9 +73,6 @@ function getMinifiedConfig() {
'./build-system/babel-plugins/babel-plugin-amp-mode-transformer',
{isEsmBuild: !!argv.esm},
],
argv.fortesting
? null
: './build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer',
].filter(Boolean);
const presetEnv = [
'@babel/preset-env',
Expand Down
5 changes: 0 additions & 5 deletions build-system/babel-config/pre-closure-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ function getPreClosureConfig() {
'./build-system/babel-plugins/babel-plugin-transform-json-import',
{freeze: false},
],
'./build-system/babel-plugins/babel-plugin-is_minified-constant-transformer',
'./build-system/babel-plugins/babel-plugin-transform-amp-extension-call',
'./build-system/babel-plugins/babel-plugin-transform-html-template',
'./build-system/babel-plugins/babel-plugin-transform-jss',
'./build-system/babel-plugins/babel-plugin-transform-version-call',
'./build-system/babel-plugins/babel-plugin-transform-simple-array-destructure',
'./build-system/babel-plugins/babel-plugin-transform-default-assignment',
replacePlugin,
Expand All @@ -84,9 +82,6 @@ function getPreClosureConfig() {
{isEsmBuild: !!argv.esm},
]
: null,
!(isFortesting || isCheckTypes)
? './build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer'
: null,
].filter(Boolean);
const presetEnv = [
'@babel/preset-env',
Expand Down
7 changes: 0 additions & 7 deletions build-system/babel-config/unminified-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
'use strict';

const argv = require('minimist')(process.argv.slice(2));
const {
VERSION: internalRuntimeVersion,
} = require('../compile/internal-version');
const {getImportResolverPlugin} = require('./import-resolver');
const {getReplacePlugin} = require('./helpers');

Expand Down Expand Up @@ -54,10 +51,6 @@ function getUnminifiedConfig() {
argv.coverage ? 'babel-plugin-istanbul' : null,
replacePlugin,
'./build-system/babel-plugins/babel-plugin-transform-json-import',
[
'./build-system/babel-plugins/babel-plugin-transform-internal-version',
{version: internalRuntimeVersion},
],
'./build-system/babel-plugins/babel-plugin-transform-json-configuration',
'./build-system/babel-plugins/babel-plugin-transform-jss',
'./build-system/babel-plugins/babel-plugin-transform-fix-leading-comments',
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading