Skip to content

Commit

Permalink
fix: remove preprocessor-specific attributes
Browse files Browse the repository at this point in the history
- global for style tags
- src for script/style tags

fixes #652
  • Loading branch information
dummdidumm committed Aug 27, 2024
1 parent 45b4604 commit 6cbc05b
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion docs/preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ export default {
/** Add a custom language preprocessor */
potatoLanguage({ content, filename, attributes }) {
const { code, map } = require('potato-language').render(content);
const { src, ...cleanedAttributes } = attributes;

return { code, map };
return { code, map, attributes: cleanedAttributes };
},
}),
}),
Expand Down
5 changes: 5 additions & 0 deletions src/modules/tagInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ export const getTagInfo = async ({
markup,
};
};

export const removeSrcAttribute = (attributes: Record<string, any>) => {
const { src, ...rest } = attributes;
return rest;

Check warning on line 73 in src/modules/tagInfo.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected blank line before this statement

Check warning on line 73 in src/modules/tagInfo.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected blank line before this statement
};
3 changes: 2 additions & 1 deletion src/processors/babel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { concat } from '../modules/utils';
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
import { prepareContent } from '../modules/prepareContent';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
Expand Down
3 changes: 2 additions & 1 deletion src/processors/coffeescript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
Expand Down
3 changes: 2 additions & 1 deletion src/processors/less.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
Expand Down
3 changes: 2 additions & 1 deletion src/processors/postcss.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
Expand Down
3 changes: 2 additions & 1 deletion src/processors/scss.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
Expand Down
3 changes: 2 additions & 1 deletion src/processors/stylus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
Expand Down
3 changes: 2 additions & 1 deletion src/processors/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfo, removeSrcAttribute } from '../modules/tagInfo';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
Expand Down
14 changes: 13 additions & 1 deletion src/transformers/globalStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,19 @@ const transformer: Transformer<Options.GlobalStyle> = async ({
map: options?.sourceMap ? { prev: map } : false,
});

return { code: css, map: newMap };
return {
code: css,
map: newMap,
attributes:
attributes &&
Object.keys(attributes).reduce((acc: any, key) => {
if (key !== 'global') {
acc[key] = attributes[key];
}

return acc;
}, {}),
};
};

export { transformer };
2 changes: 1 addition & 1 deletion test/processors/babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe(`processor - babel`, () => {
]);

expect(preprocessed.toString?.()).toMatchInlineSnapshot(`
"<script src="./fixtures/script.babel.js">export var hello = {};
"<script>export var hello = {};
export var world = hello == null ? void 0 : hello.value;</script><div></div>"
`);
});
Expand Down

0 comments on commit 6cbc05b

Please sign in to comment.