Skip to content
This repository was archived by the owner on Jul 10, 2021. It is now read-only.

Commit fe2b583

Browse files
committed
feat(typescript): rewrite typescript support use first preprocessor convention
1 parent 2ece98a commit fe2b583

File tree

1 file changed

+43
-54
lines changed

1 file changed

+43
-54
lines changed

index.js

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -62,67 +62,56 @@ function applyForcedOptions(config, forcedOptions) {
6262
return appliedChanges;
6363
}
6464

65-
function setupTypeScriptPreprocessor(svelteConfig, isBuild) {
66-
if (!svelteConfig.preprocess) {
67-
svelteConfig.preprocess = [];
68-
} else if (!Array.isArray(svelteConfig.preprocess)) {
69-
svelteConfig.preprocess = [svelteConfig.preprocess];
65+
async function hasTypescriptPreprocessor(svelteConfig) {
66+
if(!Array.isArray(svelteConfig.preprocess)) {
67+
return false;
7068
}
71-
if (svelteConfig.preprocess.some((preprocessor) => preprocessor.defaultLanguages)) {
72-
log.error(
73-
'found svelte-preprocess automatic preprocessor in your svelte config. This is not compatible with svite cli --typescript option.',
74-
);
75-
throw new Error('no dual typescript setup allowed');
76-
} else if (
77-
svelteConfig.preprocess.some((preprocessor) => preprocessor.script && preprocessor.script.toString().indexOf('typescript') > -1)
78-
) {
79-
log.error(
80-
'found a script preprocessor for typescript in your svelte config. This is not compatible with svite cli --typescript option',
81-
);
82-
throw new Error('no dual typescript setup allowed');
83-
} else {
84-
log.debug('no typescript preprocessor found, adding svelte-preprocess typescript');
69+
if(!svelteConfig.preprocess[0].script) {
70+
return false;
71+
}
72+
try {
73+
const result= await svelteConfig.preprocess[0].script({content:'const x:string="y";export{}',attributes:{lang:'ts'},filename:'Test.svelte'},{lang:"ts"},"Test.svelte")
74+
log.info('result',result);
75+
return true;
76+
} catch (e) {
77+
log.debug('script preprocessor at index 0 failed to transform typescript',e);
78+
return false;
79+
}
80+
}
81+
async function setupTypeScriptPreprocessor(svelteConfig, isBuild) {
82+
const hasTypescript = await hasTypescriptPreprocessor(svelteConfig);
83+
if(!hasTypescript) {
84+
throw new Error('svite -ts requires a typescript preprocessor at index 0 in svelte.config.js preprocess:[]')
85+
}
86+
if(!isBuild) {
87+
8588
// unfortunately esbuild preprocess does not work properly with imports of svelte components as of now
8689
// svelteConfig.preprocess.unshift(require('./tools/svelte-preprocess-ts-vite'));
87-
const sveltePreprocess = require('svelte-preprocess');
88-
const preprocessOptions = {
89-
// disable all but typescript
90-
babel: false,
91-
scss: false,
92-
sass: false,
93-
less: false,
94-
stylus: false,
95-
postcss: false,
96-
coffeescript: false,
97-
pug: false,
98-
globalStyle: false,
99-
replace: false,
100-
};
101-
if (!isBuild) {
90+
91+
92+
const {typescript} = require('svelte-preprocess');
10293
const devTS = {
103-
// in dev: force ultra modern and add vite/dist/importMeta to enable custom hmr
104-
compilerOptions: {
105-
module: 'esnext',
106-
target: 'esnext',
107-
types: ['svelte', 'vite/dist/importMeta'],
108-
},
94+
module: 'esnext',
95+
target: 'es2017',
96+
moduleResolution: "node",
97+
importsNotUsedAsValues: "error",
98+
types: ['svelte', 'vite/dist/importMeta'],
99+
sourceMap: true
109100
};
110-
log.info('overriding typescript config to support hmr in vite', devTS);
111-
preprocessOptions.typescript = devTS;
101+
log.warn('overriding typescript preprocessor to support hmr in vite', devTS);
102+
svelteConfig.preprocess[0] = typescript(devTS);
112103
}
113-
svelteConfig.preprocess.unshift(sveltePreprocess(preprocessOptions));
114-
}
115104
}
116105

117-
function finalizeConfig(config, type) {
106+
async function finalizeConfig(config, type) {
118107
const isProduction = process.env.NODE_ENV === 'production';
119108
const isDevServer = !!config.vite;
120109
const isBuild = type === 'build';
121110
const svelteConfig = config[type];
122111
const forcedOptions = forcedSvelteOptions[type];
123112
const isTypescript = config.svite.typescript;
124113
if (isTypescript) {
125-
setupTypeScriptPreprocessor(svelteConfig, isBuild);
114+
await setupTypeScriptPreprocessor(svelteConfig, isBuild);
126115
}
127116
if (isBuild) {
128117
forcedOptions.dev = (isDevServer && !!config.dev.hot) || !isProduction;
@@ -245,8 +234,8 @@ function updateViteConfig(config) {
245234
log.debug.enabled && log.debug('vite config', viteConfig);
246235
}
247236

248-
function createRollupPluginSvelteHot(config, type) {
249-
const finalSvelteConfig = finalizeConfig(config, type);
237+
async function createRollupPluginSvelteHot(config, type) {
238+
const finalSvelteConfig = await finalizeConfig(config, type);
250239
return rollupPluginSvelteHot(finalSvelteConfig);
251240
}
252241

@@ -261,7 +250,7 @@ function createDev(config) {
261250
async ({ config: viteConfig }) => {
262251
config.vite = viteConfig;
263252
updateViteConfig(config);
264-
devPlugin = createRollupPluginSvelteHot(config, 'dev');
253+
devPlugin = await createRollupPluginSvelteHot(config, 'dev');
265254
},
266255
];
267256

@@ -309,22 +298,22 @@ function createDev(config) {
309298
function rollupPluginDeferred(name, createPlugin) {
310299
const wrapper = {
311300
name,
312-
options: function (options) {
313-
const plugin = createPlugin();
301+
options: async function(options) {
302+
const plugin = await createPlugin();
314303
Object.keys(plugin).forEach((key) => {
315304
if (key !== 'options') {
316305
wrapper[key] = plugin[key];
317306
}
318307
});
319-
return plugin.options ? plugin.options.apply(this, options) : null;
308+
return plugin.options ? await plugin.options.apply(this, options) : options;
320309
},
321310
};
322311
return wrapper;
323312
}
324313

325314
function createBuildPlugins(config) {
326-
const buildPlugin = rollupPluginDeferred('svelte', () => {
327-
return createRollupPluginSvelteHot(config, 'build');
315+
const buildPlugin = rollupPluginDeferred('svelte', async () => {
316+
return await createRollupPluginSvelteHot(config, 'build');
328317
});
329318

330319
const rollupPluginDedupeSvelte = rollupPluginDeferred('node-resolve', () =>

0 commit comments

Comments
 (0)