在webpack中动态添加全局常量

Jef*_*tat 8 javascript typescript webpack vue.js

我正在尝试为我的项目添加一个全局常量.为此,我使用webpack.DefinePlugin.如果我在module.exports中添加一个,这是有效的.但是我没有条件地这样做.在下面的配置中,我可以__VERSION__在声明之后使用模块中的常量:declare var __VERSION__: string;.如果我尝试使用__VERSION2____VERSION3__在控制台中出现错误ReferenceError: __VERSION3__ is not defined.如果我的理解是正确的,那么应该更换.这是否意味着条件部分未执行或未正确执行?我该怎么调试呢?或者甚至更好,我该如何解决这个问题?

注意:目的是根据开发或生产构建切换URL.

目前的项目可以在github上找到

webpack.config.js:

// Based on https://github.com/microsoft/typescript-vue-starter#adding-webpack

var path = require('path')
var webpack = require('webpack')

module.exports = {
    mode: 'development',
  entry: './src/ts/main.ts',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax',
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
        }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  plugins: [ 
    new webpack.DefinePlugin({
        __VERSION__: JSON.stringify('1.0.0.' + Date.now())
    })],
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: 'source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = 'source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.DefinePlugin({
        __IN_DEBUG__: JSON.stringify(false),
        __VERSION2__: JSON.stringify('1.0.0.' + Date.now())
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}
else if(process.env.NODE_ENV === 'development')
{
    module.exports.plugins.push(
        new webpack.DefinePlugin({
            __VERSION3__: JSON.stringify('1.0.0.' + Date.now())
        }));
}
Run Code Online (Sandbox Code Playgroud)

Jef*_*tat 0

经过大量搜索和摆弄,我发现这个问题与 webpack 根本无关。感谢这篇文章,console.log(process.env.NODE_ENV);在底部的 if/else if 前面添加了一个undefined. 未设置环境变量,因此两个条件语句都不会执行。这实际上是 vue.js typescript 模板的问题。

像这样更改 package.json 中的脚本解决了我的问题:

  "scripts": {
    "build": "SET NODE_ENV=development&& webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
Run Code Online (Sandbox Code Playgroud)