-
Notifications
You must be signed in to change notification settings - Fork 150
Use esbuild
to transpile typescript in .svelte templates
#177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As stated in #178, I'm interested in removing the type-checking process whatsoever in |
esbuild
to transpile typescript in .svelte templates
I'm guessing it's not as simple as transpile(typescript, tsconfig): javascript...? :P |
While you could do one preprocessor that used either tsc or esbuild depending on a preference, i don't think that would be a good solution. |
This issue shouldn't fade into oblivion... transpilation with esbuild vs tsc is night and day! |
@martaver I didn't forget about this one! However, I'm not exactly sure what the API to change between For now, I think you can override the
Also, do |
https://github.com/evanw/esbuild#currently-supported
so it does read tsconfig.json this might be a good idea: Some more information about options here (could prove useful for sourcemap support) |
Yeap, what he said... in addition, it's worth noting that esbuilt just strips so much of the configuration in tsconfig is superfluous. esbuild only seems to give a damn about the options specifically related to forming output with a specific js target. |
Thanks for the tip with the preprocessor - works a treat! Any thoughts on using esbuild in sapper...? Would esbuild make or break it? There are rollup plugins that allow for using esbuild in place of babel/tsc, e.g. https://www.npmjs.com/package/rollup-plugin-esbuild. Personally I wonder if using something like Snowpack together with Sapper would work... |
@martaver The equivalent of that rollup plugin would be a |
A follow-up question to the people involved in the issue. Maybe you have some tips. I used the suggested solution and it kind of works. Let me explain. There are two ways to preprocess Typescript templates. Standard one, using svelte-preprocess (npm add -D typescript) // svelte.config.js
const autoPreprocess = require('svelte-preprocess');
module.exports = {
preprocess: autoPreprocess(),
}; And faster one, using esbuild (npm add -D esbuild). // svelte.config.js
const autoPreprocess = require('svelte-preprocess');
const { transformSync } = require('esbuild');
module.exports = {
preprocess: autoPreprocess({
typescript({ content, filename }) {
const { js: code } = transformSync(content, { loader: 'ts' });
return { code };
},
}),
}; Example CodeI have an external file export const foo = (s: string) => s.split('').reverse().join('') and <script type="ts">
import { foo } from './store';
const message: string = 'Learn Svelte';
</script>
<p>{message}</p>
<pre>{foo('dallas')}</pre> The ProblemWhen using the standard preprocess everything works fine, but when using the
The editor also gives me an error However, it works when I add a Do you have any suggestions on how to solve this? I should also add that I am using Snowpack as my bundler. |
It worked as I expected, thank you!
Is there any way to make it follows |
Hey @tomocrafter 👋 Did you add a typescript plugin to your bundler? rollup/webpack needs to know how to handle that TypeScript import. The preprocessor doesn't handle imports, it just transpiles your code. |
yes @kaisermann. I tried both // rollup.config.js
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import commonjs from '@rollup/plugin-commonjs';
import svelte from 'rollup-plugin-svelte';
import babel from '@rollup/plugin-babel';
import { terser } from 'rollup-plugin-terser';
import config from 'sapper/config/rollup.js';
import typescript from 'rollup-plugin-typescript2';
import svelteConfig from './svelte.config.js';
import pkg from './package.json';
const mode = process.env.NODE_ENV;
const dev = mode === 'development';
const legacy = !!process.env.SAPPER_LEGACY_BUILD;
const onwarn = (warning, onwarn) => (warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) || onwarn(warning);
export default {
client: {
input: config.client.input().replace(/\.js$/, '.ts'),
output: config.client.output(),
plugins: [
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
svelte({
dev,
hydratable: true,
emitCss: true,
preprocess: svelteConfig.preprocess,
}),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
typescript(),
legacy && babel({
extensions: ['.js', '.mjs', '.html', '.svelte'],
babelHelpers: 'runtime',
exclude: ['node_modules/@babel/**'],
presets: [
['@babel/preset-env', {
targets: '> 0.25%, not dead'
}]
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-runtime', {
useESModules: true
}]
]
}),
!dev && terser({
module: true
})
],
preserveEntrySignatures: false,
onwarn,
},
server: {
input: config.server.input().server.replace(/\.js$/, '.ts'),
output: config.server.output(),
plugins: [
replace({
'process.browser': false,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
svelte({
generate: 'ssr',
dev,
preprocess: svelteConfig.preprocess,
}),
resolve({
dedupe: ['svelte']
}),
commonjs(),
typescript(),
],
external: Object.keys(pkg.dependencies).concat(
require('module').builtinModules || Object.keys(process.binding('natives'))
),
preserveEntrySignatures: 'strict',
onwarn,
},
serviceworker: {
input: config.serviceworker.input(),
output: config.serviceworker.output(),
plugins: [
resolve(),
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
commonjs(),
!dev && terser()
],
preserveEntrySignatures: false,
onwarn,
}
}; //svelte.config.js
import autoPreprocess from "svelte-preprocess";
import { transformSync } from 'esbuild';
export default {
preprocess: autoPreprocess({
defaults: {
script: 'typescript',
style: 'scss'
},
typescript({ content }) {
const { js: code } = transformSync(content, {
loader: 'ts'
});
return { code };
},
//typescript: true,
scss: true,
}),
// ...other svelte options
}; |
any news on this one? @codechips ive been reading your blog about js bundler... can you share your best set up for svelte starter that uses typescript.... svite? esbuild? i know svite is a plugin for vite, and vite makes the dev process fast on cold start... and uses parcel on bundling for production... |
I would say that Svite is my favorite Svelte bundler an the moment. It has support for Typescript out of the box. I think it uses Rollup for bundling, not Parcel. Svite also comes with a few sweet templates that you can use. You can read my "reveiw" of Svite here https://codechips.me/svelte-postcss-and-typescript-with-svite/ Also, I've been playing with SWC compiler - https://swc.rs. You can use it in Rollup instead of esbuild to transpile Typescript in Svelte files using svelte-preprocess.
You can also use https://github.com/mentaljam/rollup-plugin-swc to transpile regular Typescript files in Rollup to speed up compilation. |
thanks @codechips glad you like svite. regarding performance improvements by using esbuild over tsc in svite: (to quote myself from routify discord bundler-tech channel a while ago )
If you want to play with the benchmark yourself: dominikg/svite#6 . Add |
I've been using swc successfully and my benchmarks show big speed boosts over update: see svelte-preprocess-esbuild which solves the imports issue here |
Is your feature request related to a problem? Please describe.
Typescript transpilation gets slow, quickly.
Describe the solution you'd like
esbuild is currently the fastest way to transform typescript into javascript. It's a few orders of magnitude faster than tsc or babel. Using esbuild would help keep the developer experience snappy and not punish developers for choosing typescript.
Describe alternatives you've considered
Snowpack looks promising, and uses esbuild to transform ts/tsx, but their svelte setup doesn't support typescript without using svelte-preprocess. That means we're back to running things through tsc or babel, and affects some of the dev experience.
How important is this feature to you?
Anything that makes developer feedback fast is extremely valuable to me, and to everyone I imagine.
The text was updated successfully, but these errors were encountered: