My goal is to streamline the process of adopting new features or releases by removing Laravel Mix from my project. I want to avoid using Mix's syntax or plugins and eliminate the need to contain everything within the webpackConfig
method.
I have successfully created a Webpack configuration for styles and assets. However, I am facing an issue with the vue-loader
not importing styles from Vue single file components in a static .html
file. When I use the html-webpack-plugin
, everything functions correctly. But when I include the .js bundle in a static .html
file, the styles fail to load. My aim is to get it working in a static .html
file to replicate the behavior of a PHP blade file.
Below is a simplified version of my setup:
webpack.config.js
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { VueLoaderPlugin } = require("vue-loader");
const htmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: {
main: "./src/main.js",
},
output: {
filename: 'js/[name].js',
path: path.resolve(__dirname, 'dist')
},
resolve: {
extensions: ['.js', '.vue'],
alias: {
"vue$": "vue/dist/vue.common.js"
}
},
module: {
rules: [
{
test: /\.((c|sa|sc)ss)$/i,
use: [
'vue-style-loader',
MiniCssExtractPlugin.loader,
'css-loader', 'postcss-loader', 'sass-loader'
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
new htmlWebpackPlugin({
template: path.resolve(__dirname, "public", "index.html")
}),
new CleanWebpackPlugin({
cleanStaleWebpackAssets: false,
verbose: true
}),
new VueLoaderPlugin()
],
};
main.js
import Vue from "vue";
import App from "./App.vue";
new Vue({
render: (h) => h(App),
}).$mount("#app");
App.vue
<template>
<div id="app">
<div class="nav">
Test nav
</div>
</div>
</template>
<style lang="scss">
.nav {
padding: 30px 0 100px 0;
font-family: sans-serif;
}
</style>
index.html
(used to generate another index.html
with html-webpack-plugin
)
👉 with this one everything works
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Vue app</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
index.html (static file)
👉 this one doesn't load the .css
files
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<body>
<div id="app"></div>
<script src="dist/js/main.js"></script>
</body>
</html>
What I've tried
- Not using
html-webpack-plugin
during the build (no change) - Using a different alias for $vue: (
) (no change)vue$: "vue/dist/vue.runtime.esm.js"
- Not using
mini-css-extract-plugin
(the css wouldn't load even with thehtml-webpack-plugin
)
In Laravel Mix, styles are loaded within <style>
tags and not in separate .css
files, which is not a problem for me.