diff --git a/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/index.js b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/index.js new file mode 100644 index 0000000000000..c27d1c33feeb4 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/index.js @@ -0,0 +1,35 @@ +/** + * 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 IS_DEV to false and IS_MINIFIED to true. + * The above said variables are in src/mode.js file. + * @param {Object} babelTypes + */ +module.exports = function({types: t}) { + return { + visitor: { + VariableDeclarator(path) { + const {node} = path; + const {id, init} = node; + if (t.isIdentifier(id, {name: 'IS_DEV'}) + && t.isBooleanLiteral(init, {value: true})) { + node.init = t.booleanLiteral(false); + } + }, + }, + }; +}; diff --git a/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/input.js b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/input.js new file mode 100644 index 0000000000000..21f1ed57dc773 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/input.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 IS_DEV = true; +const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/options.json b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/options.json new file mode 100644 index 0000000000000..13f0fa59df0e1 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-is_dev-constant-transformer"] +} diff --git a/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/output.js b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/output.js new file mode 100644 index 0000000000000..9319a005fcae9 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/isdev-transform/output.js @@ -0,0 +1,17 @@ +/** + * 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_DEV = false; +const IS_EXPANDED = false; diff --git a/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js new file mode 100644 index 0000000000000..88371a1445c78 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/input.js @@ -0,0 +1,20 @@ + +/** + * 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_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json new file mode 100644 index 0000000000000..13f0fa59df0e1 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-is_dev-constant-transformer"] +} diff --git a/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js new file mode 100644 index 0000000000000..bf4c3530b6af0 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js @@ -0,0 +1,17 @@ +/** + * 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_dev-constant-transformer/test/index.js b/build-system/babel-plugins/babel-plugin-is_dev-constant-transformer/test/index.js new file mode 100644 index 0000000000000..7b6d7376a7498 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_dev-constant-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); 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 new file mode 100644 index 0000000000000..3d9f8daa5157f --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/index.js @@ -0,0 +1,40 @@ +/** + * 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 IS_DEV to false and IS_MINIFIED to true. + * The above said variables are in src/mode.js file. + * @param {Object} babelTypes + */ +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 new file mode 100644 index 0000000000000..ff730f6e824f3 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/input.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 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 new file mode 100644 index 0000000000000..be67ebac87b6e --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-is_minified-constant-transformer"] +} 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 new file mode 100644 index 0000000000000..7ac0ff36819f0 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/isminified-transform/output.js @@ -0,0 +1,17 @@ +/** + * 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 new file mode 100644 index 0000000000000..b42410f328d2f --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/input.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 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 new file mode 100644 index 0000000000000..be67ebac87b6e --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-is_minified-constant-transformer"] +} 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 new file mode 100644 index 0000000000000..bf4c3530b6af0 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-transformer/test/fixtures/transform-assertions/no-transform/output.js @@ -0,0 +1,17 @@ +/** + * 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 new file mode 100644 index 0000000000000..7b6d7376a7498 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-is_minified-constant-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); diff --git a/build-system/build.conf.js b/build-system/build.conf.js index 9651ae86758f8..3e039dd3fb953 100644 --- a/build-system/build.conf.js +++ b/build-system/build.conf.js @@ -21,10 +21,16 @@ const defaultPlugins = [ './babel-plugins/babel-plugin-transform-html-template'), require.resolve( './babel-plugins/babel-plugin-transform-parenthesize-expression'), + require.resolve( + './babel-plugins/babel-plugin-is_minified-constant-transformer'), ]; module.exports = { - plugins: (isEsmBuild, isCommonJsModule) => { + plugins: ({ + isEsmBuild, + isCommonJsModule, + isForTesting, + }) => { let pluginsToApply = defaultPlugins; if (isEsmBuild) { pluginsToApply = pluginsToApply.concat([ @@ -46,6 +52,15 @@ module.exports = { [require.resolve('babel-plugin-transform-commonjs-es2015-modules')], ]); } + if (!isForTesting) { + pluginsToApply = pluginsToApply.concat([ + [ + require.resolve( + './babel-plugins/babel-plugin-is_dev-constant-transformer' + ), + ], + ]); + } return pluginsToApply; }, }; diff --git a/build-system/single-pass.js b/build-system/single-pass.js index 448b12e8a52c7..8c463d2322043 100644 --- a/build-system/single-pass.js +++ b/build-system/single-pass.js @@ -431,9 +431,11 @@ function transformPathsToTempDir(graph, config) { fs.copySync(f, `${graph.tmp}/${f}`); } else { const {code} = babel.transformFileSync(f, { - plugins: conf.plugins( - /* isEsmBuild */ config.define.indexOf('ESM_BUILD=true') !== -1, - /* isCommonJsModule */ isCommonJsModule(f)), + plugins: conf.plugins({ + isEsmBuild: config.define.indexOf('ESM_BUILD=true') !== -1, + isCommonJsModule: isCommonJsModule(f), + isForTesting: config.define.indexOf('FORTESTING=true') !== -1, + }), retainLines: true, }); fs.outputFileSync(`${graph.tmp}/${f}`, code);