Injecting variables into webpack

前端 未结 2 504
花落未央
花落未央 2021-02-13 20:35

I\'m trying to inject a variable into each module within my webpack bundle in order to have debugging information for JS errors per file. I\'ve enabled

node: {
         


        
相关标签:
2条回答
  • 2021-02-13 21:28

    Write your own loader:

    my_project/my_loaders/filename-loader.js:

    module.exports = function(source) {
      var injection = 'var __filename = "' + this.resourcePath + '";\n';
      return injection + source;
    };
    

    Add it to your pipeline and make sure to add also the configuration:

    resolveLoader: {
      modulesDirectories: ["my_loaders", "node_modules"]
    }
    

    See the documentation on how to write a loader.

    0 讨论(0)
  • 2021-02-13 21:41

    I use variables to resolve a couple of variables in my webpack.config.js file:

    plugins: [
        new webpack.DefinePlugin({
          ENVIRONMENT: JSON.stringify(process.env.NODE_ENV || 'development'),
          VERSION: JSON.stringify(require('./package.json').version)
        })
    ]
    

    It might not be dynamic enough, but if it is, then you might be able to bypass that loader solution.

    0 讨论(0)
提交回复
热议问题