vue.config.js基礎配置


const path = require('path')
const UglifyPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
    publicPath: './', // 基本路徑
    outputDir: 'dist', // 輸出文件目錄
    lintOnSave: false, // eslint-loader 是否在保存的時候檢查
    // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
    // webpack配置
    chainWebpack: (config) => {
    },
    configureWebpack: (config) => {
        if (process.env.NODE_ENV === 'production') {
            // 為生產環境修改配置...
            config.mode = 'production'
            // 將每個依賴包打包成單獨的js文件
            let optimization = {
                runtimeChunk: 'single',
                splitChunks: {
                    chunks: 'all',
                    maxInitialRequests: Infinity,
                    minSize: 20000,
                    cacheGroups: {
                        vendor: {
                            test: /[\\/]node_modules[\\/]/,
                            name (module) {
                                // get the name. E.g. node_modules/packageName/not/this/part.js
                                // or node_modules/packageName
                                const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
                                // npm package names are URL-safe, but some servers don't like @ symbols
                                return `npm.${packageName.replace('@', '')}`
                            }
                        }
                    }
                },
                minimizer: [new UglifyPlugin({
                    uglifyOptions: {
                        compress: {
                            warnings: false,
                            drop_console: true, // console
                            drop_debugger: false,
                            pure_funcs: ['console.log'] // 移除console
                        }
                    }
                })]
            }
            Object.assign(config, {
                optimization
            })
        } else {
            // 為開發環境修改配置...
            config.mode = 'development'
        }
        Object.assign(config, {
            // 開發生產共同配置
            resolve: {
                alias: {
                    '@': path.resolve(__dirname, './src'),
                    '@c': path.resolve(__dirname, './src/components'),
                    '@p': path.resolve(__dirname, './src/pages')
                } // 別名配置
            }
        })
    },
    productionSourceMap: false, // 生產環境是否生成 sourceMap 文件
    // css相關配置
    css: {
        extract: true, // 是否使用css分離插件 ExtractTextPlugin
        sourceMap: false, // 開啟 CSS source maps?
        loaderOptions: {
            css: {}, // 這里的選項會傳遞給 css-loader
            postcss: {} // 這里的選項會傳遞給 postcss-loader
        }, // css預設器配置項
        modules: false // 啟用 CSS modules for all css / pre-processor files.
    },
    parallel: require('os').cpus().length > 1, // 是否為 Babel 或 TypeScript 使用 thread-loader。該選項在系統的 CPU 有多於一個內核時自動啟用,僅作用於生產構建。
    pwa: {}, // PWA 插件相關配置 see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
    // webpack-dev-server 相關配置
    devServer: {
        open: process.platform === 'darwin',
        host: '0.0.0.0', // 允許外部ip訪問
        port: 2333, // 端口
        https: false, // 啟用https
        overlay: {
            warnings: true,
            errors: true
        }, // 錯誤、警告在頁面彈出
        proxy: {
            '/api': {
                target: 'http://www.baidu.com/api',
                changeOrigin: true, // 允許websockets跨域
                // ws: true,
                pathRewrite: {
                    '^/api': ''
                }
            }
        } // 代理轉發配置,用於調試環境
    },
    // 第三方插件配置
    pluginOptions: {}
}

  

module.exports = {
  baseUrl: process.env.NODE_ENV === 'production'
    ? '//your_url'
    : '/',

  outputDir: 'dist',

  assetsDir: 'static',

  filenameHashing: true,

  // When building in multi-pages mode, the webpack config will contain different plugins
  // (there will be multiple instances of html-webpack-plugin and preload-webpack-plugin).
  // Make sure to run vue inspect if you are trying to modify the options for those plugins.
  pages: {
    index: {
      // entry for the pages
      entry: 'src/pages/index/index.js',
      // the source template
      template: 'src/pages/index/index.html',
      // output as dist/index.html
      filename: 'index.html',
      // when using title option,
      // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
      title: '首頁',
      // chunks to include on this pages, by default includes
      // extracted common chunks and vendor chunks.
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    }
    // when using the entry-only string format,
    // template is inferred to be `public/subpage.html`
    // and falls back to `public/index.html` if not found.
    // Output filename is inferred to be `subpage.html`.
    // subpage: ''
  },

  // eslint-loader 是否在保存的時候檢查
  lintOnSave: true,

  // 是否使用包含運行時編譯器的Vue核心的構建
  runtimeCompiler: false,

  // 默認情況下 babel-loader 忽略其中的所有文件 node_modules
  transpileDependencies: [],

  // 生產環境 sourceMap
  productionSourceMap: false,

  // cors 相關 https://jakearchibald.com/2017/es-modules-in-browsers/#always-cors
  // corsUseCredentials: false,
  // webpack 配置,鍵值對象時會合並配置,為方法時會改寫配置
  // https://cli.vuejs.org/guide/webpack.html#simple-configuration
  configureWebpack: (config) => {
  },

  // webpack 鏈接 API,用於生成和修改 webapck 配置
  // https://github.com/mozilla-neutrino/webpack-chain
  chainWebpack: (config) => {
    // 因為是多頁面,所以取消 chunks,每個頁面只對應一個單獨的 JS / CSS
    config.optimization
      .splitChunks({
        cacheGroups: {}
      });

    // 'src/lib' 目錄下為外部庫文件,不參與 eslint 檢測
    config.module
      .rule('eslint')
      .exclude
      .add('/Users/maybexia/Downloads/FE/community_built-in/src/lib')
      .end()
  },

  // 配置高於chainWebpack中關於 css loader 的配置
  css: {
    // 是否開啟支持 foo.module.css 樣式
    modules: false,

    // 是否使用 css 分離插件 ExtractTextPlugin,采用獨立樣式文件載入,不采用 <style> 方式內聯至 html 文件中
    extract: true,

    // 是否構建樣式地圖,false 將提高構建速度
    sourceMap: false,

    // css預設器配置項
    loaderOptions: {
      css: {
        // options here will be passed to css-loader
      },

      postcss: {
        // options here will be passed to postcss-loader
      }
    }
  },

  // All options for webpack-dev-server are supported
  // https://webpack.js.org/configuration/dev-server/
  devServer: {
    open: true,

    host: '127.0.0.1',

    port: 3000,

    https: false,

    hotOnly: false,

    proxy: null,

    before: app => {
    }
  },
  // 構建時開啟多進程處理 babel 編譯
  parallel: require('os').cpus().length > 1,

  // https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
  pwa: {},

  // 第三方插件配置
  pluginOptions: {}
};

  

