Skip to content

Commit

Permalink
Add sourcemapBaseUrl option (#4527)
Browse files Browse the repository at this point in the history
* Add sourcemapBaseUrl option

* Use sourcemapBaseUrl if it's present

* Add tests

* Update docs

* Run lint

* Extend documentation, add bad URL test

* Align CLI docs

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
nickgarlis and lukastaegert committed Jul 8, 2022
1 parent fbb86f5 commit 768534a
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 4 deletions.
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 <url> Emit absolute sourcemap URLs with given base
--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 <url> Emit absolute sourcemap URLs with given base
--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
8 changes: 7 additions & 1 deletion docs/999-big-list-of-options.md
Expand Up @@ -1075,11 +1075,17 @@ Type: `boolean | 'inline' | 'hidden'`<br> CLI: `-m`/`--sourcemap`/`--no-sourcema

If `true`, a separate sourcemap file will be created. If `"inline"`, the sourcemap will be appended to the resulting `output` file as a data URI. `"hidden"` works like `true` except that the corresponding sourcemap comments in the bundled files are suppressed.

### output.sourcemapBaseUrl

Type: `string`<br> CLI: `--sourcemapBaseUrl <url>`

By default, sourcemap files generated by Rollup uses relative URLs to reference the files they describe. By providing an absolute base URL, e.g. `https://example.com`, sourcemaps will use absolute URLs instead.

#### output.sourcemapExcludeSources

Type: `boolean`<br> CLI: `--sourcemapExcludeSources`/`--no-sourcemapExcludeSources`<br> Default: `false`

If `true`, the actual code of the sources will not be added to the sourcemaps making them considerably smaller.
If `true`, the actual code of the sources will not be added to the sourcemaps, making them considerably smaller.

#### output.sourcemapFile

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
20 changes: 20 additions & 0 deletions src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -10,6 +10,7 @@ import { ensureArray } from '../ensureArray';
import { 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 +77,7 @@ export function normalizeOutputOptions(
? id => id
: defaultSanitizeFileName,
sourcemap: config.sourcemap || false,
sourcemapBaseUrl: getSourcemapBaseUrl(config),
sourcemapExcludeSources: config.sourcemapExcludeSources || false,
sourcemapFile: config.sourcemapFile,
sourcemapPathTransform: config.sourcemapPathTransform as
Expand Down Expand Up @@ -471,3 +473,21 @@ const getNamespaceToStringTag = (
}
return generatedCode.symbols || false;
};

const getSourcemapBaseUrl = (
config: OutputOptions
): NormalizedOutputOptions['sourcemapBaseUrl'] => {
const { sourcemapBaseUrl } = config;
if (sourcemapBaseUrl) {
if (isValidUrl(sourcemapBaseUrl)) {
return sourcemapBaseUrl;
}
return error(
errInvalidOption(
'output.sourcemapBaseUrl',
'outputsourcemapbaseurl',
`must be a valid URL, received ${JSON.stringify(sourcemapBaseUrl)}`
)
);
}
};
8 changes: 8 additions & 0 deletions src/utils/url.ts
@@ -0,0 +1,8 @@
export function isValidUrl(url: string): boolean {
try {
new URL(url);
} catch (_) {
return false;
}
return true;
}
14 changes: 14 additions & 0 deletions test/function/samples/sourcemap-base-url-invalid/_config.js
@@ -0,0 +1,14 @@
module.exports = {
description: 'throws for invalid sourcemapBaseUrl',
options: {
output: {
sourcemapBaseUrl: 'example.com'
}
},
generateError: {
code: 'INVALID_OPTION',
message:
'Invalid value for option "output.sourcemapBaseUrl" - must be a valid URL, received "example.com".',
url: 'https://rollupjs.org/guide/en/#outputsourcemapbaseurl'
}
};
1 change: 1 addition & 0 deletions test/function/samples/sourcemap-base-url-invalid/main.js
@@ -0,0 +1 @@
console.log( 42 );
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 );

0 comments on commit 768534a

Please sign in to comment.