From 73f063187fc813941e7f287e9349365ba098b123 Mon Sep 17 00:00:00 2001 From: Jake Fried <4656974+samouri@users.noreply.github.com> Date: Mon, 5 Jul 2021 20:49:45 -0400 Subject: [PATCH 01/14] Consolidate all babel replacements --- build-system/babel-config/helpers.js | 23 +++++----- build-system/babel-config/minified-config.js | 5 --- .../babel-config/pre-closure-config.js | 5 --- .../index.js | 38 ---------------- .../isdev-transform/input.js | 19 -------- .../isdev-transform/options.json | 5 --- .../isdev-transform/output.js | 17 ------- .../no-transform/input.js | 20 --------- .../no-transform/options.json | 5 --- .../no-transform/output.js | 17 ------- .../test/index.js | 19 -------- .../index.js | 43 ------------------ .../isminified-transform/input.js | 19 -------- .../isminified-transform/options.json | 5 --- .../isminified-transform/output.js | 17 ------- .../no-transform/input.js | 19 -------- .../no-transform/options.json | 5 --- .../no-transform/output.js | 17 ------- .../test/index.js | 19 -------- .../index.js | 37 --------------- .../input.js | 19 -------- .../options.json | 5 --- .../output.js | 17 ------- .../test/index.js | 19 -------- build-system/compile/build-constants.js | 45 +++++++++++++++++++ 25 files changed, 55 insertions(+), 404 deletions(-) delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/index.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/isdev-transform/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_fortesting-constant-transformer/test/index.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/index.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/index.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-version-call/index.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-transform-version-call/test/fixtures/transform/transform-internal-runtime-version-call/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-version-call/test/index.js create mode 100644 build-system/compile/build-constants.js 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-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-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..266fa031b45e --- /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: internalRuntimeVersion} = require('./internal-version'); + +const isCheckTypes = argv._.includes('check-types'); +const version = + process.env.JEST_WORKER_ID !== undefined + ? 'transform-version-call' + : internalRuntimeVersion; + +/** + * Build time constants. Used by babel but hopefully one day directly by the bundlers.. + * TODO: move to bundlers once https://github.com/google/closure-compiler/issues/1601 is resolved. + * + * @type {Object} + */ +const BUILD_CONSTANTS = { + IS_FORTESTING: !!(argv.fortesting || isCheckTypes), + IS_MINIFIED: !!argv.compiled, + VERSION: version, + '$internalRuntimeVersion$': internalRuntimeVersion, + + // 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}; From b0ee8cf0636a654f7a9d7796e7decc49202eb88b Mon Sep 17 00:00:00 2001 From: Jake Fried <4656974+samouri@users.noreply.github.com> Date: Mon, 5 Jul 2021 21:02:40 -0400 Subject: [PATCH 02/14] fix bs tsconfig --- build-system/tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build-system/tsconfig.json b/build-system/tsconfig.json index 953bf2714106..da4e0a3f680e 100644 --- a/build-system/tsconfig.json +++ b/build-system/tsconfig.json @@ -37,6 +37,7 @@ "./eslint-rules/**/*.js", "./externs/**/*.js", "./tasks/make-extension/template/**/*.js", - "./tasks/performance/cache/**/*.js" + "./tasks/performance/cache/**/*.js", + "./server/new-server/transforms/dist/**/*.js" ] } From b305587cb40e6e990b9ba85189df062fa4cb1ef1 Mon Sep 17 00:00:00 2001 From: Jake Fried <4656974+samouri@users.noreply.github.com> Date: Mon, 5 Jul 2021 23:40:13 -0400 Subject: [PATCH 03/14] also remove internal-runtime-version --- .../babel-config/unminified-config.js | 7 --- .../index.js | 61 ------------------- .../test/fixtures/transform/basic/input.js | 24 -------- .../fixtures/transform/basic/options.json | 10 --- .../test/fixtures/transform/basic/output.js | 23 ------- .../transform/template-element/input.js | 26 -------- .../transform/template-element/options.json | 10 --- .../transform/template-element/output.js | 25 -------- .../test/index.js | 19 ------ 9 files changed, 205 deletions(-) delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/index.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/basic/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/test/fixtures/transform/template-element/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-transform-internal-version/test/index.js 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-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); From 5a1f7c1f70c3c51646eb674b06ba44b901cecdf5 Mon Sep 17 00:00:00 2001 From: Jake Fried <4656974+samouri@users.noreply.github.com> Date: Tue, 6 Jul 2021 00:09:47 -0400 Subject: [PATCH 04/14] Fix constant selection, modify internal-version to use indent. --- build-system/compile/build-constants.js | 22 +++++++++++----------- src/internal-version.js | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/build-system/compile/build-constants.js b/build-system/compile/build-constants.js index 266fa031b45e..d06b57c3da68 100644 --- a/build-system/compile/build-constants.js +++ b/build-system/compile/build-constants.js @@ -14,25 +14,25 @@ * limitations under the License. */ const argv = require('minimist')(process.argv.slice(2)); -const {VERSION: internalRuntimeVersion} = require('./internal-version'); +const {VERSION} = require('./internal-version'); -const isCheckTypes = argv._.includes('check-types'); -const version = - process.env.JEST_WORKER_ID !== undefined - ? 'transform-version-call' - : internalRuntimeVersion; +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 to bundlers once https://github.com/google/closure-compiler/issues/1601 is resolved. + * + * 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: !!(argv.fortesting || isCheckTypes), - IS_MINIFIED: !!argv.compiled, - VERSION: version, - '$internalRuntimeVersion$': internalRuntimeVersion, + 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. diff --git a/src/internal-version.js b/src/internal-version.js index e2b203021594..b200139e5aa7 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; // eslint-disable-line no-undef } From 46a63470a1fd6df9ce25432599bb1d2cf82c060b Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Wed, 7 Jul 2021 18:55:20 -0400 Subject: [PATCH 05/14] minify replaced in getMode....why --- .../babel-plugins/babel-plugin-amp-mode-transformer/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js index d686ef9c7131..1b584a5f02ec 100644 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js @@ -66,9 +66,6 @@ module.exports = function ({types: t}) { if (shouldResolveDevelopmentMode && property.name === 'development') { path.replaceWith(t.booleanLiteral(false)); } - if (property.name === 'minified') { - path.replaceWith(t.booleanLiteral(true)); - } } }, }, From 037d6c47b1ed04a00bcfde3918c5899da5212aa8 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Wed, 7 Jul 2021 22:37:28 -0400 Subject: [PATCH 06/14] Minify-replace does not work when the vars are declared. --- .eslintrc.js | 2 ++ build-system/test-configs/forbidden-terms.js | 2 -- src/core/mode/for-testing.js | 11 ++++------- src/core/mode/minified.js | 6 ++---- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 188f18558c53..7da9e64ed917 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -72,6 +72,8 @@ module.exports = { ...getExperimentGlobals(), 'IS_ESM': 'readonly', 'IS_SXG': 'readonly', + 'IS_MINIFIED': 'readonly', + 'IS_FORTESTING': 'readonly', 'AMP': 'readonly', 'context': 'readonly', 'global': 'readonly', diff --git a/build-system/test-configs/forbidden-terms.js b/build-system/test-configs/forbidden-terms.js index c0a0742290fb..e09fb42758b9 100644 --- a/build-system/test-configs/forbidden-terms.js +++ b/build-system/test-configs/forbidden-terms.js @@ -148,11 +148,9 @@ const forbiddenTermsGlobal = { }, '(?: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'], }, '\\.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() { From 1ef5a022f3d1963ebe040fe8d62f9f5a669a079c Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Wed, 7 Jul 2021 22:45:31 -0400 Subject: [PATCH 07/14] remove amp-mode-transform --- build-system/babel-config/minified-config.js | 6 -- .../babel-config/pre-closure-config.js | 9 --- .../index.js | 73 ------------------- .../no-transform/input.js | 24 ------ .../no-transform/options.json | 5 -- .../no-transform/output.js | 23 ------ .../transform-assertions/nomodule/input.js | 28 ------- .../nomodule/options.json | 11 --- .../transform-assertions/nomodule/output.mjs | 26 ------- .../transform-assertions/transform/input.js | 28 ------- .../transform/options.json | 6 -- .../transform-assertions/transform/output.mjs | 26 ------- .../test/index.js | 19 ----- 13 files changed, 284 deletions(-) delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs delete mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/index.js diff --git a/build-system/babel-config/minified-config.js b/build-system/babel-config/minified-config.js index aa2d3ad167e7..fd2d015432e6 100644 --- a/build-system/babel-config/minified-config.js +++ b/build-system/babel-config/minified-config.js @@ -67,12 +67,6 @@ function getMinifiedConfig() { argv.fortesting ? null : './build-system/babel-plugins/babel-plugin-transform-json-configuration', - argv.fortesting - ? null - : [ - './build-system/babel-plugins/babel-plugin-amp-mode-transformer', - {isEsmBuild: !!argv.esm}, - ], ].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 f0741814ae75..3f532e924ccd 100644 --- a/build-system/babel-config/pre-closure-config.js +++ b/build-system/babel-config/pre-closure-config.js @@ -26,9 +26,6 @@ const {getReplacePlugin} = require('./helpers'); */ function getPreClosureConfig() { const isCheckTypes = argv._.includes('check-types'); - const testTasks = ['e2e', 'integration', 'visual-diff']; - const isTestTask = testTasks.some((task) => argv._.includes(task)); - const isFortesting = argv.fortesting || isTestTask; const reactJsxPlugin = [ '@babel/plugin-transform-react-jsx', @@ -76,12 +73,6 @@ function getPreClosureConfig() { !isCheckTypes ? './build-system/babel-plugins/babel-plugin-transform-json-configuration' : null, - !(isFortesting || isCheckTypes) - ? [ - './build-system/babel-plugins/babel-plugin-amp-mode-transformer', - {isEsmBuild: !!argv.esm}, - ] - : null, ].filter(Boolean); const presetEnv = [ '@babel/preset-env', diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js deleted file mode 100644 index 1b584a5f02ec..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js +++ /dev/null @@ -1,73 +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 values of getMode().test, getMode().localDev to false - * and getMode().localDev to true. - * @param {Object} babelTypes - */ -const {dirname, join, relative, resolve} = require('path').posix; - -let shouldResolveDevelopmentMode = true; - -// This plugin is not executed when AMP is building resources in isForTesting mode. -module.exports = function ({types: t}) { - let getModeFound = false; - return { - pre() { - const {isEsmBuild = true} = this.opts; - // Only apply the development resolution when building module output. - // This is due to the module output only applying to AMP Caches. - shouldResolveDevelopmentMode = isEsmBuild; - }, - visitor: { - ImportDeclaration({node}, state) { - const {source, specifiers} = node; - if (!source.value.endsWith('/mode')) { - return; - } - specifiers.forEach((specifier) => { - if (specifier.imported && specifier.imported.name === 'getMode') { - const filepath = relative( - join(__dirname, '../../../'), - resolve(dirname(state.file.opts.filename), source.value) - ); - if (filepath.endsWith('src/mode')) { - getModeFound = true; - } - } - }); - }, - MemberExpression(path) { - if (!getModeFound) { - return; - } - - const {node} = path; - const {object: obj, property} = node; - const {callee} = obj; - if (callee && callee.name === 'getMode') { - if (property.name === 'test' || property.name === 'localDev') { - path.replaceWith(t.booleanLiteral(false)); - } - if (shouldResolveDevelopmentMode && property.name === 'development') { - path.replaceWith(t.booleanLiteral(false)); - } - } - }, - }, - }; -}; diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js deleted file mode 100644 index d6bbdd24f835..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js +++ /dev/null @@ -1,24 +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 test = getMode().test; -const localDev = getMode().localDev; -const minified = getMode().minified; -const development = getMode().development; - -function getMode() { - return {}; -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/options.json b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/options.json deleted file mode 100644 index 3ebeb6374116..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-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-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js deleted file mode 100644 index 5e7f97a5f3db..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js +++ /dev/null @@ -1,23 +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 test = getMode().test; -const localDev = getMode().localDev; -const minified = getMode().minified; -const development = getMode().development; - -function getMode() { - return {}; -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js deleted file mode 100644 index 094cba5ba364..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js +++ /dev/null @@ -1,28 +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. - */ - -import { getMode } from '../../../../../../../src/mode'; - -const test = getMode().test; -const localDev = getMode().localDev; -const minified = getMode().minified; -const development = getMode().development; - -function foo() { - if (getMode().development == false) { - return false; - } -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json deleted file mode 100644 index c08413593a14..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "plugins": [ - [ - "../../../..", - { - "isEsmBuild": false - } - ] - ], - "sourceType": "module" -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs deleted file mode 100644 index 3ad2689e8c47..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs +++ /dev/null @@ -1,26 +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. - */ -import { getMode } from '../../../../../../../src/mode'; -const test = false; -const localDev = false; -const minified = true; -const development = getMode().development; - -function foo() { - if (getMode().development == false) { - return false; - } -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js deleted file mode 100644 index 094cba5ba364..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js +++ /dev/null @@ -1,28 +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. - */ - -import { getMode } from '../../../../../../../src/mode'; - -const test = getMode().test; -const localDev = getMode().localDev; -const minified = getMode().minified; -const development = getMode().development; - -function foo() { - if (getMode().development == false) { - return false; - } -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json deleted file mode 100644 index 239cccc175ab..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "plugins": [ - "../../../.." - ], - "sourceType": "module" -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs deleted file mode 100644 index 73c919c2189b..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs +++ /dev/null @@ -1,26 +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. - */ -import { getMode } from '../../../../../../../src/mode'; -const test = false; -const localDev = false; -const minified = true; -const development = false; - -function foo() { - if (false == false) { - return false; - } -} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/index.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/index.js deleted file mode 100644 index 7b6d7376a749..000000000000 --- a/build-system/babel-plugins/babel-plugin-amp-mode-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); From bd763acd2533b11687b734fcc5045b5cec87e222 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Thu, 8 Jul 2021 00:26:17 -0400 Subject: [PATCH 08/14] Revert "remove amp-mode-transform" This reverts commit 853c8da500d35e5aef0a202de3cc830a10dcf95f. --- build-system/babel-config/minified-config.js | 6 ++ .../babel-config/pre-closure-config.js | 9 +++ .../index.js | 73 +++++++++++++++++++ .../no-transform/input.js | 24 ++++++ .../no-transform/options.json | 5 ++ .../no-transform/output.js | 23 ++++++ .../transform-assertions/nomodule/input.js | 28 +++++++ .../nomodule/options.json | 11 +++ .../transform-assertions/nomodule/output.mjs | 26 +++++++ .../transform-assertions/transform/input.js | 28 +++++++ .../transform/options.json | 6 ++ .../transform-assertions/transform/output.mjs | 26 +++++++ .../test/index.js | 19 +++++ 13 files changed, 284 insertions(+) create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/options.json create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs create mode 100644 build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/index.js diff --git a/build-system/babel-config/minified-config.js b/build-system/babel-config/minified-config.js index fd2d015432e6..aa2d3ad167e7 100644 --- a/build-system/babel-config/minified-config.js +++ b/build-system/babel-config/minified-config.js @@ -67,6 +67,12 @@ function getMinifiedConfig() { argv.fortesting ? null : './build-system/babel-plugins/babel-plugin-transform-json-configuration', + argv.fortesting + ? null + : [ + './build-system/babel-plugins/babel-plugin-amp-mode-transformer', + {isEsmBuild: !!argv.esm}, + ], ].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 3f532e924ccd..f0741814ae75 100644 --- a/build-system/babel-config/pre-closure-config.js +++ b/build-system/babel-config/pre-closure-config.js @@ -26,6 +26,9 @@ const {getReplacePlugin} = require('./helpers'); */ function getPreClosureConfig() { const isCheckTypes = argv._.includes('check-types'); + const testTasks = ['e2e', 'integration', 'visual-diff']; + const isTestTask = testTasks.some((task) => argv._.includes(task)); + const isFortesting = argv.fortesting || isTestTask; const reactJsxPlugin = [ '@babel/plugin-transform-react-jsx', @@ -73,6 +76,12 @@ function getPreClosureConfig() { !isCheckTypes ? './build-system/babel-plugins/babel-plugin-transform-json-configuration' : null, + !(isFortesting || isCheckTypes) + ? [ + './build-system/babel-plugins/babel-plugin-amp-mode-transformer', + {isEsmBuild: !!argv.esm}, + ] + : null, ].filter(Boolean); const presetEnv = [ '@babel/preset-env', diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js new file mode 100644 index 000000000000..1b584a5f02ec --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js @@ -0,0 +1,73 @@ +/** + * 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 values of getMode().test, getMode().localDev to false + * and getMode().localDev to true. + * @param {Object} babelTypes + */ +const {dirname, join, relative, resolve} = require('path').posix; + +let shouldResolveDevelopmentMode = true; + +// This plugin is not executed when AMP is building resources in isForTesting mode. +module.exports = function ({types: t}) { + let getModeFound = false; + return { + pre() { + const {isEsmBuild = true} = this.opts; + // Only apply the development resolution when building module output. + // This is due to the module output only applying to AMP Caches. + shouldResolveDevelopmentMode = isEsmBuild; + }, + visitor: { + ImportDeclaration({node}, state) { + const {source, specifiers} = node; + if (!source.value.endsWith('/mode')) { + return; + } + specifiers.forEach((specifier) => { + if (specifier.imported && specifier.imported.name === 'getMode') { + const filepath = relative( + join(__dirname, '../../../'), + resolve(dirname(state.file.opts.filename), source.value) + ); + if (filepath.endsWith('src/mode')) { + getModeFound = true; + } + } + }); + }, + MemberExpression(path) { + if (!getModeFound) { + return; + } + + const {node} = path; + const {object: obj, property} = node; + const {callee} = obj; + if (callee && callee.name === 'getMode') { + if (property.name === 'test' || property.name === 'localDev') { + path.replaceWith(t.booleanLiteral(false)); + } + if (shouldResolveDevelopmentMode && property.name === 'development') { + path.replaceWith(t.booleanLiteral(false)); + } + } + }, + }, + }; +}; diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js new file mode 100644 index 000000000000..d6bbdd24f835 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/input.js @@ -0,0 +1,24 @@ +/** + * 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 test = getMode().test; +const localDev = getMode().localDev; +const minified = getMode().minified; +const development = getMode().development; + +function getMode() { + return {}; +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/options.json b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/options.json new file mode 100644 index 000000000000..3ebeb6374116 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [ + "../../../.." + ] +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js new file mode 100644 index 000000000000..5e7f97a5f3db --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/no-transform/output.js @@ -0,0 +1,23 @@ +/** + * 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 test = getMode().test; +const localDev = getMode().localDev; +const minified = getMode().minified; +const development = getMode().development; + +function getMode() { + return {}; +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js new file mode 100644 index 000000000000..094cba5ba364 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/input.js @@ -0,0 +1,28 @@ +/** + * 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. + */ + +import { getMode } from '../../../../../../../src/mode'; + +const test = getMode().test; +const localDev = getMode().localDev; +const minified = getMode().minified; +const development = getMode().development; + +function foo() { + if (getMode().development == false) { + return false; + } +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json new file mode 100644 index 000000000000..c08413593a14 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "../../../..", + { + "isEsmBuild": false + } + ] + ], + "sourceType": "module" +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs new file mode 100644 index 000000000000..3ad2689e8c47 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/nomodule/output.mjs @@ -0,0 +1,26 @@ +/** + * 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. + */ +import { getMode } from '../../../../../../../src/mode'; +const test = false; +const localDev = false; +const minified = true; +const development = getMode().development; + +function foo() { + if (getMode().development == false) { + return false; + } +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js new file mode 100644 index 000000000000..094cba5ba364 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/input.js @@ -0,0 +1,28 @@ +/** + * 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. + */ + +import { getMode } from '../../../../../../../src/mode'; + +const test = getMode().test; +const localDev = getMode().localDev; +const minified = getMode().minified; +const development = getMode().development; + +function foo() { + if (getMode().development == false) { + return false; + } +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json new file mode 100644 index 000000000000..239cccc175ab --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "../../../.." + ], + "sourceType": "module" +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs new file mode 100644 index 000000000000..73c919c2189b --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/fixtures/transform-assertions/transform/output.mjs @@ -0,0 +1,26 @@ +/** + * 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. + */ +import { getMode } from '../../../../../../../src/mode'; +const test = false; +const localDev = false; +const minified = true; +const development = false; + +function foo() { + if (false == false) { + return false; + } +} diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/index.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/index.js new file mode 100644 index 000000000000..7b6d7376a749 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/test/index.js @@ -0,0 +1,19 @@ +/** + * 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); From 9c49155b164180521707ca136ae373494b5f1aea Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Thu, 8 Jul 2021 00:41:40 -0400 Subject: [PATCH 09/14] bring back the amp-mode minified transform --- .../babel-plugins/babel-plugin-amp-mode-transformer/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js index 1b584a5f02ec..d686ef9c7131 100644 --- a/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js +++ b/build-system/babel-plugins/babel-plugin-amp-mode-transformer/index.js @@ -66,6 +66,9 @@ module.exports = function ({types: t}) { if (shouldResolveDevelopmentMode && property.name === 'development') { path.replaceWith(t.booleanLiteral(false)); } + if (property.name === 'minified') { + path.replaceWith(t.booleanLiteral(true)); + } } }, }, From 79d99f645361d67e27275bad50322b34e85ea076 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Thu, 8 Jul 2021 10:32:54 -0400 Subject: [PATCH 10/14] revert seemingly unrelated change --- build-system/tsconfig.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/build-system/tsconfig.json b/build-system/tsconfig.json index da4e0a3f680e..953bf2714106 100644 --- a/build-system/tsconfig.json +++ b/build-system/tsconfig.json @@ -37,7 +37,6 @@ "./eslint-rules/**/*.js", "./externs/**/*.js", "./tasks/make-extension/template/**/*.js", - "./tasks/performance/cache/**/*.js", - "./server/new-server/transforms/dist/**/*.js" + "./tasks/performance/cache/**/*.js" ] } From e4fde0c3bfb0aaf1bef580b347ab750493fea582 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Thu, 8 Jul 2021 10:43:31 -0400 Subject: [PATCH 11/14] Update build-system/compile/build-constants.js Co-authored-by: Ryan Cebulko --- build-system/compile/build-constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-system/compile/build-constants.js b/build-system/compile/build-constants.js index d06b57c3da68..7d3af04e1cfd 100644 --- a/build-system/compile/build-constants.js +++ b/build-system/compile/build-constants.js @@ -19,7 +19,7 @@ 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); +const isMinified = argv._.includes('dist') || !!argv.compiled; /** * Build time constants. Used by babel but hopefully one day directly by the bundlers.. From 205a5584c0764997979280b764f95d39e38bccc5 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Thu, 8 Jul 2021 10:52:07 -0400 Subject: [PATCH 12/14] address rcebulko feedback --- .eslintrc.js | 1 + build-system/test-configs/forbidden-terms.js | 22 +++++++++++++++++--- src/internal-version.js | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 7da9e64ed917..fbf75e24dba9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -74,6 +74,7 @@ module.exports = { 'IS_SXG': 'readonly', 'IS_MINIFIED': 'readonly', 'IS_FORTESTING': 'readonly', + 'INTERNAL_RUNTIME_VERSION': 'readonly', 'AMP': 'readonly', 'context': 'readonly', 'global': 'readonly', diff --git a/build-system/test-configs/forbidden-terms.js b/build-system/test-configs/forbidden-terms.js index e09fb42758b9..ec39543a25b6 100644 --- a/build-system/test-configs/forbidden-terms.js +++ b/build-system/test-configs/forbidden-terms.js @@ -146,11 +146,27 @@ 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.', + 'IS_FORTESTING': { + message: 'IS_FORTESTING local var only allowed in core/mode/for-testing.js', + allowlist: [ + 'src/core/mode/for-testing.js', + 'build-system/compile/build-constants.js', + ], }, - '(?:var|let|const) +IS_MINIFIED +=': { + 'IS_MINIFIED': { message: 'IS_MINIFIED local var only allowed in core/mode/minified.js', + allowlist: [ + 'src/core/mode/minified.js', + 'build-system/compile/build-constants.js', + ], + }, + 'INTERNAL_RUNTIME_VERSION': { + message: + 'INTERNAL_RUNTIME_VERSION local var only allowed in src/internal-version.js', + allowlist: [ + 'src/internal-version.js', + 'build-system/compile/build-constants.js', + ], }, '\\.prefetch\\(': { message: 'Do not use preconnect.prefetch, use preconnect.preload instead.', diff --git a/src/internal-version.js b/src/internal-version.js index b200139e5aa7..aafc9fac3a97 100644 --- a/src/internal-version.js +++ b/src/internal-version.js @@ -24,5 +24,5 @@ * @return {string} */ export function internalRuntimeVersion() { - return INTERNAL_RUNTIME_VERSION; // eslint-disable-line no-undef + return INTERNAL_RUNTIME_VERSION; } From fb2b14eb157c831b172f42145c826dd7a32221b7 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Thu, 8 Jul 2021 10:53:15 -0400 Subject: [PATCH 13/14] forbidden terms message update --- build-system/test-configs/forbidden-terms.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build-system/test-configs/forbidden-terms.js b/build-system/test-configs/forbidden-terms.js index ec39543a25b6..17f48f12c04e 100644 --- a/build-system/test-configs/forbidden-terms.js +++ b/build-system/test-configs/forbidden-terms.js @@ -147,14 +147,14 @@ const forbiddenTermsGlobal = { allowlist: ['src/mode-object.js', 'src/iframe-attributes.js'], }, 'IS_FORTESTING': { - message: 'IS_FORTESTING local var only allowed in core/mode/for-testing.js', + message: 'IS_FORTESTING only allowed in core/mode/for-testing.js', allowlist: [ 'src/core/mode/for-testing.js', 'build-system/compile/build-constants.js', ], }, 'IS_MINIFIED': { - message: 'IS_MINIFIED local var only allowed in core/mode/minified.js', + message: 'IS_MINIFIED only allowed in core/mode/minified.js', allowlist: [ 'src/core/mode/minified.js', 'build-system/compile/build-constants.js', @@ -162,7 +162,7 @@ const forbiddenTermsGlobal = { }, 'INTERNAL_RUNTIME_VERSION': { message: - 'INTERNAL_RUNTIME_VERSION local var only allowed in src/internal-version.js', + 'INTERNAL_RUNTIME_VERSION only allowed in src/internal-version.js', allowlist: [ 'src/internal-version.js', 'build-system/compile/build-constants.js', From 0cf1a3f0536e92e80aba5576e3dbce3b91fa8e66 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Thu, 8 Jul 2021 11:04:25 -0400 Subject: [PATCH 14/14] further cleanup --- build-system/test-configs/forbidden-terms.js | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/build-system/test-configs/forbidden-terms.js b/build-system/test-configs/forbidden-terms.js index 17f48f12c04e..5c45d2b49bc4 100644 --- a/build-system/test-configs/forbidden-terms.js +++ b/build-system/test-configs/forbidden-terms.js @@ -146,25 +146,13 @@ const forbiddenTermsGlobal = { message: realiasGetMode, allowlist: ['src/mode-object.js', 'src/iframe-attributes.js'], }, - 'IS_FORTESTING': { - message: 'IS_FORTESTING only allowed in core/mode/for-testing.js', - allowlist: [ - 'src/core/mode/for-testing.js', - 'build-system/compile/build-constants.js', - ], - }, - 'IS_MINIFIED': { - message: 'IS_MINIFIED only allowed in core/mode/minified.js', - allowlist: [ - 'src/core/mode/minified.js', - 'build-system/compile/build-constants.js', - ], - }, - 'INTERNAL_RUNTIME_VERSION': { + 'INTERNAL_RUNTIME_VERSION|IS_(FORTESTING|MINIFIED)': { message: - 'INTERNAL_RUNTIME_VERSION only allowed in src/internal-version.js', + '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', ], },