Skip to content

Commit 0849008

Browse files
committed
feat(vite): use vite to bundle library
1 parent 3e6d622 commit 0849008

File tree

3 files changed

+496
-4255
lines changed

3 files changed

+496
-4255
lines changed

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"version": "3.0.4",
2727
"scripts": {
2828
"dev": "vite",
29-
"build": "vite build",
29+
"build": "yarn clean && vite build && yarn build:min",
30+
"build:min": "vite build -- --minify",
31+
"clean": "rm -rf dist",
3032
"serve": "vite preview",
3133
"test": "run-s test:unit test:e2e",
3234
"test:unit": "vite test:unit",
@@ -56,13 +58,12 @@
5658
"@types/jest": "24.9.1",
5759
"@typescript-eslint/eslint-plugin": "5.20.0",
5860
"@typescript-eslint/parser": "5.20.0",
61+
"@vitejs/plugin-vue": "^1.6.1",
5962
"@vue/compiler-sfc": "3.2.47",
6063
"@vue/eslint-config-prettier": "6.0.0",
6164
"@vue/eslint-config-typescript": "10.0.0",
6265
"@vue/test-utils": "2.3.2",
6366
"@vue/vue3-jest": "27.0.0",
64-
"@vitejs/plugin-vue": "^1.6.1",
65-
"vite": "^2.5.4",
6667
"babel-jest": "27.5.1",
6768
"babel-loader": "8.3.0",
6869
"concurrently": "^7.6.0",
@@ -78,9 +79,11 @@
7879
"read-pkg": "5.2.0",
7980
"rollup": "2.79.1",
8081
"rollup-plugin-babel": "4.4.0",
82+
"rollup-plugin-license": "^3.0.1",
8183
"rollup-plugin-vue": "6.0.0",
8284
"ts-jest": "27.1.5",
8385
"typescript": "4.2.4",
86+
"vite": "^2.5.4",
8487
"vue": "3.2.47",
8588
"vue-template-compiler": "2.7.14",
8689
"yarn-run-all": "3.1.1"

vite.config.js

+57
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,66 @@
11
import { defineConfig } from 'vite';
22
import vue from '@vitejs/plugin-vue';
3+
import license from 'rollup-plugin-license';
34

45
const path = require('path');
6+
const minify = process.argv.includes('--minify');
7+
8+
// Change build process based on whether we're building a minified version.
9+
const libConfig = {
10+
minified: {
11+
formats: ['es'],
12+
entry: path.resolve(__dirname, 'src/main.ts'),
13+
name: 'imgix-vue',
14+
fileName: () => 'imgix-vue.min.js',
15+
},
16+
default: {
17+
formats: ['es', 'umd'],
18+
entry: path.resolve(__dirname, 'src/main.ts'),
19+
name: 'imgix-vue',
20+
fileName: (format) => {
21+
if (format === 'es') {
22+
return `imgix-vue.esm.js`;
23+
}
24+
return `imgix-vue.${format}.js`;
25+
},
26+
},
27+
};
28+
29+
const config = minify ? libConfig.minified : libConfig.default;
30+
531
export default defineConfig({
632
plugins: [vue()],
33+
esbuild: {
34+
legalComments: 'none', // Preserve all legal comments.
35+
banner: '/*! licenses: /vendor.LICENSE.txt */',
36+
},
37+
build: {
38+
// Don't delete the outDir between builds.
39+
// NPM script deletes outDir before building.
40+
emptyOutDir: false,
41+
minify: minify,
42+
target: 'es2018',
43+
lib: {
44+
...config,
45+
},
46+
rollupOptions: {
47+
external: ['vue'],
48+
output: {
49+
// esbuild strips comments, so we need to add a banner to the output in
50+
// order to preserve the licenses.
51+
plugins: [
52+
license({
53+
thirdParty: {
54+
output: path.resolve(__dirname, './dist/vendor.LICENSE.txt'),
55+
},
56+
}),
57+
],
58+
globals: {
59+
vue: 'Vue',
60+
},
61+
},
62+
},
63+
},
764
resolve: {
865
alias: {
966
'@': path.resolve(__dirname, './src'),

0 commit comments

Comments
 (0)