Skip to content

Commit

Permalink
fix: add a trailing slash automatically for sourcemapBaseUrl (#5022)
Browse files Browse the repository at this point in the history
fix: add a trailing slash automatically
  • Loading branch information
TrickyPi committed Jun 4, 2023
1 parent b5ac3e5 commit b221bde
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -14,7 +14,7 @@ import {
} from '../error';
import { resolve } from '../path';
import { sanitizeFileName as defaultSanitizeFileName } from '../sanitizeFileName';
import { isValidUrl } from '../url';
import { addTrailingSlashIfMissed, isValidUrl } from '../url';
import {
URL_OUTPUT_AMD_BASEPATH,
URL_OUTPUT_AMD_ID,
Expand Down Expand Up @@ -537,7 +537,7 @@ const getSourcemapBaseUrl = (
const { sourcemapBaseUrl } = config;
if (sourcemapBaseUrl) {
if (isValidUrl(sourcemapBaseUrl)) {
return sourcemapBaseUrl;
return addTrailingSlashIfMissed(sourcemapBaseUrl);
}
return error(
errorInvalidOption(
Expand Down
7 changes: 7 additions & 0 deletions src/utils/url.ts
Expand Up @@ -10,3 +10,10 @@ export function isValidUrl(url: string): boolean {
export function getRollupUrl(snippet: string) {
return `https://rollupjs.org/${snippet}`;
}

export function addTrailingSlashIfMissed(url: string) {
if (!url.endsWith('/')) {
return url + '/';
}
return url;
}
@@ -0,0 +1,17 @@
const assert = require('node:assert');

module.exports = defineTest({
description: 'add a trailing slash automatically if it is missing',
options: {
output: {
sourcemapBaseUrl: 'https://example.com/a'
}
},
test: (code, map, { format }) => {
const sourceMappingURL = code.split('\n').slice(-2)[0];
assert.strictEqual(
sourceMappingURL,
`//# sourceMappingURL=https://example.com/a/bundle.${format}.js.map`
);
}
});
@@ -0,0 +1 @@
assert.ok(true);
14 changes: 5 additions & 9 deletions test/sourcemaps/samples/sourcemap-base-url/_config.js
@@ -1,22 +1,18 @@
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');

module.exports = defineTest({
description: 'adds a sourcemap base url',
options: {
output: {
sourcemapBaseUrl: 'https://example.com'
sourcemapBaseUrl: 'https://example.com/a/'
}
},
test: (code, map, profile) => {
assert.equal(map.file, `bundle.${profile.format}.js`);
const bundlePath = path.join(__dirname, `_actual/bundle.${profile.format}.js`);
const bundledCode = fs.readFileSync(bundlePath, { encoding: 'utf8', flag: 'r' });
const sourceMappingURL = bundledCode.split('\n').slice(-2)[0];
test: (code, map, { format }) => {
assert.equal(map.file, `bundle.${format}.js`);
const sourceMappingURL = code.split('\n').slice(-2)[0];
assert.equal(
sourceMappingURL,
`//# sourceMappingURL=https://example.com/bundle.${profile.format}.js.map`
`//# sourceMappingURL=https://example.com/a/bundle.${format}.js.map`
);
}
});

0 comments on commit b221bde

Please sign in to comment.