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

Everything is working :) #1

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
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# [4.8.0](https://github.com/sveltejs/svelte-preprocess/compare/v4.7.4...v4.8.0) (2021-08-23)


### Bug Fixes

* add deprecation warning to defaults prop ([#393](https://github.com/sveltejs/svelte-preprocess/issues/393)) ([406d3a8](https://github.com/sveltejs/svelte-preprocess/commit/406d3a87c4779c16fca1264ce66fc491e5eec170))



## [4.7.4](https://github.com/sveltejs/svelte-preprocess/compare/v4.7.3...v4.7.4) (2021-07-09)


### Bug Fixes

* standalone preprocessors prepending data in other languages ([#380](https://github.com/sveltejs/svelte-preprocess/issues/380)) ([e6679e4](https://github.com/sveltejs/svelte-preprocess/commit/e6679e4b81cd145202b293aca1035cd2dc05ef71))



## [4.7.3](https://github.com/sveltejs/svelte-preprocess/compare/v4.7.2...v4.7.3) (2021-05-03)


Expand Down
14 changes: 6 additions & 8 deletions docs/preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ The following options can be passed to the preprocessor. None are required:
| `markupTagName` | `"template"` | `string` that sets the name of the tag `svelte-preprocess` looks for markup in custom languages.<br><br>i.e `markup` makes it possible to write your markup between `<markup lang="..."></markup>` tag. |
| `aliases` | `null` | A list of tuples `[alias: string, language: string]` that correlates an `alias` to a `language`<br><br>i.e `['cst', 'customLanguage']` means<br>`<... src="./file.cst">`<br>`<... lang="cst">`<br>`<... type="text/customLanguage">`<br>`<... type="application/customLanguage">`<br>are treated as `customLanguage`. |
| preserve | `[]` | A `string` list of languages/aliases that shouldn't pass through the preprocessor. (i.e `ld+json`) |
| `defaults` | `{ markup: 'html', script: 'javascript', style: 'css' }` | An `object` that defines the default languages of your components.<br><br>i.e: `{ script: 'typescript' }` makes TypeScript the default language, removing the need of adding `lang="ts"` to `script` tags.<br><br>**Note: It is generally not recommended to use this setting because not all tooling is able to deal with it, resulting in for example broken syntax highlighting for SCSS.** |
| `sourceMap` | `false` | If `true`, `svelte-preprocess` generates sourcemap for every language that supports it. |

##### Configuring preprocessors
Expand Down Expand Up @@ -369,20 +368,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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-preprocess",
"version": "4.7.3",
"version": "4.8.0",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down 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",
"strip-indent": "^3.0.0"
},
"peerDependencies": {
Expand Down
22 changes: 19 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 All @@ -63,6 +64,13 @@ export function sveltePreprocess(
...defaults,
});

// todo: remove this on v5
if (defaults != null) {
console.warn(
'[svelte-preprocess] Deprecation notice: using the "defaults" option is no longer recommended and will be removed in the next major version. Instead, define the language being used explicitly via the lang attribute.\n\nSee https://github.com/sveltejs/svelte-preprocess/issues/362',
);
}

const transformers = rest as Transformers;

if (aliases?.length) {
Expand Down Expand Up @@ -115,6 +123,7 @@ export function sveltePreprocess(
): Preprocessor => async (svelteFile) => {
let {
content,
markup,
filename,
lang,
alias,
Expand Down Expand Up @@ -151,6 +160,7 @@ export function sveltePreprocess(

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

Expand All @@ -185,11 +196,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 +212,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 +227,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 +254,7 @@ export function sveltePreprocess(

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