Skip to content

Commit

Permalink
fix: escape ids in import.meta.ROLLUP_FILE_URL_referenceId correctly (
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 5, 2024
1 parent 240be0b commit ce6cb93
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/ast/nodes/MetaProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const getGenericImportMetaMechanism =
const getFileUrlFromFullPath = (path: string) => `require('u' + 'rl').pathToFileURL(${path}).href`;

const getFileUrlFromRelativePath = (path: string) =>
getFileUrlFromFullPath(`__dirname + '/${path}'`);
getFileUrlFromFullPath(`__dirname + '/${escapeId(path)}'`);

const getUrlFromDocument = (chunkId: string, umd = false) =>
`${
Expand All @@ -187,15 +187,15 @@ const getUrlFromDocument = (chunkId: string, umd = false) =>
const relativeUrlMechanisms: Record<InternalModuleFormat, (relativePath: string) => string> = {
amd: relativePath => {
if (relativePath[0] !== '.') relativePath = './' + relativePath;
return getResolveUrl(`require.toUrl('${relativePath}'), document.baseURI`);
return getResolveUrl(`require.toUrl('${escapeId(relativePath)}'), document.baseURI`);
},
cjs: relativePath =>
`(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(
relativePath
)} : ${getRelativeUrlFromDocument(relativePath)})`,
es: relativePath => getResolveUrl(`'${relativePath}', import.meta.url`),
es: relativePath => getResolveUrl(`'${escapeId(relativePath)}', import.meta.url`),
iife: relativePath => getRelativeUrlFromDocument(relativePath),
system: relativePath => getResolveUrl(`'${relativePath}', module.meta.url`),
system: relativePath => getResolveUrl(`'${escapeId(relativePath)}', module.meta.url`),
umd: relativePath =>
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
relativePath
Expand Down
5 changes: 3 additions & 2 deletions test/form/samples/emit-asset-file/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = defineTest({
},
generateBundle(options, outputBundle) {
const keys = Object.keys(outputBundle);
assert.strictEqual(keys.length, 2);
assert.strictEqual(keys.length, 3);
assert.strictEqual(keys[0], 'assets/logo-zDlmrXar.svg');
const asset = outputBundle[keys[0]];
assert.strictEqual(asset.fileName, 'assets/logo-zDlmrXar.svg');
Expand All @@ -37,7 +37,8 @@ module.exports = defineTest({
source.equals(readFileSync(path.resolve(__dirname, 'logo.svg'))),
'asset has correct source'
);
assert.ok(keys[1].endsWith('.js'), `${keys[1]} ends with ".js"`);
assert.ok(keys[1].endsWith('.svg'), `${keys[1]} ends with ".svg"`);
assert.ok(keys[2].endsWith('.js'), `${keys[2]} ends with ".js"`);
}
}
]
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/emit-asset-file/_expected/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ define(['require'], (function (require) { 'use strict';

var logo = new URL(require.toUrl('./assets/logo-zDlmrXar.svg'), document.baseURI).href;

var logoReverse = new URL(require.toUrl('./assets/logo_reverse\'-DbGK2oiS.svg'), document.baseURI).href;

function showImage(url) {
console.log(url);
if (typeof document !== 'undefined') {
Expand All @@ -12,5 +14,6 @@ define(['require'], (function (require) { 'use strict';
}

showImage(logo);
showImage(logoReverse);

}));
3 changes: 3 additions & 0 deletions test/form/samples/emit-asset-file/_expected/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var logo = (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__dirname + '/assets/logo-zDlmrXar.svg').href : new URL('assets/logo-zDlmrXar.svg', document.currentScript && document.currentScript.src || document.baseURI).href);

var logoReverse = (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__dirname + '/assets/logo_reverse\'-DbGK2oiS.svg').href : new URL('assets/logo_reverse\'-DbGK2oiS.svg', document.currentScript && document.currentScript.src || document.baseURI).href);

function showImage(url) {
console.log(url);
if (typeof document !== 'undefined') {
Expand All @@ -12,3 +14,4 @@ function showImage(url) {
}

showImage(logo);
showImage(logoReverse);
3 changes: 3 additions & 0 deletions test/form/samples/emit-asset-file/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var logo = new URL('assets/logo-zDlmrXar.svg', import.meta.url).href;

var logoReverse = new URL('assets/logo_reverse\'-DbGK2oiS.svg', import.meta.url).href;

function showImage(url) {
console.log(url);
if (typeof document !== 'undefined') {
Expand All @@ -10,3 +12,4 @@ function showImage(url) {
}

showImage(logo);
showImage(logoReverse);
3 changes: 3 additions & 0 deletions test/form/samples/emit-asset-file/_expected/iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

var logo = new URL('assets/logo-zDlmrXar.svg', document.currentScript && document.currentScript.src || document.baseURI).href;

var logoReverse = new URL('assets/logo_reverse\'-DbGK2oiS.svg', document.currentScript && document.currentScript.src || document.baseURI).href;

function showImage(url) {
console.log(url);
if (typeof document !== 'undefined') {
Expand All @@ -13,5 +15,6 @@
}

showImage(logo);
showImage(logoReverse);

})();
3 changes: 3 additions & 0 deletions test/form/samples/emit-asset-file/_expected/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ System.register([], (function (exports, module) {

var logo = new URL('assets/logo-zDlmrXar.svg', module.meta.url).href;

var logoReverse = new URL('assets/logo_reverse\'-DbGK2oiS.svg', module.meta.url).href;

function showImage(url) {
console.log(url);
if (typeof document !== 'undefined') {
Expand All @@ -15,6 +17,7 @@ System.register([], (function (exports, module) {
}

showImage(logo);
showImage(logoReverse);

})
};
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/emit-asset-file/_expected/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

var logo = (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__dirname + '/assets/logo-zDlmrXar.svg').href : new URL('assets/logo-zDlmrXar.svg', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

var logoReverse = (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__dirname + '/assets/logo_reverse\'-DbGK2oiS.svg').href : new URL('assets/logo_reverse\'-DbGK2oiS.svg', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

function showImage(url) {
console.log(url);
if (typeof document !== 'undefined') {
Expand All @@ -15,5 +17,6 @@
}

showImage(logo);
showImage(logoReverse);

}));
60 changes: 60 additions & 0 deletions test/form/samples/emit-asset-file/logo_reverse'.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/form/samples/emit-asset-file/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logo from './logo.svg';
import logoReverse from "./logo_reverse'.svg"

function showImage(url) {
console.log(url);
Expand All @@ -10,3 +11,4 @@ function showImage(url) {
}

showImage(logo);
showImage(logoReverse);

0 comments on commit ce6cb93

Please sign in to comment.