Skip to content

Commit

Permalink
use regex for detecting import.meta (#1371)
Browse files Browse the repository at this point in the history
* use regex for detecting import.meta

* move regex out of hot code path

* update snapshot
  • Loading branch information
Rich-Harris authored Oct 20, 2020
1 parent d18ab79 commit f14b23b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion snowpack/src/build/build-import-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const SRI_ERROR_HMR_SNOWPACK = generateSRI(
readFileSync(path.join(__dirname, '../../assets/hmr-error-overlay.js')),
);

const importMetaRegex = /import\s*\.\s*meta/;

export function getMetaUrlPath(urlPath: string, config: SnowpackConfig): string {
let {metaDir} = config.buildOptions || {};
return path.posix.normalize(path.posix.join('/', metaDir, urlPath));
Expand All @@ -30,7 +32,7 @@ export function wrapImportMeta({
env: boolean;
config: SnowpackConfig;
}) {
if (!code.includes('import.meta')) {
if (!importMetaRegex.test(code)) {
return code;
}
return (
Expand Down
4 changes: 3 additions & 1 deletion test/build/base-url/__snapshots__
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export default function doNothing() {
// I do nothing 🎉
}
// Triggers a snowpack meta import URL
console.log(import.meta.env)"
console.log(import
.meta
.env)"
`;

exports[`snowpack build base-url: build/web_modules/array-flatten.js 1`] = `
Expand Down
4 changes: 3 additions & 1 deletion test/build/base-url/public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export default function doNothing() {
}

// Triggers a snowpack meta import URL
console.log(import.meta.env)
console.log(import
.meta
.env)

1 comment on commit f14b23b

@vercel
Copy link

@vercel vercel bot commented on f14b23b Oct 20, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.