From 1e7f339f2fef0aa5e1c08bfabab2c1b42684883c Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 11 Aug 2022 07:34:13 +0200 Subject: [PATCH] Support inline sourcemaps --- src/rollup/rollup.ts | 1 + src/utils/renderChunks.ts | 17 +++++++++--- .../_config.js | 26 +++++++++++++++++++ .../sourcemap-inline-generatebundle/main.js | 1 + 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/function/samples/sourcemap-inline-generatebundle/_config.js create mode 100644 test/function/samples/sourcemap-inline-generatebundle/main.js diff --git a/src/rollup/rollup.ts b/src/rollup/rollup.ts index 6ee5f34d680..ff4e146d02e 100644 --- a/src/rollup/rollup.ts +++ b/src/rollup/rollup.ts @@ -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 | undefined; let source: string | Uint8Array; if (outputFile.type === 'asset') { diff --git a/src/utils/renderChunks.ts b/src/utils/renderChunks.ts index ac9ee1c6d4a..f04cc3235a9 100644 --- a/src/utils/renderChunks.ts +++ b/src/utils/renderChunks.ts @@ -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 { @@ -70,7 +71,8 @@ export async function renderChunks( renderedChunksByPlaceholder, hashesByPlaceholder, outputBundle, - nonHashedChunksWithPlaceholders + nonHashedChunksWithPlaceholders, + outputOptions ); timeEnd('transform chunks', 2); @@ -286,7 +288,8 @@ function addChunksToBundle( renderedChunksByPlaceholder: Map, hashesByPlaceholder: Map, outputBundle: OutputBundleWithPlaceholders, - nonHashedChunksWithPlaceholders: RenderedChunkWithPlaceholders[] + nonHashedChunksWithPlaceholders: RenderedChunkWithPlaceholders[], + { sourcemap }: NormalizedOutputOptions ) { for (const { chunk, code, fileName, map } of renderedChunksByPlaceholder.values()) { const updatedCode = replacePlaceholders(code, hashesByPlaceholder); @@ -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); } } diff --git a/test/function/samples/sourcemap-inline-generatebundle/_config.js b/test/function/samples/sourcemap-inline-generatebundle/_config.js new file mode 100644 index 00000000000..ba1c28f2eff --- /dev/null +++ b/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' } + } +}; diff --git a/test/function/samples/sourcemap-inline-generatebundle/main.js b/test/function/samples/sourcemap-inline-generatebundle/main.js new file mode 100644 index 00000000000..7a4e8a723a4 --- /dev/null +++ b/test/function/samples/sourcemap-inline-generatebundle/main.js @@ -0,0 +1 @@ +export default 42;