From 3a83d6d1549bb98fed162f805db6aac0d98df7d0 Mon Sep 17 00:00:00 2001 From: bjoluc Date: Fri, 30 Sep 2022 21:31:42 +0200 Subject: [PATCH] fix: Disable compile time evaluation of import.meta.url (#897) I'm affected by https://github.com/webpack/webpack/issues/14445, fixed in https://github.com/webpack/webpack/pull/15246, available in webpack >= [v5.68.0](https://github.com/webpack/webpack/releases/tag/v5.68.0), so I'd love to have an ncc version using webpack >= v5.68.0! Co-authored-by: Steven --- package.json | 2 +- src/index.js | 5 ++- test/unit/import-meta-esm/input.js | 1 + test/unit/import-meta-esm/output-coverage.js | 7 ++++ test/unit/import-meta-esm/output.js | 7 ++++ test/unit/import-meta-esm/package.json | 3 ++ test/watcher.test.js | 36 ++++++++++++----- yarn.lock | 42 ++++++++++---------- 8 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 test/unit/import-meta-esm/input.js create mode 100644 test/unit/import-meta-esm/output-coverage.js create mode 100644 test/unit/import-meta-esm/output.js create mode 100644 test/unit/import-meta-esm/package.json diff --git a/package.json b/package.json index 95d30ed1..e338a2e5 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,7 @@ "vue": "^2.5.17", "vue-server-renderer": "^2.5.17", "web-vitals": "^0.2.4", - "webpack": "5.62.1", + "webpack": "5.70.0", "when": "^3.7.8" }, "resolutions": { diff --git a/src/index.js b/src/index.js index 593ea088..03b3f8f6 100644 --- a/src/index.js +++ b/src/index.js @@ -376,7 +376,10 @@ function ncc ( loader: eval('__dirname + "/loaders/shebang-loader.js"') }] } - ] + ], + parser: { + javascript: { importMeta: false }, + }, }, plugins }); diff --git a/test/unit/import-meta-esm/input.js b/test/unit/import-meta-esm/input.js new file mode 100644 index 00000000..d9536a69 --- /dev/null +++ b/test/unit/import-meta-esm/input.js @@ -0,0 +1 @@ +console.log(import.meta.url); diff --git a/test/unit/import-meta-esm/output-coverage.js b/test/unit/import-meta-esm/output-coverage.js new file mode 100644 index 00000000..f63d27cd --- /dev/null +++ b/test/unit/import-meta-esm/output-coverage.js @@ -0,0 +1,7 @@ +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/"; +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; +console.log(import.meta.url); \ No newline at end of file diff --git a/test/unit/import-meta-esm/output.js b/test/unit/import-meta-esm/output.js new file mode 100644 index 00000000..f63d27cd --- /dev/null +++ b/test/unit/import-meta-esm/output.js @@ -0,0 +1,7 @@ +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/"; +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; +console.log(import.meta.url); \ No newline at end of file diff --git a/test/unit/import-meta-esm/package.json b/test/unit/import-meta-esm/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/test/unit/import-meta-esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/watcher.test.js b/test/watcher.test.js index 4bce804b..0b4fbd58 100644 --- a/test/watcher.test.js +++ b/test/watcher.test.js @@ -13,6 +13,7 @@ class CustomWatchFileSystem { // paused allows the watchers to stay open for the next build this.paused = false; this.changeCallback = undefined; + this.changeCallbackUndelayed = undefined; this.watchStart = watchStart; this.watchEnd = watchEnd; @@ -21,6 +22,8 @@ class CustomWatchFileSystem { this.dirs = undefined; this.missing = undefined; this.timestamps = new Map(); + this.changes = new Set(); + this.removals = new Set(); // this will be populated for us by ncc this.inputFileSystem = undefined; @@ -38,11 +41,21 @@ class CustomWatchFileSystem { for (const file of removed) this.timestamps.set(file, null); - for (const file of changed) + for (const file of changed) { + this.changes.add(file); this.inputFileSystem.purge(file); - for (const file of removed) + } + for (const file of removed) { + this.removals.add(file); this.inputFileSystem.purge(file); + } + this.changeCallbackUndelayed( + null, + this.timestamps, + this.timestamps, + removed + ); this.changeCallback( null, this.timestamps, @@ -53,7 +66,7 @@ class CustomWatchFileSystem { } // This is called on every rebuild - watch (files, dirs, missing, startTime, options, changeCallback) { + watch (files, dirs, missing, startTime, options, changeCallback, changeCallbackUndelayed) { this.files = new Set(files); this.dirs = new Set(dirs); this.missing = new Set(missing); @@ -70,6 +83,7 @@ class CustomWatchFileSystem { this.paused = false; this.changeCallback = changeCallback; + this.changeCallbackUndelayed = changeCallbackUndelayed; // ...Start watching files, dirs, missing setImmediate(() => { @@ -83,12 +97,12 @@ class CustomWatchFileSystem { pause: () => { this.paused = true; }, - getFileTimestamps: () => { - return this.timestamps; - }, - getContextTimestamps: () => { - return this.timestamps; - } + getInfo: () => ({ + changes: this.changes, + removals: this.removals, + fileTimeInfoEntries: this.timestamps, + contextTimeInfoEntries: this.timestamps, + }), }; } } @@ -98,6 +112,8 @@ jest.setTimeout(30000); it('Should support custom watch API', async () => { let buildCnt = 0; const buildFile = path.resolve('./test/integration/twilio.js'); + const initialBuildFileContents = fs.readFileSync(buildFile).toString(); + await new Promise((resolve, reject) => { const watcher = new CustomWatchFileSystem(function watchStart (files, dirs, missing) { expect(files._set.size).toBeGreaterThan(100); @@ -136,5 +152,7 @@ it('Should support custom watch API', async () => { console.time('Watched Build'); }); }); + + fs.writeFileSync(buildFile, initialBuildFileContents); }); diff --git a/yarn.lock b/yarn.lock index d39934a4..9f258b61 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2313,10 +2313,10 @@ dependencies: "@types/ms" "*" -"@types/eslint-scope@^3.7.0": - version "3.7.0" - resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.0.tgz#4792816e31119ebd506902a482caec4951fabd86" - integrity sha512-O/ql2+rrCUe2W2rs7wMR+GqPRcgB6UiqN5RhrR5xruFlY7l9YLMn0ZkDzjoHLeiFkR8MCQZVudUuuvQ2BLC9Qw== +"@types/eslint-scope@^3.7.3": + version "3.7.3" + resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.3.tgz#125b88504b61e3c8bc6f870882003253005c3224" + integrity sha512-PB3ldyrcnAicT35TWPs5IcwKD8S333HMaa2VVv4+wdvebJkjWuW/xESoB8IwRcog8HYVYamb1g/R31Qv5Bx03g== dependencies: "@types/eslint" "*" "@types/estree" "*" @@ -2329,10 +2329,10 @@ "@types/estree" "*" "@types/json-schema" "*" -"@types/estree@*", "@types/estree@^0.0.50": - version "0.0.50" - resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83" - integrity sha512-C6N5s2ZFtuZRj54k2/zyRhNDjJwwcViAM3Nbm8zjBpbqAdZ00mr0CFxvSKeO8Y/e03WVFLpQMdHYVfUd6SB+Hw== +"@types/estree@*", "@types/estree@^0.0.51": + version "0.0.51" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.51.tgz#cfd70924a25a3fd32b218e5e420e6897e1ac4f40" + integrity sha512-CuPgU6f3eT/XgKKPqKd/gLZV1Xmvf1a2R5POBOGQa6uv82xpls89HU5zKeVoyR8XzHd1RGNOlQlvUe3CFkjWNQ== "@types/express-jwt@0.0.42": version "0.0.42" @@ -5797,7 +5797,7 @@ engine.io@~5.1.1: engine.io-parser "~4.0.0" ws "~7.4.2" -enhanced-resolve@^5.0.0, enhanced-resolve@^5.7.0, enhanced-resolve@^5.8.3: +enhanced-resolve@^5.0.0, enhanced-resolve@^5.7.0, enhanced-resolve@^5.9.2: version "5.9.3" resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.9.3.tgz#44a342c012cbc473254af5cc6ae20ebd0aae5d88" integrity sha512-Bq9VSor+kjvW3f9/MiiR4eE3XYgOl7/rS8lnSxbRbF3kS0B2r+Y9w5krBWxZgDxASVZbdYrn5wT4j/Wb0J9qow== @@ -15717,7 +15717,7 @@ watchify@^3.3.0: through2 "^2.0.0" xtend "^4.0.0" -watchpack@^2.2.0: +watchpack@^2.3.1: version "2.3.1" resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-2.3.1.tgz#4200d9447b401156eeca7767ee610f8809bc9d25" integrity sha512-x0t0JuydIo8qCNctdDrn1OzH/qDzk2+rdCOC3YzumZ42fiMqmQ7T3xQurykYMhYfHaPHTp4ZxAx2NfUo1K6QaA== @@ -15757,18 +15757,18 @@ webidl-conversions@^6.1.0: resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-6.1.0.tgz#9111b4d7ea80acd40f5270d666621afa78b69514" integrity sha512-qBIvFLGiBpLjfwmYAaHPXsn+ho5xZnGvyGvsarywGNc8VyQJUMHJ8OBKGGrPER0okBeMDaan4mNBlgBROxuI8w== -webpack-sources@^3.0.0, webpack-sources@^3.2.0: +webpack-sources@^3.0.0, webpack-sources@^3.2.3: version "3.2.3" resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde" integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w== -webpack@5.62.1: - version "5.62.1" - resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.62.1.tgz#06f09b56a7b1bb13ed5137ad4b118358a90c9505" - integrity sha512-jNLtnWChS2CMZ7vqWtztv0G6fYB5hz11Zsadp5tE7e4/66zVDj7/KUeQZOsOl8Hz5KrLJH1h2eIDl6AnlyE12Q== +webpack@5.70.0: + version "5.70.0" + resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.70.0.tgz#3461e6287a72b5e6e2f4872700bc8de0d7500e6d" + integrity sha512-ZMWWy8CeuTTjCxbeaQI21xSswseF2oNOwc70QSKNePvmxE7XW36i7vpBMYZFAUHPwQiEbNGCEYIOOlyRbdGmxw== dependencies: - "@types/eslint-scope" "^3.7.0" - "@types/estree" "^0.0.50" + "@types/eslint-scope" "^3.7.3" + "@types/estree" "^0.0.51" "@webassemblyjs/ast" "1.11.1" "@webassemblyjs/wasm-edit" "1.11.1" "@webassemblyjs/wasm-parser" "1.11.1" @@ -15776,12 +15776,12 @@ webpack@5.62.1: acorn-import-assertions "^1.7.6" browserslist "^4.14.5" chrome-trace-event "^1.0.2" - enhanced-resolve "^5.8.3" + enhanced-resolve "^5.9.2" es-module-lexer "^0.9.0" eslint-scope "5.1.1" events "^3.2.0" glob-to-regexp "^0.4.1" - graceful-fs "^4.2.4" + graceful-fs "^4.2.9" json-parse-better-errors "^1.0.2" loader-runner "^4.2.0" mime-types "^2.1.27" @@ -15789,8 +15789,8 @@ webpack@5.62.1: schema-utils "^3.1.0" tapable "^2.1.1" terser-webpack-plugin "^5.1.3" - watchpack "^2.2.0" - webpack-sources "^3.2.0" + watchpack "^2.3.1" + webpack-sources "^3.2.3" websocket-driver@>=0.5.1: version "0.7.4"