Skip to content

Better Typescript transpilation #392

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

Merged
merged 23 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@
"sass": "^1.26.8",
"stylus": "^0.54.7",
"sugarss": "^2.0.0",
"svelte": "^3.23.0",
"svelte": "^3.42.0",
"ts-jest": "^25.1.0",
"typescript": "^3.9.5"
},
"dependencies": {
"@types/pug": "^2.0.4",
"@types/sass": "^1.16.0",
"detect-indent": "^6.0.0",
"magic-string": "^0.25.7",
"sorcery": "^0.10.0",

Choose a reason for hiding this comment

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

sorcery was not supported for a long time and adds a few unnecessary dependencies.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got a good alternative to achieve source map concatenation?

Choose a reason for hiding this comment

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

I could bump sorcery to node 10 or 12 and get rid from "sander" package. Though I see svelte-preprocess still supports node 9. Do you plan major bump in near future?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does this mean you have write access to the repo? I only see Rich having contributed to that repository. Can't speak for how soon @kaisermann plans on bumping, but if a little cleanup can be done I have nothing against that. Not pressing for me since the solution seems to work so far.

Choose a reason for hiding this comment

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

I don't yet but I can ask Rich

Copy link
Member

Choose a reason for hiding this comment

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

I could bump sorcery to node 10 or 12 and get rid from "sander" package. Though I see svelte-preprocess still supports node 9. Do you plan major bump in near future?

Yeah, I've been wanting to remove the deprecated things and do a little cleanup.

Copy link
Member

Choose a reason for hiding this comment

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

Since we're already here, I've started a discussion about one of the things on my mind for the next major: #424

"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 @@ -24,7 +24,7 @@ import { transformMarkup } from './modules/markup';
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 @@ -39,6 +39,7 @@ export const transform = async (

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

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

Expand All @@ -185,11 +189,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 @@ -199,7 +205,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 @@ -214,11 +220,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 @@ -239,6 +247,7 @@ export function sveltePreprocess(

const transformed = await transform('postcss', postcssOptions, {
content: code,
markup: fullMarkup,
map,
filename,
attributes,
Expand All @@ -252,7 +261,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