Skip to content

Instantly share code, notes, and snippets.

@M-Porter
Last active January 22, 2023 02:08
Show Gist options
  • Save M-Porter/b2d2649f4c88c795616aad45375eff1b to your computer and use it in GitHub Desktop.
Save M-Porter/b2d2649f4c88c795616aad45375eff1b to your computer and use it in GitHub Desktop.
Vue3 Webpack Config
const path = require('path');
const webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/dist/plugin').default;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const isProd = process.env.NODE_ENV === 'production';
const resolvePath = (...args) => path.resolve(path.join(__dirname, ...args));
module.exports = {
entry: {
main: {
import: './frontend/main.js',
dependOn: 'vendor',
},
vendor: ['vue'],
},
output: {
filename: isProd ? '[name].[contenthash].bundle.js' : '[name].bundle.js',
path: resolvePath('public', 'assets'),
publicPath: '/assets/',
},
devtool: isProd ? 'source-map' : 'cheap-module-source-map',
resolve: {
alias: {
'@': resolvePath('frontend'),
'vue$': 'vue/dist/vue.runtime.esm-bundler.js',
},
extensions: [
'.js',
'.json',
'.vue',
],
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: file => (
/node_modules/.test(file) &&
!/\.vue\.js/.test(file)
)
},
{
test: /\.css$/,
use: [
isProd ? MiniCssExtractPlugin.loader : 'vue-style-loader',
{
loader: 'css-loader',
options: { importLoaders: 1 },
},
'postcss-loader',
],
}
],
},
plugins: [
new VueLoaderPlugin(),
new MiniCssExtractPlugin({
filename: isProd ? '[name].[contenthash].bundle.css' : '[name].bundle.css',
}),
new webpack.DefinePlugin({
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
}),
new ManifestPlugin({
writeToFileEmit: true,
}),
],
};
@M-Porter
Copy link
Author

This webpack config kinda assumes the structure where you want to output your assets to a /public/assets dir relative to your webpack config (e.g. you're working with a framework like laravel or rails)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment