Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/logotip4ik/unplugin-ltsdi

"Lazy" Tree shake dynamic imports
https://github.com/logotip4ik/unplugin-ltsdi

dynamic-import rollup tree-shake vite

Last synced: about 1 month ago
JSON representation

"Lazy" Tree shake dynamic imports

Awesome Lists containing this project

README

        

# unplugin-ltsdi

[Rollup still does not tree shake dynamic imports](https://github.com/rollup/rollup/issues/3447) (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,\")`

This will create file which exports what you specified in `only` param, like this:

```js
export { wrap, expose } from 'comlink'
```

On build rollup will tree shake that "file" and dynamic import won't weight as much as whole library

## Install

```bash
npm i unplugin-ltsdi
```

Vite

```ts
// vite.config.ts
import LTSDI from 'unplugin-ltsdi/vite'

export default defineConfig({
plugins: [
LTSDI({ /* options */ }),
],
})
```

Example: [`playground/`](./playground/)


Rollup

```ts
// rollup.config.js
import LTSDI from 'unplugin-ltsdi/rollup'

export default {
plugins: [
LTSDI({ /* options */ }),
],
}
```


Webpack

```ts
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('unplugin-ltsdi/webpack')({ /* options */ })
]
}
```


Nuxt

```ts
// nuxt.config.js
export default {
buildModules: [
['unplugin-ltsdi/nuxt', { /* options */ }],
],
}
```

> This module works for both Nuxt 2 and [Nuxt Vite](https://github.com/nuxt/vite)


Vue CLI

```ts
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-ltsdi/webpack')({ /* options */ }),
],
},
}
```


esbuild

```ts
// esbuild.config.js
import { build } from 'esbuild'
import LTSDI from 'unplugin-ltsdi/esbuild'

build({
plugins: [LTSDI()],
})
```


## Typescript / Intellisense

Create a `.d.ts` shim for `?` import like this

```typescript
// index.d.ts - for example

declare module 'comlink?*' {
import comlink from 'comlink'
export = comlink
}
```