Webpack 3 Hot Reload Very Slow - javascript

Info: I am using webpack 3.4.1 with webpack-dev-server 2.5.1
Using the below configuration with '--hot --inline', I get a very slow hot reload. I can remove '--hot' and have very fast reload, but I would prefer to change css on the fly, using hot reload.
common.js
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const { resolvePath } = require('./helpers');
const env = process.env.NODE_ENV;
const cssLoader = {
test: /\.css$/,
loader: ExtractTextPlugin.extract('css-loader?sourceMap')
};
const lessLoader = {
test: /\.less$/,
loaders: ['style-loader', 'css-loader', 'less-loader']
}
const babelLoader = {
test: /\.js?$/,
include: [
resolvePath('src/app'),
],
exclude: /(node_modules)/,
loader: 'babel-loader',
};
const htmlLoader = {
test: /\.html$/,
loader: 'ngtemplate-loader?relativeTo=' + resolvePath('src') + '/!html-loader'
};
const fontLoader = {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
};
const fileLoader = {
test: /\.(ttf|eot|svg|otf|png|jpg|gif|ico)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
};
const jsonLoader = {
test: /\.json$/,
loader: 'json-loader'
};
const tinyMCELoader = {
test: require.resolve('tinymce/tinymce'),
loaders: [
'imports-loader?this=>window',
'exports-loader?window.tinymce'
]
}
const tinyMCEThemeLoader = {
test: /tinymce\/(themes|plugins)\//,
loader: 'imports-loader?this=>window'
}
const config = {
devtool: 'inline-source-map',
entry: {
vendor: resolvePath('src/app/vendor.js'),
app: resolvePath('src/app/app.js')
},
target: 'web',
output: {
path: resolvePath('public'),
publicPath: '/',
filename: '[name].js'
},
module: {
loaders: [
babelLoader,
lessLoader,
htmlLoader,
cssLoader,
fontLoader,
fileLoader,
jsonLoader,
tinyMCELoader,
tinyMCEThemeLoader
]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
}),
new ExtractTextPlugin('[name].[contenthash].css'),
new htmlWebpackPlugin({
template: resolvePath('public/index.ejs'),
favicon: resolvePath('src/favicon.ico'),
inject: true,
env
})
],
resolve: {
alias: {
api: resolvePath('src/app/api/apiUrl.js')
}
}
};
module.exports = config;
and dev.js
const webpackMerge = require('webpack-merge');
const commonConfig = require('./common.js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { resolvePath } = require('./helpers');
const { HOST, PORT } = process.env;
const port = PORT || 1337;
const host = HOST || 'localhost';
const devConfig = {
devtool: 'cheap-module-source-map',
devServer: {
contentBase: resolvePath('public'),
port,
host,
historyApiFallback: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
},
plugins: [
new UglifyJsPlugin({
include: ['vendor.js']
})
]
};
module.exports = () => webpackMerge(commonConfig, devConfig);
I tried removing the Extract-Text-Plugin, but to no avail. Any ideas on to
make hot reload fast? I was thinking of disableing the compiling of vendor.js, but I don't know here to add that. Thanks.

Related

webpack Module not found issue

I'm new in Webpack (before I used Gulp) and now I want to move the existing application from Gulp to Webpack.
But I'm getting issues like Module not found: Error: Can't resolve '/src/app/index.js' in '/var/www/project' or Module not found: Error: Can't resolve '/src/styles/main.scss' in '/var/www/project' and same for every file i'm using on my enty chain.
Here is my file structures:
package.json
.env
.env.example
webpack.conf.js
src/
app/
index.js
other js/vue related files
styles/
main.scss and all related styles
public/
index.html
favicon.ico
images/
all images
and here is mine webpack.conf.js:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const Dotenv = require('dotenv-webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const ENV = process.env.APP_ENV;
const isDev = ENV === 'dev'
const isProd = ENV === 'prod';
function setDevTool() {
if (isDev) {
return 'inline-source-map';
} else if (isProd) {
return 'source-map';
} else {
return 'eval-source-map';
}
}
const config = {
entry: {
'bundle.min.css': [
path.resolve(__dirname, '/src/styles/main.scss'),
],
'bundle.js': [
path.resolve(__dirname, '/src/app/index.js')
]
},
output: {
filename: '[name]',
path: path.resolve(__dirname, 'dist'),
},
devtool: setDevTool(),
module: {
rules: [
// fonts loader
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
},
// babel loader
{
test: /\.js$/,
use: 'babel-loader',
exclude: [
/node_modules/
]
},
// raw html loader
{
test: /\.html/,
loader: 'raw-loader'
},
// css loader
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
},
// sass loader
{
test: /\.(sass|scss)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['style-loader', 'css-loader', 'sass-loader']
})
},
// vue loader
{
test: /\.vue$/,
loader: 'vue-loader'
},
// image url inliner
{
test: /\.(jpe?g|png|gif)$/,
loader: 'url-loader',
options: {
limit: 50 * 1024
}
},
// svg url inliner
{
test: /\.svg$/,
loader: 'svg-url-loader',
options: {
limit: 50 * 1024,
noquotes: true,
}
},
// image loader
{
test: /\.(jpg|png|gif|svg)$/,
loader: 'image-webpack-loader',
enforce: 'pre'
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: __dirname + "/src/public/index.html",
inject: 'body'
}),
new Dotenv({safe: true}),
new ExtractTextPlugin("bundle.min.css"),
new VueLoaderPlugin()
],
devServer: {
contentBase: './src',
port: 7700,
}
}
if (isProd) {
config.plugins.push(
new UglifyJSPlugin(),
new CopyWebpackPlugin([{
from: __dirname + '/src/public'
}])
);
};
module.exports = config;
I do not understand why it can't find those files if for examlpe '/var/www/project//src/styles/main.scss' it mention in error is correct path?
Try to remove the initial slashes from path.resolve(). For example:
path.resolve(__dirname, '/src/styles/main.scss')
should be:
path.resolve(__dirname, 'src/styles/main.scss')

