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

Add sourcemapBaseUrl option #4527

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/help.md
Expand Up @@ -54,6 +54,7 @@ Basic options:
--no-sanitizeFileName Do not replace invalid characters in file names
--shimMissingExports Create shim variables for missing exports
--silent Don't print warnings
--sourcemapBaseUrl Emit absolute sourcemap URLs with base URL prefix, e.g. https://example.com/
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--stdin=ext Specify file extension used for stdin input
Expand Down
2 changes: 2 additions & 0 deletions docs/01-command-line-reference.md
Expand Up @@ -90,6 +90,7 @@ export default {
preserveModules,
preserveModulesRoot,
sourcemap,
sourcemapBaseUrl,
sourcemapExcludeSources,
sourcemapFile,
sourcemapPathTransform,
Expand Down Expand Up @@ -360,6 +361,7 @@ Many options have command line equivalents. In those cases, any arguments passed
--no-sanitizeFileName Do not replace invalid characters in file names
--shimMissingExports Create shim variables for missing exports
--silent Don't print warnings
--sourcemapBaseUrl Emit absolute sourcemap URLs with base URL prefix, e.g. https://example.com/
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--stdin=ext Specify file extension used for stdin input
Expand Down
6 changes: 5 additions & 1 deletion src/rollup/rollup.ts
Expand Up @@ -284,7 +284,11 @@ async function writeOutputFile(
if (outputOptions.sourcemap === 'inline') {
url = outputFile.map.toUrl();
} else {
url = `${basename(outputFile.fileName)}.map`;
const { sourcemapBaseUrl } = outputOptions;
const sourcemapFileName = `${basename(outputFile.fileName)}.map`;
url = sourcemapBaseUrl
? new URL(sourcemapFileName, sourcemapBaseUrl).toString()
: sourcemapFileName;
writeSourceMapPromise = fs.writeFile(`${fileName}.map`, outputFile.map.toString());
}
if (outputOptions.sourcemap !== 'hidden') {
Expand Down
2 changes: 2 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -692,6 +692,7 @@ export interface OutputOptions {
preserveModulesRoot?: string;
sanitizeFileName?: boolean | ((fileName: string) => string);
sourcemap?: boolean | 'inline' | 'hidden';
sourcemapBaseUrl?: string;
sourcemapExcludeSources?: boolean;
sourcemapFile?: string;
sourcemapPathTransform?: SourcemapPathTransformOption;
Expand Down Expand Up @@ -739,6 +740,7 @@ export interface NormalizedOutputOptions {
preserveModulesRoot: string | undefined;
sanitizeFileName: (fileName: string) => string;
sourcemap: boolean | 'inline' | 'hidden';
sourcemapBaseUrl: string | undefined;
sourcemapExcludeSources: boolean;
sourcemapFile: string | undefined;
sourcemapPathTransform: SourcemapPathTransformOption | undefined;
Expand Down
1 change: 1 addition & 0 deletions src/utils/options/mergeOptions.ts
Expand Up @@ -263,6 +263,7 @@ function mergeOutputOptions(
preserveModulesRoot: getOption('preserveModulesRoot'),
sanitizeFileName: getOption('sanitizeFileName'),
sourcemap: getOption('sourcemap'),
sourcemapBaseUrl: getOption('sourcemapBaseUrl'),
sourcemapExcludeSources: getOption('sourcemapExcludeSources'),
sourcemapFile: getOption('sourcemapFile'),
sourcemapPathTransform: getOption('sourcemapPathTransform'),
Expand Down
17 changes: 16 additions & 1 deletion src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -7,9 +7,16 @@ import type {
SourcemapPathTransformOption
} from '../../rollup/types';
import { ensureArray } from '../ensureArray';
import { errInvalidExportOptionValue, errInvalidOption, error, warnDeprecation } from '../error';
import {
errFailedValidation,
errInvalidExportOptionValue,
errInvalidOption,
error,
warnDeprecation
} from '../error';
import { resolve } from '../path';
import { sanitizeFileName as defaultSanitizeFileName } from '../sanitizeFileName';
import { isValidUrl } from '../url';
import {
generatedCodePresets,
type GenericConfigObject,
Expand Down Expand Up @@ -76,6 +83,7 @@ export function normalizeOutputOptions(
? id => id
: defaultSanitizeFileName,
sourcemap: config.sourcemap || false,
sourcemapBaseUrl: config.sourcemapBaseUrl ? ensureValidUrl(config.sourcemapBaseUrl) : undefined,
sourcemapExcludeSources: config.sourcemapExcludeSources || false,
sourcemapFile: config.sourcemapFile,
sourcemapPathTransform: config.sourcemapPathTransform as
Expand Down Expand Up @@ -471,3 +479,10 @@ const getNamespaceToStringTag = (
}
return generatedCode.symbols || false;
};

const ensureValidUrl = (url: string): NormalizedOutputOptions['sourcemapBaseUrl'] => {
if (isValidUrl(url)) {
return url;
}
return error(errFailedValidation('Not a valid URL'));
};
8 changes: 8 additions & 0 deletions src/utils/url.ts
@@ -0,0 +1,8 @@
export function isValidUrl(url: string): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure where I should add tests for functions like these. It looks like all of the tests in the project are end-to-end tests and do not import typescript code from src. @lukastaegert I'd be happy to open a separate PR adding unit tests that execute with mocha -r ts-node/register.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, please don't. Then we would still need a test that actually uses the option, i.e. an E2E test. I mean that is the point, to test the effect of this option. As e2e tests in Rollup are REALLY fast, there is really no point in writing unit tests that need to be adapted with every refactoring.
A very simple way could be to add a chunking-form test because those tests just snapshot the output directory and that would include the source map. Or you add a sourcemap test, those tests pass you the source map and you make assertions on the generated map. If all else fails, I will need to look into writing a test myself.

try {
new URL(url);
} catch (_) {
return false;
}
return true;
}
4 changes: 2 additions & 2 deletions test/misc/optionList.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test/sourcemaps/samples/sourcemap-base-url/_config.js
@@ -0,0 +1,22 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path');

module.exports = {
description: 'adds a sourcemap base url',
options: {
output: {
sourcemapBaseUrl: 'https://example.com'
}
},
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];
assert.equal(
sourceMappingURL,
`//# sourceMappingURL=https://example.com/bundle.${profile.format}.js.map`
);
}
};
1 change: 1 addition & 0 deletions test/sourcemaps/samples/sourcemap-base-url/main.js
@@ -0,0 +1 @@
console.log( 42 );