Skip to content

Commit 2632a88

Browse files
authored
fix: deprecate type attribute (#417)
* fix: deprecate type attribute * fix: deprecate lang=typescript * fix: warn type/lang deprecation logs once per npm process
1 parent b9e9ff7 commit 2632a88

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

Diff for: docs/preprocessing.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ The following options can be passed to the preprocessor. None are required:
7070
| Option | Default | Description |
7171
| --------------- | -------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
7272
| `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. |
73-
| `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`. |
74-
| preserve | `[]` | A `string` list of languages/aliases that shouldn't pass through the preprocessor. (i.e `ld+json`) |
73+
| `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>are treated as `customLanguage`. |
74+
| `preserve` | `[]` | A `string` list of languages/aliases that shouldn't pass through the preprocessor. (i.e `ld+json`) |
7575
| `sourceMap` | `false` | If `true`, `svelte-preprocess` generates sourcemap for every language that supports it. |
7676

7777
##### Configuring preprocessors
@@ -133,7 +133,7 @@ export default {
133133
};
134134
```
135135

136-
Now `svelte-preprocess` will use the `potatoLanguage` preprocessor whenever it finds a tag with `lang="potato"`, `type="text/potatoLanguage"` or `src="./index.pot"`.
136+
Now `svelte-preprocess` will use the `potatoLanguage` preprocessor whenever it finds a tag with `lang="potato"` or `src="./index.pot"`.
137137

138138
These methods receive the same arguments and act exactly like a [common svelte preprocessor](https://svelte.dev/docs#svelte_preprocess), but the concept of `markup`/`style`/`script` is abstracted as they are executed whenever `svelte-preprocess` find the aforementioned attributes.
139139

@@ -273,7 +273,7 @@ The PostCSS preprocessor accepts four options:
273273

274274
In auto-preprocessing mode, you can set `postcss: true` if `postcss-load-config` is installed and `svelte-preprocess` will look for a PostCSS config file in your project.
275275

276-
When a `lang="sugarss"`/`type="text/sugarss"` is found, `sugarss` is automatically loaded and extra indentation is removed.
276+
When a `lang="sugarss"` is found, `sugarss` is automatically loaded and extra indentation is removed.
277277

278278
You can check the [PostCSS API reference](https://api.postcss.org/) for PostCSS specific options.
279279

@@ -356,7 +356,7 @@ You can check the [Sass API reference](https://sass-lang.com/documentation/js-ap
356356

357357
Note: `svelte-preprocess` automatically configures inclusion paths for your root directory, `node_modules` and for the current file's directory.
358358

359-
Note: when a `lang="sass"`/`type="text/sass"` is found, `indentedSyntax` is automatically set to `true`.
359+
Note: when a `lang="sass"` is found, `indentedSyntax` is automatically set to `true`.
360360

361361
Note: `sass`, with indented syntax, and `scss` are not interchangeable so make sure to configure the correct one that fits your needs.
362362

Diff for: examples/sapper-rollup/src/routes/blog/index.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
</script>
1010

11-
<script type="text/typescript">
11+
<script lang="ts">
1212
interface Post {
1313
title: string
1414
slug: string

Diff for: examples/svelte-rollup/src/App.svelte

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
<template type="text/pug">
1+
<template lang="pug">
22
h1 Hello!
33
br
44
input(bind:value="{label}")
55
br
66
Nested("{label}")
77
</template>
88

9-
<script type="text/typescript">
9+
<script lang="ts">
1010
import Nested from './Nested.svelte'
1111
export let label: string = ''
1212
1313
$: console.log(label)
1414
</script>
1515

16-
<style type="text/stylus">
16+
<style lang="stylus">
1717
h1
1818
color blue
1919
</style>

Diff for: src/modules/language.ts

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { basename } from 'path';
33
import type { PreprocessorArgs } from '../types';
44
import { isValidLocalPath } from './utils';
55

6+
// todo: remove on v5
7+
let hasLoggedDeprecatedLangTypescriptWarning = false;
8+
let hasLoggedDeprecatedTypeWarning = false;
9+
610
const LANGUAGE_DEFAULTS: Record<string, any> = {
711
sass: {
812
indentedSyntax: true,
@@ -85,13 +89,27 @@ export const getLanguage = (attributes: PreprocessorArgs['attributes']) => {
8589
}
8690

8791
alias = attributes.lang;
92+
93+
if (alias === 'typescript' && !hasLoggedDeprecatedLangTypescriptWarning) {
94+
hasLoggedDeprecatedLangTypescriptWarning = true;
95+
console.warn(
96+
`[svelte-preprocess] Deprecation notice: using 'lang="typescript"' is no longer recommended and will be removed in the next major version. Please use 'lang="ts"' instead.`,
97+
);
98+
}
8899
} else if (attributes.type) {
89100
// istanbul ignore if
90101
if (typeof attributes.type !== 'string') {
91102
throw new Error('type attribute must be string');
92103
}
93104

94105
alias = attributes.type.replace(/^(text|application)\/(.*)$/, '$2');
106+
107+
if (attributes.type.includes('text/') && !hasLoggedDeprecatedTypeWarning) {
108+
hasLoggedDeprecatedTypeWarning = true;
109+
console.warn(
110+
`[svelte-preprocess] Deprecation notice: using the "type" attribute is no longer recommended and will be removed in the next major version. Please use the "lang" attribute instead.`,
111+
);
112+
}
95113
} else if (
96114
typeof attributes.src === 'string' &&
97115
isValidLocalPath(attributes.src)

Diff for: test/transformers/typescript.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const autoProcessTS = (content: string, compilerOptions?: any) => {
3131
return opts.script?.({
3232
content,
3333
markup: `<script lang="ts">${content}</script>`,
34-
attributes: { type: 'text/typescript' },
34+
attributes: { lang: 'ts' },
3535
filename: resolve(__dirname, '..', 'App.svelte'),
3636
}) as Processed & { diagnostics: Diagnostic[] };
3737
};

0 commit comments

Comments
 (0)