Skip to content

WIP: Improve TypeScript imports preprocessing (#318) #342

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

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
"@types/pug": "^2.0.4",
"@types/sass": "^1.16.0",
"detect-indent": "^6.0.0",
"magic-string": "^0.25.7",
"sorcery": "^0.10.0",
"strip-indent": "^3.0.0"
},
"peerDependencies": {
Expand Down
15 changes: 12 additions & 3 deletions src/autoProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type AutoPreprocessOptions = {
export const transform = async (
name: string,
options: TransformerOptions,
{ content, map, filename, attributes }: TransformerArgs<any>,
{ content, markup, map, filename, attributes }: TransformerArgs<any>,
): Promise<Processed> => {
if (options === false) {
return { code: content };
Expand All @@ -75,6 +75,7 @@ export const transform = async (

return transformer({
content,
markup,
filename,
map,
attributes,
Expand Down Expand Up @@ -151,6 +152,7 @@ export function sveltePreprocess(
): Preprocessor => async (svelteFile) => {
let {
content,
markup,
filename,
lang,
alias,
Expand Down Expand Up @@ -187,6 +189,7 @@ export function sveltePreprocess(

const transformed = await transform(lang, transformerOptions, {
content,
markup,
filename,
attributes,
});
Expand All @@ -205,6 +208,7 @@ export function sveltePreprocess(
if (transformers.replace) {
const transformed = await transform('replace', transformers.replace, {
content,
markup: content,
filename,
});

Expand All @@ -221,11 +225,13 @@ export function sveltePreprocess(
const script: PreprocessorGroup['script'] = async ({
content,
attributes,
markup: fullMarkup,
filename,
}) => {
const transformResult: Processed = await scriptTransformer({
content,
attributes,
markup: fullMarkup,
filename,
});

Expand All @@ -235,7 +241,7 @@ export function sveltePreprocess(
const transformed = await transform(
'babel',
getTransformerOptions('babel'),
{ content: code, map, filename, attributes },
{ content: code, markup: fullMarkup, map, filename, attributes },
);

code = transformed.code;
Expand All @@ -250,11 +256,13 @@ export function sveltePreprocess(
const style: PreprocessorGroup['style'] = async ({
content,
attributes,
markup: fullMarkup,
filename,
}) => {
const transformResult = await cssTransformer({
content,
attributes,
markup: fullMarkup,
filename,
});

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

const transformed = await transform('postcss', postcssOptions, {
content: code,
markup: fullMarkup,
map,
filename,
attributes,
Expand All @@ -288,7 +297,7 @@ export function sveltePreprocess(
const transformed = await transform(
'globalStyle',
getTransformerOptions('globalStyle'),
{ content: code, map, filename, attributes },
{ content: code, markup: fullMarkup, map, filename, attributes },
);

code = transformed.code;
Expand Down
56 changes: 40 additions & 16 deletions src/modules/markup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import type { Transformer, Preprocessor } from '../types';

/** Create a tag matching regexp. */
export function createTagRegex(tagName: string, flags?: string): RegExp {
return new RegExp(
`<!--[^]*?-->|<${tagName}(\\s[^]*?)?(?:>([^]*?)<\\/${tagName}>|\\/>)`,
flags,
);
}

/** Strip script and style tags from markup. */
export function stripTags(markup: string): string {
return markup
.replace(createTagRegex('style', 'gi'), '')
.replace(createTagRegex('script', 'gi'), '');
}

/** Transform an attribute string into a key-value object */
export function parseAttributes(attributesStr: string): Record<string, any> {
return attributesStr
.split(/\s+/)
.filter(Boolean)
.reduce((acc: Record<string, string | boolean>, attr) => {
const [name, value] = attr.split('=');

// istanbul ignore next
acc[name] = value ? value.replace(/['"]/g, '') : true;

return acc;
}, {});
}

export async function transformMarkup(
{ content, filename }: { content: string; filename: string },
transformer: Preprocessor | Transformer<unknown>,
Expand All @@ -9,35 +39,29 @@ export async function transformMarkup(

markupTagName = markupTagName.toLocaleLowerCase();

const markupPattern = new RegExp(
`/<!--[^]*?-->|<${markupTagName}(\\s[^]*?)?(?:>([^]*?)<\\/${markupTagName}>|\\/>)`,
);
const markupPattern = createTagRegex(markupTagName);

const templateMatch = content.match(markupPattern);

/** If no <template> was found, run the transformer over the whole thing */
if (!templateMatch) {
return transformer({ content, attributes: {}, filename, options });
return transformer({
content,
markup: content,
attributes: {},
filename,
options,
});
}

const [fullMatch, attributesStr = '', templateCode] = templateMatch;

/** Transform an attribute string into a key-value object */
const attributes = attributesStr
.split(/\s+/)
.filter(Boolean)
.reduce((acc: Record<string, string | boolean>, attr) => {
const [name, value] = attr.split('=');

// istanbul ignore next
acc[name] = value ? value.replace(/['"]/g, '') : true;

return acc;
}, {});
const attributes = parseAttributes(attributesStr);

/** Transform the found template code */
let { code, map, dependencies } = await transformer({
content: templateCode,
markup: templateCode,
attributes,
filename,
options,
Expand Down
2 changes: 2 additions & 0 deletions src/modules/tagInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const getTagInfo = async ({
attributes,
filename,
content,
markup,
}: PreprocessorArgs) => {
const dependencies = [];
// catches empty content and self-closing tags
Expand Down Expand Up @@ -62,5 +63,6 @@ export const getTagInfo = async ({
lang,
alias,
dependencies,
markup,
};
};
2 changes: 2 additions & 0 deletions src/processors/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default (options?: Options.Typescript): PreprocessorGroup => ({
const { transformer } = await import('../transformers/typescript');
let {
content,
markup,
filename,
attributes,
lang,
Expand All @@ -22,6 +23,7 @@ export default (options?: Options.Typescript): PreprocessorGroup => ({

const transformed = await transformer({
content,
markup,
filename,
attributes,
options,
Expand Down
Loading