Skip to content

Commit

Permalink
Support inline sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Aug 11, 2022
1 parent c322542 commit 1e7f339
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/rollup/rollup.ts
Expand Up @@ -281,6 +281,7 @@ async function writeOutputFile(
// 'recursive: true' does not throw if the folder structure, or parts of it, already exist
await fs.mkdir(dirname(fileName), { recursive: true });

// TODO Lukas move logic before generateBundle
let writeSourceMapPromise: Promise<void> | undefined;
let source: string | Uint8Array;
if (outputFile.type === 'asset') {
Expand Down
17 changes: 14 additions & 3 deletions src/utils/renderChunks.ts
Expand Up @@ -20,6 +20,7 @@ import {
replaceSinglePlaceholder
} from './hashPlaceholders';
import { normalize, resolve } from './path';
import { SOURCEMAPPING_URL } from './sourceMappingURL';
import { timeEnd, timeStart } from './timers';

interface HashResult {
Expand Down Expand Up @@ -70,7 +71,8 @@ export async function renderChunks(
renderedChunksByPlaceholder,
hashesByPlaceholder,
outputBundle,
nonHashedChunksWithPlaceholders
nonHashedChunksWithPlaceholders,
outputOptions
);

timeEnd('transform chunks', 2);
Expand Down Expand Up @@ -286,7 +288,8 @@ function addChunksToBundle(
renderedChunksByPlaceholder: Map<string, RenderedChunkWithPlaceholders>,
hashesByPlaceholder: Map<string, string>,
outputBundle: OutputBundleWithPlaceholders,
nonHashedChunksWithPlaceholders: RenderedChunkWithPlaceholders[]
nonHashedChunksWithPlaceholders: RenderedChunkWithPlaceholders[],
{ sourcemap }: NormalizedOutputOptions
) {
for (const { chunk, code, fileName, map } of renderedChunksByPlaceholder.values()) {
const updatedCode = replacePlaceholders(code, hashesByPlaceholder);
Expand All @@ -297,9 +300,17 @@ function addChunksToBundle(
outputBundle[finalFileName] = chunk.generateOutputChunk(updatedCode, map, hashesByPlaceholder);
}
for (const { chunk, code, fileName, map } of nonHashedChunksWithPlaceholders) {
const updatedCode = hashesByPlaceholder.size
let updatedCode = hashesByPlaceholder.size
? replacePlaceholders(code, hashesByPlaceholder)
: code;
// TODO Lukas use shared code for placeholder files above
// TODO Lukas support other sourcemap options
if (map) {
if (sourcemap === 'inline') {
const url = map.toUrl();
updatedCode += `//# ${SOURCEMAPPING_URL}=${url}\n`;
}
}
outputBundle[fileName] = chunk.generateOutputChunk(updatedCode, map, hashesByPlaceholder);
}
}
26 changes: 26 additions & 0 deletions test/function/samples/sourcemap-inline-generatebundle/_config.js
@@ -0,0 +1,26 @@
const assert = require('assert');

module.exports = {
solo: true,
description: 'includes inline sourcemap comments in generateBundle hook',
options: {
plugins: [
{
name: 'test',
generateBundle(options, bundle) {
assert.strictEqual(
bundle['main.js'].code,
`'use strict';
var main = 42;
module.exports = main;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibWFpbi5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MjtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLFdBQWUsRUFBRTs7OzsifQ==
`
);
}
}
],
output: { sourcemap: 'inline' }
}
};
@@ -0,0 +1 @@
export default 42;

0 comments on commit 1e7f339

Please sign in to comment.