404 not found ( webpack image )

How can I import an image using this web pack config?
I get the following error in the console after I build the app using yarn build. How can I import the image to the dashboard file from the assets/public folder? (please see image). Thanks in advance.
this the webpack config file.
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const extractCSS = new ExtractTextPlugin('[name].fonts.css');
const extractSCSS = new ExtractTextPlugin('[name].styles.css');
const BUILD_DIR = path.resolve(__dirname, 'build');
const SRC_DIR = path.resolve(__dirname, 'src');
console.log('BUILD_DIR', BUILD_DIR);
console.log('SRC_DIR', SRC_DIR);
module.exports = (env = {}) => {
return {
entry: {
index: [SRC_DIR + '/index.js']
},
output: {
path: BUILD_DIR,
filename: '[name].bundle.js',
},
// watch: true,
devtool: env.prod ? 'source-map' : 'cheap-module-eval-source-map',
devServer: {
contentBase: BUILD_DIR,
// port: 9001,
compress: true,
hot: true,
open: true
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: ['react', 'env']
}
}
},
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.(scss)$/,
use: ['css-hot-loader'].concat(extractSCSS.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {alias: {'../img': '../public/img'}}
},
{
loader: 'sass-loader'
}
]
}))
},
{
test: /\.css$/,
use: extractCSS.extract({
fallback: 'style-loader',
use: 'css-loader'
})
},
{
test: /\.(png|jpg|jpeg|gif|ico)$/,
use: [
{
// loader: 'url-loader'
loader: 'file-loader',
options: {
name: './img/[name].[hash].[ext]'
}
}
]
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader',
options: {
name: './fonts/[name].[hash].[ext]'
}
}]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.UglifyJsPlugin({sourceMap: true}),
new webpack.NamedModulesPlugin(),
extractCSS,
extractSCSS,
new HtmlWebpackPlugin(
{
inject: true,
template: './public/index.html'
}
),
new CopyWebpackPlugin([
{from: './public/img', to: 'img'}
],
{copyUnmodified: false}
)
]
}
};
Please see images below.
What Am I missing here?
Thanks in advance.
first of all , you should import your image like :
import MyImage from '../relative/path/to/your/image'
then use it like:
function App() {
return (
<div className="App">
<img src={MyImage} alt='any kind of description'>
</div>
);
}

CSS don't work in production using webpack 2

