diff --git a/src/index.ts b/src/index.ts index 081660d3d81b..1caf5b5abc10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,7 +50,7 @@ function parseAttributes(str: string) { return attrs; } -async function replaceTagContents(source, type: 'script' | 'style', preprocessor: Preprocessor) { +async function replaceTagContents(source, type: 'script' | 'style', preprocessor: Preprocessor, options: PreprocessOptions) { const exp = new RegExp(`<${type}([\\S\\s]*?)>([\\S\\s]*?)<\\/${type}>`, 'ig'); const match = exp.exec(source); @@ -59,7 +59,8 @@ async function replaceTagContents(source, type: 'script' | 'style', preprocessor const content: string = match[2]; const processed: { code: string, map?: SourceMap | string } = await preprocessor({ content, - attributes + attributes, + filename : options.filename }); if (processed && processed.code) { @@ -77,16 +78,19 @@ async function replaceTagContents(source, type: 'script' | 'style', preprocessor export async function preprocess(source: string, options: PreprocessOptions) { const { markup, style, script } = options; if (!!markup) { - const processed: { code: string, map?: SourceMap | string } = await markup({ content: source }); + const processed: { code: string, map?: SourceMap | string } = await markup({ + content: source, + filename: options.filename + }); source = processed.code; } if (!!style) { - source = await replaceTagContents(source, 'style', style); + source = await replaceTagContents(source, 'style', style, options); } if (!!script) { - source = await replaceTagContents(source, 'script', script); + source = await replaceTagContents(source, 'script', script, options); } return { diff --git a/src/interfaces.ts b/src/interfaces.ts index 0d884f847212..9773d330d394 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -83,9 +83,10 @@ export interface CustomElementOptions { } export interface PreprocessOptions { - markup?: (options: {content: string}) => { code: string, map?: SourceMap | string }; + markup?: (options: {content: string, filename: string}) => { code: string, map?: SourceMap | string }; style?: Preprocessor; script?: Preprocessor; + filename?: string } -export type Preprocessor = (options: {content: string, attributes: Record}) => { code: string, map?: SourceMap | string }; +export type Preprocessor = (options: {content: string, attributes: Record, filename?: string}) => { code: string, map?: SourceMap | string }; diff --git a/test/preprocess/index.js b/test/preprocess/index.js index 29eaff67f1d2..6ed452f672a4 100644 --- a/test/preprocess/index.js +++ b/test/preprocess/index.js @@ -126,6 +126,41 @@ describe('preprocess', () => { }); }); + it('provides filename to processing hooks', () => { + const source = ` +

Hello __MARKUP_FILENAME__!

+ + + `; + + const expected = ` +

Hello file.html!

+ + + `; + + return svelte.preprocess(source, { + filename: 'file.html', + markup: ({ content, filename }) => { + return { + code: content.replace('__MARKUP_FILENAME__', filename) + }; + }, + style: ({ content, filename }) => { + return { + code: content.replace('__STYLE_FILENAME__', filename) + }; + }, + script: ({ content, filename }) => { + return { + code: content.replace('__SCRIPT_FILENAME__', filename) + }; + } + }).then(processed => { + assert.equal(processed.toString(), expected); + }); + }); + it('ignores null/undefined returned from preprocessor', () => { const source = `