-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e3cb2a
commit 4ca3232
Showing
5 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/app/src/sandbox/eval/transpilers/babel/ast/__snapshots__/rewrite-meta.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\"; | ||
" | ||
`; |
3 changes: 2 additions & 1 deletion
3
packages/app/src/sandbox/eval/transpilers/babel/ast/collect-dependencies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/app/src/sandbox/eval/transpilers/babel/ast/rewrite-meta.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
packages/app/src/sandbox/eval/transpilers/babel/ast/rewrite-meta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters