Skip to content

Commit 6cbc05b

Browse files
committed
fix: remove preprocessor-specific attributes
- global for style tags - src for script/style tags fixes #652
1 parent 45b4604 commit 6cbc05b

File tree

11 files changed

+35
-10
lines changed

11 files changed

+35
-10
lines changed

docs/preprocessing.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ export default {
123123
/** Add a custom language preprocessor */
124124
potatoLanguage({ content, filename, attributes }) {
125125
const { code, map } = require('potato-language').render(content);
126+
const { src, ...cleanedAttributes } = attributes;
126127

127-
return { code, map };
128+
return { code, map, attributes: cleanedAttributes };
128129
},
129130
}),
130131
}),

src/modules/tagInfo.ts

+5
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ export const getTagInfo = async ({
6767
markup,
6868
};
6969
};
70+
71+
export const removeSrcAttribute = (attributes: Record<string, any>) => {
72+
const { src, ...rest } = attributes;
73+
return rest;
74+
};

src/processors/babel.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { concat } from '../modules/utils';
2-
import { getTagInfo } from '../modules/tagInfo';
2+
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
33
import { prepareContent } from '../modules/prepareContent';
44

55
import type { PreprocessorGroup, Options } from '../types';
@@ -22,6 +22,7 @@ const babel = (options?: Options.Babel): PreprocessorGroup => ({
2222

2323
return {
2424
...transformed,
25+
attributes: removeSrcAttribute(transformed.attributes || attributes),
2526
dependencies: concat(dependencies, transformed.dependencies),
2627
};
2728
},

src/processors/coffeescript.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTagInfo } from '../modules/tagInfo';
1+
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
22
import { concat } from '../modules/utils';
33
import { prepareContent } from '../modules/prepareContent';
44

@@ -32,6 +32,7 @@ const coffeescript = (options?: Options.Coffeescript): PreprocessorGroup => ({
3232

3333
return {
3434
...transformed,
35+
attributes: removeSrcAttribute(transformed.attributes || attributes),
3536
dependencies: concat(dependencies, transformed.dependencies),
3637
};
3738
},

src/processors/less.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTagInfo } from '../modules/tagInfo';
1+
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
22
import { concat } from '../modules/utils';
33
import { prepareContent } from '../modules/prepareContent';
44

@@ -25,6 +25,7 @@ const less = (options?: Options.Less): PreprocessorGroup => ({
2525

2626
return {
2727
...transformed,
28+
attributes: removeSrcAttribute(transformed.attributes || attributes),
2829
dependencies: concat(dependencies, transformed.dependencies),
2930
};
3031
},

src/processors/postcss.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTagInfo } from '../modules/tagInfo';
1+
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
22
import { concat } from '../modules/utils';
33
import { prepareContent } from '../modules/prepareContent';
44

@@ -23,6 +23,7 @@ const postcss = (options?: Options.Postcss): PreprocessorGroup => ({
2323

2424
return {
2525
...transformed,
26+
attributes: removeSrcAttribute(transformed.attributes || attributes),
2627
dependencies: concat(dependencies, transformed.dependencies),
2728
};
2829
},

src/processors/scss.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTagInfo } from '../modules/tagInfo';
1+
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
22
import { concat } from '../modules/utils';
33
import { prepareContent } from '../modules/prepareContent';
44

@@ -33,6 +33,7 @@ const scss = (options?: Options.Sass): PreprocessorGroup => ({
3333

3434
return {
3535
...transformed,
36+
attributes: removeSrcAttribute(transformed.attributes || attributes),
3637
dependencies: concat(dependencies, transformed.dependencies),
3738
};
3839
},

src/processors/stylus.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTagInfo } from '../modules/tagInfo';
1+
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
22
import { concat } from '../modules/utils';
33
import { prepareContent } from '../modules/prepareContent';
44

@@ -31,6 +31,7 @@ const stylus = (options?: Options.Stylus): PreprocessorGroup => ({
3131

3232
return {
3333
...transformed,
34+
attributes: removeSrcAttribute(transformed.attributes || attributes),
3435
dependencies: concat(dependencies, transformed.dependencies),
3536
};
3637
},

src/processors/typescript.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTagInfo } from '../modules/tagInfo';
1+
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
22
import { concat } from '../modules/utils';
33
import { prepareContent } from '../modules/prepareContent';
44

@@ -26,6 +26,7 @@ const typescript = (options?: Options.Typescript): PreprocessorGroup => ({
2626

2727
return {
2828
...transformed,
29+
attributes: removeSrcAttribute(transformed.attributes || attributes),
2930
dependencies: concat(dependencies, transformed.dependencies),
3031
};
3132
},

src/transformers/globalStyle.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,19 @@ const transformer: Transformer<Options.GlobalStyle> = async ({
8383
map: options?.sourceMap ? { prev: map } : false,
8484
});
8585

86-
return { code: css, map: newMap };
86+
return {
87+
code: css,
88+
map: newMap,
89+
attributes:
90+
attributes &&
91+
Object.keys(attributes).reduce((acc: any, key) => {
92+
if (key !== 'global') {
93+
acc[key] = attributes[key];
94+
}
95+
96+
return acc;
97+
}, {}),
98+
};
8799
};
88100

89101
export { transformer };

test/processors/babel.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe(`processor - babel`, () => {
2323
]);
2424

2525
expect(preprocessed.toString?.()).toMatchInlineSnapshot(`
26-
"<script src="./fixtures/script.babel.js">export var hello = {};
26+
"<script>export var hello = {};
2727
export var world = hello == null ? void 0 : hello.value;</script><div></div>"
2828
`);
2929
});

0 commit comments

Comments
 (0)