css files not being copied to dist folder webpack5 - sass

I am getting the following error, along with broken css being applied to my webpage.
This at first glance looks like an issue with css, however this file doesn't exist at ALL in the dist folder that gets generated when I run npm run build:development
Refused to apply style from 'http://localhost:3457/dist/main.css' because its MIME type
('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled
these are the run scripts in my package.json
"scripts": {
"start": "cross-env BABEL_ENV=development webpack-dev-server --progress --config conf/webpack.config.development.js",
"build:server": "cross-env BABEL_ENV=development webpack-dev-server --progress --config conf/webpack.config.development.js",
"build:development": "cross-env BABEL_ENV=development webpack --progress --config conf/webpack.config.development.js",
"build:staging": "cross-env BABEL_ENV=production webpack --progress --config conf/webpack.config.staging.js",
"build:production": "cross-env BABEL_ENV=production webpack --progress --config conf/webpack.config.production.js",
"clean": "rimraf dist",
"lint": "eslint . --ext=jsx --ext=js || true",
"lint:styles": "stylelint \"scss/*.scss\" --syntax scss || true"
},
and this is my webpack.config.development.js file that is supposed to configure the webpack, supposedly.
const webpack = require('webpack');
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const mainPackageJson = require('./../package.json');
// File locations
const PATHS = {
js: path.resolve(__dirname, '../js'),
dist: path.resolve(__dirname, '../dist'),
modules: [
path.resolve(__dirname, '../node_modules/cleave.js')
]
};
exports = module.exports = {
mode: 'development',
stats: {
errorDetails: true
},
entry: [
'babel-polyfill',
'webpack-dev-server/client?http://localhost:3457',
'webpack/hot/only-dev-server',
'./js/index'
],
output: {
path: PATHS.dist,
filename: '[name].js',
publicPath: '/dist'
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {name: 'vendor', filename: 'vendor.js'}
}
}
},
resolve: {
extensions: ['.js', '.jsx']
},
devtool: 'eval-source-map',
module: {
noParse: /node_modules\/quill\/dist\/quill.js/,
rules: [
{
test: /\.gif$/,
type: 'asset/inline'
// loader: 'url-loader',
// query: {
// mimetype: 'image/png'
// }
},
{
test: /\.woff(2)?(\?v=[0-9].[0-9].[0-9])?$/,
type: 'asset/inline'
// loader: 'url-loader',
// query: {
// mimetype: 'application/font-woff'
// }
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9].[0-9].[0-9])?$/,
type: 'asset/resource'
// loader: 'file-loader',
// query: {
// name: '[name].[ext]'
// }
},
{
test: /\.(sa|sc|c)ss$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: PATHS.dist
}
},
"css-loader",
"postcss-loader",
"sass-loader",
],
},
{
test: /\.jsx?$/,
rules: [{loader: 'react-hot-loader/webpack'}, {loader: 'babel-loader'}],
include: [
PATHS.js,
...PATHS.modules
]
}
]
},
plugins: [
// new webpack.optimize.CommonsChunkPlugin({name: 'vendor', filename: 'vendor.js'}),
require('autoprefixer'),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
// new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
__CONTENT__: JSON.stringify('#content'),
__DEV__: true,
__NOOP__: function () {
},
__VERSION__: JSON.stringify(mainPackageJson.version)
}),
new CopyPlugin(
{
patterns: [{
from: path.resolve(__dirname, '..', 'conf.js'),
to: 'conf.js'
}]
}
),
],
devServer: {
static: path.resolve(__dirname, '..'),
historyApiFallback: true,
hot: true,
host: 'localhost',
port: 3457
}
};
Perhaps I am misunderstanding something.
I am attempting to update the application from webpack3 to webpack5, among other outdated dependencies.
What is wrong?, why isn't a main.css file being generated or copied into the dist folder for it to be accessed?
When I had (which I deleted) files from an older dist folder built, it appeared to work correctly.

Related

Webpack add query string to bundle for cache-busting

so in my index.html, I have this line to include the webpack bundle
<script src="/dist/bundle.js"></script>
And my webpack config looks like this:
const path = require("path");
const webpack = require("webpack");
module.exports = {
entry: "./src/main.js",
mode: "development",
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: "babel-loader",
options: { presets: ["#babel/env"] }
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
resolve: { extensions: ["*", ".js", ".jsx"] },
output: {
path: path.resolve(__dirname, "dist/"),
publicPath: "/dist/",
filename: "bundle.js"
},
devServer: {
contentBase: path.join(__dirname, "public/"),
port: 3000,
publicPath: "http://localhost:3000/dist/",
hotOnly: true
},
plugins: [new webpack.HotModuleReplacementPlugin()]
};
How could I modify this, to add a query string (?v=some_nr) to the bundle when I run webpack --mode production. I can copy the index.html to dist/index.html or some other location, if needed, I'll be moving it to the webroot automatically anyway afterwards.

Browser debugger not working with Webpack 2 and webpack-dev-server

