Skip to content

Commit

Permalink
fix: 🐛 remove support for 'type' attribute
Browse files Browse the repository at this point in the history
Please use the `lang="..."` attribute to identify the language of a
style or script tag instead

BREAKING CHANGE: 🧨 Cannot use "type" attribute to identify the language of a style or
script tag anymore. Use `lang` instead
  • Loading branch information
kaisermann committed Dec 10, 2022
1 parent 9f4c29f commit 07bc8aa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 34 deletions.
25 changes: 0 additions & 25 deletions src/modules/language.ts
Expand Up @@ -4,10 +4,6 @@ import { isValidLocalPath } from './utils';

import type { PreprocessorArgs } from '../types';

// todo: remove on v5
let hasLoggedDeprecatedLangTypescriptWarning = false;
let hasLoggedDeprecatedTypeWarning = false;

const LANGUAGE_DEFAULTS: Record<string, any> = {
sass: {
indentedSyntax: true,
Expand Down Expand Up @@ -90,27 +86,6 @@ export const getLanguage = (attributes: PreprocessorArgs['attributes']) => {
}

alias = attributes.lang;

if (alias === 'typescript' && !hasLoggedDeprecatedLangTypescriptWarning) {
hasLoggedDeprecatedLangTypescriptWarning = true;
console.warn(
`[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.`,
);
}
} else if (attributes.type) {
// istanbul ignore if
if (typeof attributes.type !== 'string') {
throw new Error('type attribute must be string');
}

alias = attributes.type.replace(/^(text|application)\/(.*)$/, '$2');

if (attributes.type.includes('text/') && !hasLoggedDeprecatedTypeWarning) {
hasLoggedDeprecatedTypeWarning = true;
console.warn(
`[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.`,
);
}
} else if (
typeof attributes.src === 'string' &&
isValidLocalPath(attributes.src)
Expand Down
14 changes: 5 additions & 9 deletions test/autoProcess/autoProcess.test.ts
Expand Up @@ -9,8 +9,6 @@ import { getLanguage } from '../../src/modules/language';

describe('detect - mimetype', () => {
const MIMETYPES = [
{ type: 'application/ld+json', targetLanguage: 'ld+json' },
{ type: 'text/some-other', targetLanguage: 'some-other' },
{ lang: 'stylus', targetLanguage: 'stylus' },
{ src: '../foo.js', targetLanguage: 'javascript' },
{
Expand All @@ -24,11 +22,9 @@ describe('detect - mimetype', () => {
},
];

MIMETYPES.forEach(({ type, lang, src, targetLanguage }) => {
it(`should detect '${
src ?? type ?? lang
}' as '${targetLanguage}'`, async () => {
const language = getLanguage({ type, lang, src } as any);
MIMETYPES.forEach(({ lang, src, targetLanguage }) => {
it(`should detect '${src ?? lang}' as '${targetLanguage}'`, async () => {
const language = getLanguage({ lang, src } as any);

expect(language).toMatchObject({ lang: targetLanguage });
});
Expand Down Expand Up @@ -111,7 +107,7 @@ describe('options', () => {
});

it('should NOT preprocess preserved languages', async () => {
const input = `<div></div><script type="application/ld+json">{"json":true}</script>`;
const input = `<div></div><script lang="ld+json">{"json":true}</script>`;
const opts = sveltePreprocess({
preserve: ['ld+json'],
aliases: [['ld+json', 'structuredData']],
Expand All @@ -123,7 +119,7 @@ describe('options', () => {
const preprocessed = await preprocess(input, opts);

expect(preprocessed.toString?.()).toContain(
`<script type="application/ld+json">{"json":true}</script>`,
`<script lang="ld+json">{"json":true}</script>`,
);
});

Expand Down

0 comments on commit 07bc8aa

Please sign in to comment.