瀏覽代碼

多入口配置

duwendi 1 年之前
父節點
當前提交
468d036089

+ 56 - 1
build/utils.js

@@ -3,6 +3,59 @@ const path = require('path')
3 3
 const config = require('../config')
4 4
 const ExtractTextPlugin = require('extract-text-webpack-plugin')
5 5
 const packageConfig = require('../package.json')
6
+// glob是webpack安装时依赖的一个第三方模块,还模块允许你使用 *等符号, 例如lib/*.js就是获取lib文件夹下的所有js后缀名的文件
7
+var glob = require('glob')
8
+// 页面模板
9
+var HtmlWebpackPlugin = require('html-webpack-plugin')
10
+// 取得相应的页面路径,因为之前的配置,所以是src文件夹下的pages文件夹
11
+var PAGE_PATH = path.resolve(__dirname, '../src/pages')
12
+// 用于做相应的merge处理
13
+var merge = require('webpack-merge')
14
+
15
+
16
+//多入口配置
17
+// 通过glob模块读取pages文件夹下的所有对应文件夹下的js后缀文件,如果该文件存在
18
+// 那么就作为入口处理
19
+exports.entries = function() {
20
+  var entryFiles = glob.sync(PAGE_PATH + '/*/*.js')
21
+  var map = {}
22
+  entryFiles.forEach((filePath) => {
23
+    var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
24
+    map[filename] = filePath
25
+  })
26
+  return map
27
+}
28
+
29
+//多页面输出配置
30
+// 与上面的多页面入口配置相同,读取pages文件夹下的对应的html后缀文件,然后放入数组中
31
+exports.htmlPlugin = function() {
32
+  let entryHtml = glob.sync(PAGE_PATH + '/*/*.html')
33
+  let arr = []
34
+  entryHtml.forEach((filePath) => {
35
+    let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
36
+    let conf = {
37
+      // 模板来源
38
+      template: filePath,
39
+      // 文件名称
40
+      filename: filename + '.html',
41
+      // 页面模板需要加对应的js脚本,如果不加这行则每个页面都会引入所有的js脚本
42
+      chunks: ['manifest', 'vendor', filename],
43
+      inject: true
44
+    }
45
+    if (process.env.NODE_ENV === 'production') {
46
+      conf = merge(conf, {
47
+        minify: {
48
+          removeComments: true,
49
+          collapseWhitespace: true,
50
+          removeAttributeQuotes: true
51
+        },
52
+        chunksSortMode: 'dependency'
53
+      })
54
+    }
55
+    arr.push(new HtmlWebpackPlugin(conf))
56
+  })
57
+  return arr
58
+}
6 59
 
