Skip to content

Commit

Permalink
modify babel-transform to cover #mode
Browse files Browse the repository at this point in the history
  • Loading branch information
samouri committed Jul 30, 2021
1 parent 0f409c8 commit 276aee1
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Expand Up @@ -74,7 +74,7 @@ module.exports = {
'IS_SXG': 'readonly',
'IS_MINIFIED': 'readonly',
'IS_PROD': 'readonly',
'VERSION': 'readonly',
'INTERNAL_RUNTIME_VERSION': 'readonly',
'AMP': 'readonly',
'context': 'readonly',
'global': 'readonly',
Expand Down
Expand Up @@ -24,15 +24,25 @@ const {dirname, join, relative, resolve} = require('path').posix;
// This plugin is only executed when bundling for production builds (minified).
module.exports = function ({types: t}) {
let getModeFound = false;
let modeNamespaceFound = false;

return {
visitor: {
ImportDeclaration({node}, state) {
const {source, specifiers} = node;
if (!source.value.endsWith('/mode')) {
return;
}

if (source.value === '#core/mode') {
if (specifiers.length === 1 && specifiers[0].local.name === 'mode') {
modeNamespaceFound = true;
}
}

specifiers.forEach((specifier) => {
if (specifier.imported && specifier.imported.name === 'getMode') {
const name = specifier.imported?.name ?? '';
if (name === 'getMode') {
const filepath = relative(
join(__dirname, '../../../'),
resolve(dirname(state.file.opts.filename), source.value)
Expand All @@ -51,7 +61,7 @@ module.exports = function ({types: t}) {
const {node} = path;
const {object: obj, property} = node;
const {callee} = obj;
const {VERSION} = this.opts;
const {INTERNAL_RUNTIME_VERSION: version} = this.opts;

if (callee && callee.name === 'getMode') {
if (property.name === 'test' || property.name === 'localDev') {
Expand All @@ -61,14 +71,22 @@ module.exports = function ({types: t}) {
} else if (property.name === 'minified') {
path.replaceWith(t.booleanLiteral(true));
} else if (property.name === 'version') {
path.replaceWith(t.stringLiteral(VERSION));
path.replaceWith(t.stringLiteral(version));
}
}
},
CallExpression(path) {
if (!modeNamespaceFound) {
return;
}

if (callee && callee.name === 'mode') {
if (property.name === 'version') {
path.replaceWith(t.stringLiteral(VERSION));
}
const {INTERNAL_RUNTIME_VERSION: version} = this.opts;
const {callee} = path.node;
if (
callee?.object?.name === 'mode' &&
callee?.property?.name === 'version'
) {
path.replaceWith(t.stringLiteral(version));
}
},
},
Expand Down
Expand Up @@ -14,13 +14,15 @@
* limitations under the License.
*/

import { getMode } from '../../../../../../../src/mode';
import {getMode} from '../../../../../../../src/mode';
import * as mode from '#core/mode';

const test = getMode().test;
const localDev = getMode().localDev;
const minified = getMode().minified;
const development = getMode().development;
const version = getMode().version;
const namespaceVersion = mode.version();

function foo() {
if (getMode().development == false) {
Expand Down
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/
import { getMode } from '../../../../../../../src/mode';
import * as mode from '#core/mode';
const test = false;
const localDev = false;
const minified = true;
const development = false;
const version = "$internalRuntimeVersion$";
const namespaceVersion = "$internalRuntimeVersion$";

function foo() {
if (false == false) {
Expand Down
2 changes: 1 addition & 1 deletion build-system/compile/build-constants.js
Expand Up @@ -39,7 +39,7 @@ const isMinified = argv._.includes('dist') || !!argv.compiled;
const BUILD_CONSTANTS = {
IS_PROD: isProd,
IS_MINIFIED: isMinified,
VERSION: isTestTask ? '$internalRuntimeVersion$' : VERSION,
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.
Expand Down
2 changes: 1 addition & 1 deletion build-system/test-configs/forbidden-terms.js
Expand Up @@ -146,7 +146,7 @@ const forbiddenTermsGlobal = {
message: realiasGetMode,
allowlist: ['src/mode-object.js', 'src/iframe-attributes.js'],
},
'VERSION|IS_(PROD|MINIFIED)': {
'INTERNAL_RUNTIME_VERSION|IS_(PROD|MINIFIED)': {
message:
'Do not use build constants directly. Instead, use the helpers in `#core/mode`.',
allowlist: [
Expand Down
2 changes: 1 addition & 1 deletion src/core/mode/version.js
Expand Up @@ -24,5 +24,5 @@
* @return {string}
*/
export function version() {
return VERSION;
return INTERNAL_RUNTIME_VERSION;
}
2 changes: 1 addition & 1 deletion src/service/url-replacements-impl.js
Expand Up @@ -592,7 +592,7 @@ export class GlobalVariableSource extends VariableSource {
});

// returns the AMP version number
this.set('AMP_VERSION', () => mode.version()); // eslint-disable-line local/no-forbidden-terms
this.set('AMP_VERSION', () => mode.version());

this.set('BACKGROUND_STATE', () => {
return this.ampdoc.isVisible() ? '0' : '1';
Expand Down

0 comments on commit 276aee1

Please sign in to comment.