Skip to content

Commit

Permalink
feat(experimental): add optimizeDeps option
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 26, 2021
1 parent b549345 commit 671ffc3
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 398 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
// All options are optional
include: /\.[jt]sx?$/, // default, inferred from `loaders` option
exclude: /node_modules/, // default
sourceMap: false, // default
sourceMap: false, // by default inferred from rollup's `output.sourcemap` option
minify: process.env.NODE_ENV === 'production',
target: 'es2017', // default, or 'es20XX', 'esnext'
jsx: 'transform', // default, or 'preserve'
Expand Down Expand Up @@ -93,13 +93,23 @@ export default {
}
```

### Bundle mode
### Optimizing Deps

This plugin also includes an experimental `bundle` mode which lets rollup `resolve`, `load`, and `transform` imported files but leaves bundling to esbuild. In my simple test it's around 50% faster than non-bundle mode, but still 10x slower than raw esbuild.
You can use this plugin to pre-bundle dependencies using esbuild and inline them in the Rollup-generated bundle:

To enable this mode, passing `experimentalBundling: true` to the options.
```js
esbuild({
optimizeDeps: {
include: ['vue', 'vue-router'],
},
})
```

This eliminates the need of `@rollup/plugin-node-modules` and `@rollup/plugin-commonjs`.

Note that this is an **experimental features**, breaking changes might happen across minor version bump.

Current limitation: no code splitting yet.
TODO: Maybe we can scan Rollup input files to get a list of deps to optimize automatically.

## Sponsors

Expand Down
1 change: 1 addition & 0 deletions bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hyperfine "dum example:rollup-node-resolve" "dum example:esbuild-optimize-deps" --warmup 2
6 changes: 5 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import Foo from './foo'
import * as Vue from 'vue'
import React from 'react'
import * as Three from 'three'
import * as _ from 'lodash'

console.log(Foo)
export { Foo, Vue, React, Three, _ }
19 changes: 15 additions & 4 deletions example/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import path from 'path'
import nodeResolve from '@rollup/plugin-node-resolve'
import cjs from '@rollup/plugin-commonjs'

// @ts-check
const esbuild = require('../dist/index')
const optimize = !!process.env.OPTIMIZE

export default {
input: 'example/index.js',
input: path.join(__dirname, 'index.js'),
output: {
file: 'example/dist/index.js',
dir: path.join(__dirname, 'dist'),
format: 'cjs',
},
plugins: [
esbuild({
minify: !isDev,
esbuild.default({
minify: process.env.NODE_ENV === 'production',
optimizeDeps: {
include: optimize ? ['vue', 'react', 'three', 'lodash'] : [],
},
}),
!optimize && cjs(),
!optimize && nodeResolve(),
],
}
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,38 @@
"repository": "https://github.com/egoist/rollup-plugin-esbuild",
"scripts": {
"test": "jest",
"example": "npm run build && rollup -c example/rollup.config.js",
"example:rollup-node-resolve": "rollup -c example/rollup.config.js",
"example:esbuild-optimize-deps": "OPTIMIZE=true rollup -c example/rollup.config.js",
"build": "rm -rf dist && tsup src/index.ts --format esm,cjs --dts",
"prepublishOnly": "npm run build"
},
"files": [
"dist"
],
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.1",
"@types/debug": "^4.1.7",
"@types/jest": "^27.0.2",
"@types/node": "14.14.37",
"esbuild": "^0.14.8",
"istextorbinary": "^6.0.0",
"jest": "^27.3.1",
"lodash": "^4.17.21",
"prettier": "^2.4.1",
"react": "^17.0.2",
"rollup": "^2.59.0",
"three": "^0.136.0",
"ts-essentials": "^9.1.0",
"ts-jest": "^27.0.7",
"tsup": "^5.7.2",
"typescript": "^4.4.4"
"typescript": "^4.4.4",
"vue": "^3.2.26"
},
"dependencies": {
"@rollup/pluginutils": "^4.1.1",
"debug": "^4.3.3",
"es-module-lexer": "^0.9.3",
"joycon": "^3.0.1",
"jsonc-parser": "^3.0.0"
},
Expand Down
Loading

0 comments on commit 671ffc3

Please sign in to comment.