Providing a module definition in webpack configuration

I call webpack programmatically. At the time I call it, I have a settings object that I would like to include as a module in webpack. Is it possible?

I am looking for something similar to DefinePlugin , but I would like to define a module, not free variables.

My app.js application code is as follows:

 var settings = require('settings'); console.log('Build number is', settings.buildNumber); 

My webpack runner, webpack-runner.js :

 var settings = { buildNumber: 100 }; // Can I pass settings into webpack config such that // app.js will be able to access it with require('settings')? var config = { entry: "./app.js", output: { path: __dirname, filename: "build.js" } }; webpack(config, function(err, stats) { console.log(stats.toString()); }); 

Currently, the only way I found this is to save my settings in a file, and then set an alias to the file path. But I would like to avoid saving the file just to open it in a minute.

+7
javascript webpack dependency-management
source share
1 answer

Yes, just require settings in your file. An example is below.

 // settings.js module.exports = { buildNumber: 100 }; // webpack.config.js var settings = require('./settings'); // settings.buildNumber => 100 var config = { entry: './entry.js', output: { path: __dirname, filename: 'build.js' } }; webpack(config, function( err, stats ) { console.log(stats.toString({ colors: true, modules: true, chunkModules: true })); }); 
-one
source share

All Articles