Webpack热模块更换不适用于babel loader和预设es2015
热模块更换在无加载程序、另一加载程序或另一预设程序的情况下工作 但它不适用于babel loader和预设es2015。es2016有效。 预设“env”也存在同样的问题 是否可以使用网页包热模块替换es2015或env 以下是我的档案: webpack.config.jsWebpack热模块更换不适用于babel loader和预设es2015,webpack,babeljs,webpack-dev-server,babel-loader,Webpack,Babeljs,Webpack Dev Server,Babel Loader,热模块更换在无加载程序、另一加载程序或另一预设程序的情况下工作 但它不适用于babel loader和预设es2015。es2016有效。 预设“env”也存在同样的问题 是否可以使用网页包热模块替换es2015或env 以下是我的档案: webpack.config.js const path = require('path'); const webpack = require('webpack'); module.exports = { entry: './src/index.js',
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
}
]
},
plugins: [
new webpack.NamedModulesPlugin()
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
export default function printMe() {
console.log('Updating print.js...')
}
import _ from 'lodash';
import printMe from './print.js';
function component() {
var element = document.createElement('div');
var btn = document.createElement('button');
// Lodash, now imported by this script
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
btn.innerHTML = 'Click me and check the console!';
btn.onclick = printMe;
element.appendChild(btn);
return element;
}
let element = component(); // Store the element to re-render on print.js changes
document.body.appendChild(element);
if (module.hot) {
module.hot.accept('./print.js', function() {
console.log('Accepting the updated printMe module!');
document.body.removeChild(element);
element = component(); // Re-render the "component" to update the click handler
document.body.appendChild(element);
})
}
print.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
}
]
},
plugins: [
new webpack.NamedModulesPlugin()
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
export default function printMe() {
console.log('Updating print.js...')
}
import _ from 'lodash';
import printMe from './print.js';
function component() {
var element = document.createElement('div');
var btn = document.createElement('button');
// Lodash, now imported by this script
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
btn.innerHTML = 'Click me and check the console!';
btn.onclick = printMe;
element.appendChild(btn);
return element;
}
let element = component(); // Store the element to re-render on print.js changes
document.body.appendChild(element);
if (module.hot) {
module.hot.accept('./print.js', function() {
console.log('Accepting the updated printMe module!');
document.body.removeChild(element);
element = component(); // Re-render the "component" to update the click handler
document.body.appendChild(element);
})
}
index.html
<html>
<head>
<title>Output Management</title>
</head>
<body>
<script src="./bundle.js"></script>
</body>
</html>
index.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
}
]
},
plugins: [
new webpack.NamedModulesPlugin()
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
export default function printMe() {
console.log('Updating print.js...')
}
import _ from 'lodash';
import printMe from './print.js';
function component() {
var element = document.createElement('div');
var btn = document.createElement('button');
// Lodash, now imported by this script
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
btn.innerHTML = 'Click me and check the console!';
btn.onclick = printMe;
element.appendChild(btn);
return element;
}
let element = component(); // Store the element to re-render on print.js changes
document.body.appendChild(element);
if (module.hot) {
module.hot.accept('./print.js', function() {
console.log('Accepting the updated printMe module!');
document.body.removeChild(element);
element = component(); // Re-render the "component" to update the click handler
document.body.appendChild(element);
})
}
.babelrc
{ "presets": ["es2015"] }
找到了解决办法
将其放入.babelrc以禁用模块语法转换
{ "presets": [['es2015', { modules: false }]] }