From 242c03201dd75e345445fcd6596485d2b77a2408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Mon, 7 Oct 2024 15:55:12 +0200 Subject: [PATCH 1/3] fix: fix resolving and respect `external` rollup option --- .../src/rollup-plugin-import-meta-assets.js | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/rollup-plugin-import-meta-assets/src/rollup-plugin-import-meta-assets.js b/packages/rollup-plugin-import-meta-assets/src/rollup-plugin-import-meta-assets.js index 245ec98e4..8d4ea49c3 100644 --- a/packages/rollup-plugin-import-meta-assets/src/rollup-plugin-import-meta-assets.js +++ b/packages/rollup-plugin-import-meta-assets/src/rollup-plugin-import-meta-assets.js @@ -161,21 +161,35 @@ ${` default: return new Promise(function(resolve, reject) { if (importMetaUrlType === 'static') { const absoluteScriptDir = path.dirname(id); const relativeAssetPath = getRelativeAssetPath(node); - const absoluteAssetPath = path.resolve(absoluteScriptDir, relativeAssetPath); - const assetName = path.basename(absoluteAssetPath); try { - const assetContents = await fs.promises.readFile(absoluteAssetPath); + const resolved = await this.resolve(relativeAssetPath, id); + if (resolved == null) { + // Do not process directories, just skip + const absoluteAssetPath = path.resolve(absoluteScriptDir, relativeAssetPath); + try { + if (fs.lstatSync(absoluteAssetPath).isDirectory()) { + return; + } + } catch (error) { + // ignore, the file probably doesn't exists + } + this.error(`Unable to resolve "${relativeAssetPath}" from "${id}"`); + return; + } + if (resolved.external) { + return; + } + + const assetContents = await fs.promises.readFile(resolved.id); const transformedAssetContents = - transform != null - ? await transform(assetContents, absoluteAssetPath) - : assetContents; + transform != null ? await transform(assetContents, resolved.id) : assetContents; if (transformedAssetContents === null) { return; } const ref = this.emitFile({ type: 'asset', - name: assetName, + name: path.basename(resolved.id), source: transformedAssetContents, }); magicString.overwrite( @@ -185,13 +199,10 @@ ${` default: return new Promise(function(resolve, reject) { ); modifiedCode = true; } catch (error) { - // Do not process directories, just skip - if (error.code !== 'EISDIR') { - if (warnOnError) { - this.warn(error, node.arguments[0].start); - } else { - this.error(error, node.arguments[0].start); - } + if (warnOnError) { + this.warn(error, node.arguments[0].start); + } else { + this.error(error, node.arguments[0].start); } } } From fe8fbb0fd9dc492618866dfe86edb0c7c41e25fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Thu, 10 Oct 2024 17:28:31 +0200 Subject: [PATCH 2/3] test: update tests with new error messages --- .../test/integration.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/rollup-plugin-import-meta-assets/test/integration.test.js b/packages/rollup-plugin-import-meta-assets/test/integration.test.js index 07d6c31e5..d5eedc4c8 100644 --- a/packages/rollup-plugin-import-meta-assets/test/integration.test.js +++ b/packages/rollup-plugin-import-meta-assets/test/integration.test.js @@ -237,7 +237,9 @@ describe('rollup-plugin-import-meta-assets', () => { error = e; } - expect(error.message).to.match(/no such file or directory/); + expect(error.message).to.eq( + `Unable to resolve "/absolute-path.svg" from "/home/loic/git/web/packages/rollup-plugin-import-meta-assets/test/fixtures/bad-url-entrypoint.js"`, + ); }); it('bad URL example with warnOnError: true', async () => { @@ -251,10 +253,10 @@ describe('rollup-plugin-import-meta-assets', () => { expect(consoleStub.callCount).to.equal(2); expect(consoleStub.getCall(0).args[0]).to.match( - /ENOENT: no such file or directory, open '.*[/\\]absolute-path\.svg'/, + /\(rollup-plugin-import-meta-assets plugin\) test[/\\]fixtures[/\\]bad-url-entrypoint\.js \(1:26\) Unable to resolve "[/\\]absolute-path\.svg" from ".*bad-url-entrypoint\.js"/, ); expect(consoleStub.getCall(1).args[0]).to.match( - /ENOENT: no such file or directory, open '.*[/\\]missing-relative-path\.svg'/, + /\(rollup-plugin-import-meta-assets plugin\) test[/\\]fixtures[/\\]bad-url-entrypoint\.js \(2:26\) Unable to resolve "..[/\\]..[/\\]missing-relative-path.svg" from ".*bad-url-entrypoint\.js"/, ); }); From f7843763bef3983c3f7644f366f639d330167958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Thu, 10 Oct 2024 17:28:57 +0200 Subject: [PATCH 3/3] test: add rollup resolve test --- .../test/integration.test.js | 41 ++++++++++++++++++- .../test/snapshots/simple-bundle-switched.js | 13 ++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 packages/rollup-plugin-import-meta-assets/test/snapshots/simple-bundle-switched.js diff --git a/packages/rollup-plugin-import-meta-assets/test/integration.test.js b/packages/rollup-plugin-import-meta-assets/test/integration.test.js index d5eedc4c8..a6bd8f549 100644 --- a/packages/rollup-plugin-import-meta-assets/test/integration.test.js +++ b/packages/rollup-plugin-import-meta-assets/test/integration.test.js @@ -237,8 +237,8 @@ describe('rollup-plugin-import-meta-assets', () => { error = e; } - expect(error.message).to.eq( - `Unable to resolve "/absolute-path.svg" from "/home/loic/git/web/packages/rollup-plugin-import-meta-assets/test/fixtures/bad-url-entrypoint.js"`, + expect(error.message).to.match( + /Unable to resolve "[/\\]absolute-path.svg" from ".*[/\\]bad-url-entrypoint.js"/, ); }); @@ -296,4 +296,41 @@ describe('rollup-plugin-import-meta-assets', () => { expectAsset(output, 'snapshots/four.svg', 'four.svg', 'assets/four-lJVunLww.svg'), ]); }); + + it('respects the rollup resolution', async () => { + const config = { + input: { 'simple-bundle-switched': require.resolve('./fixtures/simple-entrypoint.js') }, + plugins: [ + importMetaAssets(), + { + resolveId(source, importer) { + if (source == './one.svg') { + return path.resolve(path.dirname(importer), 'two.svg'); + } + if (source == './two.svg') { + return path.resolve(path.dirname(importer), 'one.svg'); + } + if (source === './three.svg') { + return { + id: source, + external: true, + }; + } + return undefined; + }, + }, + ], + }; + + const bundle = await rollup.rollup(config); + const { output } = await bundle.generate(outputConfig); + + expect(output.length).to.equal(5); + expectChunk(output, 'snapshots/simple-bundle-switched.js', 'simple-bundle-switched.js', [ + expectAsset(output, 'snapshots/two.svg', 'two.svg', 'assets/two--yckvrYd.svg'), + expectAsset(output, 'snapshots/one.svg', 'one.svg', 'assets/one-ZInu4dBJ.svg'), + expectAsset(output, 'snapshots/four.svg', 'four.svg', 'assets/four-lJVunLww.svg'), + expectAsset(output, 'snapshots/five', 'five', 'assets/five-Z74_0e9C'), + ]); + }); }); diff --git a/packages/rollup-plugin-import-meta-assets/test/snapshots/simple-bundle-switched.js b/packages/rollup-plugin-import-meta-assets/test/snapshots/simple-bundle-switched.js new file mode 100644 index 000000000..dc2a623e9 --- /dev/null +++ b/packages/rollup-plugin-import-meta-assets/test/snapshots/simple-bundle-switched.js @@ -0,0 +1,13 @@ +const justUrlObject = new URL(new URL('assets/two--yckvrYd.svg', import.meta.url).href, import.meta.url); +const href = new URL(new URL('assets/one-ZInu4dBJ.svg', import.meta.url).href, import.meta.url).href; +const pathname = new URL('./three.svg', import.meta.url).pathname; +const searchParams = new URL(new URL('assets/four-lJVunLww.svg', import.meta.url).href, import.meta.url).searchParams; +const noExtension = new URL(new URL('assets/five-Z74_0e9C', import.meta.url).href, import.meta.url); + +console.log({ + justUrlObject, + href, + pathname, + searchParams, + noExtension, +});