I'm workign in a react app using webpack 2 to bundle JS files.
I'm trying to deploy this app in a production enviroment now, but when i deploy the static content in NGinx the CSS doesn't load.
This works fine in dev enviroment. Somone could helpe to solve this problem?
webpack.common.js
/* eslint-disable */
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
/* eslint-enable */
const ExtractNormalCSS = new ExtractTextPlugin("app.css")
const ExtractCriticalCSS = new ExtractTextPlugin("styles/style_crit.css")
module.exports = () => {
return {
entry: ['./src/main/webapp/app/index'],
node: {
fs: "empty"
},
resolve: {
extensions: [
'.js', '.jsx', '.json'
],
modules: ['node_modules']
},
module: {
rules: [
{
test: /\.json/,
loaders: ['json-loader']
},
{
test: /\.css/i,
exclude: /\.crit.css/,
loaders: ExtractNormalCSS.extract({ fallback: 'style-loader', use: 'css-loader' })
},
{
test: /\.crit.css/i,
loaders: ExtractCriticalCSS.extract({ fallback: 'style-loader', use: 'css-loader' })
},
{
test: /\.scss/i,
exclude: /\.crit.css/,
loaders: ExtractNormalCSS.extract({ fallback: 'style-loader', use: ['css-loader', 'postcss-loader', 'sass-loader'] })
},
{
test: /\.mp4'$/,
loader: 'url-loader?limit=100000'
},
{
test: /\.(jpe?g|png|gif|svg|woff|woff2|ttf|eot)$/i,
loaders: [
'file-loader?hash=sha512&digest=hex&name=[hash].[ext]', {
loader: 'image-webpack-loader',
query: {
gifsicle: {
interlaced: false
},
optipng: {
optimizationLevel: 7
}
}
}
]
}
]
},
plugins: [
new CopyWebpackPlugin([
{
from: './node_modules/swagger-ui/dist',
to: 'swagger-ui/dist'
}, {
from: './src/main/webapp/swagger-ui/',
to: 'swagger-ui'
}, {
from: './src/main/webapp/static/',
to: 'static'
}, {
from: './src/main/webapp/favicon.ico',
to: 'favicon.ico'
}, {
from: './src/main/webapp/robots.txt',
to: 'robots.txt'
}
]),
new HtmlWebpackPlugin({
template: './src/main/webapp/index.html',
chunksSortMode: 'dependency',
inject: 'body'
}),
//new ExtractTextPlugin('styles.css'),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'window.$': 'jquery'
}),
ExtractNormalCSS,
ExtractCriticalCSS
]
};
};
webpack.prod.js
/* eslint-disable */
const path = require('path');
const webpack = require('webpack');
// const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpackMerge = require('webpack-merge');
const commonConfig = require('./webpack.common.js');
/* eslint-enable */
module.exports = webpackMerge(commonConfig(), {
devtool: 'source-map',
output: {
path: path.resolve('./target/www'),
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
minChunks(module) {
return (module.resource && module.resource.indexOf(path.resolve('node_modules')) === 0);
}
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
// this conflicts with -p option
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
})
],
module: {
rules: [
{
enforce: 'pre',
test: /\.css$/,
loader: 'stripcomment-loader'
},
{
test: /\.js[x]?$/,
loaders: ['react-hot-loader', 'babel-loader?cacheDirectory'],
exclude: /node_modules/,
include: path.resolve('./src/main/webapp/app')
}
]
}
});
webpack.dev.js
/* eslint-disable */
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpackMerge = require('webpack-merge');
const commonConfig = require('./webpack.common.js');
/* eslint-enable */
module.exports = webpackMerge(commonConfig(), {
devtool: 'inline-source-map',
output: {
path: path.resolve('./target/www'),
filename: 'app.js'
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development')
}
})
],
module: {
rules: [
{
test: /\.js[x]?$/,
loaders: ['react-hot-loader', 'babel-loader?cacheDirectory'],
exclude: /node_modules/,
include: path.resolve('./src/main/webapp/app')
}
]
},
devServer: {
contentBase: './target/www',
proxy: [
{
context: [
'/api', '/management', '/swagger-resources', '/v2/api-docs', '/h2-console'
],
target: 'http://127.0.0.1:8080/cond21cloud',
secure: false
}, {
context: ['/websocket'],
target: 'ws://l27.0.0.1:8080/cond21cloud',
ws: true
}
]
}
});

Can the PurifyCss plugin be forced to wait until the bundle is ready?

