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!
Related
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.
I'm having troubles with compiling sass with Webpack. ExtractTextPlugin gives images wrong path, prepending 'css/' to it. When I change the filename in ExtractTextPlugin options to '/app.css', it puts app.css to the dist/ folder instead of css, but it gives the images correct path. How should I correctly specify the paths in config?
Here is my webpack.config.js:
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const path = require('path')
const autoprefixer = require('autoprefixer')
let isProd = process.env.NODE_ENV == 'production';
let cssDev = [
'style-loader',
'css-loader?sourseMap&importLoaders=2',
'postcss-loader',
'sass-loader'];
let cssProd = ExtractTextPlugin.extract({
allChunks: true,
fallback: "style-loader",
use: [
{
loader: 'css-loader',
options: {
minimize: false,
importLoaders: 2
}
},
{
loader: 'postcss-loader',
options: {
minimize: false
}
},
{
loader: 'sass-loader',
options: {
minimize: false
}
}
]
});
let cssConfig = isProd ? cssProd : cssDev;
module.exports | webpack.config
module.exports = {
entry: {
'js/app': './src/app.js'
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
publicPath: path.join(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
include: [ path.resolve(__dirname, 'src/js/') ],
loader: 'babel-loader',
query: {
presets: ['es2015'],
}
},
{
test: /\.sass$/,
use: cssConfig
},
{
test: /\.pug$/,
use: 'pug-loader?pretty=true'
},
{
test: /\.(jpe?g|png|gif|svg)$/,
use: [
'file-loader?name=[name].[ext]&publicPath=img/&outputPath=img/'
]
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: 'file-loader?name=[name].[ext]&publicPath=fonts/&outputPath=fonts/'
}
]
},
devServer: {
contentBase: __dirname + '/dist',
compress: true,
port: 9000,
open: true,
hot: true,
stats: 'errors-only'
},
plugins: [
new HtmlWebpackPlugin({
title: 'Product Description',
minimize: false,
favicon: false,
hash: true,
template: './src/index.pug'
}),
new HtmlWebpackPlugin({
title: 'Distribution and Channels',
minimize: false,
favicon: false,
hash: true,
filename: 'distrib.html',
template: './src/distrib.pug'
}),
new ExtractTextPlugin({
filename: '/app.css',
publicPath: '/',
disable: !isProd,
allChunks: true
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.LoaderOptionsPlugin({ options:
{ postcss: [
autoprefixer({
browsers: ['last 5 versions'],
supports: true,
flexbox: true
})
] }
})
]
};
this is what I have in my babel-options.js file. My app fails to load and without any errors in the console
I also tried updating the presets to [['latest', { loose: true, modules: modules }], 'stage-3], but gulp complained about not being able to find the relative path to latest
babel-options.js
var path = require('path');
var paths = require('./paths');
module.exports = function(modules) {
return {
filename: '',
filenameRelative: '',
sourceMap: true,
sourceRoot: '',
moduleRoot: path.resolve('src').replace(/\\/g, '/'),
moduleIds: false,
comments: false,
compact: false,
code: true,
presets: [ ['es2015', { loose: true, modules: modules }], 'stage-1'],
plugins: [
'syntax-flow',
'transform-decorators-legacy',
'transform-flow-strip-types',
'transform-async-to-generator'
]
};
};
skeleton pack I'm using: https://github.com/aurelia/skeleton-navigation/tree/master/skeleton-esnext
sample babel-options.js
https://github.com/aurelia/skeleton-navigation/blob/master/skeleton-esnext/build/babel-options.js
I use babel with webpack and async/await works for me. This is my webpack.config.babel.js:
var path = require( 'path' );
var webpack = require( 'webpack' );
var VersionFile = require('webpack-version-file-plugin');
var wwwPath = path.join(__dirname, 'public/dist');
module.exports = {
entry: {
preload: [ 'babel-polyfill', './src/main.js' ]
},
cache: true,
debug: true,
devtool: 'source-map',
output: {
path: path.join( __dirname, 'public/dist' ),
publicPath: '../public/dist/',
filename: '[name].js',
chunkFilename: '[id].js',
libraryTarget: 'var',
library: 'ViewerEntryPoint'
},
resolve: {
root: [
path.join( __dirname, '..', 'app', 'src' ),
],
alias: {
jquery$: 'jquery/jquery',
lodash$: 'lodash/lodash',
_$: 'lodash/lodash'
}
},
resolveLoader: {
root: [
path.join( __dirname, 'node_modules' )
]
},
module: {
loaders: [
{
loader: "babel-loader",
// Skip any files outside of your project's `src` directory
include: [
path.resolve( __dirname, "src" ),
],
// Only run `.js` and `.jsx` files through Babel
test: /\.jsx?$/,
// Options to configure babel with
query: {
plugins: [ 'transform-runtime' ],
presets: [ 'es2015', 'stage-0' ] //, 'react'],
}
},
{ test: /\.css$/, loaders: [ 'style/useable', 'css' ] },
{ test: /[\/\\]jquery\.js$/, loader: 'exports?window.$' }
],
noParse: [
/[\/\\]jquery\.js$/
]
},
plugins: [
//new Clean(['dist']),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.SourceMapDevToolPlugin( {
test: /\.js$/,
moduleFilenameTemplate: '[absolute-resource-path]',
fallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]',
filename: "[file].map",
sourceRoot: '/src/'
} ),
new VersionFile( {
packageFile: path.join( __dirname, 'package.json' ),
template: path.join( __dirname, 'version.ejs' ),
outputFile: path.join( wwwPath, 'version.json' )
} )
]
};
After hours of pulling my hair out, I finally got webpack to run on an existing project, that I'm trying to port to Laravel.
When I run webpack it compiles now...
...but when I run gulp in the root, which executes Elixir, I get this error message:
node_modules/webpack/lib/NullFactory.js:9 undefined is not a function
My webpack.config.js looks like this:
var path = require('path');
var webpack = require('webpack');
var debug = process.env.NODE_ENV !== "production";
module.exports = {
devtool: 'source-map',
entry: debug ? [
'webpack-hot-middleware/client',
'./resources/assets/js/client/index'
] : [
'./resources/assets/js/client/index'
],
output: {
path: path.join(__dirname, 'public/js'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: debug ? [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': "'production'"
}
}),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false,warnings: false })
],
resolve: {
root: [
path.resolve('./resources/assets/js/client')
],
alias: {
},
},
module: {
loaders: [
// js
{
test: /\.js$/,
loaders: ['babel'],
include: path.join(__dirname, 'resources/assets/js/client')
},
// CSS
{
test: /\.styl$/,
include: path.join(__dirname, 'resources/assets/js/client'),
loader: 'style-loader!css-loader!stylus-loader'
},
{
test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'
},
{
test: /\.json?$/,
loader: 'json'
},
{ test: /\.html$/, loader: "html" }
/* {
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/,
query: {
cacheDirectory: true,
presets: ['react', 'es2015']
}
}*/
]
}
};
Ok, I have very outdated version of Node... upgrading fixed it.
I'm getting no prefixes with these setup. Cssnano and write to style.css is working but there is no prefixes added from my sass to css.
I'm just started with webpack so maybe I'm just not gettings it.
Config:
var development = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
var precss = require('precss');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var cssnano = require('cssnano');
var autoprefixer = require('autoprefixer');
var extractCSS = new ExtractTextPlugin('style.css');
module.exports = [
{
name: 'app-bundle',
entry: "./src/js/main.js",
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
},
]
},
output: {
path: "",
filename: "bundle.min.js"
},
plugins: development ? [
]: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
},
{
name: 'css/scss',
entry: './src/sass/style.scss',
module: {
loaders:
[
{
test: /\.scss$/,
loader: extractCSS.extract('style', 'css!postcss!sass')
}
]
},
postcss: function(webpack)
{
returnĀ [
cssnano({
autoprefixer: {
add: true,
remove: false,
browsers: [
'last 2 versions',
'ie >= 9'
]
},
discardComments: {
removeAll: true
},
discardUnused: false,
mergeIdents: false,
reduceIdents: false,
safe: true,
sourcemap: true
})
]
},
output: {
path: "",
filename: "style.css"
},
plugins: development ? [
extractCSS
] : []
}
];
There is a problem with your postcss plugin declaration
postcss: function(webpack)
{
return [
autoprefixer(), // Should be a function call and not reside inside cssnano config
cssnano({
discardComments: {
removeAll: true
},
discardUnused: false,
mergeIdents: false,
reduceIdents: false,
safe: true,
sourcemap: true
}),
]
},