From 6cbc05b4f7fe64734bfccea08ddc3d262164372b Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 27 Aug 2024 22:40:39 +0200 Subject: [PATCH] fix: remove preprocessor-specific attributes - global for style tags - src for script/style tags fixes #652 --- docs/preprocessing.md | 3 ++- src/modules/tagInfo.ts | 5 +++++ src/processors/babel.ts | 3 ++- src/processors/coffeescript.ts | 3 ++- src/processors/less.ts | 3 ++- src/processors/postcss.ts | 3 ++- src/processors/scss.ts | 3 ++- src/processors/stylus.ts | 3 ++- src/processors/typescript.ts | 3 ++- src/transformers/globalStyle.ts | 14 +++++++++++++- test/processors/babel.test.ts | 2 +- 11 files changed, 35 insertions(+), 10 deletions(-) diff --git a/docs/preprocessing.md b/docs/preprocessing.md index 28aa83bc..8e5a4de8 100644 --- a/docs/preprocessing.md +++ b/docs/preprocessing.md @@ -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 }; }, }), }), diff --git a/src/modules/tagInfo.ts b/src/modules/tagInfo.ts index 0f6190c0..a7b384ef 100644 --- a/src/modules/tagInfo.ts +++ b/src/modules/tagInfo.ts @@ -67,3 +67,8 @@ export const getTagInfo = async ({ markup, }; }; + +export const removeSrcAttribute = (attributes: Record) => { + const { src, ...rest } = attributes; + return rest; +}; diff --git a/src/processors/babel.ts b/src/processors/babel.ts index 890d2974..79e1843c 100644 --- a/src/processors/babel.ts +++ b/src/processors/babel.ts @@ -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'; @@ -22,6 +22,7 @@ const babel = (options?: Options.Babel): PreprocessorGroup => ({ return { ...transformed, + attributes: removeSrcAttribute(transformed.attributes || attributes), dependencies: concat(dependencies, transformed.dependencies), }; }, diff --git a/src/processors/coffeescript.ts b/src/processors/coffeescript.ts index 97be16fc..a2836b02 100644 --- a/src/processors/coffeescript.ts +++ b/src/processors/coffeescript.ts @@ -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'; @@ -32,6 +32,7 @@ const coffeescript = (options?: Options.Coffeescript): PreprocessorGroup => ({ return { ...transformed, + attributes: removeSrcAttribute(transformed.attributes || attributes), dependencies: concat(dependencies, transformed.dependencies), }; }, diff --git a/src/processors/less.ts b/src/processors/less.ts index bed77022..aa5e8c6e 100644 --- a/src/processors/less.ts +++ b/src/processors/less.ts @@ -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'; @@ -25,6 +25,7 @@ const less = (options?: Options.Less): PreprocessorGroup => ({ return { ...transformed, + attributes: removeSrcAttribute(transformed.attributes || attributes), dependencies: concat(dependencies, transformed.dependencies), }; }, diff --git a/src/processors/postcss.ts b/src/processors/postcss.ts index 14ee3822..c2b7538a 100644 --- a/src/processors/postcss.ts +++ b/src/processors/postcss.ts @@ -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'; @@ -23,6 +23,7 @@ const postcss = (options?: Options.Postcss): PreprocessorGroup => ({ return { ...transformed, + attributes: removeSrcAttribute(transformed.attributes || attributes), dependencies: concat(dependencies, transformed.dependencies), }; }, diff --git a/src/processors/scss.ts b/src/processors/scss.ts index 0e50bf9c..c45c7c46 100644 --- a/src/processors/scss.ts +++ b/src/processors/scss.ts @@ -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'; @@ -33,6 +33,7 @@ const scss = (options?: Options.Sass): PreprocessorGroup => ({ return { ...transformed, + attributes: removeSrcAttribute(transformed.attributes || attributes), dependencies: concat(dependencies, transformed.dependencies), }; }, diff --git a/src/processors/stylus.ts b/src/processors/stylus.ts index b2d190db..eb8cc59c 100644 --- a/src/processors/stylus.ts +++ b/src/processors/stylus.ts @@ -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'; @@ -31,6 +31,7 @@ const stylus = (options?: Options.Stylus): PreprocessorGroup => ({ return { ...transformed, + attributes: removeSrcAttribute(transformed.attributes || attributes), dependencies: concat(dependencies, transformed.dependencies), }; }, diff --git a/src/processors/typescript.ts b/src/processors/typescript.ts index 8d00c97b..806b025b 100644 --- a/src/processors/typescript.ts +++ b/src/processors/typescript.ts @@ -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'; @@ -26,6 +26,7 @@ const typescript = (options?: Options.Typescript): PreprocessorGroup => ({ return { ...transformed, + attributes: removeSrcAttribute(transformed.attributes || attributes), dependencies: concat(dependencies, transformed.dependencies), }; }, diff --git a/src/transformers/globalStyle.ts b/src/transformers/globalStyle.ts index 9dbbbdbe..8f2c4f8a 100644 --- a/src/transformers/globalStyle.ts +++ b/src/transformers/globalStyle.ts @@ -83,7 +83,19 @@ const transformer: Transformer = 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 }; diff --git a/test/processors/babel.test.ts b/test/processors/babel.test.ts index 4e31ca5f..0cd5d98c 100644 --- a/test/processors/babel.test.ts +++ b/test/processors/babel.test.ts @@ -23,7 +23,7 @@ describe(`processor - babel`, () => { ]); expect(preprocessed.toString?.()).toMatchInlineSnapshot(` - "
" `); });