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

Better Typescript transpilation #392

Merged
merged 23 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 6 additions & 7 deletions docs/preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,19 @@ Note: `svelte-preprocess` automatically configures inclusion paths for your root

### TypeScript

| Option | Default | Description |
| ------------------- | ----------- | ----------------------------------------------------------------------------------------------------------- |
| `tsconfigDirectory` | `undefined` | optional `string` that specifies from where to load the tsconfig from.<br><br>i.e `'./configs'` |
| `tsconfigFile` | `undefined` | optional `string` pointing torwards a `tsconfig` file.<br><br>i.e `'./tsconfig.app.json'` |
| `compilerOptions` | `undefined` | optional compiler options configuration. These will be merged with options from the tsconfig file if found. |
| Option | Default | Description |
| -------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `tsconfigDirectory` | `undefined` | optional `string` that specifies from where to load the tsconfig from.<br><br>i.e `'./configs'` |
| `tsconfigFile` | `undefined` | optional `string` pointing torwards a `tsconfig` file.<br><br>i.e `'./tsconfig.app.json'` |
| `compilerOptions` | `undefined` | optional compiler options configuration. These will be merged with options from the tsconfig file if found. |
| `handleMixedImports` | inferred | optional `boolean` that defines the transpilation strategy. If set to `true`, you don't need to strictly separate types and values in imports. You need at least Svelte version 3.39 if you want to use this. `true` by default if you meet the minimum version requirement, else `false`. |

You can check the [`compilerOptions` reference](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) for specific TypeScript options.

#### Typescript - Limitations

- Since `v4`, `svelte-preprocess` doesn't type-check your component, its only concern is to transpile it into valid JavaScript for the compiler. If you want to have your components type-checked, you can use [svelte-check](https://github.com/sveltejs/language-tools/blob/master/packages/svelte-check/README.md).

- As we're only transpiling, it's not possible to import types or interfaces into your svelte component without using the new TS 3.8 `type` import modifier: `import type { SomeInterface } from './MyModule'` otherwise bundlers will complain that the name is not exported by `MyModule`.

- Using TypeScript inside a component's markup is currently **not** supported. See [#318](https://github.com/sveltejs/svelte-preprocess/issues/318) for development updates to this.

### `globalStyle`
Expand Down
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,
};
};
51 changes: 51 additions & 0 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,54 @@ export function setProp(obj, keyList, val) {

obj[keyList[i]] = val;
}

export const javascriptReservedKeywords = new Set([
'arguments',
'await',
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'enum',
'eval',
'export',
'extends',
'false',
'finally',
'for',
'function',
'if',
'implements',
'import',
'in',
'instanceof',
'interface',
'let',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'static',
'super',
'switch',
'this',
'throw',
'true',
'try',
'typeof',
'var',
'void',
'while',
'with',
'yield',
]);
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