Skip to content

Commit 0ae82f3

Browse files
authored
Merge pull request #987 from tivac/preprocess-options
feat: Pass filename option to preprocess hooks
2 parents be8bc79 + 304bd74 commit 0ae82f3

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function parseAttributes(str: string) {
5050
return attrs;
5151
}
5252

53-
async function replaceTagContents(source, type: 'script' | 'style', preprocessor: Preprocessor) {
53+
async function replaceTagContents(source, type: 'script' | 'style', preprocessor: Preprocessor, options: PreprocessOptions) {
5454
const exp = new RegExp(`<${type}([\\S\\s]*?)>([\\S\\s]*?)<\\/${type}>`, 'ig');
5555
const match = exp.exec(source);
5656

@@ -59,7 +59,8 @@ async function replaceTagContents(source, type: 'script' | 'style', preprocessor
5959
const content: string = match[2];
6060
const processed: { code: string, map?: SourceMap | string } = await preprocessor({
6161
content,
62-
attributes
62+
attributes,
63+
filename : options.filename
6364
});
6465

6566
if (processed && processed.code) {
@@ -77,16 +78,19 @@ async function replaceTagContents(source, type: 'script' | 'style', preprocessor
7778
export async function preprocess(source: string, options: PreprocessOptions) {
7879
const { markup, style, script } = options;
7980
if (!!markup) {
80-
const processed: { code: string, map?: SourceMap | string } = await markup({ content: source });
81+
const processed: { code: string, map?: SourceMap | string } = await markup({
82+
content: source,
83+
filename: options.filename
84+
});
8185
source = processed.code;
8286
}
8387

8488
if (!!style) {
85-
source = await replaceTagContents(source, 'style', style);
89+
source = await replaceTagContents(source, 'style', style, options);
8690
}
8791

8892
if (!!script) {
89-
source = await replaceTagContents(source, 'script', script);
93+
source = await replaceTagContents(source, 'script', script, options);
9094
}
9195

9296
return {

src/interfaces.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ export interface CustomElementOptions {
8383
}
8484

8585
export interface PreprocessOptions {
86-
markup?: (options: {content: string}) => { code: string, map?: SourceMap | string };
86+
markup?: (options: {content: string, filename: string}) => { code: string, map?: SourceMap | string };
8787
style?: Preprocessor;
8888
script?: Preprocessor;
89+
filename?: string
8990
}
9091

91-
export type Preprocessor = (options: {content: string, attributes: Record<string, string | boolean>}) => { code: string, map?: SourceMap | string };
92+
export type Preprocessor = (options: {content: string, attributes: Record<string, string | boolean>, filename?: string}) => { code: string, map?: SourceMap | string };

test/preprocess/index.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,41 @@ describe('preprocess', () => {
126126
});
127127
});
128128

129+
it('provides filename to processing hooks', () => {
130+
const source = `
131+
<h1>Hello __MARKUP_FILENAME__!</h1>
132+
<style>.red { color: __STYLE_FILENAME__; }</style>
133+
<script>console.log('__SCRIPT_FILENAME__');</script>
134+
`;
135+
136+
const expected = `
137+
<h1>Hello file.html!</h1>
138+
<style>.red { color: file.html; }</style>
139+
<script>console.log('file.html');</script>
140+
`;
141+
142+
return svelte.preprocess(source, {
143+
filename: 'file.html',
144+
markup: ({ content, filename }) => {
145+
return {
146+
code: content.replace('__MARKUP_FILENAME__', filename)
147+
};
148+
},
149+
style: ({ content, filename }) => {
150+
return {
151+
code: content.replace('__STYLE_FILENAME__', filename)
152+
};
153+
},
154+
script: ({ content, filename }) => {
155+
return {
156+
code: content.replace('__SCRIPT_FILENAME__', filename)
157+
};
158+
}
159+
}).then(processed => {
160+
assert.equal(processed.toString(), expected);
161+
});
162+
});
163+
129164
it('ignores null/undefined returned from preprocessor', () => {
130165
const source = `
131166
<script>
@@ -145,4 +180,4 @@ describe('preprocess', () => {
145180
assert.equal(processed.toString(), expected);
146181
});
147182
});
148-
});
183+
});

0 commit comments

Comments
 (0)