Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove preprocessor-specific attributes #653

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/preprocessing.md
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example does not make sense as i assume potato-language uses the lang attribute value.

If you want to show it here, the preprocessor would have to use a custom potato attribute that then gets filtered


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

export const removeSrcAttribute = (attributes: Record<string, any>) => {
Copy link
Member

@dominikg dominikg Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a generic util?

export const filterAttributes(attributes: Record<string,any>, filterFn: (a:string) => boolean) {
  return Object.fromEntries(Object.entries(attributes).filter(([a])=>filterFn(a)))
}

usage:

const cleanAttributes = filterAttributes(attributes, a => a !=='src');
// or
const attributesToRemove = ['foo','bar'];
const cleanMultipleAttributes = filterAttributes(attributes, a => !attributesToRemove.includes(a))

that can be reused for global and potentially others?

const { src, ...rest } = attributes;

return rest;
};
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';
@@ -22,6 +22,7 @@ const babel = (options?: Options.Babel): PreprocessorGroup => ({

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
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';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
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';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
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';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
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';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
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';

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

return {
...transformed,
attributes: removeSrcAttribute(transformed.attributes || attributes),
dependencies: concat(dependencies, transformed.dependencies),
};
},
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';

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

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

return { code: css, map: newMap };
if (attributes?.global) {
const { global, ...rest } = attributes;

attributes = rest;
}

return {
code: css,
map: newMap,
attributes,
};
};

export { transformer };
2 changes: 1 addition & 1 deletion test/processors/babel.test.ts
Original file line number Diff line number Diff line change
@@ -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>"
`);
});
Loading
Oops, something went wrong.