diff --git a/.eslintrc.js b/.eslintrc.js index 188f18558c53..fbf75e24dba9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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', diff --git a/build-system/babel-config/helpers.js b/build-system/babel-config/helpers.js index bc7c3ded89f3..9d57c76c74ff 100644 --- a/build-system/babel-config/helpers.js +++ b/build-system/babel-config/helpers.js @@ -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 @@ -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}; + 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) { diff --git a/build-system/babel-config/minified-config.js b/build-system/babel-config/minified-config.js index 7c361883413b..aa2d3ad167e7 100644 --- a/build-system/babel-config/minified-config.js +++ b/build-system/babel-config/minified-config.js @@ -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, @@ -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', diff --git a/build-system/babel-config/pre-closure-config.js b/build-system/babel-config/pre-closure-config.js index 52664937ad0e..f0741814ae75 100644 --- a/build-system/babel-config/pre-closure-config.js +++ b/build-system/babel-config/pre-closure-config.js @@ -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, @@ -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', diff --git a/build-system/babel-config/unminified-config.js b/build-system/babel-config/unminified-config.js index 0f06a114830b..6bb7145535ab 100644 --- a/build-system/babel-config/unminified-config.js +++ b/build-system/babel-config/unminified-config.js @@ -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'); @@ -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', diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/index.js b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/index.js deleted file mode 100644 index f2912031a086..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/index.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2018 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. - */ - -/** - * Changes the value of IS_FORTESTING to false. - * The above variable is in src/mode.js. - * @param {{types: string}} options - * @return {!Object} - */ -module.exports = function ({types: t}) { - return { - visitor: { - VariableDeclarator(path) { - const {node} = path; - const {id, init} = node; - if ( - t.isIdentifier(id, {name: 'IS_FORTESTING'}) && - t.isBooleanLiteral(init, {value: true}) - ) { - node.init = t.booleanLiteral(false); - } - }, - }, - }; -}; diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/input.js b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/input.js deleted file mode 100644 index 7eafb7e9bc9c..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/input.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2018 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. - */ - - -const IS_FORTESTING = true; -const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/options.json b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/options.json deleted file mode 100644 index 3ebeb6374116..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/options.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "plugins": [ - "../../../.." - ] -} diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/output.js b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/output.js deleted file mode 100644 index addce9217cc8..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/output.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2018 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. - */ -const IS_FORTESTING = false; -const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js deleted file mode 100644 index 88371a1445c7..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js +++ /dev/null @@ -1,20 +0,0 @@ - -/** - * Copyright 2018 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. - */ - - -const IS_DEVELOPMENT = false; -const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json deleted file mode 100644 index 3ebeb6374116..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "plugins": [ - "../../../.." - ] -} diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js deleted file mode 100644 index bf4c3530b6af..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2018 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. - */ -const IS_DEVELOPMENT = false; -const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/index.js b/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/index.js deleted file mode 100644 index 7b6d7376a749..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/index.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2018 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. - */ - -const runner = require('@babel/helper-plugin-test-runner').default; - -runner(__dirname); diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/index.js b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/index.js deleted file mode 100644 index 590d85b16018..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/index.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2018 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. - */ - -/** - * Changes the value of IS_MINIFIED to true. - * The above variable is in src/mode.js and src/core/mode/minified.js. - * @param {Object} babelTypes - * @return {!Object} - */ -module.exports = function (babelTypes) { - const {types: t} = babelTypes; - return { - visitor: { - VariableDeclarator(path) { - const {id, init} = path.node; - if ( - t.isIdentifier(id, {name: 'IS_MINIFIED'}) && - t.isBooleanLiteral(init, {value: false}) - ) { - path.replaceWith( - t.variableDeclarator( - t.identifier('IS_MINIFIED'), - t.booleanLiteral(true) - ) - ); - } - }, - }, - }; -}; diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/input.js b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/input.js deleted file mode 100644 index ff730f6e824f..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/input.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2018 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. - */ - - -const IS_DEVELOPMENT = false; -const IS_MINIFIED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/options.json b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/options.json deleted file mode 100644 index 3ebeb6374116..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/options.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "plugins": [ - "../../../.." - ] -} diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/output.js b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/output.js deleted file mode 100644 index 7ac0ff36819f..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/output.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2018 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. - */ -const IS_DEVELOPMENT = false; -const IS_MINIFIED = true; diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js deleted file mode 100644 index b42410f328d2..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2018 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. - */ - - -const IS_DEVELOPMENT = false; -const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json deleted file mode 100644 index 3ebeb6374116..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "plugins": [ - "../../../.." - ] -} diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js deleted file mode 100644 index bf4c3530b6af..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2018 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. - */ -const IS_DEVELOPMENT = false; -const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/index.js b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/index.js deleted file mode 100644 index 7b6d7376a749..000000000000 --- a/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/index.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2018 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. - */ - -const runner = require('@babel/helper-plugin-test-runner').default; - -runner(__dirname); diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/index.js b/build-system/babel-plugins/babel-plugin-transform-internal-version/index.js deleted file mode 100644 index 4322a1f0d4b0..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/index.js +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright 2021 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. - */ - -module.exports = function (babel, options = {}) { - const {types: t} = babel; - const {version} = options; - - if (!version) { - throw new Error( - 'Need to specify runtime version as an option to babel transformer' - ); - } - - return { - name: 'transform-internal-version', - - visitor: { - StringLiteral(path) { - const {node} = path; - const {value} = node; - if (!value.includes('$internalRuntimeVersion$')) { - return; - } - - const replacement = t.stringLiteral( - value.replace(/\$internalRuntimeVersion\$/g, version) - ); - t.inherits(replacement, node); - path.replaceWith(replacement); - }, - - TemplateElement(path) { - const {node} = path; - const {cooked, raw} = node.value; - if (!raw.includes('$internalRuntimeVersion$')) { - return; - } - - const replacement = t.templateElement({ - cooked: cooked.replace(/\$internalRuntimeVersion\$/g, version), - raw: raw.replace(/\$internalRuntimeVersion\$/g, version), - }); - t.inherits(replacement, node); - path.replaceWith(replacement); - }, - }, - }; -}; diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/input.js b/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/input.js deleted file mode 100644 index 9c5499316dd7..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/input.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright 2021 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. - */ - -0; // Stop directive parsing - -"$internalRuntimeVersion$"; -'before$internalRuntimeVersion$after'; - -function internalRuntimeVersion() { - return '$internalRuntimeVersion$'; -} diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/options.json b/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/options.json deleted file mode 100644 index 9d73505664d3..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/options.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "plugins": [ - [ - "../../../../", - { - "version": "123" - } - ] - ] -} diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/output.js b/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/output.js deleted file mode 100644 index 78df5e15ac89..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/output.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Copyright 2021 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. - */ -0; // Stop directive parsing - -"123"; -"before123after"; - -function internalRuntimeVersion() { - return "123"; -} diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/input.js b/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/input.js deleted file mode 100644 index 83d7e0f2d19a..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/input.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright 2021 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. - */ - -0; // Stop directive parsing - -`$internalRuntimeVersion$`; -`before$internalRuntimeVersion$after`; - -function internalRuntimeVersion() { - `$internalRuntimeVersion$`; - `\$internalRuntimeVersion$`; - `$\internalRuntimeVersion$`; -} diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/options.json b/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/options.json deleted file mode 100644 index 9d73505664d3..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/options.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "plugins": [ - [ - "../../../../", - { - "version": "123" - } - ] - ] -} diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/output.js b/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/output.js deleted file mode 100644 index d35fab32e67b..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/output.js +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright 2021 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. - */ -0; // Stop directive parsing - -`123`; -`before123after`; - -function internalRuntimeVersion() { - `123`; - `\123`; - `$\internalRuntimeVersion$`; -} diff --git a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/index.js b/build-system/babel-plugins/babel-plugin-transform-internal-version/test/index.js deleted file mode 100644 index 762295c3a9c8..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-internal-version/test/index.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2021 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. - */ - -const runner = require('@babel/helper-plugin-test-runner').default; - -runner(__dirname); diff --git a/build-system/babel-plugins/babel-plugin-transform-version-call/index.js b/build-system/babel-plugins/babel-plugin-transform-version-call/index.js deleted file mode 100644 index fab76277db2c..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-version-call/index.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2019 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. - */ - -const {VERSION} = require('../../compile/internal-version'); - -module.exports = function (babel) { - const {types: t} = babel; - return { - name: 'transform-version-call', - visitor: { - CallExpression(path) { - if (path.node.callee.name === 'internalRuntimeVersion') { - // Detect if we're in a test run and if we are replace the value - // with a static string. - if (process.env.JEST_WORKER_ID !== undefined) { - path.replaceWith(t.stringLiteral('transform-version-call')); - return; - } - path.replaceWith(t.stringLiteral(VERSION)); - } - }, - }, - }; -}; diff --git a/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/input.js b/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/input.js deleted file mode 100644 index e582dba6e7f3..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/input.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2019 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. - */ - -version(); - -internalRuntimeVersion(); diff --git a/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/options.json b/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/options.json deleted file mode 100644 index 3ebeb6374116..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/options.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "plugins": [ - "../../../.." - ] -} diff --git a/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/output.js b/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/output.js deleted file mode 100644 index 67f874889ca4..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/output.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2019 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. - */ -version(); -"transform-version-call"; diff --git a/build-system/babel-plugins/babel-plugin-transform-version-call/test/index.js b/build-system/babel-plugins/babel-plugin-transform-version-call/test/index.js deleted file mode 100644 index 44564fa07fec..000000000000 --- a/build-system/babel-plugins/babel-plugin-transform-version-call/test/index.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2019 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. - */ - -const runner = require('@babel/helper-plugin-test-runner').default; - -runner(__dirname); diff --git a/build-system/compile/build-constants.js b/build-system/compile/build-constants.js new file mode 100644 index 000000000000..7d3af04e1cfd --- /dev/null +++ b/build-system/compile/build-constants.js @@ -0,0 +1,45 @@ +/** + * Copyright 2021 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. + */ +const argv = require('minimist')(process.argv.slice(2)); +const {VERSION} = require('./internal-version'); + +const testTasks = ['e2e', 'integration', 'visual-diff', 'unit', 'check-types']; +const isTestTask = testTasks.some((task) => argv._.includes(task)); +const isForTesting = argv.fortesting || isTestTask; +const isMinified = argv._.includes('dist') || !!argv.compiled; + +/** + * Build time constants. Used by babel but hopefully one day directly by the bundlers.. + * + * TODO: move constant replacement to bundlers once either https://github.com/google/closure-compiler/issues/1601 + * is resolved, or we switch to using a different bundler. + * + * @type {Object} + */ +const BUILD_CONSTANTS = { + IS_FORTESTING: isForTesting, + IS_MINIFIED: isMinified, + INTERNAL_RUNTIME_VERSION: isTestTask ? '$internalRuntimeVersion$' : VERSION, + + // 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. + IS_ESM: !!(argv.esm || argv.sxg), + IS_SXG: !!argv.sxg, +}; + +module.exports = {BUILD_CONSTANTS}; diff --git a/build-system/test-configs/forbidden-terms.js b/build-system/test-configs/forbidden-terms.js index c0a0742290fb..5c45d2b49bc4 100644 --- a/build-system/test-configs/forbidden-terms.js +++ b/build-system/test-configs/forbidden-terms.js @@ -146,13 +146,15 @@ const forbiddenTermsGlobal = { message: realiasGetMode, allowlist: ['src/mode-object.js', 'src/iframe-attributes.js'], }, - '(?:var|let|const) +IS_FORTESTING +=': { - message: 'IS_FORTESTING local var only allowed in mode.js.', - allowlist: ['src/core/mode/for-testing.js'], - }, - '(?:var|let|const) +IS_MINIFIED +=': { - message: 'IS_MINIFIED local var only allowed in core/mode/minified.js', - allowlist: ['src/core/mode/minified.js'], + 'INTERNAL_RUNTIME_VERSION|IS_(FORTESTING|MINIFIED)': { + message: + 'Do not use build constants directly. Instead, use the helpers in `#core/mode`.', + allowlist: [ + 'src/internal-version.js', + 'src/core/mode/minified.js', + 'src/core/mode/for-testing.js', + 'build-system/compile/build-constants.js', + ], }, '\\.prefetch\\(': { message: 'Do not use preconnect.prefetch, use preconnect.preload instead.', diff --git a/src/core/mode/for-testing.js b/src/core/mode/for-testing.js index de3e2a02be1a..7bbafb1c236b 100644 --- a/src/core/mode/for-testing.js +++ b/src/core/mode/for-testing.js @@ -14,14 +14,11 @@ * limitations under the License. */ -// Magic constant that is replaced by babel. -// IS_FORTESTING is only replaced when `amp dist` is called without the -// --fortesting flag. -const IS_FORTESTING = true; - /** - * Returns true whenever closure compiler is used with --fortesting. Calls are - * DCE'd when compiled. + * Returns true whenever --fortesting is used. + * This is a magic constant replaced by babel. + * + * Calls are DCE'd when compiled. * @return {boolean} */ export function isFortesting() { diff --git a/src/core/mode/minified.js b/src/core/mode/minified.js index 04726161a507..eda24dc8b011 100644 --- a/src/core/mode/minified.js +++ b/src/core/mode/minified.js @@ -14,12 +14,10 @@ * limitations under the License. */ -// Magic constant that is replaced by babel. -// IS_MINIFIED is always replaced with true when closure compiler is used -const IS_MINIFIED = false; - /** * Returns true whenever closure compiler is used. + * This is a magic constant that is replaced by babel. + * * @return {boolean} */ export function isMinified() { diff --git a/src/internal-version.js b/src/internal-version.js index e2b203021594..aafc9fac3a97 100644 --- a/src/internal-version.js +++ b/src/internal-version.js @@ -24,5 +24,5 @@ * @return {string} */ export function internalRuntimeVersion() { - return '$internalRuntimeVersion$'; + return INTERNAL_RUNTIME_VERSION; }