webpack3.x 多入口打包的简单配置
来源:互联网 发布:淘宝营业执照查询系统 编辑:程序博客网 时间:2024/06/07 11:56
目录结构
dist是生成的目录
pages是你的多入口文件的目录
我把每个入口的文件的js和css都抽离出来了,静态资源集中放在同一个目录
首先是 webpack.config.js
const webpack = require('webpack');const path = require('path');// 匹配文件const glob = require('glob')// 自动安装比如JS中的require import的未安装的依赖const NpmInstallPlugin = require('npm-install-webpack-plugin');// 提取css文件const ExtractTextPlugin = require("extract-text-webpack-plugin");// 清空打包后的目录const CleanWebpackPlugin = require('clean-webpack-plugin');//生成HTML模板 const HtmlWebpackPlugin = require('html-webpack-plugin')// webpack服务const wepackDevServer = require('webpack-dev-server')// 提取jsonvar GenerateAssetPlugin = require('generate-asset-webpack-plugin');// 压缩混淆jsconst UglifyJSPlugin = require('uglifyjs-webpack-plugin');// 动态获取所有入口文件const getEntries = () => { let obj = {}; glob.sync(path.join(__dirname, './pages/*'), {}).forEach(item => { obj[`${item.split('/').pop()}`] = `${item}/index.js` }) return obj}const webConfig = module.exports = {// entry: {// app: './pages/test/index.js',// // 应用程序开始执行// }, entry: getEntries(), devtool: 'inline-source-map', output: { filename: './[name]/[name].[hash:5].js', // 文件名称 path: path.resolve(__dirname, 'dist'), // 打包后文件输出的目录 publicPath: '../', //指定资源文件引用的目录 当前打包的文件夹 }, devServer: { // 告诉服务器从哪里提供内容 contentBase: path.resolve(__dirname, 'static'), // 是否压缩 compress: true, publicPath: '/', // 指定端口号 port: 9000 }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: ['babel-loader'] }, { test: /\.(css|sass|less|scss)$/, exclude: /node_modules/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', // 当css没有被提取的loader use: [ { loader: 'css-loader', options: { minimize: true //css压缩 } }, { loader: 'postcss-loader', // 浏览器兼容等 options: { minimize: true //css压缩 } } ] }) }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, exclude: /node_modules/, loader: 'url-loader', options: { limit: 8192, name: '[name].[hash:5].[ext]', outputPath: './static/images/' } }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, exclude: /node_modules/, loader: 'url-loader', options: { limit: 8192, name: '[name].[hash:5].[ext]', outputPath: './static/fonts/' // outputPath: './xy/fonts/' // outputPath: './test/fonts/' } }, { test: /\.html$/, exclude: /node_modules/, use: [{ loader: 'html-loader', options: { minmize: true } }] }, { test: /\.json$/, exclude: /node_modules/, loader: 'file-loader', options: { limit: 0, name: '[name].[hash:5].[ext]', outputPath: './static/json/', } } ] }, plugins: [ // 启用HMR webpack的热加载 new webpack.HotModuleReplacementPlugin(), /* * 生成带有hash的css js引用的html文件实例 * filename 指定要生成的html文件 * template 生成的html模板 可以指定你的原始html文件当做模板 * chunks 指定需要的快 比如下面的test就需要test里面的css、js啥的,就可以直接给个['test']就可以了 * */// new HtmlWebpackPlugin({// filename: './test/index.html',// template: './pages/test/index.html',// chunks: ['test']// }),// // new HtmlWebpackPlugin({// filename: './xy/index.html',// template: './pages/xy/crm/index.html',// chunks: ['xy']// }), // 自动安装依赖 // new NpmInstallPlugin(), // 清空dist目录实例 new CleanWebpackPlugin(['dist']), // 提取并生成css实例 控制css的输出在这里 new ExtractTextPlugin('./[name]/[name].[hash:5].css'), new UglifyJSPlugin() ]} // 引入多入口的目录 const pageArr = require('./pageArr.config.js') pageArr.forEach((page) => { const htmlPlugin = new HtmlWebpackPlugin({ filename: `./${page}/index.html`, // 根目录是dist template: `./pages/${page}/index.html`, // 根目录就是当前根目录 chunks: [page],// title: , hash: true, // 为静态资源生成hash值// minify: true,// xhtml: true,// showErrors: true }); webConfig.plugins.push(htmlPlugin); });
postcss.config.js 的配置
module.exports = { plugins: [ require('autoprefixer')({ browsers: ['last 5 versions'] }) ]}
pageArr.config.js 的配置 这个就是你的多文件目录的文件夹名字 避免每次都new一个 HtmlWebpackPlugin
module.exports = [ 'xy', 'test'];
我感觉比较容易晕的还是路径的 配置 这个自己多写写 多试试 就明白啦 小弟还很菜 希望大佬们多多关照 多多提建议 感谢大佬 给大佬递茶
阅读全文
0 0
- webpack3.x 多入口打包的简单配置
- webpack3.x文件配置
- webpack3的常用配置
- webpack3最新版本配置研究(三)多入口,html-webpack-plugin, extract-text-webpack-plugin
- webpack3最新版本配置研究(二)多入口,html-webpack-plugin, extract-text-webpack-plugin
- webpack3+React 的配置全解
- Webpack3.x入门篇——webpack.config配置
- 手把手教你webpack3(3)入口(多入口)entry
- Webpack3.x 中 html-webpack-plugin的用法
- webpack3实战(5)打包一个多页、jQuery、图片转base64、压缩混淆、异步模块加载的项目
- postcss webpack3.0配置
- webpack 多入口文件打包
- 深入浅出的webpack3入门教程
- webpack3实战(1)打包一个CDN引入的jQuery项目
- webpack3实战(3)打包有es6、es7语法的js代码
- webpack3实战(4)打包带异步加载功能的模块
- webpack3插件安装和配置
- webpack3
- c语言实现二叉树的插入、查找、删除、打印树
- java-exe-finished-with-non-zero-exit-value-1
- SQL 数据类型
- Java设计模式之六:访问者模式
- spring aop:aspect
- webpack3.x 多入口打包的简单配置
- KMP讲解
- 剑指Offer_面试题28.5_字符串的排列扩展_排列组合_八皇后问题
- RabbitMQ实现‘Hello world’
- 常用正则表达式
- 多线程基础一、线程实现
- php一个数组的值为key与另一个数组的值为值组成数组
- MyBatis Generator 逆向工程
- netsh windows7 自带端口映射(转发)使用教程