美文网首页
Window环境下Vue+Webpack前端环境搭建及nginx

Window环境下Vue+Webpack前端环境搭建及nginx

作者: 造一个大大的轮子 | 来源:发表于2018-11-07 15:00 被阅读0次

1. 通过vue-cli初始化项目以后的项目结构

在第一篇中,我们使用了vue-cli脚手架在一个文件夹下初始化了一个项目,接下来我们来看看项目的目录结构:


捕获.PNG

大概解释下每个文件夹代表的含义:


10868449-01a038fa573b22c8.png
  • tips : bulid文件夹和config文件夹 这两个文件夹怎么理解。build中配置了webpack的基本配置、开发环境配置、生产环境配置等,config中配置了路径端口值等,为什么不合并到一起?
    答案:
    便于管理。build中的是webpack的配置,是工程内部的配置,而config的是有关部署的配置。当然是分来,更便于管理啊。

2. 启动项目

config/index.js中可以配置项目构建以及其他一些参数:

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {},

    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST 项目访问地址
    port: 8081, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined 访问端口号
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    // Use Eslint Loader?
    // If true, your code will be linted during bundling and
    // linting errors and warnings will be shown in the console.
    useEslint: true,
    // If true, eslint errors and warnings will also be shown in the error overlay
    // in the browser.
    showEslintErrorsInOverlay: false,

    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },

  build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',   // 打包后的文件路径

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }

这里我们重点关注3个配置:

host: 'localhost', // can be overwritten by process.env.HOST 项目访问地址
port: 8081, // 访问端口号
assetsPublicPath: '/',   // 打包后的文件路径

还有,如果本地调试项目时,建议将build 里的assetsPublicPath的路径前缀修改为 ' ./ '(开始是 ' / '),因为打包之后,外部引入 js 和 css 文件时,如果路径以 ' / ' 开头,在本地是无法找到对应文件的(服务器上没问题)。所以如果需要在本地打开打包后的文件,就得修改文件路径。
我的端口没有被占用,直接成功(服务启动成功后浏览器会默认打开一个“欢迎页面”):(http://localhost:8081)

捕获.PNG

3. Vue-cli的webpack配置分析

    1. package.json中可以看到开发和生产环境的入口:
"scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "unit": "jest --config test/unit/jest.conf.js --coverage",
    "e2e": "node test/e2e/runner.js",
    "test": "npm run unit && npm run e2e",
    "lint": "eslint --ext .js,.vue src test/unit test/e2e/specs",
    "build": "node build/build.js"
 }
    1. 可以看到dev中的设置,build/webpack.dev.conf.js,该文件是开发环境中webpack的配置入口.
    1. webpack.dev.conf.js中出现webpack.base.conf.js,这个文件是开发环境和生产环境,甚至测试环境,这些环境的公共webpack配置。可以说,这个文件相当重要。
const baseWebpackConfig = require('./webpack.base.conf')
...
const devWebpackConfig = merge(baseWebpackConfig, {
...})

其他更多的参考: https://segmentfault.com/a/1190000008644830

水平有限,欢迎大家批评指正~

相关文章

网友评论

      本文标题:Window环境下Vue+Webpack前端环境搭建及nginx

      本文链接:https://www.haomeiwen.com/subject/kzhvxqtx.html