Skip to content

Commit

Permalink
New option prefixFileName added,#31
Browse files Browse the repository at this point in the history
  • Loading branch information
tettusud committed May 12, 2018
1 parent 0f82287 commit c54be46
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 193 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ build/
__build__/
.tmp/
stats.json
*.log


.vscode/

Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/app/groupby/countries/usa.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name":"USA",
"currency":"Dollars",
"language":"English"
"language":"English and spanish"
}
]
}
4 changes: 3 additions & 1 deletion example/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ <h2>Group By Jsons (fr) </h2>
<div id="fr"></div>
<hr>
<h2>Group By Jsons </h2>
<div id="country"></div>
<div>
<pre id="country"></pre>
</div>
</body>

</html>
30 changes: 25 additions & 5 deletions example/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,30 @@ $(document).ready(function () {
$("#fr").append("<strong>"+i+"</strong>:"+field + " <p>");
});
});
$.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)
});
});


function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
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 '<span class="' + cls + '">' + match + '</span>';
});
}
48 changes: 0 additions & 48 deletions example/npm-debug.log

This file was deleted.

Empty file removed example/npm-debug.log.2719404393
Empty file.
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"webpack-dev-server": "^2.7.1"
},
"devDependencies": {
"html-webpack-plugin": "^2.30.1"
"html-webpack-plugin": "3.2.0"
}
}
1 change: 1 addition & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {

new MergeJsonWebpackPlugin({
"debug":true,
"prefixFileName":true,
"encoding": "ascii",
"output": {
"groupBy": [
Expand Down
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -72,7 +73,7 @@ class MergeJsonWebpackPlugin {
}
}
});
this.logger.debug("MergetJsonsWebpackPlugin after-emit completed...");
this.logger.debug("MergeJsonsWebpackPlugin after-emit completed...");
callback();
});
};
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
Loading

0 comments on commit c54be46

Please sign in to comment.