Rah*_*gli 44 javascript reactjs webpack react-hot-loader
在数组中添加'react-hot'加载器后出现错误.我已经按照这个教程:https://robots.thoughtbot.com/setting-up-webpack-for-react-and-hot-module-replacement但是,我得到了react-hot
.
var WebpackDevServer = require("webpack-dev-server");
var webpack = require('webpack');
var path = require('path');
require("babel-polyfill");
var BUILD_DIR = path.resolve(__dirname, 'build');
var APP_DIR = path.resolve(__dirname, 'src');
module.exports = {
entry: [
'babel-polyfill',
'bootstrap-loader',
'webpack/hot/dev-server',
APP_DIR + '/import.js',
],
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
loaders: ['react-hot', 'babel'],
exclude: /node_modules/,
query: {
plugins: ['transform-runtime'],
presets: ['es2015', 'stage-0', 'react']
}
}, {
test: /\.css$/,
loader: "style-loader!css-loader"
}, {
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}, {
test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
loader: 'url-loader?limit=100000'
}]
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
};
Run Code Online (Sandbox Code Playgroud)
Nik*_*los 76
看起来查询是一种自定义单个加载器行为的替代方法,比内联指定这些参数更简洁(见下文).如果存在多个加载器,则Webpack不知道该query
配置适用于哪个.
以下应解决您的问题:
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loaders: ['react-hot', 'babel?presets[]=es2015,presets[]=stage-0,presets[]=react,plugins[]=transform-runtime']
}
Run Code Online (Sandbox Code Playgroud)
编辑:虽然此解决方案适用于Webpack 1,但请参阅其他答案,了解适用于更新版本的更清洁解决方案.
ken*_*ley 16
我的解决方案
loaders: [{
test: /\.(js|jsx)$/,
loaders: ['react-hot', 'babel?' + JSON.stringify({
cacheDirectory: true,
plugins: [
'transform-runtime',
'transform-decorators-legacy'
],
presets: ['es2015', 'react', 'stage-0'],
env: {
production: {
presets: ['react-optimize']
}
}
}), 'eslint'],
include: src,
exclude: /node_modules/
}
Run Code Online (Sandbox Code Playgroud)
dav*_*wil 10
在webpack 2和3中,这可以更加干净地配置.
加载器可以在一组加载器对象中传递.每个加载器对象可以指定一个options
对象,该对象的行为类似于该query
特定加载器的webpack 1 .
例如,同时使用react-hot-loader
和babel-loader
,与babel-loader
配置有一些选项,在的WebPack三分之二
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'react-hot-loader'
}, {
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
'es2015-native-modules'
'stage-0',
'react'
]
}
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
为了比较,这里使用查询字符串方法在webpack 1中使用相同的配置.
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
loaders: [
'react-hot',
'babel-loader?' +
'babelrc=false,' +
'presets[]=es2015,' +
'presets[]=stage-0,' +
'presets[]=react'
]
}]
}
Run Code Online (Sandbox Code Playgroud)
注意链中所有已更改的属性名称.
另请注意,我已将es2015
预设更改es2015-native-modules
为babel-loader
配置中的预设.这与规范无关options
,只是包括es6模块允许你使用v2中引入的webpack树抖动功能.它可以保持独立,它仍然可以工作,但如果没有指出明显的升级,答案会感觉不完整:-)
免责声明:这与我对类似问题的回答相同,但这个问题有类似的投票/观点/谷歌排名,所以我也会在这里发布答案.
归档时间: |
|
查看次数: |
18495 次 |
最近记录: |