Skip to content

Commit bb0cef8

Browse files
committed
refactor: 💡 use import type
1 parent 0b35b4d commit bb0cef8

25 files changed

+75
-47
lines changed

‎src/autoProcess.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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 { throwUnsupportedError, throwError } from './modules/errors';
15+
import { throwError } from './modules/errors';
1616

1717
interface Transformers {
1818
typescript?: TransformerOptions<Options.Typescript>;
@@ -28,10 +28,38 @@ interface Transformers {
2828
[languageName: string]: TransformerOptions;
2929
}
3030

31-
type AutoPreprocessOptions = Transformers & {
31+
type AutoPreprocessOptions = {
32+
/** @deprecated for svelte v3 use instead a array of processors */
33+
onBefore?: ({
34+
content,
35+
filename,
36+
}: {
37+
content: string;
38+
filename: string;
39+
}) => Promise<string> | string;
3240
markupTagName?: string;
41+
/** @deprecated add transformer config directly to svelte-preprocess options object */
42+
transformers?: Transformers;
3343
aliases?: Array<[string, string]>;
3444
preserve?: string[];
45+
typescript?: TransformerOptions<Options.Typescript>;
46+
scss?: TransformerOptions<Options.Sass>;
47+
sass?: TransformerOptions<Options.Sass>;
48+
less?: TransformerOptions<Options.Less>;
49+
stylus?: TransformerOptions<Options.Stylus>;
50+
postcss?: TransformerOptions<Options.Postcss>;
51+
babel?: TransformerOptions<Options.Babel>;
52+
coffeescript?: TransformerOptions<Options.Coffeescript>;
53+
pug?: TransformerOptions<Options.Pug>;
54+
globalStyle?: Options.GlobalStyle;
55+
// workaround while we don't have this
56+
// https://github.com/microsoft/TypeScript/issues/17867
57+
[languageName: string]:
58+
| string
59+
| Promise<string>
60+
| Array<[string, string]>
61+
| string[]
62+
| TransformerOptions;
3563
};
3664

3765
const ALIAS_OPTION_OVERRIDES: Record<string, any> = {
@@ -130,14 +158,14 @@ export function autoPreprocess(
130158
return;
131159
}
132160

133-
if (lang === targetLanguage) {
161+
if (
162+
lang === targetLanguage ||
163+
transformers[lang] === false ||
164+
transformers[alias] === false
165+
) {
134166
return { code: content, dependencies };
135167
}
136168

137-
if (transformers[lang] === false || transformers[alias] === false) {
138-
throwUnsupportedError(alias, filename);
139-
}
140-
141169
const transformed = await runTransformer(
142170
lang,
143171
getTransformerOptions(lang, alias),

‎src/modules/language.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { basename } from 'path';
22

3-
import { PreprocessorArgs } from '../types';
3+
import type { PreprocessorArgs } from '../types';
44

55
export const ALIAS_MAP = new Map([
66
['pcss', 'css'],

‎src/modules/parseFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFile, access } from 'fs';
22
import { resolve, dirname } from 'path';
33

4-
import { PreprocessorArgs } from '../types';
4+
import type { PreprocessorArgs } from '../types';
55
import { getLanguage } from './language';
66

77
export const resolveSrc = (importerFile: string, srcPath: string) =>

‎src/processors/babel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PreprocessorGroup, Options } from '../types';
1+
import type { PreprocessorGroup, Options } from '../types';
22
import { concat } from '../modules/concat';
33
import { parseFile } from '../modules/parseFile';
44

‎src/processors/coffeescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PreprocessorGroup, Options } from '../types';
1+
import type { PreprocessorGroup, Options } from '../types';
22
import { parseFile } from '../modules/parseFile';
33
import { concat } from '../modules/concat';
44

‎src/processors/globalStyle.ts

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

33
export default (): PreprocessorGroup => {
44
return {

‎src/processors/less.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PreprocessorGroup, Options } from '../types';
1+
import type { PreprocessorGroup, Options } from '../types';
22
import { parseFile } from '../modules/parseFile';
33
import { concat } from '../modules/concat';
44

‎src/processors/postcss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { PreprocessorGroup, Options } from '../types';
12
import { parseFile } from '../modules/parseFile';
2-
import { PreprocessorGroup, Options } from '../types';
33
import { concat } from '../modules/concat';
44

55
/** Adapted from https://github.com/TehShrike/svelte-preprocess-postcss */

‎src/processors/pug.ts

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

33
export default (options?: Options.Pug): PreprocessorGroup => ({
44
async markup({ content, filename }) {

‎src/processors/replace.ts

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

33
export default (options: Options.Replace): PreprocessorGroup => ({
44
async markup({ content, filename }) {

‎src/processors/scss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { PreprocessorGroup, Options } from '../types';
12
import { parseFile } from '../modules/parseFile';
2-
import { PreprocessorGroup, Options } from '../types';
33
import { concat } from '../modules/concat';
44

55
export default (options?: Options.Sass): PreprocessorGroup => ({

‎src/processors/stylus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { Options, PreprocessorGroup } from '../types';
12
import { parseFile } from '../modules/parseFile';
2-
import { Options, PreprocessorGroup } from '../types';
33
import { concat } from '../modules/concat';
44

55
export default (options?: Options.Stylus): PreprocessorGroup => ({

‎src/processors/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Options, PreprocessorGroup } from '../types';
1+
import type { Options, PreprocessorGroup } from '../types';
22
import { parseFile } from '../modules/parseFile';
33
import { concat } from '../modules/concat';
44

‎src/transformers/babel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { transformAsync } from '@babel/core';
22

3-
import { Transformer, Options } from '../types';
3+
import type { Transformer, Options } from '../types';
44

55
const transformer: Transformer<Options.Babel> = async ({
66
content,

‎src/transformers/coffeescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import coffeescript from 'coffeescript';
22

3-
import { Transformer, Options } from '../types';
3+
import type { Transformer, Options } from '../types';
44

55
const transformer: Transformer<Options.Coffeescript> = ({
66
content,

‎src/transformers/globalStyle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import postcss, { AtRule } from 'postcss';
22

3-
import { Transformer, Options } from '../types';
3+
import type { Transformer, Options } from '../types';
44
import { globalifySelector } from '../modules/globalifySelector';
55

66
const selectorPattern = /:global(?!\()/;

‎src/transformers/less.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import less from 'less';
22

33
import { getIncludePaths } from '../modules/getIncludePaths';
4-
import { Transformer, Options } from '../types';
4+
import type { Transformer, Options } from '../types';
55

66
const transformer: Transformer<Options.Less> = async ({
77
content,

‎src/transformers/postcss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import postcss from 'postcss';
22

3-
import { Transformer, Options } from '../types';
3+
import type { Transformer, Options } from '../types';
44

55
const process = async (
66
{ plugins, parser, syntax }: Options.Postcss,

‎src/transformers/pug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import detectIndent from 'detect-indent';
22
import pug from 'pug';
33

4-
import { Transformer, Options } from '../types';
4+
import type { Transformer, Options } from '../types';
55

66
// Mixins to use svelte template features
77
const GET_MIXINS = (identationType: 'tab' | 'space') =>

‎src/transformers/replace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Transformer, Options } from '../types';
1+
import type { Transformer, Options } from '../types';
22

33
const transformer: Transformer<Options.Replace> = async ({
44
content,
@@ -7,7 +7,7 @@ const transformer: Transformer<Options.Replace> = async ({
77
let newContent = content;
88

99
for (const [regex, replacer] of options) {
10-
newContent = newContent.replace(regex, replacer);
10+
newContent = newContent.replace(regex, replacer as any);
1111
}
1212

1313
return {

‎src/transformers/scss.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Result } from 'sass';
1+
import type { Result } from 'sass';
22

3-
import { Transformer, Processed, Options } from '../types';
3+
import type { Transformer, Processed, Options } from '../types';
44
import { getIncludePaths } from '../modules/getIncludePaths';
55
import { importAny } from '../modules/importAny';
66

‎src/transformers/stylus.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import stylus from 'stylus';
44

55
import { getIncludePaths } from '../modules/getIncludePaths';
6-
import { Processed, Transformer, Options } from '../types';
6+
import type { Transformer, Options } from '../types';
77

88
const transformer: Transformer<Options.Stylus> = ({
99
content,
@@ -15,7 +15,7 @@ const transformer: Transformer<Options.Stylus> = ({
1515
...options,
1616
};
1717

18-
return new Promise<Processed>((resolve, reject) => {
18+
return new Promise((resolve, reject) => {
1919
const style = stylus(content, {
2020
filename,
2121
...options,

‎src/transformers/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { dirname, basename, resolve } from 'path';
33

44
import ts from 'typescript';
55

6-
import { Transformer, Options } from '../types';
6+
import type { Transformer, Options } from '../types';
77

88
type CompilerOptions = Options.Typescript['compilerOptions'];
99

‎src/types/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import {
1+
import type {
22
Processed as SvelteProcessed,
33
Preprocessor,
44
} from 'svelte/types/compiler/preprocess';
55

6-
import * as Options from './options';
6+
import type * as Options from './options';
77

8-
export { Options };
8+
export type { Options };
99

10-
export {
10+
export type {
1111
Processed as SvelteProcessed,
1212
PreprocessorGroup,
1313
Preprocessor,
@@ -17,14 +17,14 @@ export type PreprocessorArgs = Preprocessor extends (options: infer T) => any
1717
? T
1818
: never;
1919

20-
export interface TransformerArgs<T> {
20+
export type TransformerArgs<T> = {
2121
content: string;
2222
filename: string;
2323
attributes?: Record<string, any>;
2424
map?: string | object;
2525
dianostics?: unknown[];
2626
options?: T;
27-
}
27+
};
2828

2929
export type Processed = SvelteProcessed & {
3030
diagnostics?: any[];

‎src/types/options.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import postcss from 'postcss';
2-
import { Options as SassOptions, render, renderSync } from 'sass';
3-
import { Options as PugOptions } from 'pug';
4-
import { CompilerOptions } from 'typescript';
5-
import { TransformOptions as BabelOptions } from '@babel/core';
6-
7-
export type Replace = Array<
8-
[RegExp, string] | [RegExp, (substring: string, ...args: any[]) => string]
9-
>;
1+
import type postcss from 'postcss';
2+
import type { Options as SassOptions, render, renderSync } from 'sass';
3+
import type { Options as PugOptions } from 'pug';
4+
import type { CompilerOptions } from 'typescript';
5+
import type { TransformOptions as BabelOptions } from '@babel/core';
106

117
export interface Coffeescript {
128
inlineMap?: boolean;
@@ -71,3 +67,7 @@ export interface Typescript {
7167
export interface GlobalStyle {
7268
sourceMap: boolean;
7369
}
70+
71+
export type Replace = Array<
72+
[RegExp, string] | [RegExp, (substring: string, ...args: any[]) => string]
73+
>;

0 commit comments

Comments
 (0)