<
Meta charset="utf-8">
<
Meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<
Meta name="theme-color" content="#000000">
React App
这个index.html就是我们的模版html。
进入正题,开始install webpack和配置webpack
1.安装依赖。将package.json文件用下面的文件替代
css-loader": "^0.28.7","extract-text-webpack-plugin": "^3.0.0","file-loader": "^1.0.0","glob": "^7.1.2","html-webpack-plugin": "^2.30.1","post
css-loader": "^2.0.6","style-loader": "^0.18.2","url-loader": "^0.5.9","webpack": "^3.5.6","webpack-dev-server": "^2.8.1"
},"scripts": {
"start": "webpack-dev-server --open","build": "webpack"
}
}
2.删除当前目录中的node_modules,然后重新在控制台执行
3.在根目录下也就是/demo下新建一个webpack.config.js文件,写入如下代码
const webpackConfig = {
entry: {},output:{
path:path.resolve(
dirname,'./dist/'),filename:'[name].[chunkhash:6].js'
},//设置开发者工具的端口号,不设置则默认为8080端口
devServer: {
inline: true,port: 8181
},module:{
rules:[
{
test:/.js?$/,exclude:/node_modules/,loader:'babel-loader',query:{
presets:['es2015','react']
}
},{
test: /.(scss|sass|css)$/,loader: ExtractTextPlugin.extract({fallback: "style-loader",use: "css-loader"})
},]
},plugins: [
new ExtractTextPlugin("[name].[chunkhash:6].css"),new CleanWebpackPlugin(
['dist'],
{
root: dirname,
verbose: true,
dry: false
}
)
],};
// 获取指定路径下的入口文件
function getEntries(globPath) {
const files = glob.sync(globPath),entries = {};
files.forEach(function(filepath) {
const split = filepath.split('/');
const name = split[split.length - 2];
entries[name] = './' + filepath;
});
return entries;
}
const entries = getEntries('src/**/index.js');
Object.keys(entries).forEach(function(name) {
webpackConfig.entry[name] = entries[name];
const plugin = new HtmlWebpackPlugin({
filename: name + '.html',template: './public/index.html',inject: true,chunks: [name]
});
webpackConfig.plugins.push(plugin);
})
module.exports = webpackConfig;
4.然后用直接执行如下代码
成功在dist中看到你的两个页面app1和app2.
如果要自己调试用直接启用npm run start,然后在localhost:8181/app1.html查看页面进行调试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。