Georgy Sharapov
Georgy Sharapov

Reputation: 59

Webpack compile config.js to config.json

Is there a way to compile for example from config.js this:

module.exports = {
 param: 'value',
 param1: 'value2'
}

compiling this into JSON format into config.json file for the output.. some loader?

Upvotes: 0

Views: 420

Answers (2)

Georgy Sharapov
Georgy Sharapov

Reputation: 59

Solved! That was pretty simple with a module called extract-text-webpack-plugin just by adding to the module.rules one rule. Webpack sample configuration:

const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
    entry: "./app.js"
    output: {
        filename: "bundle.js"
    },
    module: {
        rules: [{
            test: /\.json\.js/,
            // extract the text
            use: ExtractTextPlugin.extract({
                use: {}
            })
        }]
    },
    plugins: [
        new ExtractTextPlugin('config.json', {
            // some options if you want
        })
    ]
}

Do not forget to stringify the object when exporting on the config.json.js file. Should be something like:

module.exports = JSON.stringify({
    param: 'value',
    param1: 'value2'
});

That is, hope it helps someone.

Upvotes: 0

Slyke
Slyke

Reputation: 134

Is this what you are looking for?

var myConfig = {
 param: 'value',
 param1: 'value2'
};

console.log(JSON.stringify(myConfig)); // You can delete this if you want.

fs = require('fs');
fs.writeFile('config.json', JSON.stringify(myConfig), function (err) {
    if (err) {
        return console.log(err);
    }
});

module.exports = myConfig;

Upvotes: 1

Related Questions