Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to webpack5 #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
language: node_js
node_js:
- 8.9.4
- 10
cache: npm
before_install:
- npm i -g [email protected]
install:
- npm i
script:
- npm run test
- npm run test:coverage
branches:
only:
- master
- master
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports = {
| **filename** | Output file name. | `String` | `build-manifest.json` | |
| **objectToString** | Function to be used to format the output. | `Function` with signature `(result) => string` | `result => JSON.stringify(result)` | By default we output `JSON`, but you can opt in for any other format as well. Just define your output here and adjust `filename`
| **publicPath** | String to prepend to all chunk file names. You probably should set it to the same value as `webpackConfig.output.publicPath`. | `String` | `''` | Empty string by default |
| **showLog** | Prints file creation log | `Boolean` | `true` | True by default |

## Additional Info
To better understand your custom options, you can learn more about chunks [here](https://github.com/webpack/docs/wiki/how-to-write-a-plugin#exploring-assets-chunks-modules-and-dependencies).
Expand All @@ -78,4 +79,4 @@ Feel free to open an [issue](https://github.com/homeday-de/chunks-2-json-webpack


## Contribution or feature request?
Please open a [PR](https://github.com/homeday-de/chunks-2-json-webpack-plugin/pulls) or [issue](https://github.com/homeday-de/chunks-2-json-webpack-plugin/issues).
Please open a [PR](https://github.com/homeday-de/chunks-2-json-webpack-plugin/pulls) or [issue](https://github.com/homeday-de/chunks-2-json-webpack-plugin/issues).
35 changes: 25 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const defaultOptions = {
filename: 'build-manifest.json',
// generate contents to save to manifest file
objectToString: result => JSON.stringify(result),
publicPath: ''
publicPath: '',
showLog: true
};

class Chunks2JsonWebpackPlugin {
Expand All @@ -28,20 +29,30 @@ class Chunks2JsonWebpackPlugin {
if (this.result[chunk.name] === undefined) {
this.result[chunk.name] = {};
}

chunk.files.forEach(filename => {
if (this._excludeChunk(this.options.excludeFile, filename, chunk) === true) {
return;
}
const ext = this.options.chunkGroupName(filename, chunk);
if (this.result[chunk.name][ext] === undefined) {
this.result[chunk.name][ext] = [];
}
this.result[chunk.name][ext].push(`${this.options.publicPath}${filename}`);
this._processFile(filename, chunk);
});

(chunk.auxiliaryFiles || []).forEach(filename => {
this._processFile(filename, chunk);
});
});
this.saveJson();
});
}

_processFile(filename, chunk) {
if (this._excludeChunk(this.options.excludeFile, filename, chunk) === true) {
return;
}
const ext = this.options.chunkGroupName(filename, chunk);
if (this.result[chunk.name][ext] === undefined) {
this.result[chunk.name][ext] = [];
}
this.result[chunk.name][ext].push(`${this.options.publicPath}${filename}`);
}

saveJson() {
// try to create outputDir folder if it is within project root
if (this._shouldFolderBeCreated(this.options.outputDir) === true) {
Expand All @@ -61,12 +72,16 @@ class Chunks2JsonWebpackPlugin {
const blob = this.options.objectToString(this.result);
try {
fs.writeFileSync(file, blob, { flag: 'w' });
console.log(`File successfully created - ${file}`);
this._showLogFile(file);
} catch(e) {
console.error(e);
}
}

_showLogFile(file) {
this.options.showLog && console.log(`File successfully created - ${file}`)
}

/**
* We might want to skip some entries
* this function checks users criteria and returns a bool
Expand Down
Loading