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