2017-11-30 5 views
0
config.json

Gibt es eine Möglichkeit zum Beispiel von config.js dies zu kompilieren:Webpack kompilieren config.js

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

Kompilieren dieses in JSON-Format in config.json Datei für die Ausgabe .. irgendein Ladeprogramm?

Antwort

1

Ist das wonach Sie suchen?

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; 
+0

Danke! Das ist. Aber es funktioniert nicht mit Webpack. Die config.json wird nicht erstellt, vielleicht gibt es eine Webpack-Lösung? –

0

Gelöst! Das war ziemlich einfach mit einem Modul namens extract-text-webpack-plugin nur durch Hinzufügen einer Regel zu module.rules. Webpack Beispielkonfiguration:

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 
     }) 
    ] 
} 

Vergessen Sie nicht, um das Objekt stringify, wenn auf der config.json.js Datei zu exportieren. Sollte etwas wie sein:

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

Das heißt, hoffe, es hilft jemandem.

Verwandte Themen