0
ホットモジュールの交換はなしローダー、別のローダー、または別のプリセットで動作します。WebPACKのホットモジュールの交換は、バベル・ローダーで動作していないし、プリセットes2015
しかし、それはバベルローダおよびプリセットes2015では動作しません。 es2016が動作します。 プリセット "env"に同じ問題があります。
それはes2015またはENVでWebPACKのホットモジュールの交換を使用することで、すべてのことは可能ですか?以下は
が私のファイルです: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')
}
};
print.js
webpack.config.js
export default function printMe() {
console.log('Updating print.js...')
}
index.htmlを
<html>
<head>
<title>Output Management</title>
</head>
<body>
<script src="./bundle.js"></script>
</body>
</html>
package.json
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"start": "webpack-dev-server --inline --hot"
},
...
index.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"] }