My first "Hello Webpack" project was going great until I started integrating purifycss-webpack. If I delete my /dist folder manually and then build, this config works. But if the /dist folder exists and CleanWebpackPlugin deletes it as its designed to do, then PurifyCSS throws this error:
clean-webpack-plugin: \dist has been removed.
purifycss-webpack\dist\index.js:52 \ if (!_fs2.default.existsSync(p))
throw new Error('Path ' + p + ' does not exist.');
Is there another way to have Webpack run PurifyCSS only AFTER all the other plugins have executed? I thought I was doing that by placing it last in the plugins array, but this feels like execution order issues to me. Here's my config:
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const globAll = require('glob-all');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const PurifyCSSPlugin = require('purifycss-webpack');
const webpack = require("webpack");
var extractPluginCSS = new ExtractTextPlugin({
filename: "app.css"
});
module.exports = {
entry: "./src/js/app.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "app.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["env"]
}
}
]
},
{
test: /app\.scss$/,
use: extractPluginCSS.extract({
use: [
{
loader: "css-loader",
options: {}
},
{
loader: "sass-loader",
options: {}
}
]
})
},
{
test: /\.html$/,
use: ["html-loader"]
},
{
test: /\.(jpg|png)$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
outputPath: "img/"
}
}
]
}
]
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
template: "./src/html/index.html"
}),
new webpack.optimize.UglifyJsPlugin(),
extractPluginCSS,
new PurifyCSSPlugin({
// Give paths to parse for rules. These should be absolute!
paths: globAll.sync([
path.join(__dirname, '/dist/*.html')
]),
// minimize: true,
verbose: true
})
]
};

Webpack - devtool: source-map for CSS and eval-source-map for JS?

If I use devtool: 'source-map' it works great with CSS:
But, my JavaScript variable names are no fun:
So, if I use devtool: eval-source-maps. Life is great - debugging JS. But my CSS then points to the big bundle-css instead of the separate files:
How do I have my cake and eat it too?!
I would like to use eval-source-map for JS and source-map for CSS during the same build.
This is my webpack config (using the latest of everything as of this writing):
/* eslint no-console:"off" */
const {resolve} = require('path');
const webpack = require('webpack');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineManifestWebpackPlugin = require('inline-manifest-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// const OfflinePlugin = require('offline-plugin/runtime').install();
module.exports = (env) => {
const {ifProd, ifNotProd} = getIfUtils(env);
const config = {
context: resolve('src'),
entry: './js/index/index.js',
output: {
filename: 'bundle.[name].[hash].js',
path: resolve('dist'),
pathinfo: ifNotProd()
},
// devtool: 'source-map' //This works for CSS but not JS,
devtool: 'eval-source-map' //This works for JS but not css
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules)/
},
{
test: /\.js$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {importLoaders: 1, minimize: false, sourceMap: true}
}
]
})
},
{
test: /\.html$/,
use: {
loader: 'html-loader'
}
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff',
query: {
name: 'static/media/files/[name].[hash:8].[ext]'
}
}, {
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader',
query: {
name: 'static/media/fonts/[name].[hash:8].[ext]'
}
},
{
test: /\.(gif|jpe?g|png)$/,
loader: 'url-loader?limit=25000',
query: {
limit: 10000,
name: 'static/media/images/[name].[hash:8].[ext]'
}
}
]
},
plugins: removeEmpty([
// ifProd(new InlineManifestWebpackPlugin()),
// ifProd(new webpack.optimize.CommonsChunkPlugin({
// names: ['manifest']
// })),
new HtmlWebpackPlugin({
template: './index.html'
// inject: 'head'
}),
// new OfflinePlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: ifProd('"production"', '"development"')
}
}),
new UglifyJSPlugin({
parallel: {
cache: true
},
sourceMap: true
}
),
new OptimizeCssAssetsPlugin({
cssProcessorOptions: {
preset: 'default',
map: {inline: false}
}
}),
new ExtractTextPlugin('styles.[name].[hash].css'),
new BundleAnalyzerPlugin(),
new ProgressBarPlugin(),
])
};
if (env.debug) {
console.log(config);
debugger // eslint-disable-line
}
return config;
};
This seems to work:
{
...webpackConfig,
devtool: false,
plugins: [
new webpack.SourceMapDevToolPlugin({
test: /\.s?[ac]ss$/
}),
new webpack.EvalSourceMapDevToolPlugin({
test: /\.(vue|[jt]sx?)$/
}),
]
}
This will give you inline-source-map for css, scss and sass and eval-source-map for vue, js, jsx, ts and tsx

Categories

Resources