http://www.javashuo.com/article/p-gjhtdgph-g.htmljavascript
cnpm install html-webpack-plugin --save-dev
minify: { collapseWhitespace: true, //打包后是否删除空格(压缩) removeAttributeQuotes: true // 移除属性的引号 },
plugins: [ new httpWebpackPlugin({ chunks: ['index','main'] }) ]
// 须要暴露在全局(模块的导出) // __dirname目录就是E:\MyLocalProject\webpackDemo const path = require('path') const htmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: { general: './src/skin/general/js/general.js', index: './src/skin/index/js/index.js', course: './src/skin/course/js/index.js', about: './src/skin/about/js/index.js', }, output: { path: path.resolve(__dirname, 'dist'), filename: 'skin/[name]/[name].js', //必须是相对路径 publicPath: '../../' }, module: { rules: [ //配置babel,自动编译es6语法 { test: /\.js$/, exclude: /(node_modules)/, loader: 'babel-loader' }, ] }, //插件的相关配置 plugins: [ //配置html文件 new htmlWebpackPlugin({ template: path.join(__dirname, '/src/static/index.html'), filename: 'static/index.html', minify: { collapseWhitespace: true }, hash: true, favicon: 'favicon.ico', chunks: ['general', 'index'] }), new htmlWebpackPlugin({ template: path.join(__dirname, '/src/static/course/index.html'), filename: 'static/course/index.html', minify: { collapseWhitespace: true, }, hash: true, favicon: 'favicon.ico', chunks: ['general', 'course'] }), new htmlWebpackPlugin({ template: path.join(__dirname, '/src/static/about/index.html'), filename: 'static/about/index.html', minify: { collapseWhitespace: true }, hash: true, chunks: ['general', 'about'], favicon: 'favicon.ico' }), ] }
项目结构:php
参考:http://www.javashuo.com/article/p-twywvglm-bs.htmlcss
// __dirname目录就是E:\MyLocalProject\webpackDemo const path = require('path') const htmlWebpackPlugin = require('html-webpack-plugin') //扫描入口目录static的路径 const glob = require("glob"); const staticHtmlPath = glob.sync('src/static/**/*.html'); //定义入口对象entrys const entrys = {}; //设置公共的js入口文件 const commonJSObj = { general: './src/skin/general/js/general.js', } Object.assign(entrys, commonJSObj) //定义html-webpack-plugin配置项 const htmlCfgs = []; const htmlCfgsObj = {}; staticHtmlPath.forEach((filePath) => { let path = filePath.split('/') let pathLength = path.length //获取文件名 let filename = path[pathLength - 1].split('.')[0] //动态配置入口文件路径 let entryJSName = path[pathLength - 2] + '-' + filename entrys[entryJSName] = './src/skin/' + path[pathLength - 2] + '/js/' + filename + '.js'; htmlCfgs.push( //动态配置入口文件插件 new htmlWebpackPlugin({ template: filePath, filename: filePath.replace('src', ''), minify: { collapseWhitespace: true }, hash: true, favicon: 'favicon.ico', chunks: [entryJSName, 'general'], }) ) }); module.exports = { devtool: 'inline-source-map', // 入口 entry: entrys, // 出口 output: { path: path.resolve(__dirname, 'dist'), //必须是绝对路径 filename: 'skin/[name]/[name].js', //必须是相对路径 publicPath: '../../' }, // 开发服务器 devServer: {}, // 模块配置 module: { rules: [ //配置babel,自动编译es6语法 { test: /\.js$/, exclude: /(node_modules)/, loader: 'babel-loader' }, ] }, //插件配置 plugins: htmlCfgs, //配置解析 resolve: {} }
glob自动扫描路径参考:https://blog.csdn.net/qq593249106/article/details/84964816html