VUE config/index.js文件配置


當我們需要和后臺分離部署的時候,必須配置config/index.js:

用vue-cli 自動構建的目錄里面 ?(環境變量及其基本變量的配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var?path = require('path')
module.exports = {
??build: {
????index: path.resolve(__dirname,?'dist/index.html'),
????assetsRoot: path.resolve(__dirname,?'dist'),
????assetsSubDirectory:?'static',
????assetsPublicPath:?'/',
????productionSourceMap:?true
??},
??dev: {
????port: 8080,
????proxyTable: {}
??}
}

  

在'build'部分,我們有以下選項:

build.index

必須是本地文件系統上的絕對路徑。

index.html?(帶著插入的資源路徑) 會被生成。

如果你在后臺框架中使用此模板,你可以編輯index.html路徑指定到你的后臺程序生成的文件。例如Rails程序,可以是app/views/layouts/application.html.erb,或者Laravel程序,可以是resources/views/index.blade.php

build.assetsRoot

必須是本地文件系統上的絕對路徑。

應該指向包含應用程序的所有靜態資產的根目錄。public/?對應Rails/Laravel。

build.assetsSubDirectory

被webpack編譯處理過的資源文件都會在這個build.assetsRoot目錄下,所以它不可以混有其它可能在build.assetsRoot里面有的文件。例如,假如build.assetsRoot參數是/path/to/distbuild.assetsSubDirectory?參數是?static, 那么所以webpack資源會被編譯到path/to/dist/static目錄。

每次編譯前,這個目錄會被清空,所以這個只能放編譯出來的資源文件。

static/目錄的文件會直接被在構建過程中,直接拷貝到這個目錄。這意味著是如果你改變這個規則,所有你依賴于static/中文件的絕對地址,都需要改變。

build.assetsPublicPath【資源的根目錄】

這個是通過http服務器運行的url路徑。在大多數情況下,這個是根目錄(/)。如果你的后臺框架對靜態資源url前綴要求,你僅需要改變這個參數。在內部,這個是被webpack當做output.publicPath來處理的。

后臺有要求的話一般要加上./ 或者根據具體目錄添加,不然引用不到靜態資源

build.productionSourceMap

在構建生產環境版本時是否開啟source map。

dev.port

開發服務器監聽的特定端口

dev.proxyTable

定義開發服務器的代理規則。

?項目中配置的config/index.js,有dev和production兩種環境的配置 以下介紹的是production環境下的webpack配置的理解

復制代碼
 1 var path = require('path')
 2 
 3 module.exports = {
 4   build: { // production 環境
 5     env: require('./prod.env'), // 使用 config/prod.env.js 中定義的編譯環境
 6     index: path.resolve(__dirname, '../dist/index.html'), // 編譯輸入的 index.html 文件
 7     assetsRoot: path.resolve(__dirname, '../dist'), // 編譯輸出的靜態資源路徑
 8     assetsSubDirectory: 'static', // 編譯輸出的二級目錄
 9     assetsPublicPath: '/', // 編譯發布的根目錄,可配置為資源服務器域名或 CDN 域名
10     productionSourceMap: true, // 是否開啟 cssSourceMap
11     // Gzip off by default as many popular static hosts such as
12     // Surge or Netlify already gzip all static assets for you.
13     // Before setting to `true`, make sure to:
14     // npm install --save-dev compression-webpack-plugin
15     productionGzip: false, // 是否開啟 gzip
16     productionGzipExtensions: ['js', 'css'] // 需要使用 gzip 壓縮的文件擴展名
17   },
18   dev: { // dev 環境
19     env: require('./dev.env'), // 使用 config/dev.env.js 中定義的編譯環境
20     port: 8080, // 運行測試頁面的端口
21     assetsSubDirectory: 'static', // 編譯輸出的二級目錄
22     assetsPublicPath: '/', // 編譯發布的根目錄,可配置為資源服務器域名或 CDN 域名
23     proxyTable: {}, // 需要 proxyTable 代理的接口(可跨域)
24     // CSS Sourcemaps off by default because relative paths are "buggy"
25     // with this option, according to the CSS-Loader README
26     // (https://github.com/webpack/css-loader#sourcemaps)
27     // In our experience, they generally work as expected,
28     // just be aware of this issue when enabling this option.
29     cssSourceMap: false // 是否開啟 cssSourceMap
30   }
31 }
復制代碼

?

下面是vue中的build/webpack.base.conf.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//引入依賴模塊
var?path = require('path')
var?config = require('../config')?// 獲取配置
var?utils = require('./utils')
var?projectRoot = path.resolve(__dirname,?'../')
var?env = process.env.NODE_ENV
// check env & config/index.js to decide weither to enable CSS Sourcemaps for the
// various preprocessor loaders added to vue-loader at the end of this file
var?cssSourceMapDev = (env ===?'development'?&& config.dev.cssSourceMap)/* 是否在 dev 環境下開啟 cssSourceMap ,在 config/index.js 中可配置 */
var?cssSourceMapProd = (env ===?'production'?&& config.build.productionSourceMap)/* 是否在 production 環境下開啟 cssSourceMap ,在 config/index.js 中可配置 */
var?useCssSourceMap = cssSourceMapDev || cssSourceMapProd?/* 最終是否使用 cssSourceMap */
module.exports = {
??entry: {???// 配置webpack編譯入口
????app:?'./src/main.js'?
??},
??output: {????// 配置webpack輸出路徑和命名規則
????path: config.build.assetsRoot,?// webpack輸出的目標文件夾路徑(例如:/dist)
????publicPath: process.env.NODE_ENV ===?'production'?? config.build.assetsPublicPath : config.dev.assetsPublicPath,??// webpack編譯輸出的發布路徑(判斷是正式環境或者開發環境等)
????filename:?'[name].js'???// webpack輸出bundle文件命名格式,基于文件的md5生成Hash名稱的script來防止緩存
??},
??resolve: {
????extensions: ['',?'.js',?'.vue',?'.scss'],??//自動解析確定的拓展名,使導入模塊時不帶拓展名
????fallback: [path.join(__dirname,?'../node_modules')],
????alias: {??// 創建import或require的別名,一些常用的,路徑長的都可以用別名
??????'vue$':?'vue/dist/vue',
??????'src': path.resolve(__dirname,?'../src'),
??????'assets': path.resolve(__dirname,?'../src/assets'),
??????'components': path.resolve(__dirname,?'../src/components'),
??????'scss_vars': path.resolve(__dirname,?'../src/styles/vars.scss')
????}
??},
??resolveLoader: {
????fallback: [path.join(__dirname,?'../node_modules')]
??},
??module: {
????loaders: [
????????{
????????????test: /\.vue$/,?// vue文件后綴
????????????loader:?'vue'???//使用vue-loader處理
????????},
????????{
????????????test: /\.js$/,
????????????loader:?'babel',
????????????include: projectRoot,
????????????exclude: /node_modules/
????????},
????????{
????????????test: /\.json$/,
????????????loader:?'json'
????????},
????????{
????????????test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
????????????loader:?'url',
????????????query: {
??????????????limit: 10000,
??????????????name: utils.assetsPath('img/[name].[hash:7].[ext]')
????????????}
????????},
????????{
????????????test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
????????????loader:?'url',
????????????query: {
??????????????limit: 10000,
??????????????name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
????????????}
????????}
????]
??},
??vue: {????// .vue 文件配置 loader 及工具 (autoprefixer)
????loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }),? 調用cssLoaders方法返回各類型的樣式對象(css: loader)
????postcss: [
??????require('autoprefixer')({
????????browsers: ['last 2 versions']
??????})
????]
??}
}

  webpack.prod.conf.js 生產環境下的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var?path = require('path')
