Rollup still does not tree shake dynamic imports (so vite as well), and fix is pretty simple - to create another file which will reexport functions that are needed. But i am too lazy, so i created this.
TL;DR
import("comlink?only=wrap,expose,\<whatever>")
This will create file which exports what you specified in only
param, like this:
export { wrap, expose } from 'comlink'
On build rollup will tree shake that "file" and dynamic import won't weight as much as whole library
npm i unplugin-ltsdi
Vite
// vite.config.ts
import LTSDI from 'unplugin-ltsdi/vite'
export default defineConfig({
plugins: [
LTSDI({ /* options */ }),
],
})
Example: playground/
Rollup
// rollup.config.js
import LTSDI from 'unplugin-ltsdi/rollup'
export default {
plugins: [
LTSDI({ /* options */ }),
],
}
Webpack
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('unplugin-ltsdi/webpack')({ /* options */ })
]
}
Nuxt
// nuxt.config.js
export default {
buildModules: [
['unplugin-ltsdi/nuxt', { /* options */ }],
],
}
This module works for both Nuxt 2 and Nuxt Vite
Vue CLI
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-ltsdi/webpack')({ /* options */ }),
],
},
}
esbuild
// esbuild.config.js
import { build } from 'esbuild'
import LTSDI from 'unplugin-ltsdi/esbuild'
build({
plugins: [LTSDI()],
})
Create a .d.ts
shim for ?
import like this
// index.d.ts - for example
declare module 'comlink?*' {
import comlink from 'comlink'
export = comlink
}