Skip to content

Commit

Permalink
fix: 🐛 remove support for custom default languages
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 🧨 Languages must be explicitly defined via the lang attribute.
  • Loading branch information
kaisermann committed Dec 10, 2022
1 parent 2806ada commit 3d60856
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 75 deletions.
24 changes: 7 additions & 17 deletions src/autoProcess.ts
Expand Up @@ -22,6 +22,12 @@ import type {
Transformers,
} from './types';

const TARGET_LANGUAGES = Object.freeze({
markup: 'html',
style: 'css',
script: 'javascript',
});

export const transform = async (
name: string | null | undefined,
options: TransformerOptions,
Expand Down Expand Up @@ -53,25 +59,10 @@ export function sveltePreprocess(
aliases,
markupTagName = 'template',
preserve = [],
defaults,
sourceMap = process?.env?.NODE_ENV === 'development' ?? false,
...rest
} = {} as AutoPreprocessOptions,
): AutoPreprocessGroup {
const defaultLanguages = Object.freeze({
markup: 'html',
style: 'css',
script: 'javascript',
...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 @@ -133,7 +124,7 @@ export function sveltePreprocess(
await getTagInfo(svelteFile);

if (lang == null || alias == null) {
alias = defaultLanguages[type];
alias = TARGET_LANGUAGES[type];
lang = getLanguageFromAlias(alias);
}

Expand Down Expand Up @@ -284,7 +275,6 @@ export function sveltePreprocess(
};

return {
defaultLanguages,
markup,
script,
style,
Expand Down
14 changes: 1 addition & 13 deletions src/types/index.ts
Expand Up @@ -63,24 +63,12 @@ export interface Transformers {
[language: string]: TransformerOptions;
}

export type AutoPreprocessGroup = PreprocessorGroup & {
defaultLanguages: Readonly<{
markup: string;
style: string;
script: string;
}>;
};
export type AutoPreprocessGroup = PreprocessorGroup;

export type AutoPreprocessOptions = {
markupTagName?: string;
aliases?: Array<[string, string]>;
preserve?: string[];
/** @deprecated Don't use "defaults" anymore, define the language being used explicitly instead */
defaults?: {
markup?: string;
style?: string;
script?: string;
};
sourceMap?: boolean;

// transformers
Expand Down
45 changes: 0 additions & 45 deletions test/autoProcess/autoProcess.test.ts
Expand Up @@ -137,34 +137,6 @@ describe('options', () => {
expect(await doesCompileThrow(input, opts)).toBe(true);
});

it('should accept other languages as default', async () => {
const input = `<template>markup</template><style>style</style><script>script</script>`;

const opts = sveltePreprocess({
defaults: {
markup: 'customMarkup',
script: 'customScript',
style: 'customStyle',
},
globalStyle: false,
customMarkup({ content }: any) {
return { code: content.replace('markup', 'potato') };
},
customScript({ content }: any) {
return { code: content.replace('script', 'potato') };
},
customStyle({ content }: any) {
return { code: content.replace('style', 'potato') };
},
});

const preprocessed = await preprocess(input, opts);

expect(preprocessed.toString?.()).toContain(
'potato<style>potato</style><script>potato</script>',
);
});

it('should respect lang/type attributes even if another default language is set', async () => {
const input = `<script lang="tomatoScript">script</script>`;

Expand All @@ -186,21 +158,4 @@ describe('options', () => {
'<script lang="tomatoScript">tomato</script>',
);
});

it('should be able to use default markup language with template tags', async () => {
const input = `potato`;

const opts = sveltePreprocess({
defaults: {
markup: 'potatoScript',
},
potatoScript({ content }: any) {
return { code: content.replace('potato', 'french-fries') };
},
});

const preprocessed = await preprocess(input, opts);

expect(preprocessed.toString?.()).toContain('french-fries');
});
});

0 comments on commit 3d60856

Please sign in to comment.