Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rollup-plugin-import-meta-assets] Fix resolving and respect external rollup option #2819

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}
}
}
Expand Down
45 changes: 42 additions & 3 deletions packages/rollup-plugin-import-meta-assets/test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.match(
/Unable to resolve "[/\\]absolute-path.svg" from ".*[/\\]bad-url-entrypoint.js"/,
);
});

it('bad URL example with warnOnError: true', async () => {
Expand All @@ -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"/,
);
});

Expand Down Expand Up @@ -294,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'),
]);
});
});
Original file line number Diff line number Diff line change
@@ -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,
});