From c1d25d6fd111e90b6e0760f127d589f20afa224e Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 26 Aug 2024 16:21:06 +0200 Subject: [PATCH 1/4] Call and await closeWatcher hooks on exit signals (#5619) --- cli/run/watch-cli.ts | 12 +++++----- .../rollup-plugin-esm-test/index.mjs | 4 ++-- .../watch/signal-close-watcher/_config.js | 14 ++++++++++++ .../signal-close-watcher/_expected/main.js | 3 +++ .../watch/signal-close-watcher/main.js | 1 + .../signal-close-watcher/rollup.config.mjs | 22 +++++++++++++++++++ 6 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 test/cli/samples/watch/signal-close-watcher/_config.js create mode 100644 test/cli/samples/watch/signal-close-watcher/_expected/main.js create mode 100644 test/cli/samples/watch/signal-close-watcher/main.js create mode 100644 test/cli/samples/watch/signal-close-watcher/rollup.config.mjs diff --git a/cli/run/watch-cli.ts b/cli/run/watch-cli.ts index d1f0fcc0f..b28ff8386 100644 --- a/cli/run/watch-cli.ts +++ b/cli/run/watch-cli.ts @@ -28,7 +28,7 @@ export async function watch(command: Record): Promise { const configFile = command.config ? await getConfigPath(command.config) : null; const runWatchHook = createWatchHooks(command); - onExit(close as any); + onExit(close); process.on('uncaughtException', closeWithError); if (!process.stdin.isTTY) { process.stdin.on('end', close); @@ -146,14 +146,16 @@ export async function watch(command: Record): Promise { }); } - async function close(code: number | null | undefined): Promise { + function close(code: number | null | undefined): true { process.removeListener('uncaughtException', closeWithError); // removing a non-existent listener is a no-op process.stdin.removeListener('end', close); - - if (watcher) await watcher.close(); if (configWatcher) configWatcher.close(); - if (code) process.exit(code); + Promise.resolve(watcher?.close()).finally(() => { + process.exit(typeof code === 'number' ? code : 0); + }); + // Tell signal-exit that we are handling this gracefully + return true; } // return a promise that never resolves to keep the process running diff --git a/test/cli/node_modules/rollup-plugin-esm-test/index.mjs b/test/cli/node_modules/rollup-plugin-esm-test/index.mjs index 14daca500..f923e4d82 100644 --- a/test/cli/node_modules/rollup-plugin-esm-test/index.mjs +++ b/test/cli/node_modules/rollup-plugin-esm-test/index.mjs @@ -1,8 +1,8 @@ export function esmTest() { return { transform(code, id) { - if (id.includes("\0")) return null; - const name = JSON.stringify(id.replace(/^.*\/advanced-esm\//, "esm-test: ")); + if (id.includes('\0')) return null; + const name = JSON.stringify(id.replace(/^.*\/advanced-esm\//, 'esm-test: ')); return `console.log(${name});\n${code}`; } }; diff --git a/test/cli/samples/watch/signal-close-watcher/_config.js b/test/cli/samples/watch/signal-close-watcher/_config.js new file mode 100644 index 000000000..4bd7998bc --- /dev/null +++ b/test/cli/samples/watch/signal-close-watcher/_config.js @@ -0,0 +1,14 @@ +const { assertIncludes } = require('../../../../utils.js'); + +module.exports = defineTest({ + description: 'calls closeWatcher plugin hooks when rollup is terminated due to a signal', + command: 'rollup -cw', + abortOnStderr(data) { + if (data.includes('created _actual')) { + return true; + } + }, + stderr(stderr) { + assertIncludes(stderr, 'close first\nclose second'); + } +}); diff --git a/test/cli/samples/watch/signal-close-watcher/_expected/main.js b/test/cli/samples/watch/signal-close-watcher/_expected/main.js new file mode 100644 index 000000000..c3e302331 --- /dev/null +++ b/test/cli/samples/watch/signal-close-watcher/_expected/main.js @@ -0,0 +1,3 @@ +var main = 42; + +export { main as default }; diff --git a/test/cli/samples/watch/signal-close-watcher/main.js b/test/cli/samples/watch/signal-close-watcher/main.js new file mode 100644 index 000000000..7a4e8a723 --- /dev/null +++ b/test/cli/samples/watch/signal-close-watcher/main.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/cli/samples/watch/signal-close-watcher/rollup.config.mjs b/test/cli/samples/watch/signal-close-watcher/rollup.config.mjs new file mode 100644 index 000000000..2637108a4 --- /dev/null +++ b/test/cli/samples/watch/signal-close-watcher/rollup.config.mjs @@ -0,0 +1,22 @@ +export default { + input: 'main.js', + output: { + dir: '_actual', + format: 'es' + }, + plugins: [ + { + name: 'first', + closeWatcher() { + console.error('close first'); + } + }, + { + name: 'second', + async closeWatcher() { + await new Promise(resolve => setTimeout(resolve)); + console.error('close second'); + } + } + ] +}; From eb07813c19984d702af2607734802d73eaf58cd8 Mon Sep 17 00:00:00 2001 From: XiaoPi <530257315@qq.com> Date: Mon, 26 Aug 2024 23:19:40 +0800 Subject: [PATCH 2/4] fix: add space for DOCUMENT_CURRENT_SCRIPT template (#5624) * add space for DOCUMENT_CURRENT_SCRIPT template * update micromatch version --- package-lock.json | 7 +++--- src/utils/interopHelpers.ts | 2 +- .../import-meta-url-with-compact/_config.js | 22 +++++++++++++++++++ .../import-meta-url-with-compact/main.js | 6 +++++ 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 test/function/samples/import-meta-url-with-compact/_config.js create mode 100644 test/function/samples/import-meta-url-with-compact/main.js diff --git a/package-lock.json b/package-lock.json index 4e7c0532a..db65010bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9674,11 +9674,10 @@ "license": "MIT" }, "node_modules/micromatch": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz", - "integrity": "sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, - "license": "MIT", "dependencies": { "braces": "^3.0.3", "picomatch": "^2.3.1" diff --git a/src/utils/interopHelpers.ts b/src/utils/interopHelpers.ts index 2c4f87cff..f4bc54184 100644 --- a/src/utils/interopHelpers.ts +++ b/src/utils/interopHelpers.ts @@ -72,7 +72,7 @@ const HELPER_GENERATORS: Record< ) => string > = { [DOCUMENT_CURRENT_SCRIPT](_t, { _, n }) { - return `var${_}${DOCUMENT_CURRENT_SCRIPT}${_}=${_}typeof${_}document${_}!==${_}'undefined'${_}?${_}document.currentScript${_}:${_}null;${n}`; + return `var ${DOCUMENT_CURRENT_SCRIPT}${_}=${_}typeof document${_}!==${_}'undefined'${_}?${_}document.currentScript${_}:${_}null;${n}`; }, [INTEROP_DEFAULT_COMPAT_VARIABLE](_t, snippets, liveBindings) { const { _, getDirectReturnFunction, n } = snippets; diff --git a/test/function/samples/import-meta-url-with-compact/_config.js b/test/function/samples/import-meta-url-with-compact/_config.js new file mode 100644 index 000000000..63f5864ea --- /dev/null +++ b/test/function/samples/import-meta-url-with-compact/_config.js @@ -0,0 +1,22 @@ +const assert = require('node:assert'); + +const source = 'example.com/main.js'; + +module.exports = defineTest({ + description: 'Get the right URL with compact output', + context: { + document: { + currentScript: { + src: source + } + } + }, + options: { + output: { + compact: true + } + }, + exports(exports) { + assert.strictEqual(exports(), source); + } +}); diff --git a/test/function/samples/import-meta-url-with-compact/main.js b/test/function/samples/import-meta-url-with-compact/main.js new file mode 100644 index 000000000..98e4e438c --- /dev/null +++ b/test/function/samples/import-meta-url-with-compact/main.js @@ -0,0 +1,6 @@ +export default function getUrl() { + document.currentScript = { src: '' }; + const url = import.meta.url; + assert.ok(import.meta.abc || true); + return url; +} From fadebcd6fd717bb8290ab1efdfe2269eb296cd7f Mon Sep 17 00:00:00 2001 From: XiaoPi <530257315@qq.com> Date: Mon, 26 Aug 2024 23:27:50 +0800 Subject: [PATCH 3/4] fix: get the right chunk name for preserve modules in Windows (#5625) * add test * remove Windows drive prefix --------- Co-authored-by: Lukas Taegert-Atkinson --- src/Chunk.ts | 12 ++++++++--- test/cli/samples/get-chunk-name/_config.js | 11 ++++++++++ test/cli/samples/get-chunk-name/main.js | 2 ++ .../samples/get-chunk-name/rollup.config.mjs | 21 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 test/cli/samples/get-chunk-name/_config.js create mode 100644 test/cli/samples/get-chunk-name/main.js create mode 100644 test/cli/samples/get-chunk-name/rollup.config.mjs diff --git a/src/Chunk.ts b/src/Chunk.ts index 0e7e1a663..14b985fb1 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -1069,9 +1069,15 @@ export default class Chunk { ? sanitizedId.slice(0, -extensionName.length) : sanitizedId; if (isAbsolute(idWithoutExtension)) { - return preserveModulesRoot && resolve(idWithoutExtension).startsWith(preserveModulesRoot) - ? idWithoutExtension.slice(preserveModulesRoot.length).replace(/^[/\\]/, '') - : relative(this.inputBase, idWithoutExtension); + if (preserveModulesRoot && resolve(idWithoutExtension).startsWith(preserveModulesRoot)) { + return idWithoutExtension.slice(preserveModulesRoot.length).replace(/^[/\\]/, ''); + } else { + // handle edge case in Windows + if (this.inputBase === '/' && !idWithoutExtension.startsWith('/')) { + return relative(this.inputBase, idWithoutExtension.replace(/^[a-zA-Z]:[/\\]/, '/')); + } + return relative(this.inputBase, idWithoutExtension); + } } else { return ( this.outputOptions.virtualDirname.replace(/\/$/, '') + '/' + basename(idWithoutExtension) diff --git a/test/cli/samples/get-chunk-name/_config.js b/test/cli/samples/get-chunk-name/_config.js new file mode 100644 index 000000000..4a389c8b4 --- /dev/null +++ b/test/cli/samples/get-chunk-name/_config.js @@ -0,0 +1,11 @@ +const assert = require('node:assert'); +module.exports = defineTest({ + description: 'get right chunk name for preserve modules', + command: 'rollup -c', + result(code) { + assert.ok( + code.includes(`//→ ${__dirname.replace(/^(\/|[a-zA-Z]:\\)/, '').replace(/\\/g, '/')}/main.js`) + ); + assert.ok(code.includes(`//→ sub.js`)); + } +}); diff --git a/test/cli/samples/get-chunk-name/main.js b/test/cli/samples/get-chunk-name/main.js new file mode 100644 index 000000000..63cd1928d --- /dev/null +++ b/test/cli/samples/get-chunk-name/main.js @@ -0,0 +1,2 @@ +import sub from '/sub.js'; +assert.ok(sub); diff --git a/test/cli/samples/get-chunk-name/rollup.config.mjs b/test/cli/samples/get-chunk-name/rollup.config.mjs new file mode 100644 index 000000000..511c0dd2d --- /dev/null +++ b/test/cli/samples/get-chunk-name/rollup.config.mjs @@ -0,0 +1,21 @@ +export default { + input: 'main.js', + output: { + format: 'es', + preserveModules: true + }, + plugins: [ + { + resolveId(id) { + if (id.endsWith('sub.js')) { + return id; + } + }, + load(id) { + if (id.endsWith('sub.js')) { + return 'export default "sub.js url"'; + } + } + } + ] +}; From c33c6ceb7da712c3d14b67b81febf9303fbbd96c Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 26 Aug 2024 17:31:09 +0200 Subject: [PATCH 4/4] 4.21.1 --- CHANGELOG.md | 19 +++++++++++++++++++ browser/package.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f53fe57f..61e9e21e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,24 @@ # rollup changelog +## 4.21.1 + +_2024-08-26_ + +### Bug Fixes + +- Ensure `closeWatcher` hook is called when watch mode is aborted via Ctrl+C (#5618) +- Do not produce invalid code for `import.meta.url` in compact mode (#5624) +- Do not throw when generating chunk names when preserving modules in Windows (#5625) + +### Pull Requests + +- [#5591](https://github.com/rollup/rollup/pull/5591): chore(deps): update dependency @types/eslint to v9 (@renovate[bot], @lukastaegert) +- [#5618](https://github.com/rollup/rollup/pull/5618): preload the WASM file even though the version is undefined. (@TrickyPi) +- [#5619](https://github.com/rollup/rollup/pull/5619): Call and await closeWatcher hooks on exit signals (@lukastaegert) +- [#5622](https://github.com/rollup/rollup/pull/5622): chore(deps): lock file maintenance minor/patch updates (@renovate[bot], @lukastaegert) +- [#5624](https://github.com/rollup/rollup/pull/5624): fix: add space for DOCUMENT_CURRENT_SCRIPT template (@TrickyPi) +- [#5625](https://github.com/rollup/rollup/pull/5625): fix: get the right chunk name for preserve modules in Windows (@TrickyPi, @lukastaegert) + ## 4.21.0 _2024-08-18_ diff --git a/browser/package.json b/browser/package.json index 11261ccaf..6068df66b 100644 --- a/browser/package.json +++ b/browser/package.json @@ -1,6 +1,6 @@ { "name": "@rollup/browser", - "version": "4.21.0", + "version": "4.21.1", "description": "Next-generation ES module bundler browser build", "main": "dist/rollup.browser.js", "module": "dist/es/rollup.browser.js", diff --git a/package-lock.json b/package-lock.json index db65010bd..fc1cfb231 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rollup", - "version": "4.21.0", + "version": "4.21.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "rollup", - "version": "4.21.0", + "version": "4.21.1", "license": "MIT", "dependencies": { "@types/estree": "1.0.5" diff --git a/package.json b/package.json index 8273a2e86..dc1d8b3b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup", - "version": "4.21.0", + "version": "4.21.1", "description": "Next-generation ES module bundler", "main": "dist/rollup.js", "module": "dist/es/rollup.js",