From c54be468fc61890992001977ede929eca47c7425 Mon Sep 17 00:00:00 2001 From: "sudharsan.tk@gmail.com" Date: Sat, 12 May 2018 11:14:07 +0800 Subject: [PATCH 1/2] New option prefixFileName added,#31 --- .gitignore | 2 + README.md | 16 +- example/app/groupby/countries/usa.json | 2 +- example/app/index.html | 4 +- example/app/main.js | 30 ++- example/npm-debug.log | 48 ----- example/npm-debug.log.2719404393 | 0 example/package.json | 2 +- example/webpack.config.js | 1 + index.js | 20 +- index.ts | 249 +++++++++++++------------ package.json | 3 +- tsconfig.json | 7 +- 13 files changed, 191 insertions(+), 193 deletions(-) delete mode 100644 example/npm-debug.log delete mode 100644 example/npm-debug.log.2719404393 diff --git a/.gitignore b/.gitignore index 0618b8a..4f1848c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ build/ __build__/ .tmp/ stats.json +*.log + .vscode/ diff --git a/README.md b/README.md index a24b2b8..0518acf 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,7 @@ new MergeJsonWebpackPlugin({ |----------------- |---------------------------------- | | files | Array of json files to be merged | | output.fileName | Name of merged output file ,relative path from output.path entry | -| encoding | Optional,encoding to be used default is utf-8 | -| debug | if true,logs will be enabled, by default it is false | + **2) By Patterns** This plugin uses glob for searching file patterns,please refer glob for usage for sample pattern. You can specify a pattern to pull all the files that satify the particular pattern and output a single json file. @@ -91,14 +90,20 @@ new MergeJsonWebpackPlugin({ | | Do **not use** curly brackets if there is only single pattern to consider | pattern:"./node_modules/**/en.json" | | | **Use** curly brackets to group more than one pattern together | pattern:"{./node_modules/**/en.json,./src/assets/i18n/en.json}" | | groupBy[].fileName | output file name for the corresponding pattern.Relative path from output.path entry | | -| encoding | Optional, encoding to be used default is utf-8 | | -| globOptions | Optional, [glob options](https://github.com/isaacs/node-glob#options) to change pattern matching behavior | | -| debug | if true ,logs will be enabled, by default debug is false | + + **3) Run time files** Files generated at run time can also be specified as input to the pattern.The plugin will lookup in the compilation.assets of webpack and try to load it for processing. +## Options +| key | Description. | | +|--------------------|-----------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------| +| debug | if true ,logs will be enabled, by default debug is false. | +| encoding | Optional, encoding to be used default is utf-8. | | +| globOptions | Optional, [glob options](https://github.com/isaacs/node-glob#options) to change pattern matching behavior. | | +| prefixFileName | if true ,file names will be prefixed to each file content and merged with outfile. | ## Change Logs @@ -110,6 +115,7 @@ new MergeJsonWebpackPlugin({ | 1.0.12 | Added glob options handling | | 1.0.13 | When using groupBy,fixed issue of compilation more than once, added capability to read dynamically generated files,and conditional logging | | 1.0.14 | Webpack 4 breaking changes fix | +| 1.0.15 | prefixFileName option added, for feature request #31 | ## Sample Please navigate to example folder diff --git a/example/app/groupby/countries/usa.json b/example/app/groupby/countries/usa.json index aed021f..2fc9766 100644 --- a/example/app/groupby/countries/usa.json +++ b/example/app/groupby/countries/usa.json @@ -3,7 +3,7 @@ { "name":"USA", "currency":"Dollars", - "language":"English" + "language":"English and spanish" } ] } \ No newline at end of file diff --git a/example/app/index.html b/example/app/index.html index 6c3fdd9..87eaaee 100644 --- a/example/app/index.html +++ b/example/app/index.html @@ -17,7 +17,9 @@

Group By Jsons (fr)


Group By Jsons

