简体   繁体   English

Webpack将config.js编译为config.json

[英]Webpack compile config.js to config.json

Is there a way to compile for example from config.js this: 有没有一种方法可以从config.js进行编译,例如:

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

compiling this into JSON format into config.json file for the output.. some loader? 编译成JSON格式到config.json文件中作为输出..一些加载器?

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;

Solved! 解决了! That was pretty simple with a module called extract-text-webpack-plugin just by adding to the module.rules one rule. 使用名为extract-text-webpack-plugin的模块,这非常简单,只需将其添加到module.rules 。规则一个规则。 Webpack sample configuration: Webpack示例配置:

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. 在config.json.js文件上导出时,请不要忘记对对象进行字符串化处理。 Should be something like: 应该是这样的:

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

That is, hope it helps someone. 也就是说,希望它能对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM