Build Vue Components with ESBuild

Last updated October 1, 2021 by Jacob Paris

Simple projects may be able to compile modules using the command line, but if you're trying do anything complicated enough to need plugins, you need to use the ESBuild node modules instead.

Create a file named build.js and compile by running the file with node build.js

Single entry point

With multiple entry points, you must specify an output directory

1const ESBuild = require('esbuild')
2const vue = require('esbuild-vue')
3
4ESBuild.build({
5 entryPoints: ['./src/stories/DevToolbar.vue'],
6 bundle: true,
7 outfile: 'lib/index.vue',
8 plugins: [vue()],
9 define: {
10 'process.env.NODE_ENV': JSON.stringify(
11 'production',
12 ),
13 },
14})
15

Multiple entry points

With multiple entry points, you must specify an output directory

1const ESBuild = require('esbuild')
2const vue = require('esbuild-vue')
3
4ESBuild.build({
5 entryPoints: [
6 './src/stories/DevToolbar.vue',
7 './src/stories/Dropdown.vue',
8 './src/stories/HostItem.vue',
9 './src/stories/Item.vue',
10 './src/stories/Button.vue',
11 ],
12 bundle: true,
13 outdir: 'lib',
14 plugins: [vue()],
15 define: {
16 'process.env.NODE_ENV': JSON.stringify(
17 'production',
18 ),
19 },
20})
21