module.exports = {
  // 部署應用時的基本 URL
  baseUrl: process.env.NODE_ENV === 'production' ? '192.168.60.110:8080' : '192.168.60.110:8080',
  // build時構建文件的目錄 構建時傳入 --no-clean 可關閉該行為
  outputDir: 'dist',
  // build時放置生成的靜態資源 (js、css、img、fonts) 的 (相對於 outputDir 的) 目錄
  assetsDir: '',
  // 指定生成的 index.html 的輸出路徑 (相對於 outputDir)。也可以是一個絕對路徑。
  indexPath: 'index.html',
  // 默認在生成的靜態資源文件名中包含hash以控制緩存
  filenameHashing: true,
  // 構建多頁面應用,頁面的配置
  pages: {
    index: {
      // page 的入口
      entry: 'src/index/main.js',
      // 模板來源
      template: 'public/index.html',
      // 在 dist/index.html 的輸出
      filename: 'index.html',
      // 當使用 title 選項時,template 中的 title 標簽需要是 <title><%= htmlWebpackPlugin.options.title %></title>
      title: 'Index Page',
      // 在這個頁面中包含的塊,默認情況下會包含
      // 提取出來的通用 chunk 和 vendor chunk。
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    },
    // 當使用只有入口的字符串格式時,模板會被推導為 `public/subpage.html`,並且如果找不到的話,就回退到 `public/index.html`。
    // 輸出文件名會被推導為 `subpage.html`。
    subpage: 'src/subpage/main.js'
  },
  // 是否在開發環境下通過 eslint-loader 在每次保存時 lint 代碼 (在生產構建時禁用 eslint-loader)
  lintOnSave: process.env.NODE_ENV !== 'production',
  // 是否使用包含運行時編譯器的 Vue 構建版本
  runtimeCompiler: false,
  // Babel 顯式轉譯列表
  transpileDependencies: [],
  // 如果你不需要生產環境的 source map,可以將其設置為 false 以加速生產環境構建
  productionSourceMap: true,
  // 設置生成的 HTML 中 <link rel="stylesheet"> 和 <script> 標簽的 crossorigin 屬性(注:僅影響構建時注入的標簽)

  crossorigin: '',
  // 在生成的 HTML 中的 <link rel="stylesheet"> 和 <script> 標簽上啟用 Subresource Integrity (SRI)
  integrity: false,
  // 如果這個值是一個對象,則會通過 webpack-merge 合並到最終的配置中
  // 如果你需要基於環境有條件地配置行為,或者想要直接修改配置,那就換成一個函數 (該函數會在環境變量被設置之后懶執行)。該方法的第一個參數會收到已經解析好的配置。在函數內,你可以直接修改配置,或者返回一個將會被合並的對象
  configureWebpack: {},
  // 對內部的 webpack 配置(比如修改、增加Loader選項)(鏈式操作)
  chainWebpack: () => { },
  // css的處理
  css: {
    // 當為true時,css文件名可省略 module 默認為 false
    modules: true,
    // 是否將組件中的 CSS 提取至一個獨立的 CSS 文件中,當作為一個庫構建時,你也可以將其設置為 false 免得用戶自己導入 CSS
    // 默認生產環境下是 true,開發環境下是 false
    extract: false,
    // 是否為 CSS 開啟 source map。設置為 true 之后可能會影響構建的性能
    sourceMap: false,
    //向 CSS 相關的 loader 傳遞選項(支持 css-loader postcss-loader sass-loader less-loader stylus-loader)
    loaderOptions: { css: {}, less: {} }
  },
  // 所有 webpack-dev-server 的選項都支持
  devServer: {},
  // 是否為 Babel 或 TypeScript 使用 thread-loader
  parallel: require('os').cpus().length > 1,
  // 向 PWA 插件傳遞選項
  pwa: {},
  // 可以用來傳遞任何第三方插件選項
  pluginOptions: {}
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM