Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

adding babel plugin for IS_DEV and IS_MINIFIED transformation #19690

Merged
merged 15 commits into from Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);
}
},
},
};
};
@@ -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;
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-is_dev-constant-transformer"]
}
@@ -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;
@@ -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;
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-is_dev-constant-transformer"]
}
@@ -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;
@@ -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);
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: inline this into the parameter

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)
)
);
}
},
},
};
};
@@ -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;
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-is_minified-constant-transformer"]
}
@@ -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;
@@ -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;
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-is_minified-constant-transformer"]
}
@@ -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;
@@ -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);
17 changes: 16 additions & 1 deletion build-system/build.conf.js
Expand Up @@ -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([
Expand All @@ -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;
},
};
8 changes: 5 additions & 3 deletions build-system/single-pass.js
Expand Up @@ -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);
Expand Down