grapewheel / avvw

Apicloud + Vue2 + Vant(有赞前端)+ Webpack4打包,极速开发APP框架,将apicloud的渲染效率和vue数据绑定特性发挥极致!
153 stars 55 forks source link

page 目录 和 dist目录 添加 新建目录功能 引发的问题 #22

Closed viarotel closed 5 years ago

viarotel commented 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>
grapewheel commented 5 years ago

暂时不支持pages多级目录,后续会升级