I've spent last few hours to set up a new project utilizing Webpack 2 and Babel 6.
The problem I have is non working debugger (any browser). Breakpoints are simply ignored. Using debugger; works fine but browser does not breaks execution exactly in that line.
webpack.config.js:
const path = require("path");
module.exports = {
entry: path.resolve(__dirname, "src", "index.js"),
output: {
filename: "app.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
}]
},
devServer: {
contentBase: path.resolve(__dirname, "dist")
}
};
.babel.rc:
{
"presets": [
"latest"
]
}
I'm running the whole thing with webpack-dev-server -d
devtool: "source-map", option in webpack config should solve your problem.
entry: ['babel-polyfill', './src/index.jsx'],
output: {
filename: 'js/bundle.[hash].js',
path: path.resolve(__dirname, 'build'),
publicPath: '/'
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {
config: path.join(__dirname, `config/${env}.configuration.js`)
}
},
**devtool: "source-map",**
module: {
rules: [
{
test: require.resolve('jquery'),
use: [
{
loader: 'expose-loader',
options: 'jQuery'
}
]
},

Webpack Config did not export an object

WEBPACK # 2.2
WEBPACK-MERGE # 2.4
I am using webpack merge to do a smart dev or production config.
My start script looks like
}
"scripts": {
"start": "webpack --env=production & node start.js",
"dev": "webpack-dev-server --env=dev",
},
and my webpack-config looks like this:
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const webpackMerge = require('webpack-merge')
const baseConfig = function(env) {
return {
output: {
path: '/public/',
filename: 'index.js',
publicPath: '/public/',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallbackLoader: "style-loader",
loader: "css-loader",
publicPath: "/public/",
}),
},
],
},
resolve: {
extensions: ['.js', '.css'],
},
plugins: [
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(env) },
}),
new ExtractTextPlugin({
filename: "bundle.css",
disable: false,
allChunks: true,
}),
],
}
}
module.exports = function(env) {
return webpackMerge(baseConfig(env), env === 'dev' ? {
devtool: 'cheap-module-source-map',
entry: [
'react-hot-loader/patch',
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./app/index.js',
],
devServer: {
hot: true,
publicPath: '/public/',
proxy: {
"/api/**": "http://localhost:3333",
"/auth/**": "http://localhost:3333",
},
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
],
} : {
devtool: 'inline-source-map',
entry: [
'./app/index.js',
],
plugins: [
new webpack.optimize.UglifyJsPlugin({
comments: false,
}),
new webpack.LoaderOptionsPlugin({
minimize: true,
}),
new webpack.optimize.AggressiveMergingPlugin(),
new CompressionPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8,
}),
],
})
}
Webpack successfully compiles locally but when I attempt to deploy it to heroku, the output in papertrail is as seen below:
> webpack --env=production & node start.js
Config did not export an object.
Any ideas?
I had the same issue, I forgot to install webpack dev server:
npm install --save-dev webpack-dev-server
Hope it helps!

Webpack dev server live reload with proxy

I'm running a PHP app at localhost:8000.
I want to use webpack-dev-server to hot reload css, react and js components.
Had set a proxy to http://localhost:8000 but webpack-dev-server isn't reloading the browser.
Here's the webpack.config.js:
var path = require( 'path' );
var autoprefixer = require( 'autoprefixer' );
module.exports = {
entry: [
'./src/app.js'
],
output: {
path: path.join( __dirname, 'dist' ),
publicPath: 'http://localhost:8000',
filename: 'app.js'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: [ 'react-hot', 'babel-loader' ]
},
{
test: /\.scss$/,
loaders: [ 'style-loader', 'css-loader', 'sass-loader' ]
}
]
},
postcss: [
autoprefixer(
{
browsers: [
'last 2 versions'
]
}
)
],
devServer: {
port: 3000,
proxy: {
'**': {
target: 'http://localhost:8000',
secure: false,
changeOrigin: true
}
}
}
}
I'm accessing the webpack-dev-server at http://localhost:3000/webpack-dev-server/.
Changing my react component does cause webpack-dev-server to recompile, but the browser doesn't update.
Running webpack does compile the dist/app.js file, as calling it manually and reloading the browser works.
So my publicPath was wrong.
Here's the fix:
output: {
path: path.join( __dirname, 'dist' ),
publicPath: 'http://localhost:3000/dist/',
filename: 'app.js'
},
update: But it seems to be reloading the browser ¯_(ツ)_/¯

Webpack, "hot-reload" or "live-reload" sass?

Is there a way to get webpack to "recompile" sass and reload your page on the fly?
I've been searching around but have not found an example.
Here is what I've got:
My folder structure
-app
---public
------dist
---------index.html
---------sass
------------webpack.scss
It seems like my sass only recompiles when I run webpack but it doesn't recompile when I am running the webpack-dev-server
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require('webpack');
module.exports = {
entry: {
app: './app/main.js',
//All 3rd party source
vendor: ['react', 'redux', 'lodash']
},
output: {
path: path.join(__dirname, 'app', 'public', 'dist'),
publicPath: '/app/public/dist/',
filename: 'app.js'
},
module: {
loaders: [
{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/ },
{ test: /.jsx?$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.css/, loader: ExtractTextPlugin.extract('css') },
{ test: /\.scss$/, loader: ExtractTextPlugin.extract('!css!sass?sourceMap') }
]
},
sassLoader: {
includePaths: [ path.resolve(__dirname, './app/public/sass/')]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.optimize.CommonsChunkPlugin("vendor", "vendor.js", Infinity),
new ExtractTextPlugin('./css/style.css',{
allChunks: true
})
],
debug: true,
//watch: true
};

Resources