Skip to content

Commit 12c1157

Browse files
committed
feat: pass source markup to transformers (if provided)
1 parent bf47776 commit 12c1157

File tree

8 files changed

+34
-7
lines changed

8 files changed

+34
-7
lines changed

src/autoProcess.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type AutoPreprocessOptions = {
6060
export const transform = async (
6161
name: string,
6262
options: TransformerOptions,
63-
{ content, map, filename, attributes }: TransformerArgs<any>,
63+
{ content, markup, map, filename, attributes }: TransformerArgs<any>,
6464
): Promise<Processed> => {
6565
if (options === false) {
6666
return { code: content };
@@ -75,6 +75,7 @@ export const transform = async (
7575

7676
return transformer({
7777
content,
78+
markup,
7879
filename,
7980
map,
8081
attributes,
@@ -151,6 +152,7 @@ export function sveltePreprocess(
151152
): Preprocessor => async (svelteFile) => {
152153
let {
153154
content,
155+
markup,
154156
filename,
155157
lang,
156158
alias,
@@ -187,6 +189,7 @@ export function sveltePreprocess(
187189

188190
const transformed = await transform(lang, transformerOptions, {
189191
content,
192+
markup,
190193
filename,
191194
attributes,
192195
});
@@ -205,6 +208,7 @@ export function sveltePreprocess(
205208
if (transformers.replace) {
206209
const transformed = await transform('replace', transformers.replace, {
207210
content,
211+
markup: content,
208212
filename,
209213
});
210214

@@ -221,11 +225,13 @@ export function sveltePreprocess(
221225
const script: PreprocessorGroup['script'] = async ({
222226
content,
223227
attributes,
228+
markup: fullMarkup,
224229
filename,
225230
}) => {
226231
const transformResult: Processed = await scriptTransformer({
227232
content,
228233
attributes,
234+
markup: fullMarkup,
229235
filename,
230236
});
231237

@@ -235,7 +241,7 @@ export function sveltePreprocess(
235241
const transformed = await transform(
236242
'babel',
237243
getTransformerOptions('babel'),
238-
{ content: code, map, filename, attributes },
244+
{ content: code, markup: fullMarkup, map, filename, attributes },
239245
);
240246

241247
code = transformed.code;
@@ -250,11 +256,13 @@ export function sveltePreprocess(
250256
const style: PreprocessorGroup['style'] = async ({
251257
content,
252258
attributes,
259+
markup: fullMarkup,
253260
filename,
254261
}) => {
255262
const transformResult = await cssTransformer({
256263
content,
257264
attributes,
265+
markup: fullMarkup,
258266
filename,
259267
});
260268

@@ -275,6 +283,7 @@ export function sveltePreprocess(
275283

276284
const transformed = await transform('postcss', postcssOptions, {
277285
content: code,
286+
markup: fullMarkup,
278287
map,
279288
filename,
280289
attributes,
@@ -288,7 +297,7 @@ export function sveltePreprocess(
288297
const transformed = await transform(
289298
'globalStyle',
290299
getTransformerOptions('globalStyle'),
291-
{ content: code, map, filename, attributes },
300+
{ content: code, markup: fullMarkup, map, filename, attributes },
292301
);
293302

294303
code = transformed.code;

src/modules/markup.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ export async function transformMarkup(
1717

1818
/** If no <template> was found, run the transformer over the whole thing */
1919
if (!templateMatch) {
20-
return transformer({ content, attributes: {}, filename, options });
20+
return transformer({
21+
content,
22+
markup: content,
23+
attributes: {},
24+
filename,
25+
options,
26+
});
2127
}
2228

2329
const [fullMatch, attributesStr = '', templateCode] = templateMatch;
@@ -38,6 +44,7 @@ export async function transformMarkup(
3844
/** Transform the found template code */
3945
let { code, map, dependencies } = await transformer({
4046
content: templateCode,
47+
markup: templateCode,
4148
attributes,
4249
filename,
4350
options,

src/modules/tagInfo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const getTagInfo = async ({
2727
attributes,
2828
filename,
2929
content,
30+
markup,
3031
}: PreprocessorArgs) => {
3132
const dependencies = [];
3233
// catches empty content and self-closing tags
@@ -62,5 +63,6 @@ export const getTagInfo = async ({
6263
lang,
6364
alias,
6465
dependencies,
66+
markup,
6567
};
6668
};

src/processors/typescript.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default (options?: Options.Typescript): PreprocessorGroup => ({
88
const { transformer } = await import('../transformers/typescript');
99
let {
1010
content,
11+
markup,
1112
filename,
1213
attributes,
1314
lang,
@@ -22,6 +23,7 @@ export default (options?: Options.Typescript): PreprocessorGroup => ({
2223

2324
const transformed = await transformer({
2425
content,
26+
markup,
2527
filename,
2628
attributes,
2729
options,

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type TransformerArgs<T> = {
2222
filename: string;
2323
attributes?: Record<string, any>;
2424
map?: string | object;
25+
markup?: string;
2526
dianostics?: unknown[];
2627
options?: T;
2728
};

test/autoProcess/externalFiles.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,24 @@ describe('external files', () => {
2626
afterEach(warnSpy.mockClear);
2727

2828
it('should insert external file content and add as deps', async () => {
29+
const code = `<template src="./fixtures/template.html"></template>
30+
<style src="./fixtures/style.css"></style>
31+
<script src="./fixtures/script.js"></script>`;
32+
2933
const [markup, script, style] = [
3034
await markupProcessor({
31-
content: `<template src="./fixtures/template.html"></template>
32-
<style src="./fixtures/style.css"></style>
33-
<script src="./fixtures/script.js"></script>`,
35+
content: code,
3436
filename: resolve(__dirname, '..', 'App.svelte'),
3537
}),
3638
await scriptProcessor({
3739
content: ``,
40+
markup: code,
3841
filename: resolve(__dirname, '..', 'App.svelte'),
3942
attributes: { src: `./fixtures/script.js` },
4043
}),
4144
await styleProcessor({
4245
content: ``,
46+
markup: code,
4347
filename: resolve(__dirname, '..', 'App.svelte'),
4448
attributes: { src: `./fixtures/style.css` },
4549
}),

test/modules/modules.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe(`get tag information`, () => {
5959
it('should only include src files if content is empty', async () => {
6060
let parsedFile = await getTagInfo({
6161
content: '',
62+
markup: '',
6263
attributes: { src: './fixtures/style.scss' },
6364
filename: getTestAppFilename(),
6465
});

test/transformers/typescript.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const autoProcessTS = (content: string, compilerOptions?: any) => {
2929

3030
return opts.script({
3131
content,
32+
markup: `<script lang="ts">${content}</script>`,
3233
attributes: { type: 'text/typescript' },
3334
filename: resolve(__dirname, '..', 'App.svelte'),
3435
}) as Processed & { diagnostics: Diagnostic[] };

0 commit comments

Comments
 (0)