Closed viarotel closed 5 years ago
/* * webpack.dev.js */ const merge = require('webpack-merge') const HtmlWebpackPlugin = require('html-webpack-plugin') const HotModuleReplacementPlugin = require('webpack').HotModuleReplacementPlugin const CopyWebpackPlugin = require('copy-webpack-plugin') const base = require('./webpack.base') const fs = require('fs') const path = require('path') // Collect the page's names let files = []; function getAllVueFile(path) { let tempArr = fs.readdirSync(path); tempArr.forEach(function(ele){ let isDirectory = fs.statSync(path + '/' + ele).isDirectory(); //当前文件是否为目录 if(isDirectory){ getAllVueFile(path + '/' + ele); } else { files.push({ path: path + '/' + ele, name: ele.replace('.vue',""), }) } }) } getAllVueFile('./src/pages'); //获取指定目录内所有.vue文件 let entry = {}, htmlWebpacks = [], filePath ='', jsPath = ''; for (let file of files) { filePath = file.path.replace('.vue','').replace('./src/pages',''); jsPath = retPath((filePath.split('/')).length-2); //动态获取路径, 防止页面分目录后资源无法引用的问题 entry[file.name] = file.path; htmlWebpacks.push(new HtmlWebpackPlugin({ name: file.name, vue: jsPath + 'js/vue.js', fastclick: jsPath + 'js/fastclick.min.js', filename: `${filePath}.html`, chunks: [file.name, 'runtime'], template: './src/templates/page.ejs' })) } function retPath(num){ // 多层文件夹获取资源路径 let path = './'; for (let idx = 0; idx < num; idx++) { path += '../'; } return path; } // Modify the index html for HMR! htmlWebpacks.push(new HtmlWebpackPlugin({ env: 'development', filename: `index.html`, chunks: [], template: './src/templates/index.html' })) /* * page.ejs */ //如下更改 <script type="text/javascript" src="<%= htmlWebpackPlugin.options.vue %>"></script> <script type="text/javascript" src="<%= htmlWebpackPlugin.options.fastclick %>"></script>
暂时不支持pages多级目录,后续会升级