Skip to content

Commit

Permalink
Use correct import.meta.url slashes on Windows (#4693)
Browse files Browse the repository at this point in the history
* Test for wrong import-meta slashes

* Use forward slashes in preserveModules chunk ids

* Ensure ids in import.meta are always properly escaped

* Fix tests on Node 18

* Allow Github install without git
  • Loading branch information
lukastaegert committed Oct 31, 2022
1 parent f518bec commit bac9826
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 7 deletions.
11 changes: 8 additions & 3 deletions build-plugins/get-banner.ts
Expand Up @@ -23,9 +23,14 @@ function generateBanner(commitHash: string, version: string): string {

let getBannerPromise: Promise<string> | null = null;

export default async function getBanner(): Promise<string> {
export default function getBanner(): Promise<string> {
return (getBannerPromise ||= Promise.all([
execPromise('git rev-parse HEAD'),
execPromise('git rev-parse HEAD')
.then(({ stdout }) => stdout.trim())
.catch(error => {
console.error('Could not determine commit hash:', error);
return 'unknown';
}),
fs.readFile(new URL('../package.json', import.meta.url), 'utf8')
]).then(([{ stdout }, package_]) => generateBanner(stdout.trim(), JSON.parse(package_).version)));
]).then(([commit, package_]) => generateBanner(commit, JSON.parse(package_).version)));
}
4 changes: 2 additions & 2 deletions src/Chunk.ts
Expand Up @@ -54,7 +54,7 @@ import {
} from './utils/interopHelpers';
import type { OutputBundleWithPlaceholders } from './utils/outputBundle';
import { FILE_PLACEHOLDER } from './utils/outputBundle';
import { basename, extname, isAbsolute, resolve } from './utils/path';
import { basename, extname, isAbsolute, normalize, resolve } from './utils/path';
import { getAliasName, getImportPath } from './utils/relativeId';
import type { RenderOptions } from './utils/renderHelpers';
import { makeUnique, renderNamePattern } from './utils/renderNamePattern';
Expand Down Expand Up @@ -974,7 +974,7 @@ export default class Chunk {
const predefinedChunkName = getPredefinedChunkNameFromModule(module);
if (predefinedChunkName) return predefinedChunkName;
const { preserveModulesRoot, sanitizeFileName } = this.outputOptions;
const sanitizedId = sanitizeFileName(module.id.split(QUERY_HASH_REGEX, 1)[0]);
const sanitizedId = sanitizeFileName(normalize(module.id.split(QUERY_HASH_REGEX, 1)[0]));
const extensionName = extname(sanitizedId);
const idWithoutExtension = NON_ASSET_EXTENSIONS.has(extensionName)
? sanitizedId.slice(0, -extensionName.length)
Expand Down
7 changes: 5 additions & 2 deletions src/ast/nodes/MetaProperty.ts
@@ -1,6 +1,7 @@
import type MagicString from 'magic-string';
import type { InternalModuleFormat } from '../../rollup/types';
import type { PluginDriver } from '../../utils/PluginDriver';
import { escapeId } from '../../utils/escapeId';
import type { GenerateCodeSnippets } from '../../utils/generateCodeSnippets';
import { dirname, normalize, relative } from '../../utils/path';
import type { RenderOptions } from '../../utils/renderHelpers';
Expand Down Expand Up @@ -145,7 +146,7 @@ const getResolveUrl = (path: string, URL = 'URL') => `new ${URL}(${path}).href`;

const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
getResolveUrl(
`'${relativePath}', ${
`'${escapeId(relativePath)}', ${
umd ? `typeof document === 'undefined' ? location.href : ` : ''
}document.currentScript && document.currentScript.src || document.baseURI`
);
Expand All @@ -164,7 +165,9 @@ const getGenericImportMetaMechanism =
const getUrlFromDocument = (chunkId: string, umd = false) =>
`${
umd ? `typeof document === 'undefined' ? location.href : ` : ''
}(document.currentScript && document.currentScript.src || new URL('${chunkId}', document.baseURI).href)`;
}(document.currentScript && document.currentScript.src || new URL('${escapeId(
chunkId
)}', document.baseURI).href)`;

const relativeUrlMechanisms: Record<InternalModuleFormat, (relativePath: string) => string> = {
amd: relativePath => {
Expand Down
@@ -0,0 +1 @@
export const url = import.meta.url;
16 changes: 16 additions & 0 deletions test/chunking-form/samples/import-meta-numeric-paths/_config.js
@@ -0,0 +1,16 @@
const assert = require('node:assert');

module.exports = {
description: 'supports nested numeric paths without issues on Windows',
options: {
output: { preserveModules: true },
plugins: [
{
name: 'test',
generateBundle(options, bundle) {
assert.deepStrictEqual(Object.keys(bundle), ['main.js', '0/1/nested.js']);
}
}
]
}
};
@@ -0,0 +1,7 @@
define(['module', 'exports'], (function (module, exports) { 'use strict';

const url = new URL(module.uri, document.baseURI).href;

exports.url = url;

}));
@@ -0,0 +1,7 @@
define(['exports', './0/1/nested'], (function (exports, nested) { 'use strict';



exports.url = nested.url;

}));
@@ -0,0 +1,5 @@
'use strict';

const url = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('0/1/nested.js', document.baseURI).href));

exports.url = url;
@@ -0,0 +1,7 @@
'use strict';

var nested = require('./0/1/nested.js');



exports.url = nested.url;
@@ -0,0 +1,3 @@
const url = import.meta.url;

export { url };
@@ -0,0 +1 @@
export { url } from './0/1/nested.js';
@@ -0,0 +1,10 @@
System.register([], (function (exports, module) {
'use strict';
return {
execute: (function () {

const url = exports('url', module.meta.url);

})
};
}));
@@ -0,0 +1,13 @@
System.register(['./0/1/nested.js'], (function (exports) {
'use strict';
return {
setters: [function (module) {
exports('url', module.url);
}],
execute: (function () {



})
};
}));
@@ -0,0 +1 @@
export { url } from './0/1/nested.js';
1 change: 1 addition & 0 deletions test/cli/samples/watch/clearScreen/wrapper.js
Expand Up @@ -2,4 +2,5 @@

process.stdout.isTTY = true;
process.stderr.isTTY = true;
process.stderr.hasColors = () => true;
require('../../../../../dist/bin/rollup');
1 change: 1 addition & 0 deletions test/cli/samples/watch/no-clearScreen-command/wrapper.js
Expand Up @@ -2,4 +2,5 @@

process.stdout.isTTY = true;
process.stderr.isTTY = true;
process.stderr.hasColors = () => true;
require('../../../../../dist/bin/rollup');
1 change: 1 addition & 0 deletions test/cli/samples/watch/no-clearScreen/wrapper.js
Expand Up @@ -2,4 +2,5 @@

process.stdout.isTTY = true;
process.stderr.isTTY = true;
process.stderr.hasColors = () => true;
require('../../../../../dist/bin/rollup');
1 change: 1 addition & 0 deletions test/cli/samples/watch/no-watch-by-default/wrapper.js
Expand Up @@ -2,4 +2,5 @@

process.stdout.isTTY = true;
process.stderr.isTTY = true;
process.stderr.hasColors = () => true;
require('../../../../../dist/bin/rollup');
1 change: 1 addition & 0 deletions test/cli/samples/watch/watch-event-hooks-error/wrapper.js
Expand Up @@ -2,4 +2,5 @@

process.stdout.isTTY = true;
process.stderr.isTTY = true;
process.stderr.hasColors = () => true;
require('../../../../../dist/bin/rollup');
1 change: 1 addition & 0 deletions test/cli/samples/watch/watch-event-hooks/wrapper.js
Expand Up @@ -2,4 +2,5 @@

process.stdout.isTTY = true;
process.stderr.isTTY = true;
process.stderr.hasColors = () => true;
require('../../../../../dist/bin/rollup');

0 comments on commit bac9826

Please sign in to comment.