My webpack config can run in MacOS,but it is showing error on Windows.
webpack.config.js
var path = require("path");
var webpack = require("webpack");
var FileSystem = require("fs");
var argv = require('yargs').argv;
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
devtool: 'cheap-module-source-map',
entry: {
main: ['webpack/hot/dev-server','webpack-dev-server/client?http://localhost:8090',path.resolve(__dirname, 'app','index.jsx')],
vendor:['react', 'redux', 'amazeui-react','react-redux' ,'react-router']
},
output: {
path: path.resolve(__dirname, 'dist','app'),
publicPath: '/dist/app/',
filename: 'bundle.[hash].js',
chunkFilename: '[id].[hash].chunk.js',
},
plugins: [
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js'),
new ExtractTextPlugin('[name]-[chunkhash].css', {allChunks: true}),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('dev')
}
}),
new webpack.DefinePlugin({
'process.env.db_env': JSON.stringify(argv['db_env'] ? argv['db_env'] + '' : 'dev'),
}),
function() {
this.plugin("done", function(statsData) {
var stats = statsData.toJson();
var bundlejs,maincss;
var mains = stats.assetsByChunkName.main;
console.log(mains);
for (var i = 0; i < mains.length; i++) {
if (/^(bundle).+(js)$/.test(mains[i])) {
bundlejs = mains[i]
}
if (/^(main).+(css)$/.test(mains[i])) {
maincss = mains[i]
}
}
if (!stats.errors.length) {
var htmlFileName = "index.html";
var html = FileSystem.readFileSync(path.join('./resources/temp', htmlFileName), "utf8");
var htmlOutput = html.replace('bundle.js', bundlejs).replace('main.css', maincss);
FileSystem.writeFileSync(path.join('./', htmlFileName), htmlOutput);
}
});
}
],
module: {
loaders: [
{
test: /\.css$/,
loader: "style!css"
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style','css?modules&importLoaders=2&sourceMap&localIdentName=[local]___[hash:base64:5]!sass?outputStyle=expanded&sourceMap')
},
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel', // 'babel-loader' is also a legal name to reference
query: {
plugins: ['transform-runtime', 'add-module-exports', "transform-decorators-legacy"],
presets: ['react', 'es2015', 'stage-0']
}
},
{
test: /\.js$/,
loaders: [ 'babel' ],
exclude: /node_modules/,
include: __dirname
},
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=8192' }
]
},
externals: {
BMap:'BMap',
BMapLib:'BMapLib'
},
resolve: {
root:path.resolve(__dirname),
modulesDirectories: [
'app',
'node_modules'
],
extensions: ['', '.js', '.jsx','.scss','.css']
},
resolveLoader: {
root:path.resolve(__dirname,"node_modules"),
},
devServer: {
port: 8090,
hot: true,
host:"0.0.0.0",
historyApiFallback: {
index: 'index.html'
}
}
};
My directory structure is this:
+src
+app
+node_modules
+webpack.config.js
and everything is working fine on MacOS,but when I run it on Windows,I get an error:
Module not found: Error: Cannot resolve module 'css' in somefile.
The file just like this:
const style = require('./BuildingDetailContainer.scss');
and I'm sure that I have installed css-loader sass-loader style-loader
This is my package.json, I suspect it is caused by this file.Maybe because I don't add my loader to dependencies.
{
"name": "souban-website",
"version": "1.0.0",
"description": "souban website",
"author": "souban team",
"license": "UNLICENSED",
"private": true,
"engines": {
"node": ">=5.0.0",
"npm": "^3.0.0"
},
"scripts": {
"clean": "rm -rf dist",
"start": "npm run webpack-dev",
"deploy": "npm run test && npm run clean && npm run compile",
"webpack": "rm -rf dist && NODE_ENV=production webpack --display-error-details --colors --progress -p --config webpack.prod.config.js",
"webpack-dev": "webpack-dev-server --progress --colors --hot --inline",
"webpack-release": "rm -rf dist && NODE_ENV=production webpack --display-error-details --colors --progress -p --config webpack.rele.config.js --db_env production",
"webpack-dev-release": "rm -rf dist && NODE_ENV=production webpack --display-error-details --colors --progress -p --config webpack.rele.config.js --db_env dev"
},
"dependencies": {
"amazeui-react": "^1.0.1",
"babel-runtime": "^6.6.1",
"color": "^0.11.3",
"colormin": "^1.1.2",
"css-loader": "^0.24.0",
"extract-text-webpack-plugin": "^1.0.1",
"moment": "^2.12.0",
"node-sass": "^3.8.0",
"postcss-colormin": "^2.2.0",
"react": "^0.14.7",
"react-count-to": "^0.4.0",
"react-dom": "^0.14.6",
"react-motion": "^0.4.2",
"react-redux": "^4.0.6",
"react-router": "^2.4.0",
"redux": "^3.0.6",
"sass-loader": "^4.0.0",
"style-loader": "^0.13.1",
"webpack": "^1.12.12"
},
"devDependencies": {
"autobind-decorator": "^1.3.3",
"babel-core": "^6.4.5",
"babel-loader": "^6.2.1",
"babel-plugin-add-module-exports": "^0.1.4",
"babel-plugin-react-transform": "^2.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.6.0",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.5.0",
"css-loader": "^0.23.1",
"file-loader": "^0.8.5",
"isomorphic-fetch": "^2.2.1",
"jsx-loader": "^0.13.2",
"lodash": "^4.3.0",
"node-sass": "^3.6.0",
"radium": "^0.16.6",
"react-addons-css-transition-group": "^0.14.6",
"react-cookie": "^0.4.3",
"react-dom": "^0.14.5",
"react-hot-loader": "^1.3.0",
"react-modal": "^0.6.1",
"react-motion": "^0.4.1",
"redux-logger": "^2.3.1",
"redux-persist": "^1.5.5",
"redux-persist-crosstab": "^1.0.1",
"redux-thunk": "^1.0.3",
"sass-loader": "^3.2.0",
"scroll-behavior": "^0.3.0",
"style-loader": "^0.13.0",
"url-loader": "^0.5.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0",
"yargs": "^4.7.1"
}
}
I do work on windows and it was a nightmare to make things running smooth on it, everything worked fine on mac but not on windows, so If this is only wrong with Windows and Sass you might have to add the path manually to the sass loader.
The trick on Windows is here:
Display hidden files and folders.
Check the folder at 'user/appData', path should be: C:\Users\user\AppData\Roaming\npm
Add the enviroment variable to the Windows: NODE_PATH and point it to the nodeModules C:\Users\user\AppData\Roaming\npm\nodeModules
Run npm install -g
Close and reopen the terminal.
I'm using the sass loader 3.0, and in my webpack.config it's like this:
const path = require("path");
const srcPath = path.join(__dirname, 'src');
const sassLoaders = [
"css-loader",
"autoprefixer-loader?browsers=last 2 version",
"sass-loader?indentedSyntax=sass&includePaths[]=" + path.resolve(__dirname, "./src"),
];
I have the in my package.json for the loading:
"autoprefixer-loader": "3.1.0"
"sass-loader": "^3.0.0",
"style-loader": "0.12.4"
My resolve uses path.sep it is necessary in Windows as far as I reserached:
resolve: {
extensions: ["", ".js", ".scss"],
modulesDirectories: ["src", "node_modules"],
root: [__dirname + path.sep + 'scripts'],
}
As a heads up, for the loaders I use like this, and I noticed you don't have sass loaders in your modules:
{test: /\.scss$/, loaders: ["style", "css", "sass"]},
{test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},
Related
I have 3 webpack.config files to enable Server-Sive-Rendering.
webpack.base.js
webpack.client.js
webpack.server.js
Build Script
"scripts": {
"dev": "npm-run-all --parallel dev:*",
"dev:bundle": "nodemon --watch build --exec \"node build/bundle.js\"",
"dev:server": "webpack --config webpack.server.js --watch",
"dev:client": "webpack --config webpack.client.js --watch"
}
I was struggling to setup css on SSR.
When I tried :
npm run dev:client -> It works with no error!
npm run dev:server -> It works with no error!
but,
npm run dev:bundle I got an error message.
Here is my webpack settings
webpack.base.js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const devMode = process.env.NODE_ENV !== 'production';
module.exports = {
module: {
rules: [
{
test: /\.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [
'react',
'stage-0',
['env', { target: { browsers: ['last 2 versions'] }}]
]
}
},
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
})
]
};
webpack.client.js
const path = require('path');
const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.js');
const config = {
mode: 'development',
//Tell webpack the root file of our
//server application
entry: './src/client/client.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public')
}
};
module.exports = merge(baseConfig, config);
webpack.server.js
const path = require('path');
const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.js');
const webpackNodeExternals = require('webpack-node-externals');
const config = {
//Inform webpack that we're building a bundle
//for nodeJS, rather than for the browser
mode: 'development',
target: 'node',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build')
},
externals: [webpackNodeExternals()]
};
module.exports = merge(baseConfig, config);
package.json
{
"name": "shawnbaek.com",
"version": "1.0.0",
"description": "Server-Side-Rendering Wordpress Site",
"main": "index.js",
"scripts": {
"dev": "npm-run-all --parallel dev:*",
"dev:bundle": "nodemon --watch build --exec \"node build/bundle.js\"",
"dev:server": "webpack --config webpack.server.js --watch",
"dev:client": "webpack --config webpack.client.js --watch"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ShawnBaek/shawnbaek.com.git"
},
"keywords": [
"wordpress"
],
"author": "Shawn Baek",
"license": "ISC",
"bugs": {
"url": "https://github.com/ShawnBaek/shawnbaek.com/issues"
},
"homepage": "https://github.com/ShawnBaek/shawnbaek.com#readme",
"dependencies": {
"autoprefixer": "^9.1.5",
"axios": "^0.18.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"clean-webpack-plugin": "^0.1.19",
"compression": "^1.7.2",
"concurrently": "^3.5.1",
"css-loader": "^1.0.0",
"express": "^4.16.3",
"express-http-proxy": "^1.2.0",
"html-webpack-plugin": "^3.2.0",
"lodash": "^4.17.10",
"mini-css-extract-plugin": "^0.4.3",
"node-sass": "^4.9.3",
"nodemon": "^1.17.4",
"npm-run-all": "^4.1.2",
"postcss": "^7.0.2",
"postcss-loader": "^3.0.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-helmet": "^5.2.0",
"react-redux": "^5.0.7",
"react-router-config": "^1.0.0-beta.4",
"react-router-dom": "^4.2.2",
"redux": "^4.0.0",
"redux-thunk": "^2.2.0",
"sass-loader": "^7.1.0",
"serialize-javascript": "^1.5.0",
"style-loader": "^0.23.0",
"tailwindcss": "^0.6.6",
"webpack": "^4.7.0",
"webpack-dev-server": "^3.1.4",
"webpack-merge": "^4.1.2",
"webpack-node-externals": "^1.7.2"
},
"devDependencies": {
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"webpack-cli": "^2.1.2"
}
}
add globalObject: 'this' within output block to fix this issue
New to webpack trying to understand how to make file structure, webpack.config.js and package.json work together, but not sure what is failing as the client.min.js(my outputted bunfdle file) is never regenerated upon NPM run dev
webpack.config.js:
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
module.exports = {
context: path.join(__dirname, "src"),
devtool: debug ? "inline-sourcemap" : null,
entry: "./js/client.js",
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0'],
plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
}
}
]
},
output: {
path: __dirname+"/src/",
filename: "client.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
};
package.json:
{
"name": "appThing",
"version": "0.0.0",
"main": "webpack.config.js",
"dependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.0",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-react-html-attrs": "^2.0.0",
"babel-plugin-transform-class-properties": "^6.3.13",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"flux": "^2.1.1",
"history": "^1.17.0",
"react": "^0.14.6",
"react-dom": "^0.14.6",
"react-router": "^1.0.3",
"webpack": "^2",
"webpack-dev-server": "^2"
},
"scripts": {
"dev": "webpack-dev-server --content-base src --inline --hot"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^2.7.0",
"webpack-cli": "^2.0.10"
},
"description": ""
}
File structure:
NPM run dev result:
webpack-dev-server does not generate files. It serves all assets from memory.
If you want to generate files, run webpack.
I updated my project's react version from 15.3.1 to 16.2.0. I'm getting the following runtime error message.
Uncaught TypeError: Cannot read property 'bool' of undefined
at eval (Transition.js?0efa:259)
at Object.<anonymous> (bundle.js:4059)
at __webpack_require__ (bundle.js:1337)
at fn (bundle.js:744)
at eval (398:19)
at Object.<anonymous> (bundle.js:3348)
at __webpack_require__ (bundle.js:1337)
at fn (bundle.js:744)
at eval (99:30)
at Object.<anonymous> (bundle.js:1647)
I did the update gradually and the app worked well in 15.6.2. The error started appearing after I updated it to 16.0.0. I updated it to 16.2.0 hoping it would resolve it. But no luck. My webpack.config.js and package.json is below
package.json
{
"name": "myProject",
"version": "28.0.0",
"description": "New front end",
"main": "main.js",
"scripts": {
"test": "jest",
"test-coverage": "jest --coverage",
"dev": "webpack --config webpack.config.js -d --watch",
"build": "webpack --config ./webpack.config.production.js --progress -p",
"start": "webpack-dev-server --config ./webpack.config.js --hot"
},
"repository": "",
"author": "Sarani",
"license": "BSD",
"dependencies": {
"babel": "~6.5.2",
"babel-core": "~6.13.2",
"babel-loader": "~6.2.4",
"babel-polyfill": "~6.16.0",
"babel-preset-es2015": "~6.13.2",
"babel-preset-react": "~6.11.1",
"create-react-class": "^15.6.3",
"css-loader": "~0.18.0",
"file-loader": "~0.9.0",
"isomorphic-fetch": "~2.2.1",
"json-loader": "~0.5.4",
"less": "~2.7.1",
"less-loader": "~2.2.3",
"moment-range": "~3.0.3",
"moment-timezone": "~0.5.13",
"prop-types": "^15.6.0",
"react": "^16.0.0",
"react-bootstrap": "~0.30.2",
"react-dom": "^16.0.0",
"react-redux": "~4.4.5",
"react-router": "^3.2.0",
"redux": "~3.5.2",
"redux-immutable-state-invariant": "~1.2.3",
"redux-thunk": "~2.1.0",
"style-loader": "~0.13.1",
"url-loader": "~0.5.7",
"webpack": "^3.10.0"
},
"devDependencies": {
"babel-eslint": "~6.1.2",
"babel-jest": "~14.1.0",
"eslint": "~3.5.0",
"eslint-config-airbnb": "~11.1.0",
"eslint-loader": "~1.5.0",
"eslint-plugin-import": "~1.15.0",
"eslint-plugin-jsx-a11y": "~2.2.2",
"eslint-plugin-react": "~6.2.2",
"gulp": "~3.9.1",
"gulp-csslint": "~1.0.0",
"gulp-watch": "~4.3.10",
"jest": "~14.1.0",
"jest-cli": "~14.1.0",
"react-a11y": "~0.3.3",
"react-addons-test-utils": "~15.3.2",
"react-test-renderer": "~15.3.1",
"redux-mock-store": "~1.2.1",
"webpack-dev-server": "~1.14.1"
},
"jest": {
"automock": false,
"moduleNameMapper": {
"^.+\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js",
"^.+\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js",
"^config$": "<rootDir>/__mocks__/configMock.js"
},
"moduleFileExtensions": [
"js",
"jsx"
]
}
}
webpack.config.js
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool: 'eval-source-map',
entry: {
bundle: ['babel-polyfill', './Main.js'],
bundleIntegrated: ['babel-polyfill', './MainIntegrated.js']
},
output: {
path: path.resolve(__dirname, '../assets/myProject'),
filename: '../assets/myProject/[name].js'
},
target: 'web',
devServer: {
inline: true,
port: 8080,
proxy: {
'/myct/**': {
target: 'http://localhost:9000',
secure: false,
changeOrigin: true
}
}
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
],
resolve: {
extensions: ['.js', '.jsx'],
alias: {
config: path.join(__dirname, 'config/config.dev')
}
},
module: {
rules: [
{
test: /.(js|jsx)$/,
enforce: 'pre',
exclude: /node_modules/,
use: [
{
loader: "eslint-loader",
}
],
},
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|__tests__)/,
loader: "babel-loader",
},
{
test: /\.css$/,
use: [
"style-loader",
"css-loader",
],
},
{
test: /\.less$/,
use: [
"style-loader",
"css-loader",
"less-loader",
],
},
{
test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
use: [
{
loader: "file-loader?name=../assets/myProject/fonts/[name].[ext]",
},
],
},
]
}
}
Any ideas on how to fix this issue. Thanks in advance :)
I had the same issue.
If you run yarn info react-bootstrap peerDependencies, you will see the following:
{
react: '>=16.8.0',
'react-dom': '>=16.8.0'
}
So, make sure the versions are correct.
I've fixed it by running yarn add react#latest and also yarn add react-dom#latest. In addition, I set react-bootstrap to 0.32.4, as version 1.0.0 threw different errors.
"react": "^16.13.1",
"react-bootstrap": "0.32.4",
"react-dom": "^16.13.1",
Thank you all for your help. I managed to fix the issue by updating the following to latest versions. The solution can be found here
"react": "latest",
"react-dom": "latest",
"react-bootstrap": "latest",
"react-router-dom": "latest"
Why is my bundle.js so large?
What is wrong with my configuration file?
All my React projects tend to be incredibly large in file size (bundle.js is 7.58 mb). I have no idea why is it this large. I already have uglifyJS on, but this doesn't seem to help a lot.
Here are the details:
bundle.js 7.58 MB 0 [emitted] main
index.html 942 bytes [emitted]
My webpack.config.js
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const BUILD_DIR = path.resolve(__dirname, 'dist');
const APP_DIR = path.resolve(__dirname, 'src/components');
const DATA_DIR = path.resolve(__dirname, 'data');
const config = {
entry: APP_DIR + '/App.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?/,
include: APP_DIR,
loader: [
'babel'
],
query: {
presets: ["es2015", "react"]
}
},
{
test: /\.css$/,
loader:'style-loader!css-loader?importLoaders=1!postcss-loader'
},
{
test:/\.scss$/,
loader:'style-loader!css-loader?importLoaders=1!postcss-loader!sass-loader'
},
{
test: /\.html/,
loader:'html-loader'
},
{
test: /\.(json)([\?]?.*)$/,
include: DATA_DIR,
loader: "file-loader",
query:{
name:"data/[name].[ext]"
}
},
{
test: /\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,
loader: "file-loader",
query:{
name:"asserts/fonts/[name].[ext]"
}
},
{
test: /\.(gif|png|jpe?g)$/i,
include: DATA_DIR,
loader: "file-loader",
query:{
name:"data/images/[name]-[hash:5].[ext]"
}
}
]
},
postcss:[
require('autoprefixer')({
broswers:['last 5 versions']
})
],
devtool:'eval-source-map',
devServer:{
historyApiFallback:true,
hot:true,
inline:true,
proxy:{
'/api/*':{
target:'http://localhost:8081',
secure:false
}
}
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
}),
new htmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
title:'this is a title', //一个title 属性
inject:'body'
})
]
};
module.exports = config;
My package.json
{
"name": "react-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --progress --profile --colors --hot --inline --port 3000 --host 0.0.0.0",
"dev": "webpack -d --watch",
"webpack": "webpack -p --config webpack.config.js --colors --display-reasons --display-error-details --display-modules --sort-modules-by size"
},
"author": "Sharp",
"license": "MIT",
"dependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"webpack": "^1.12.8"
},
"devDependencies": {
"autoprefixer": "^6.7.7",
"axios": "^0.15.3",
"babel-plugin-import": "^1.1.1",
"babel-plugin-transform-runtime": "^6.23.0",
"css-loader": "^0.27.3",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.10.1",
"history": "^4.6.1",
"html-loader": "^0.4.5",
"html-webpack-plugin": "^2.28.0",
"lodash": "^4.17.4",
"node-sass": "^4.5.0",
"postcss-loader": "^1.3.3",
"react-addons-update": "^15.4.2",
"react-bootstrap": "^0.30.8",
"react-bootstrap-datetimepicker": "0.0.22",
"react-redux": "^5.0.3",
"react-router": "^3.0.2",
"redux": "^3.6.0",
"redux-logger": "^2.8.2",
"redux-thunk": "^2.2.0",
"remove": "^0.1.5",
"sass-loader": "^6.0.3",
"scss-loader": "0.0.1",
"style-loader": "^0.14.1",
"webpack-dev-server": "^1.16.3"
}
}
Does anyone have any idea how to fix this?
Caveat: OP's config is a webpack v1 config, while my answer is for v2.
You are using the type of source maps that are contained in the bundle itself:
devtool:'eval-source-map'
This type of source maps is for developmnent only, so if the bundle size is huge it's not an issue. So nothing is wrong with you configuration file per se, you just have to make two separate configs (maybe both exending a base config) for development and production, and use different source maps types in both.
See https://webpack.js.org/configuration/devtool/ for source map types that should be used in production and development. For production you could use something like cheap-source-map or not use source maps at all.
Generally source maps can be output as a standalone bundle/chunk or be contained in the code bundle itself, and have different levels of detail, from line/column info to no source maps at all. This is up to you to decide how much debugging info you need in production and if you are ok with making your sources publicly available.
I have no problem whatsoever working on the dev env, hot reloading and everything works fine. Trying to make a production build its proving to be quite challenging, getting nothing but a blank page. There seems to be similar questions on here but I'm not using any html as an entry point. Thanks in advance.
package.json
{
"name": "dc",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server -d --content-base public --inline --hot --host 0.0.0.0",
"prod": "webpack -p --progress --config prod.config.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"axios": "^0.9.1",
"babel-core": "^6.7.2",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-react-hmre": "^1.1.1",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.8.5",
"history": "^2.0.1",
"isomorphic-fetch": "^2.2.1",
"node-sass": "^3.4.2",
"react": "^0.14.7",
"react-css-transition-replace": "^1.1.0",
"react-dom": "^0.14.7",
"react-hot-loader": "^1.3.0",
"react-redux": "^4.4.1",
"react-router": "^2.0.1",
"redux": "^3.3.1",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.0.1",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"webpack": "^1.12.14"
},
"dependencies": {
"axios": "^0.9.1",
"history": "^2.0.1",
"isomorphic-fetch": "^2.2.1",
"react": "^0.14.7",
"react-redux": "^4.4.1",
"react-router": "^2.0.1",
"redux": "^3.3.1"
}
}
production config
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry : ["./app/App.js"],
output : {
filename: "bundle.js",
publicPath: 'dist/',
path : path.resolve(__dirname, 'dist/')
},
devtool: 'source-map',
devServer: {
contentBase: 'dist/'
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
},
__DEVELOPMENT__: false,
}),
new webpack.optimize.OccurenceOrderPlugin(),
new ExtractTextPlugin("styles.css"),
new webpack.NoErrorsPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: true,
},
}),
],
module : {
loaders : [
{ test : /\.jsx?$/, loader : 'babel-loader',
query : {
presets : ['react', 'es2015', 'react-hmre']
}
},
{ test: /\.(jpg|png)$/, exclude: /node_modules/, loader: "file?name=images/[name].[ext]"},
{ test: /\.css$/, exclude: /node_modules/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") },
{ test: /\.scss$/, exclude: /node_modules/, loader: ExtractTextPlugin.extract("style-loader", "css-loader!sass-loader") }
]
}
};
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>lol</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>
I have been working with a bit different solution. What I have been doing is bundling the files through webpack then use a koa server to serve a static file and then have a npm start script which sets NODE_ENV to production. Take a look:
package.json:
{
"name": "react",
"version": "1.0.0",
"description": "some description",
"main": "index.js",
"scripts": {
"test": "test",
"start": "NODE_ENV=production webpack --progress && NODE_ENV=production node server.js",
"dev": "webpack-dev-server --progress --colors --watch",
"build": "webpack --progress --watch"
},
"author": "your_name",
"license": "ISC",
"dependencies":{
"babel-core": "^6.7.2",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"copy-webpack-plugin": "^1.1.1",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"image-webpack-loader": "^1.6.3",
"json-loader": "^0.5.4",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.0",
"koa": "2.0.0-alpha.3",
"koa-convert": "1.2.0",
"koa-static": "2.0.0",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1"
}
}
server.js:
'use strict';
const port = process.env.PORT || 3000;
const Koa = require('koa');
const serve = require('koa-static');
const convert = require('koa-convert');
const app = new Koa();
const _use = app.use;
app.use = (x) => _use.call(app, convert(x));
app.use(serve('./build'));
const server = app.listen(port, function () {
let host = server.address().address;
let port = server.address().port;
console.log('listening at http://%s:%s', host, port);
});
and finaly webpack.config.js:
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: './main.js',
output: { path: __dirname + "/build/", filename: 'bundle.js' },
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract("style", "css!sass?")
},
{
test: /\.json$/,
loader: "json"
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'file?hash=sha512&digest=hex&name=[hash].[ext]',
'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
]
}
]
},
plugins: [
new ExtractTextPlugin("main.css"),
new CopyWebpackPlugin([
{
from: __dirname + '/index.html',
to: __dirname + '/index.html'
},
])
]
};
If you will run those with an index.html file and an main.js file rendering some react to it it will run on production :) I recently wrote an article about how exactly my solution looks like. Feel free to take a look: https://medium.com/#TheBannik/get-ready-to-deploy-a-react-js-app-8f62c8e08282#.9gcd329h6