Skip to content

Commit 0b35b4d

Browse files
committed
refactor: 💡 simplify transformer calls
1 parent 397a83a commit 0b35b4d

24 files changed

+55
-80
lines changed

‎src/autoProcess.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import stripIndent from 'strip-indent';
2-
import { version } from 'svelte/package.json';
32

43
import {
54
PreprocessorGroup,
6-
TransformerOptions,
75
Preprocessor,
86
Options,
97
Processed,
8+
TransformerArgs,
9+
TransformerOptions,
1010
} from './types';
1111
import { hasPostCssInstalled } from './modules/hasPostcssInstalled';
1212
import { concat } from './modules/concat';
1313
import { parseFile } from './modules/parseFile';
1414
import { addLanguageAlias } from './modules/language';
15-
import { runTransformer } from './modules/transformers';
16-
import { throwUnsupportedError } from './modules/errors';
15+
import { throwUnsupportedError, throwError } from './modules/errors';
1716

1817
interface Transformers {
1918
typescript?: TransformerOptions<Options.Typescript>;
@@ -33,14 +32,6 @@ type AutoPreprocessOptions = Transformers & {
3332
markupTagName?: string;
3433
aliases?: Array<[string, string]>;
3534
preserve?: string[];
36-
// workaround while we don't have this
37-
// https://github.com/microsoft/TypeScript/issues/17867
38-
[languageName: string]:
39-
| string
40-
| Promise<string>
41-
| Array<[string, string]>
42-
| string[]
43-
| TransformerOptions;
4435
};
4536

4637
const ALIAS_OPTION_OVERRIDES: Record<string, any> = {
@@ -49,6 +40,35 @@ const ALIAS_OPTION_OVERRIDES: Record<string, any> = {
4940
},
5041
};
5142

43+
export const runTransformer = async (
44+
name: string,
45+
options: TransformerOptions,
46+
{ content, map, filename, attributes }: TransformerArgs<any>,
47+
): Promise<Processed> => {
48+
// remove any unnecessary indentation (useful for coffee, pug and sugarss)
49+
content = stripIndent(content);
50+
51+
if (typeof options === 'function') {
52+
return options({ content, map, filename, attributes });
53+
}
54+
55+
try {
56+
const { transformer } = await import(`./transformers/${name}`);
57+
58+
return transformer({
59+
content,
60+
filename,
61+
map,
62+
attributes,
63+
options: typeof options === 'boolean' ? null : options,
64+
});
65+
} catch (e) {
66+
throwError(
67+
`Error transforming '${name}'.\n\nMessage:\n${e.message}\n\nStack:\n${e.stack}`,
68+
);
69+
}
70+
};
71+
5272
export function autoPreprocess(
5373
{
5474
aliases,

‎src/modules/transformers.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎src/processors/babel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseFile } from '../modules/parseFile';
44

55
export default (options?: Options.Babel): PreprocessorGroup => ({
66
async script(svelteFile) {
7-
const { default: transformer } = await import('../transformers/babel');
7+
const { transformer } = await import('../transformers/babel');
88

99
const { content, filename, dependencies, attributes } = await parseFile(
1010
svelteFile,

‎src/processors/coffeescript.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { concat } from '../modules/concat';
44

55
export default (options?: Options.Coffeescript): PreprocessorGroup => ({
66
async script(svelteFile) {
7-
const { default: transformer } = await import(
8-
'../transformers/coffeescript'
9-
);
7+
const { transformer } = await import('../transformers/coffeescript');
108

119
const {
1210
content,

‎src/processors/globalStyle.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { PreprocessorGroup } from '../types';
33
export default (): PreprocessorGroup => {
44
return {
55
async style({ content, attributes, filename }) {
6-
const { default: transformer } = await import(
7-
'../transformers/globalStyle'
8-
);
6+
const { transformer } = await import('../transformers/globalStyle');
97

108
if (!attributes.global) return { code: content };
119

‎src/processors/less.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { concat } from '../modules/concat';
44

55
export default (options?: Options.Less): PreprocessorGroup => ({
66
async style(svelteFile) {
7-
const { default: transformer } = await import('../transformers/less');
7+
const { transformer } = await import('../transformers/less');
88
const {
99
content,
1010
filename,

‎src/processors/postcss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { concat } from '../modules/concat';
55
/** Adapted from https://github.com/TehShrike/svelte-preprocess-postcss */
66
export default (options?: Options.Postcss): PreprocessorGroup => ({
77
async style(svelteFile) {
8-
const { default: transformer } = await import('../transformers/postcss');
8+
const { transformer } = await import('../transformers/postcss');
99
const { content, filename, attributes, dependencies } = await parseFile(
1010
svelteFile,
1111
'css',

‎src/processors/pug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Options, PreprocessorGroup } from '../types';
22

33
export default (options?: Options.Pug): PreprocessorGroup => ({
44
async markup({ content, filename }) {
5-
const { default: transformer } = await import('../transformers/pug');
5+
const { transformer } = await import('../transformers/pug');
66

77
return transformer({ content, filename, options });
88
},

‎src/processors/replace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { PreprocessorGroup, Options } from '../types';
22

33
export default (options: Options.Replace): PreprocessorGroup => ({
44
async markup({ content, filename }) {
5-
const { default: transformer } = await import('../transformers/replace');
5+
const { transformer } = await import('../transformers/replace');
66

77
return transformer({ content, filename, options });
88
},

‎src/processors/scss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { concat } from '../modules/concat';
44

55
export default (options?: Options.Sass): PreprocessorGroup => ({
66
async style(svelteFile) {
7-
const { default: transformer } = await import('../transformers/scss');
7+
const { transformer } = await import('../transformers/scss');
88
const {
99
content,
1010
filename,

‎src/processors/stylus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { concat } from '../modules/concat';
44

55
export default (options?: Options.Stylus): PreprocessorGroup => ({
66
async style(svelteFile) {
7-
const { default: transformer } = await import('../transformers/stylus');
7+
const { transformer } = await import('../transformers/stylus');
88
const {
99
content,
1010
filename,

‎src/processors/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { concat } from '../modules/concat';
44

55
export default (options?: Options.Typescript): PreprocessorGroup => ({
66
async script(svelteFile) {
7-
const { default: transformer } = await import('../transformers/typescript');
7+
const { transformer } = await import('../transformers/typescript');
88
const {
99
content,
1010
filename,

‎src/transformers/babel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ const transformer: Transformer<Options.Babel> = async ({
3030
};
3131
};
3232

33-
export default transformer;
33+
export { transformer };

‎src/transformers/coffeescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ const transformer: Transformer<Options.Coffeescript> = ({
1717
return { code, map };
1818
};
1919

20-
export default transformer;
20+
export { transformer };

‎src/transformers/globalStyle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ const transformer: Transformer<Options.GlobalStyle> = async ({
7878
return { code: css, map: newMap };
7979
};
8080

81-
export default transformer;
81+
export { transformer };

‎src/transformers/less.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ const transformer: Transformer<Options.Less> = async ({
2626
};
2727
};
2828

29-
export default transformer;
29+
export { transformer };

‎src/transformers/postcss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ const transformer: Transformer<Options.Postcss> = async ({
6868
return process(options, content, filename, map);
6969
};
7070

71-
export default transformer;
71+
export { transformer };

‎src/transformers/pug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ const transformer: Transformer<Options.Pug> = async ({
6868
};
6969
};
7070

71-
export default transformer;
71+
export { transformer };

‎src/transformers/replace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ const transformer: Transformer<Options.Replace> = async ({
1515
};
1616
};
1717

18-
export default transformer;
18+
export { transformer };

‎src/transformers/scss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ const transformer: Transformer<Options.Sass> = async ({
6161
});
6262
};
6363

64-
export default transformer;
64+
export { transformer };

‎src/transformers/stylus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ const transformer: Transformer<Options.Stylus> = ({
3737
});
3838
};
3939

40-
export default transformer;
40+
export { transformer };

‎src/transformers/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,4 @@ const transformer: Transformer<Options.Typescript> = ({
382382
};
383383
};
384384

385-
export default transformer;
385+
export { transformer };

‎src/types/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CompilerOptions } from 'typescript';
55
import { TransformOptions as BabelOptions } from '@babel/core';
66

77
export type Replace = Array<
8-
[RegExp, (substring: string, ...args: any[]) => string | string]
8+
[RegExp, string] | [RegExp, (substring: string, ...args: any[]) => string]
99
>;
1010

1111
export interface Coffeescript {

‎test/transformers/replace.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import autoProcess from '../../src';
22
import { preprocess } from '../utils';
3+
import { Options } from '../../src/types';
34

4-
const options = [
5+
const options: Options.Replace = [
56
[/@if\s*\((.*?)\)$/gim, '{#if $1}'],
67
[/@elseif\s*\((.*?)\)$/gim, '{:else if $1}'],
78
[/@else$/gim, '{:else}'],

0 commit comments

Comments
 (0)