7 60
 exports.assetsPath = function (_path) {
8 61
   const assetsSubDirectory = process.env.NODE_ENV === 'production'
@@ -47,7 +100,9 @@ exports.cssLoaders = function (options) {
47 100
     if (options.extract) {
48 101
       return ExtractTextPlugin.extract({
49 102
         use: loaders,
50
-        fallback: 'vue-style-loader'
103
+        fallback: 'vue-style-loader',
104
+        // 加入相对位置
105
+        publicPath: '../../'
51 106
       })
52 107
     } else {
53 108
       return ['vue-style-loader'].concat(loaders)

+ 4 - 3
build/webpack.base.conf.js

@@ -21,9 +21,10 @@ const createLintingRule = () => ({
21 21
 
22 22
 module.exports = {
23 23
   context: path.resolve(__dirname, '../'),
24
-  entry: {
25
-    app: './src/main.js'
26
-  },
24
+  // entry: {
25
+  //   app: './src/main.js'
26
+  // },
27
+  entry: utils.entries(),
27 28
   output: {
28 29
     path: config.build.assetsRoot,
29 30
     filename: '[name].js',

+ 6 - 6
build/webpack.dev.conf.js

@@ -52,11 +52,11 @@ const devWebpackConfig = merge(baseWebpackConfig, {
52 52
     new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
53 53
     new webpack.NoEmitOnErrorsPlugin(),
54 54
     // https://github.com/ampedandwired/html-webpack-plugin
55
-    new HtmlWebpackPlugin({
56
-      filename: 'index.html',
57
-      template: 'index.html',
58
-      inject: true
59
-    }),
55
+    // new HtmlWebpackPlugin({
56
+    //   filename: 'index.html',
57
+    //   template: 'index.html',
58
+    //   inject: true
59
+    // }),
60 60
     // copy custom static assets
61 61
     new CopyWebpackPlugin([
62 62
       {
@@ -65,7 +65,7 @@ const devWebpackConfig = merge(baseWebpackConfig, {
65 65
         ignore: ['.*']
66 66
       }
67 67
     ])
68
-  ]
68
+  ].concat(utils.htmlPlugin())
69 69
 })
70 70
 
71 71
 module.exports = new Promise((resolve, reject) => {

+ 16 - 16
build/webpack.prod.conf.js

@@ -46,7 +46,7 @@ const webpackConfig = merge(baseWebpackConfig, {
46 46
       filename: utils.assetsPath('css/[name].[contenthash].css'),
47 47
       // Setting the following option to `false` will not extract CSS from codesplit chunks.
48 48
       // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
49
-      // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`, 
49
+      // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
50 50
       // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
51 51
       allChunks: true,
52 52
     }),
@@ -60,20 +60,20 @@ const webpackConfig = merge(baseWebpackConfig, {
60 60
     // generate dist index.html with correct asset hash for caching.
61 61
     // you can customize output by editing /index.html
62 62
     // see https://github.com/ampedandwired/html-webpack-plugin
63
-    new HtmlWebpackPlugin({
64
-      filename: config.build.index,
65
-      template: 'index.html',
66
-      inject: true,
67
-      minify: {
68
-        removeComments: true,
69
-        collapseWhitespace: true,
70
-        removeAttributeQuotes: true
71
-        // more options:
72
-        // https://github.com/kangax/html-minifier#options-quick-reference
73
-      },
74
-      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
75
-      chunksSortMode: 'dependency'
76
-    }),
63
+    // new HtmlWebpackPlugin({
64
+    //   filename: config.build.index,
65
+    //   template: 'index.html',
66
+    //   inject: true,
67
+    //   minify: {
68
+    //     removeComments: true,
69
+    //     collapseWhitespace: true,
70
+    //     removeAttributeQuotes: true
71
+    //     // more options:
72
+    //     // https://github.com/kangax/html-minifier#options-quick-reference
73
+    //   },
74
+    //   // necessary to consistently work with multiple chunks via CommonsChunkPlugin
75
+    //   chunksSortMode: 'dependency'
76
+    // }),
77 77
     // keep module.id stable when vendor modules does not change
78 78
     new webpack.HashedModuleIdsPlugin(),
79 79
     // enable scope hoisting
@@ -116,7 +116,7 @@ const webpackConfig = merge(baseWebpackConfig, {
116 116
         ignore: ['.*']
117 117
       }
118 118
     ])
119
-  ]
119
+  ].concat(utils.htmlPlugin())
120 120
 })
121 121
 
122 122
 if (config.build.productionGzip) {

+ 1 - 1
config/index.js

@@ -50,7 +50,7 @@ module.exports = {
50 50
     // Paths
51 51
     assetsRoot: path.resolve(__dirname, '../dist'),
52 52
     assetsSubDirectory: 'static',
53
-    assetsPublicPath: '/',
53
+    assetsPublicPath: './',
54 54
 
55 55
     /**
56 56
      * Source Maps

+ 0 - 23
src/App.vue

@@ -1,23 +0,0 @@
1
-<template>
2
-  <div id="app">
3
-    <img src="./assets/logo.png">
4
-    <router-view/>
5
-  </div>
6
-</template>
7
-
8
-<script>
9
-export default {
10
-  name: 'App'
11
-}
12
-</script>
13
-
14
-<style>
15
-#app {
16
-  font-family: 'Avenir', Helvetica, Arial, sans-serif;
17
-  -webkit-font-smoothing: antialiased;
18
-  -moz-osx-font-smoothing: grayscale;
19
-  text-align: center;
20
-  color: #2c3e50;
21
-  margin-top: 60px;
22
-}
23
-</style>

+ 12 - 0
src/pages/homepage/homepage.html

@@ -0,0 +1,12 @@
1
+<!DOCTYPE html>
2
+<html>
3
+  <head>
4
+    <meta charset="utf-8">
5
+    <meta name="viewport" content="width=device-width,initial-scale=1.0">
6
+    <title>data_asset_directory_frontend</title>
7
+  </head>
8
+  <body>
9
+    <div id="homepage"></div>
10
+    <!-- built files will be auto injected -->
11
+  </body>
12
+</html>

+ 7 - 0
src/pages/homepage/homepage.js

@@ -0,0 +1,7 @@
1
+import Vue from 'vue'
2
+import homepage from './homepage.vue'
3
+/* eslint-disable no-new */
4
+new Vue({
5
+  el: '#homepage',
6
+  render: h => h(homepage)
7
+})

+ 5 - 0
src/pages/homepage/homepage.vue

@@ -0,0 +1,5 @@
1
+<template>
2
+  <div>
3
+    <h1>我是homepage页面</h1>
4
+  </div>
5
+</template>

+ 1 - 1
index.html

@@ -6,7 +6,7 @@
6 6
     <title>data_asset_directory_frontend</title>
7 7
   </head>
8 8
   <body>
9
-    <div id="app"></div>
9
+    <div id="index"></div>
10 10
     <!-- built files will be auto injected -->
11 11
   </body>
12 12
 </html>

+ 7 - 0
src/pages/index/index.js

@@ -0,0 +1,7 @@
1
+import Vue from 'vue'
2
+import index from './index.vue'
3
+/* eslint-disable no-new */
4
+new Vue({
5
+  el: '#index',
6
+  render: h => h(index)
7
+})

+ 5 - 0
src/pages/index/index.vue

@@ -0,0 +1,5 @@
1
+<template>
2
+  <div>
3
+    <h1>我是index页面</h1>
4
+  </div>
5
+</template>

+ 16 - 0
static/mung-local-config.js

@@ -0,0 +1,16 @@
1
+window.mungConfig = {
2
+  dev: {
3
+    BASE_API: "https://172.16.36.211:8800/westport"
4
+  },
5
+  build: {
6
+    BASE_API: "https://10.252.0.229:38443/westport"
7
+  },
8
+  // casConfig: {
9
+  //   isLogin: false,
10
+  //   loginUrl: "https://10.252.0.229:38443/authui/login?service=",
11
+  //   hurl:
12
+  //     "https://10.252.0.229:38443/westport-portal/homepage.html#/redirectHomepage",
13
+  //   workBenchUrl: "https://10.252.0.229:38443/esf-web/"
14
+  // },
15
+  applicationName: "资产目录"
16
+};