Skip to content

Commit

Permalink
adding babel plugin for IS_DEV and IS_MINIFIED transformation (#1…
Browse files Browse the repository at this point in the history
…9690)

* adding babel plugin for const transformation

* adding test for transformer

* fixing the is fortesting condition

* fixing lints

* fixing lints

* fixing `is_minified` always true condition

* lint fixes

* fixing babel transformer

* fixes copyrights
  • Loading branch information
prateekbh committed Dec 14, 2018
1 parent a34b0e5 commit 36f31e0
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 4 deletions.
@@ -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;
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

0 comments on commit 36f31e0

Please sign in to comment.