From f17cb38a9857ddd7052f555f60f1028dfcab835e Mon Sep 17 00:00:00 2001 From: Geoff Goodman Date: Fri, 11 Oct 2019 12:55:16 -0400 Subject: [PATCH 1/2] Support a function for includeContent in Bundle Suppose you have a `magic-string` built up from a variety of sources, some of which can be loaded by the runtime and some that can't. Those that can be loaded might be, for example, public http(s) urls. In this scenario, you might want to embed source content for those sources that _can't_ be loaded at runtime. --- README.md | 3 ++- index.d.ts | 14 ++++++++++++-- src/Bundle.js | 4 +++- test/MagicString.Bundle.js | 26 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6115cba..cc762c3 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,8 @@ bundle.toString(); // console.log( answer ); // }()); -// options are as per `s.generateMap()` above +// options are as per `s.generateMap()` above except that `options.includeContent` can also +// be a function with the signature `function(source: { filename: string, content: string }): boolean` var map = bundle.generateMap({ file: 'bundle.js', includeContent: true, diff --git a/index.d.ts b/index.d.ts index 251701c..5dcdc0c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,6 +10,16 @@ export interface SourceMapOptions { includeContent: boolean; } + +export interface BundledSourceFileRecord { + filename: string | null; + content: string; +} + +export interface BundleSourceMapOptions extends SourceMapOptions { + includeContent: (source: BundledSourceFileRecord) => boolean; +} + export type SourceMapSegment = | [number] | [number, number, number, number] @@ -42,8 +52,8 @@ export class Bundle { addSource(source: MagicString | { filename?: string, content: MagicString }): Bundle; append(str: string, options?: BundleOptions): Bundle; clone(): Bundle; - generateMap(options?: Partial): SourceMap; - generateDecodedMap(options?: Partial): DecodedSourceMap; + generateMap(options?: Partial): SourceMap; + generateDecodedMap(options?: Partial): DecodedSourceMap; getIndentString(): string; indent(indentStr?: string): Bundle; indentExclusionRanges: ExclusionRange | Array; diff --git a/src/Bundle.js b/src/Bundle.js index 76fa317..ff7609c 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -147,7 +147,9 @@ export default class Bundle { return options.file ? getRelativePath(options.file, source.filename) : source.filename; }), sourcesContent: this.uniqueSources.map(source => { - return options.includeContent ? source.content : null; + const includeContent = typeof options.includeContent === 'function' ? options.includeContent(source) : options.includeContent; + + return includeContent ? source.content : null; }), names, mappings: mappings.raw diff --git a/test/MagicString.Bundle.js b/test/MagicString.Bundle.js index 05883e9..07d583e 100644 --- a/test/MagicString.Bundle.js +++ b/test/MagicString.Bundle.js @@ -495,6 +495,32 @@ var template = (function () { column: 16 }); }); + + it('supports a function for includeContent', () => { + const b = new MagicString.Bundle(); + + const files = { + 'one.js': new MagicString('function one () {}', { filename: 'one.js' }), + 'two.js': new MagicString('function two () {}', { filename: 'two.js' }), + }; + + b.addSource(files['one.js']); + b.addSource(files['two.js']); + + const map = b.generateMap({ + file: 'output.js', + source: 'input.js', + includeContent(source) { + assert.ok(files[source.filename]); + assert.equal(files[source.filename].original, source.content); + + return source.filename === 'one.js'; + } + }); + + assert.equal(map.sourcesContent[0], files['one.js'].original); + assert.equal(map.sourcesContent[1], null); + }); }); describe('indent', () => { From 4c1bdf5f29356eb8fa5679a8db6c5c53d3619689 Mon Sep 17 00:00:00 2001 From: Geoff Goodman Date: Fri, 11 Oct 2019 13:54:10 -0400 Subject: [PATCH 2/2] Fix typing to allow includeContent as boolean --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 5dcdc0c..f390ed3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -17,7 +17,7 @@ export interface BundledSourceFileRecord { } export interface BundleSourceMapOptions extends SourceMapOptions { - includeContent: (source: BundledSourceFileRecord) => boolean; + includeContent: boolean | ((source: BundledSourceFileRecord) => boolean); } export type SourceMapSegment =