Webpack将config.js编译为config.json

时间:2017-11-30 05:10:05

标签: javascript json webpack compilation config

有没有办法编译,例如从 config.js 编译:

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

将这个转换成JSON格式到config.json文件中输出..一些加载器?

2 个答案:

答案 0 :(得分:1)

这是你在找什么?

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;

答案 1 :(得分:0)

解决!只需添加module.rules一个规则,对于名为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 }) ] } 的模块,这非常简单。 Webpack示例配置:

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

在config.json.js文件中导出时,不要忘记对对象进行字符串化。应该是这样的:

var liObj  = $("#taco li");
//Convert the list to an Array
var arr = jQuery.makeArray(liObj);

也就是说,希望它有所帮助。