javascript - 如何在 Vue-cli 中配置 Webpack?

标签 javascript webpack vue.js vue-cli

编辑:我不再尝试让它工作。


我正在使用 Vue-cli ( https://github.com/chrisvfritz/vue-enterprise-boilerplate ),它在水下使用 Webpack 创建网站。

现在我想更改我的 webpack.config.js 但是这个文件不存在。

vue.config.js 包含一个 configureWebpack 属性,但这似乎与普通的 Webpack 配置格式不同。

我想使用的 Webpack 配置是这样的:

// Webpack config
const webpack = require('webpack');
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const StartServerPlugin = require('start-server-webpack-plugin');

module.exports = {
    entry: ['webpack/hot/poll?1000', './src/index'],
    watch: true,
    target: 'node',
    node: {
        __filename: true,
        __dirname: true
    },
    externals: [nodeExternals({ whitelist: ['webpack/hot/poll?1000'] })],
    module: {
        rules: [
            ...
        ]
    },
    plugins: [
        new StartServerPlugin('server.js'),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.DefinePlugin({
            'process.env': { BUILD_TARGET: JSON.stringify('server') }
        })
    ],
    output: { path: path.join(__dirname, 'dist'), filename: 'server.js' }
};

我不知道在我的 vue.config.js 中放在哪里,它看起来像这样:

// Vue config
const appConfig = require('./src/app.config')

module.exports = {
  configureWebpack: {
    // We provide the app's title in Webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: appConfig.title,
    // Set up all the aliases we use in our app.
    resolve: {
      alias: require('./aliases.config').webpack,
    },
    module: {
      rules: [
        {
          test: /\.styl$/,
          use: [
            'style-loader',
            {
              loader: 'css-loader',
              options: {
                modules: true,
                localIdentName: '[name]__[local]___[hash:base64:5]',
              },
            },
            'stylus-loader',
          ],
        },
      ],
    },
  },
  css: {
    // Enable CSS source maps.
    sourceMap: true,
    // Enable CSS modules for all CSS/pre-processor files.
    // This option does not affect *.vue files.
    modules: true,
  },
  // Split dependencies into their own bundle.
  // https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#dll-mode
  dll: true,
  // Configure Webpack's dev server.
  // https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md
  devServer: {
    ...(process.env.API_BASE_URL
      ? // Proxy API endpoints to the production base URL.
        { proxy: { '/api': { target: process.env.API_BASE_URL } } }
      : // Proxy API endpoints a local mock API.
        { before: require('./tests/mock-api') }),
  },
}

到目前为止,我已经尝试将 webpack.config.js 转换为 app.config.js 但现在 Vue-cli 提示未满足依赖关系。

These dependencies were not found:

* @components/_globals in ./src/main.js
* @router in ./src/main.js
...

最佳答案

Vue CLI 3.0.0 的解决方案

在项目主目录下创建vue.config.js文件:

module.exports = {
  configureWebpack: {
    devServer: {
        watchOptions: {
          ignored: ['node_modules'],
          aggregateTimeout: 300,
          poll: 1500
        },
        public: '192.168.56.132' // vagrant machine address
    }
  }
}

关于javascript - 如何在 Vue-cli 中配置 Webpack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49094720/

相关文章:

javascript - JavaScript 中数组的最大深度

javascript - 输入字段值并使用 Javascript 进行重定向

javascript - React 应用程序编译但控制台中出现错误

vue.js - V-工具提示 : Close Popover from a method

spring-boot - 通过 Vue axios 将数据作为对象发布到回弹端服务器

javascript - ReactJS - 无法加载资源 : the server responded with a status of 404 (Not Found)

javascript - 我如何 promise node-adodb?

webpack - SyntaxError : expected expression, 得到 '<' 错误 - vue

css - webpack:在使用 CSS 时了解源映射

javascript - vue拼接方法如何去掉商品名?