-
+
+

+  
\ No newline at end of file diff --git a/example/app/main.js b/example/app/main.js index b5ad4a6..81525ca 100644 --- a/example/app/main.js +++ b/example/app/main.js @@ -16,10 +16,30 @@ $(document).ready(function () { $("#fr").append(""+i+":"+field + "

"); }); }); - $.getJSON("assets/jsons/countries.json", function (result) { - $.each(result, function (i, field) { - $("#country").append(JSON.stringify(field)); - }); + $.getJSON("assets/jsons/countries.json", function (result) { + + document.getElementById("country").innerHTML=JSON.stringify(result,undefined,10) }); }); - \ No newline at end of file + +function syntaxHighlight(json) { + if (typeof json != 'string') { + json = JSON.stringify(json, undefined, 2); + } + json = json.replace(/&/g, '&').replace(//g, '>'); + return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { + var cls = 'number'; + if (/^"/.test(match)) { + if (/:$/.test(match)) { + cls = 'key'; + } else { + cls = 'string'; + } + } else if (/true|false/.test(match)) { + cls = 'boolean'; + } else if (/null/.test(match)) { + cls = 'null'; + } + return '' + match + ''; + }); +} \ No newline at end of file diff --git a/example/npm-debug.log b/example/npm-debug.log deleted file mode 100644 index ea9f76c..0000000 --- a/example/npm-debug.log +++ /dev/null @@ -1,48 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe', -1 verbose cli 'C:\\Users\\SAMSUNG\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js', -1 verbose cli 'run', -1 verbose cli 'build' ] -2 info using npm@4.1.1 -3 info using node@v7.4.0 -4 verbose run-script [ 'prebuild', 'build', 'postbuild' ] -5 info lifecycle webpack-dev@1.0.0~prebuild: webpack-dev@1.0.0 -6 silly lifecycle webpack-dev@1.0.0~prebuild: no script for prebuild, continuing -7 info lifecycle webpack-dev@1.0.0~build: webpack-dev@1.0.0 -8 verbose lifecycle webpack-dev@1.0.0~build: unsafe-perm in lifecycle true -9 verbose lifecycle webpack-dev@1.0.0~build: PATH: C:\Users\SAMSUNG\AppData\Roaming\npm\node_modules\npm\bin\node-gyp-bin;E:\Tettu\projects\merge-jsons-webpack-plugin\example\node_modules\.bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Diskeeper Corporation\ExpressCache\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Heroku\bin;C:\Program Files (x86)\git\cmd;C:\WINDOWS\system32\config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Bin\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\Doctrine extensions for PHP\;C:\ProgramData\ComposerSetup\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Skype\Phone\;C:\ProgramData\chocolatey\bin;C:\Program Files\Java\jdk1.8.0_131\bin;C:\Android\android-sdk\tools;C:\Android\android-sdk\platform-tools;C:\Go\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\Yarn\bin;E:\Tettu\php7;C:\Users\SAMSUNG\AppData\Roaming\Composer\vendor\bin;C:\Users\SAMSUNG\AppData\Local\Microsoft\WindowsApps;C:\Users\SAMSUNG\AppData\Roaming\npm;C:\Users\SAMSUNG\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin;%GORROT%\bin;E:\Tettu\untitled\openshift-origin-client-tools-v1.5.1-7b451fc-windows;C:\Program Files\Docker Toolbox;E:\Tettu\projects\goprojects\bin;C:\Program Files\Microsoft VS Code\bin;C:\Users\SAMSUNG\AppData\Local\Programs\Fiddler;E:\Tettu\python364;E:\Tettu\python364\Scripts;C:\Users\SAMSUNG\AppData\Local\Yarn\bin -10 verbose lifecycle webpack-dev@1.0.0~build: CWD: E:\Tettu\projects\merge-jsons-webpack-plugin\example -11 silly lifecycle webpack-dev@1.0.0~build: Args: [ '/d /s /c', 'webpack' ] -12 silly lifecycle webpack-dev@1.0.0~build: Returned: code: 1 signal: null -13 info lifecycle webpack-dev@1.0.0~build: Failed to exec build script -14 verbose stack Error: webpack-dev@1.0.0 build: `webpack` -14 verbose stack Exit status 1 -14 verbose stack at EventEmitter. (C:\Users\SAMSUNG\AppData\Roaming\npm\node_modules\npm\lib\utils\lifecycle.js:279:16) -14 verbose stack at emitTwo (events.js:106:13) -14 verbose stack at EventEmitter.emit (events.js:191:7) -14 verbose stack at ChildProcess. (C:\Users\SAMSUNG\AppData\Roaming\npm\node_modules\npm\lib\utils\spawn.js:40:14) -14 verbose stack at emitTwo (events.js:106:13) -14 verbose stack at ChildProcess.emit (events.js:191:7) -14 verbose stack at maybeClose (internal/child_process.js:885:16) -14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) -15 verbose pkgid webpack-dev@1.0.0 -16 verbose cwd E:\Tettu\projects\merge-jsons-webpack-plugin\example -17 error Windows_NT 10.0.15063 -18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\SAMSUNG\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "build" -19 error node v7.4.0 -20 error npm v4.1.1 -21 error code ELIFECYCLE -22 error webpack-dev@1.0.0 build: `webpack` -22 error Exit status 1 -23 error Failed at the webpack-dev@1.0.0 build script 'webpack'. -23 error Make sure you have the latest version of node.js and npm installed. -23 error If you do, this is most likely a problem with the webpack-dev package, -23 error not with npm itself. -23 error Tell the author that this fails on your system: -23 error webpack -23 error You can get information on how to open an issue for this project with: -23 error npm bugs webpack-dev -23 error Or if that isn't available, you can get their info via: -23 error npm owner ls webpack-dev -23 error There is likely additional logging output above. -24 verbose exit [ 1, true ] diff --git a/example/npm-debug.log.2719404393 b/example/npm-debug.log.2719404393 deleted file mode 100644 index e69de29..0000000 diff --git a/example/package.json b/example/package.json index 3ab409b..10ad934 100644 --- a/example/package.json +++ b/example/package.json @@ -14,6 +14,6 @@ "webpack-dev-server": "^2.7.1" }, "devDependencies": { - "html-webpack-plugin": "^2.30.1" + "html-webpack-plugin": "3.2.0" } } diff --git a/example/webpack.config.js b/example/webpack.config.js index 79adcd5..d11ebb7 100644 --- a/example/webpack.config.js +++ b/example/webpack.config.js @@ -20,6 +20,7 @@ module.exports = { new MergeJsonWebpackPlugin({ "debug":true, + "prefixFileName":true, "encoding": "ascii", "output": { "groupBy": [ diff --git a/index.js b/index.js index 35bbe43..99451ba 100644 --- a/index.js +++ b/index.js @@ -5,11 +5,12 @@ const Glob = require("glob"); const fs = require("fs"); const _root = path.resolve(__dirname, "./"); const UTF8_ENCODING = "utf8"; +const allowedExtensions = ".json"; class MergeJsonWebpackPlugin { constructor(options) { this.apply = (compiler) => { compiler.plugin('emit', (compilation, done) => { - this.logger.debug('MergetJsonsWebpackPlugin emit started...'); + this.logger.debug('MergeJsonsWebpackPlugin emit started...'); this.fileDependencies = []; let files = this.options.files; let output = this.options.output; @@ -56,10 +57,10 @@ class MergeJsonWebpackPlugin { this.handleErrors(compilation, err, done); }); } - this.logger.debug('MergetJsonsWebpackPlugin emit completed...'); + this.logger.debug('MergeJsonsWebpackPlugin emit completed...'); }); compiler.plugin("after-emit", (compilation, callback) => { - this.logger.debug("MergetJsonsWebpackPlugin after-emit starts..."); + this.logger.debug("MergeJsonsWebpackPlugin after-emit starts..."); const compilationFileDependencies = new Set(compilation.fileDependencies); this.fileDependencies.forEach((file) => { let filePath = path.join(compiler.context, file); @@ -72,7 +73,7 @@ class MergeJsonWebpackPlugin { } } }); - this.logger.debug("MergetJsonsWebpackPlugin after-emit completed..."); + this.logger.debug("MergeJsonsWebpackPlugin after-emit completed..."); callback(); }); }; @@ -98,7 +99,8 @@ class MergeJsonWebpackPlugin { }; this.readFile = (compilation, f, resolve, reject) => { f = f.trim(); - if (!f.endsWith(".json") && !f.endsWith(".JSON")) { + let extn = path.extname(f).toLowerCase(); + if (extn !== allowedExtensions) { reject(`MergeJsonWebpackPlugin: Not a valid Json file ${f}`); return; } @@ -124,7 +126,13 @@ class MergeJsonWebpackPlugin { } let entryDataAsJSON = {}; try { - entryDataAsJSON = JSON.parse(entryData); + let fileContent = JSON.parse(entryData); + if (this.options.prefixFileName) { + entryDataAsJSON[path.basename(f, allowedExtensions)] = fileContent; + } + else { + entryDataAsJSON = fileContent; + } } catch (e) { this.logger.error(`MergeJsonWebpackPlugin: Error parsing the json file [ ${f} ] and error is `, e); diff --git a/index.ts b/index.ts index 8c045b0..44d6323 100644 --- a/index.ts +++ b/index.ts @@ -6,27 +6,27 @@ import fs = require('fs'); const _root = path.resolve(__dirname, "./"); // project root folder const UTF8_ENCODING = "utf8"; +const allowedExtensions = ".json" + - - class MergeJsonWebpackPlugin { //options for the plugin options: any; fileDependencies; - logger:Logger; + logger: Logger; constructor(options: any) { this.options = options; this.options.encoding = this.options.encoding != null ? this.options.encoding : UTF8_ENCODING; - this.logger=new Logger(this.options.debug); + this.logger = new Logger(this.options.debug); } - + apply = (compiler: any) => { - compiler.plugin('emit', (compilation,done) => { - this.logger.debug('MergetJsonsWebpackPlugin emit started...'); + compiler.plugin('emit', (compilation, done) => { + this.logger.debug('MergeJsonsWebpackPlugin emit started...'); //initialize fileDependency array - this.fileDependencies=[]; + this.fileDependencies = []; let files = this.options.files; let output = this.options.output; let groupBy = output.groupBy; @@ -35,65 +35,65 @@ class MergeJsonWebpackPlugin { 'of file(s) to merge. '); } if (files) { - let outputPath = output.fileName; - new Promise((resolve,reject)=>{ - this.processFiles(compilation,files,outputPath,resolve,reject); - }) - .then((res:Response)=>{ - this.addAssets(compilation,res); - done(); + let outputPath = output.fileName; + new Promise((resolve, reject) => { + this.processFiles(compilation, files, outputPath, resolve, reject); }) - .catch((err)=>{ - this.handleErrors(compilation,err,done); - }); + .then((res: Response) => { + this.addAssets(compilation, res); + done(); + }) + .catch((err) => { + this.handleErrors(compilation, err, done); + }); - } else if (groupBy) { + } else if (groupBy) { if (groupBy.length == 0) { compilation.errors.push('MergeJsonWebpackPlugin: \"groupBy\" must be non empty object'); } let globOptions = this.options.globOptions || {}; - let groupByPromises =groupBy.map( (globs:any)=>{ - return new Promise( (resolve,reject)=>{ + let groupByPromises = groupBy.map((globs: any) => { + return new Promise((resolve, reject) => { let pattern = globs.pattern; let outputPath = globs.fileName; this._glob(pattern, globOptions).then((files) => { - this.processFiles(compilation,files,outputPath, resolve,reject); + this.processFiles(compilation, files, outputPath, resolve, reject); }); }); }); //wait for all groupBy array operations to finish Promise.all(groupByPromises) - .then((opsResponse)=>{ - //res contains Response of all groupBy operations - opsResponse.forEach ( (res:Response)=>{ - this.addAssets(compilation,res); - }); - done(); - }) - .catch((err)=>{ - this.handleErrors(compilation,err,done); - } ); + .then((opsResponse) => { + //res contains Response of all groupBy operations + opsResponse.forEach((res: Response) => { + this.addAssets(compilation, res); + }); + done(); + }) + .catch((err) => { + this.handleErrors(compilation, err, done); + }); } - this.logger.debug('MergetJsonsWebpackPlugin emit completed...'); + this.logger.debug('MergeJsonsWebpackPlugin emit completed...'); }); compiler.plugin("after-emit", (compilation, callback) => { - this.logger.debug("MergetJsonsWebpackPlugin after-emit starts..."); - const compilationFileDependencies = new Set(compilation.fileDependencies); - this.fileDependencies.forEach((file) => { - let filePath= path.join(compiler.context, file) - if (!compilationFileDependencies.has(filePath)) { - if(compilation.fileDependencies.add){ + this.logger.debug("MergeJsonsWebpackPlugin after-emit starts..."); + const compilationFileDependencies = new Set(compilation.fileDependencies); + this.fileDependencies.forEach((file) => { + let filePath = path.join(compiler.context, file) + if (!compilationFileDependencies.has(filePath)) { + if (compilation.fileDependencies.add) { //for webpack4 compilation.fileDependencies.add(filePath); - }else{ - //webpack 3 + } else { + //webpack 3 compilation.fileDependencies.push(filePath); - } + } } - }); - this.logger.debug("MergetJsonsWebpackPlugin after-emit completed...") + }); + this.logger.debug("MergeJsonsWebpackPlugin after-emit completed...") callback(); }); }; @@ -101,73 +101,80 @@ class MergeJsonWebpackPlugin { /** * */ - processFiles = (compilation,files,outputPath,resolve,reject) =>{ - this.fileDependencies=this.fileDependencies.concat(files); - let readFiles= files.map((f)=>{ - return new Promise((res,rej)=>{ - this.readFile(compilation,f,res,rej) + processFiles = (compilation, files, outputPath, resolve, reject) => { + this.fileDependencies = this.fileDependencies.concat(files); + let readFiles = files.map((f) => { + return new Promise((res, rej) => { + this.readFile(compilation, f, res, rej) }) }); let mergedContents: any = {}; Promise.all(readFiles) - .then((contents)=>{ - contents.forEach((content) => { - mergedContents = this.mergeDeep(mergedContents, content); + .then((contents) => { + contents.forEach((content) => { + mergedContents = this.mergeDeep(mergedContents, content); + }); + mergedContents = JSON.stringify(mergedContents); + resolve(new Response(outputPath, mergedContents)); + }) + .catch((error) => { + reject(error); }); - mergedContents = JSON.stringify(mergedContents); - resolve(new Response(outputPath,mergedContents)); - }) - .catch((error)=>{ - reject(error); - }); } - readFile = (compilation,f,resolve,reject) =>{ - //cleanup the spaces - f = f.trim(); - //check if valid json file or not ,if not reject - if (!f.endsWith(".json") && !f.endsWith(".JSON")) { - reject(`MergeJsonWebpackPlugin: Not a valid Json file ${f}`); - return; - } - let entryData = undefined; - try { - entryData = fs.readFileSync(f, this.options.encoding); - } catch (e) { - //check if its available in assets, it happens in case of dynamically generated files - //for details check issue#25 - this.logger.error(`${f} missing,looking for it in assets.`); - if(compilation.assets[f]){ + readFile = (compilation, f, resolve, reject) => { + //cleanup the spaces + f = f.trim(); + //check if valid json file or not ,if not reject + let extn = path.extname(f).toLowerCase(); + if (extn !== allowedExtensions) { + reject(`MergeJsonWebpackPlugin: Not a valid Json file ${f}`); + return; + } + let entryData = undefined; + try { + entryData = fs.readFileSync(f, this.options.encoding); + } catch (e) { + //check if its available in assets, it happens in case of dynamically generated files + //for details check issue#25 + this.logger.error(`${f} missing,looking for it in assets.`); + if (compilation.assets[f]) { this.logger.debug(`${f} found in the compilation assets so loading from assets.`) entryData = compilation.assets[f].source(); - }else{ + } else { this.logger.error(`MergeJsonWebpackPlugin: File missing [ ${f}] in path or assets `, e); reject(`MergeJsonWebpackPlugin: Unable to locate file ${f}`); return; - } - } - if (!entryData) { - this.logger.error(`MergeJsonWebpackPlugin: Data appears to be empty in file [${f}]`); - reject(`MergeJsonWebpackPlugin: Data appears to be empty in file [ ${f} ]`); - } - // try to get a JSON object from the file data - let entryDataAsJSON = {}; - try { - entryDataAsJSON = JSON.parse(entryData); - } catch (e) { - this.logger.error(`MergeJsonWebpackPlugin: Error parsing the json file [ ${f} ] and error is `, e); - reject(`MergeJsonWebpackPlugin: Error parsing the json file [${f}] `,e); - return; - } - if (typeof entryDataAsJSON !== 'object') { - this.logger.error(`MergeJsonWebpackPlugin: Not a valid object , file [ ${f} ]`); - reject(`MergeJsonWebpackPlugin: Not a valid object , file [${f} ]`); - return; - } - resolve(entryDataAsJSON); + } + } + if (!entryData) { + this.logger.error(`MergeJsonWebpackPlugin: Data appears to be empty in file [${f}]`); + reject(`MergeJsonWebpackPlugin: Data appears to be empty in file [ ${f} ]`); + } + // try to get a JSON object from the file data + let entryDataAsJSON = {}; + try { + let fileContent = JSON.parse(entryData); + //to prefix object with filename ,requirement as request in issue#31 + if (this.options.prefixFileName) { + entryDataAsJSON[path.basename(f, allowedExtensions)] = fileContent; + } else { + entryDataAsJSON = fileContent + } + } catch (e) { + this.logger.error(`MergeJsonWebpackPlugin: Error parsing the json file [ ${f} ] and error is `, e); + reject(`MergeJsonWebpackPlugin: Error parsing the json file [${f}] `, e); + return; + } + if (typeof entryDataAsJSON !== 'object') { + this.logger.error(`MergeJsonWebpackPlugin: Not a valid object , file [ ${f} ]`); + reject(`MergeJsonWebpackPlugin: Not a valid object , file [${f} ]`); + return; + } + resolve(entryDataAsJSON); } - + /** * deep merging of json child object * code contributed by @leonardopurro @@ -209,14 +216,14 @@ class MergeJsonWebpackPlugin { resolve(matches); }) }); - } + } /** * after succesful generation of assets ,add it to compilation.assets * @param compilation * @param res */ - private addAssets(compilation:any,res:Response){ + private addAssets(compilation: any, res: Response) { compilation.assets[res.filepath] = { size: function () { return res.content.length; @@ -224,39 +231,39 @@ class MergeJsonWebpackPlugin { source: function () { return res.content; } - }; + }; } - + /** * handle errors at the time of compilation */ - handleErrors=(compilation:any,error:string,done)=>{ - compilation.errors.push(error); - done(); - } + handleErrors = (compilation: any, error: string, done) => { + compilation.errors.push(error); + done(); + } } -class Response{ - filepath:string; - content:string; - constructor(filepath:string,content:string){ - this.filepath=filepath; - this.content=content; +class Response { + filepath: string; + content: string; + constructor(filepath: string, content: string) { + this.filepath = filepath; + this.content = content; } } -class Logger{ - isDebug:boolean=false; - constructor(isDebug:boolean){ - this.isDebug=isDebug; +class Logger { + isDebug: boolean = false; + constructor(isDebug: boolean) { + this.isDebug = isDebug; + } + debug = (msg) => { + if (this.isDebug) + console.log(msg); } - debug =(msg) =>{ - if(this.isDebug) - console.log(msg); + error = (msg, e?: any) => { + console.error(msg, e != undefined ? e : ""); } - error =(msg,e?:any) =>{ - console.error(msg,e!=undefined?e:""); - } } export = MergeJsonWebpackPlugin; diff --git a/package.json b/package.json index 72e62ec..c7631d1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "merge-jsons-webpack-plugin", "description": "This plugin is used to merge json files into single json file,using glob or file names", - "version": "1.0.14", + "version": "1.0.15", "author": { "email": "sudharsan_tk@yahoo.co.in", "name": "Sudharsan Tettu" @@ -41,6 +41,7 @@ "main": "index.js", "scripts": { "build": "tsc", + "watch" :" tsc -w", "post-install": "typings install", "deploy": "npm publish" } diff --git a/tsconfig.json b/tsconfig.json index 221fc66..06e8567 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,13 @@ -{ +{ "compilerOptions": { "module": "commonjs", "noImplicitAny": false, "removeComments": true, "preserveConstEnums": true, "sourceMap": false, - "target": "es6" + "target": "es6" }, "files": [ "./index.ts" ] - -} +} \ No newline at end of file From 45eac2f78dec652226ccff245103c1d258a96ab3 Mon Sep 17 00:00:00 2001 From: "sudharsan.tk@gmail.com" Date: Sat, 12 May 2018 11:25:11 +0800 Subject: [PATCH 2/2] New option prefixFileName added,#31 --- example/app/prefixFileName/sign_in.json | 3 +++ example/app/prefixFileName/sign_up.json | 3 +++ example/webpack.config.js | 16 +++++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 example/app/prefixFileName/sign_in.json create mode 100644 example/app/prefixFileName/sign_up.json diff --git a/example/app/prefixFileName/sign_in.json b/example/app/prefixFileName/sign_in.json new file mode 100644 index 0000000..308268b --- /dev/null +++ b/example/app/prefixFileName/sign_in.json @@ -0,0 +1,3 @@ +{ + "submit": "Sign in" +} \ No newline at end of file diff --git a/example/app/prefixFileName/sign_up.json b/example/app/prefixFileName/sign_up.json new file mode 100644 index 0000000..d3391b8 --- /dev/null +++ b/example/app/prefixFileName/sign_up.json @@ -0,0 +1,3 @@ +{ + "submit": "Sign up" +} \ No newline at end of file diff --git a/example/webpack.config.js b/example/webpack.config.js index d11ebb7..d61eabf 100644 --- a/example/webpack.config.js +++ b/example/webpack.config.js @@ -16,11 +16,8 @@ module.exports = { filename: 'index.html', template: 'app/index.html' }), - - new MergeJsonWebpackPlugin({ - "debug":true, - "prefixFileName":true, + "debug":true, "encoding": "ascii", "output": { "groupBy": [ @@ -59,6 +56,15 @@ module.exports = { "fileName": "jsons/result.json" } }), - + //prefix file name + new MergeJsonWebpackPlugin({ + "debug":true, + "prefixFileName":true, + "files": ['app/prefixFileName/sign_in.json', + 'app/prefixFileName/sign_up.json'], + "output": { + "fileName": "jsons/prefixFileName.json" + } + }) ] }; \ No newline at end of file