So as it says in the title, when bundling Vue 3 using the composition API with webpack, the bundle file is very large. So first off some information about my setup:
webpack.config.js
const path = require("path");
const { VueLoaderPlugin } = require("vue-loader");
module.exports = (env) => {
return {
entry: "./src/index.ts",
mode: env.production ? "production" : "developent",
output: {
filename: "index.js",
path: path.resolve(__dirname, "dist"),
library: {
name: "Guide",
type: "umd",
},
clean: true,
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
appendTsSuffixTo: [/\.vue$/],
},
exclude: /node_modules/,
},
{
test: /\.vue$/,
loader: "vue-loader",
},
{
test: /\.css$/i,
use: ["style-loader", "css-loader", "postcss-loader"],
},
],
},
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
plugins: [new VueLoaderPlugin()],
};
};
My package.json has a build script which looks like this:
cross-env NODE_ENV=production webpack --env production
cross-env - a way to set environment variables cross-platform
Not sure if it's relevant, but here is my tsconfig.json file:
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "CommonJS",
"lib": ["DOM", "ES2020"],
"outDir": "dist",
"esModuleInterop": true,
"sourceMap": false,
"declaration": true,
"skipLibCheck": true
}
}
Now, after running the build script, I notice something odd in the output as seen here:
modules by path ./node_modules/@vue/ 633 KiB 3 modules
modules by path ./node_modules/vue/ 2.26 KiB 2 modules
modules by path ./node_modules/css-loader/dist/runtime/*.js 2.33 KiB 2 modules
As you can see, @vue is 633kb which is beyond crazy. And when I inspect the actual bundle file, I have all sorts of artefacts that definitely shouldn't be there. There's objects that have properties which have links to the Vue documentation. I'm clearly importing the wrong thing somewhere, but I can't figure it out.
EDIT
It seems that the vue compiler is being bundled as well which is what's causing the bloat. I can't quite figure out how to get rid of it though.