From f14b23bfbe01d3667fb75be4c739a1e5b48e60a3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 20 Oct 2020 11:26:38 -0400 Subject: [PATCH] use regex for detecting import.meta (#1371) * use regex for detecting import.meta * move regex out of hot code path * update snapshot --- snowpack/src/build/build-import-proxy.ts | 4 +++- test/build/base-url/__snapshots__ | 4 +++- test/build/base-url/public/index.js | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/snowpack/src/build/build-import-proxy.ts b/snowpack/src/build/build-import-proxy.ts index 792da5ddba..826b132556 100644 --- a/snowpack/src/build/build-import-proxy.ts +++ b/snowpack/src/build/build-import-proxy.ts @@ -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)); @@ -30,7 +32,7 @@ export function wrapImportMeta({ env: boolean; config: SnowpackConfig; }) { - if (!code.includes('import.meta')) { + if (!importMetaRegex.test(code)) { return code; } return ( diff --git a/test/build/base-url/__snapshots__ b/test/build/base-url/__snapshots__ index e253b1118f..58682c7876 100644 --- a/test/build/base-url/__snapshots__ +++ b/test/build/base-url/__snapshots__ @@ -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`] = ` diff --git a/test/build/base-url/public/index.js b/test/build/base-url/public/index.js index 806d99849e..6bd832009f 100644 --- a/test/build/base-url/public/index.js +++ b/test/build/base-url/public/index.js @@ -7,4 +7,6 @@ export default function doNothing() { } // Triggers a snowpack meta import URL -console.log(import.meta.env) \ No newline at end of file +console.log(import + .meta + .env) \ No newline at end of file