var?config = require('../config')
var?utils = require('./utils')
var?webpack = require('webpack')
var?merge = require('webpack-merge')// 一個可以合并數組和對象的插件
var?baseWebpackConfig = require('./webpack.base.conf')
// 用于從webpack生成的bundle中提取文本到特定文件中的插件
// 可以抽取出css,js文件將其與webpack輸出的bundle分離
var?ExtractTextPlugin = require('extract-text-webpack-plugin')??//如果我們想用webpack打包成一個文件,css js分離開,需要這個插件
var?HtmlWebpackPlugin = require('html-webpack-plugin')// 一個用于生成HTML文件并自動注入依賴文件(link/script)的webpack插件
var?env = config.build.env
// 合并基礎的webpack配置
var?webpackConfig = merge(baseWebpackConfig, {
????// 配置樣式文件的處理規則,使用styleLoaders
??module: {
????loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract:?true?})
??},
??devtool: config.build.productionSourceMap ??'#source-map'?:?false,?// 開啟source-map,生產環境下推薦使用cheap-source-map或source-map,后者得到的.map文件體積比較大,但是能夠完全還原以前的js代碼
??output: {
????path: config.build.assetsRoot,// 編譯輸出目錄
????filename: utils.assetsPath('js/[name].[chunkhash].js'),??// 編譯輸出文件名格式
????chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')??// 沒有指定輸出名的文件輸出的文件名格式
??},
??vue: {?// vue里的css也要單獨提取出來
????loaders: utils.cssLoaders({?// css加載器,調用了utils文件中的cssLoaders方法,用來返回針對各類型的樣式文件的處理方式,
??????sourceMap: config.build.productionSourceMap,
??????extract:?true
????})
??},
??// 重新配置插件項
??plugins: [
????// http://vuejs.github.io/vue-loader/en/workflow/production.html
????// 位于開發環境下
????new?webpack.DefinePlugin({
??????'process.env': env
????}),
????new?webpack.optimize.UglifyJsPlugin({// 丑化壓縮代碼
??????compress: {
????????warnings:?false
??????}
????}),
????new?webpack.optimize.OccurenceOrderPlugin(),
????// extract css into its own file
????new?ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')),??// 抽離css文件
????// generate dist index.html with correct asset hash for caching.
????// you can customize output by editing /index.html
????// see https://github.com/ampedandwired/html-webpack-plugin
?????// filename 生成網頁的HTML名字,可以使用/來控制文件文件的目錄結構,最
??????// 終生成的路徑是基于webpac配置的output.path的
????new?HtmlWebpackPlugin({
????????// 生成html文件的名字,路徑和生產環境下的不同,要與修改后的publickPath相結合,否則開啟服務器后頁面空白
??????filename: config.build.index,
??????// 源文件,路徑相對于本文件所在的位置
??????template:?'index.html',
??????inject:?true,// 要把

  

?vue 中build/build.js頁面

復制代碼
 1 // https://github.com/shelljs/shelljs
 2 require('./check-versions')() // 檢查 Node 和 npm 版本
 3 require('shelljs/global')  // 使用了 shelljs 插件,可以讓我們在 node 環境的 js 中使用 shell
 4 env.NODE_ENV = 'production'
 5 
 6 var path = require('path') 
 7 var config = require('../config') // 加載 config.js
 8 var ora = require('ora') // 一個很好看的 loading 插件
 9 var webpack = require('webpack')  // 加載 webpack
10 var webpackConfig = require('./webpack.prod.conf')  // 加載 webpack.prod.conf
11 
12 console.log( //  輸出提示信息 ~ 提示用戶請在 http 服務下查看本頁面,否則為空白頁
13   '  Tip:\n' +
14   '  Built files are meant to be served over an HTTP server.\n' +
15   '  Opening index.html over file:// won\'t work.\n'
16 )
17 
18 var spinner = ora('building for production...')  // 使用 ora 打印出 loading + log
19 spinner.start()  // 開始 loading 動畫
20 
21 /* 拼接編譯輸出文件路徑 */
22 var assetsPath = path.join(config.build.assetsRoot, config.build.assetsSubDirectory)
23 rm('-rf', assetsPath) /* 刪除這個文件夾 (遞歸刪除) */
24 mkdir('-p', assetsPath) /* 創建此文件夾 */ 
25 cp('-R', 'static/*', assetsPath) /* 復制 static 文件夾到我們的編譯輸出目錄 */
26 
27 webpack(webpackConfig, function (err, stats) {  //  開始 webpack 的編譯
28     // 編譯成功的回調函數
29   spinner.stop()
30   if (err) throw err
31   process.stdout.write(stats.toString({
32     colors: true,
33     modules: false,
34     children: false,
35     chunks: false,
36     chunkModules: false
37   }) + '\n')
38 })
復制代碼

項目入口,由package.json 文件可以看出

1
2
3
4
5
"scripts": {
????"dev":?"node build/dev-server.js",
????"build":?"node build/build.js",
????"watch":?"node build/build-watch.js"
??},

  當我們執行 npm run dev / npm run build ?/ npm run watch時運行的是 node build/dev-server.js 或 node build/build.js 或node build/build-watch.js

node build/build-watch.js 是我配置的載production環境的配置基礎上在webpack的配置模塊加上 watch:true ?便可實現代碼的實時編譯

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:
http://www.pswp.cn/news/387094.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387094.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387094.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據規則列表加導入導出

1.進入bos,打開數據規則,進入列表菜單 2.點擊事件-新增操作 3.點擊新增 4.點擊操作類型,輸入%引入 5.點擊確定,保存后生效,導出 、引入模板設置同理轉載于:https://www.cnblogs.com/RogerLu/p/10643521.html

Lecture 6 Order Statistics

Given n elements in array, find kth smallest element (element of rank k) Worst-case linear time order statistics --by Blum, Floyd, Pratt, Rivest, Tarjan --idea: generate good pivot recursively. Not so hot, because the constant is pretty big.

C++ qsort() 函數調用時實參與形參不兼容的問題解決

《劍指OFFER》刷題筆記 —— 撲克牌順子 LL今天心情特別好,因為他去買了一副撲克牌,發現里面居然有2個大王,2個小王(一副牌原本是54張^_^)...他隨機從中抽出了5張牌,想測測自己的手氣,看看能不能抽到順子,如果抽到的話,他決定去買體育彩票,嘿嘿!!“紅心A…

linux jenkins部署之路之,ftp部署怎么匿名還好用咋解決思密達

怎么安裝就不說了,網上一堆 這噶搭是配置 目錄是/etc/vsftpd/vsftpd.conf # Example config file /etc/vsftpd/vsftpd.conf# # The default compiled in settings are fairly paranoid. This sample file # loosens things up a bit, to make the ftp daemon more u…

powerCat進行常規tcp端口轉發

實戰中,我們也會遇到需要我們進行端口轉發的情況,比如已經拿下的目標機1是在dmz區,而目標1所在內網的其他目標只能通過目標1去訪問,這時候我們就需要端口轉發或者代理來進行后滲透。這次就要介紹一個加強版的nc,基于po…

Lecture 7 Hashing Table I

Hash |---Hash function: Division, Multiplication |---Collision: Chaining, Open addressing(Linear,Double hasing) Symbol-table problem: Table S holding n records pointer --> key|satelite data (record) Hashing: Hash function h maps keys “randomly”…

SpringCloud 微服務

一微服務架構概述1.1 微服務特性以及優點每個服務可以獨立運行在自己的進程里一系列獨立運行的微服務(goods,order,pay,user,search…)共同構建了整個系統每個服務為獨立的業務開發,一個微服務只關注某個特定的功能,例如用戶管理,商品管理微服…

window起別名

http://www.bagualu.net/wordpress/archives/1714 轉載于:https://www.cnblogs.com/wei-huan/p/10654026.html

vue在ie9中的兼容問題

問題總結 https://github.com/vuejs-templates/webpack/issues/260 首先npm install --save babel-polyfill然后在main.js中的最前面引入babel-polyfillimport babel-polyfill在index.html 加入以下代碼&#xff08;非必須&#xff09;<meta http-equiv"X-UA-Compatib…

Lecture 9 Random built Binary Search Trees BSTs

Random built Binary Search Trees BSTs E[hight] near 3logn Quick Sort? Relation to Quick Sort: BST sort & Quick sort make same comparisons but in a different order. Randomized BST Sort: 1. Randomly permute A 2. BST sort(A)

spring boot 帶遠程調試啟動方式

比如啟動service-system-0.0.1-SNAPSHOT.jar和service-file-0.0.1-SNAPSHOT.jar nohup java -Xdebug -Xrunjdwp:servery,transportdt_socket,address7999,suspendn -jar service-system-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &nohup java -Xdebug -Xrunjdwp:se…

文件讀寫

讀寫文件通常都是IO操作&#xff0c;Python內置了讀文件的函數&#xff0c;用法和C是兼容的。 讀寫文件前&#xff0c;我們先必須了解一下&#xff0c;在磁盤上讀寫文件的功能都是有操作系統提供的&#xff0c;現代操作系統不允許普通的程序直接操作磁盤&#xff0c;所以&#…

Vue項目中遇到了大文件分片上傳的問題

Vue項目中遇到了大文件分片上傳的問題&#xff0c;之前用過webuploader&#xff0c;索性就把Vue2.0與webuploader結合起來使用&#xff0c;封裝了一個vue的上傳組件&#xff0c;使用起來也比較舒爽。 上傳就上傳吧&#xff0c;為什么搞得那么麻煩&#xff0c;用分片上傳&#x…

NDK學習筆記-使用現有so動態庫

前面將的都是如何使用C/C文件生成so動態庫&#xff0c;那么在使用別人的so動態庫的時候應該怎么做呢&#xff1f;這篇文章就是使用一個變聲功能的動態庫&#xff0c;完成對于以有so動態庫的說明。 動態庫來源 在互聯網中&#xff0c;有著許許多多動態庫&#xff0c;很多廠商也對…

Spring cloud系列十四 分布式鏈路監控Spring Cloud Sleuth

1. 概述 Spring Cloud Sleuth實現對Spring cloud 分布式鏈路監控 本文介紹了和Sleuth相關的內容&#xff0c;主要內容如下&#xff1a; Spring Cloud Sleuth中的重要術語和意義&#xff1a;Span、Trance、AnnotationZipkin中圖形化展示分布式鏈接監控數據并說明字段意義Spring …

Linux源碼編譯安裝程序

一、程序的組成部分 Linux下程序大都是由以下幾部分組成&#xff1a; 二進制文件&#xff1a;也就是可以運行的程序文件 庫文件&#xff1a;就是通常我們見到的lib目錄下的文件 配置文件&#xff1a;這個不必多說&#xff0c;都知道 幫助文檔&#xff1a;通常是我們在linux下用…

selenium用法詳解

selenium用法詳解 selenium主要是用來做自動化測試&#xff0c;支持多種瀏覽器&#xff0c;爬蟲中主要用來解決JavaScript渲染問題。 模擬瀏覽器進行網頁加載&#xff0c;當requests,urllib無法正常獲取網頁內容的時候一、聲明瀏覽器對象 注意點一&#xff0c;Python文件名或者…