Skip to content

Commit

Permalink
rewrite import.meta to (#6031)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper authored Aug 10, 2021
1 parent 2e3cb2a commit 4ca3232
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Rewrite import.meta Can detect and rewrite import.meta to $csb__import_meta 1`] = `
"\\"use strict\\";
var $csb__import_meta = {
url: \\"https://csb.io/index.js\\"
};
const a = $csb__import_meta.url;
"
`;

exports[`Rewrite import.meta Should not add a global $csb__import_meta when there is no need for it 1`] = `
"\\"use strict\\";
const a = \\"hello\\";
"
`;
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { walk } from 'estree-walker';

import { ESTreeAST } from './utils';
import { Syntax as n } from './syntax';

export function collectDependenciesFromAST(ast: ESTreeAST): Array<string> {
const deps: Set<string> = new Set();
walk(ast.program, {
enter(node) {
// @ts-ignore
if (node.type === 'CallExpression' && node.callee.name === 'require') {
if (node.type === n.CallExpression && node.callee.name === 'require') {
// @ts-ignore
if (node.arguments.length && node.arguments[0].value) {
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { rewriteImportMeta } from './rewrite-meta';
import { generateCode, parseModule } from './utils';

describe('Rewrite import.meta', () => {
it('Can detect and rewrite import.meta to $csb__import_meta', () => {
const code = `const a = import.meta.url;`;
const ast = parseModule(code);
rewriteImportMeta(ast, {
url: 'https://csb.io/index.js',
});
const result = generateCode(ast);
expect(result).toMatchSnapshot();
});

it('Should not add a global $csb__import_meta when there is no need for it', () => {
const code = `const a = "hello";`;
const ast = parseModule(code);
rewriteImportMeta(ast, {
url: 'https://csb.io/index.js',
});
const result = generateCode(ast);
expect(result).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This code is written to be performant, that's why we opted to ignore these linting issues
/* eslint-disable no-loop-func, no-continue */
import * as meriyah from 'meriyah';
import { walk } from 'estree-walker';

import { Syntax as n } from './syntax';
import { ESTreeAST } from './utils';

export interface IESModuleMeta {
url: string;
}

const CSB_IMPORT_META_NAME = '$csb__import_meta';
export function rewriteImportMeta(ast: ESTreeAST, meta: IESModuleMeta): void {
let hasImportMeta = false;
walk(ast.program, {
enter(node: meriyah.ESTree.MemberExpression) {
if (node.type === n.MemberExpression) {
if (
node.object.type === n.MetaProperty &&
node.object.meta.name === 'import'
) {
node.object = {
type: n.Identifier,
name: CSB_IMPORT_META_NAME,
};

hasImportMeta = true;

this.skip();
}
}
},
});

if (hasImportMeta) {
ast.program.body.unshift({
type: n.VariableDeclaration,
kind: 'var',
declarations: [
{
type: n.VariableDeclarator,
id: {
type: n.Identifier,
name: CSB_IMPORT_META_NAME,
},
init: {
type: n.ObjectExpression,
properties: [
{
type: n.Property,
kind: 'init',
computed: false,
shorthand: false,
method: false,
key: {
type: n.Identifier,
name: 'url',
},
value: {
type: n.Literal,
value: meta.url,
raw: JSON.stringify(meta.url),
},
},
],
},
},
],
});
}
}
11 changes: 11 additions & 0 deletions packages/app/src/sandbox/eval/transpilers/babel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getSyntaxInfoFromAst } from './ast/syntax-info';
import { convertEsModule } from './ast/convert-esmodule';
import { ESTreeAST, generateCode, parseModule } from './ast/utils';
import { collectDependenciesFromAST } from './ast/collect-dependencies';
import { rewriteImportMeta } from './ast/rewrite-meta';

const MAX_WORKER_ITERS = 100;

Expand All @@ -32,6 +33,13 @@ function addCollectedDependencies(
return Promise.all(deps.map(dep => loaderContext.addDependency(dep)));
}

function getModuleUrl(path: string): string {
if (isUrl(path)) {
return path;
}
return new URL(path, window.location.href).href;
}

// Right now this is in a worker, but when we're going to allow custom plugins
// we need to move this out of the worker again, because the config needs
// to support custom plugins
Expand Down Expand Up @@ -100,6 +108,9 @@ class BabelTranspiler extends WorkerTranspiler {
// Which is actually invalid but we probably don't wanna break anyone's code if it works in other bundlers...
const deps = collectDependenciesFromAST(ast);
await addCollectedDependencies(loaderContext, deps);
rewriteImportMeta(ast, {
url: getModuleUrl(loaderContext.path),
});
endMeasure(`esconvert-${path}`, { silent: true });
return {
transpiledCode: generateCode(ast),
Expand Down

0 comments on commit 4ca3232

Please sign in to comment.