Skip to content

Commit e39ef79

Browse files
committed
Merge branch 'master' into feat/issue-template
2 parents 37bf41d + 9550230 commit e39ef79

File tree

8 files changed

+210
-34
lines changed

8 files changed

+210
-34
lines changed

.circleci/config.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: 2.1
2+
3+
jobs:
4+
test:
5+
parameters:
6+
node-version:
7+
type: string
8+
docker:
9+
- image: cimg/node:<< parameters.node-version >>
10+
steps:
11+
- checkout
12+
- run: |
13+
echo "Node version: $(node --version)"
14+
echo "NPM version: $(npm --version)"
15+
npm install
16+
npm run ci
17+
18+
workflows:
19+
all-tests:
20+
jobs:
21+
- test:
22+
matrix:
23+
parameters:
24+
node-version:
25+
# See https://nodejs.org/en/about/releases/
26+
- "current" # Right now: 16.5
27+
- "lts" # Right now: 14.17
28+
- "12.22" # Oldest maintenance LTS, End-of-Life 2022-04-30
29+
name: test-on-node-<< matrix.node-version >>

.travis.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Webpack Bundle Tracker [![Join the chat at https://gitter.im/owais/webpack-bundle-tracker](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/owais/webpack-bundle-tracker?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2+
[![master build status](https://circleci.com/gh/django-webpack/webpack-bundle-tracker.svg?style=svg)](https://circleci.com/gh/django-webpack/webpack-bundle-tracker)
23

34
Spits out some stats about webpack compilation process to a file.
45

@@ -10,6 +11,13 @@ Spits out some stats about webpack compilation process to a file.
1011
npm install --save-dev webpack-bundle-tracker
1112
```
1213

14+
<br>
15+
16+
## Compatibility
17+
18+
This project is compatible with NodeJS versions 12 and up.
19+
20+
1321
<br>
1422

1523
## Usage
@@ -173,10 +181,11 @@ passed to the `space` parameter in `JSON.stringify`. More information can be fou
173181

174182
| Name | Type | Default | Description |
175183
| ----------------- | ----------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
176-
| `path` | `{String}` | `'.'` | Output directory of bundle tracker JSON file . |
184+
| `path` | `{String}` | `'.'` | Output directory of bundle tracker JSON file. |
177185
| `filename` | `{String}` | `'webpack-stats.json'` | Name of the bundle tracker JSON file. |
178-
| `publicPath` | `{String}` | `?` | Override `output.publicPath` from Webpack config. |
179-
| `relativePath` | `{Boolean}` | `?` | Show relative path instead of absolute path for bundles in JSON Tracker file. Path are relative from path of JSON Tracker file. |
186+
| `publicPath` | `{String}` | (ignored) | Override `output.publicPath` from Webpack config. |
187+
| `relativePath` | `{Boolean}` | `false` | Show relative path instead of absolute path for bundles in JSON Tracker file. Path are relative from path of JSON Tracker file. |
180188
| `logTime` | `{Boolean}` | `false` | Output `startTime` and `endTime` properties in bundle tracker JSON file. |
181189
| `integrity` | `{Boolean}` | `false` | Output `integrity` property for each asset entry. |
182190
| `integrityHashes` | `{Array}` | `['sha256', 'sha384', 'sha512']` | Cryptographic hash functions used to compute integrity for each asset. |
191+
| `indent` | `{Integer}` | `undefined` | Format resulting JSON file for better readability. |

lib/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
/** @typedef {import("lodash.defaults")} defaults */
3-
/** @typedef {import("lodash.merge")} merge */
3+
/** @typedef {import("lodash.assign")} assign */
44
/** @typedef {import("lodash.get")} get */
55
/** @typedef {import("webpack").Compiler} Compiler */
66
/** @typedef {import("webpack").Stats} Stats */
@@ -15,7 +15,7 @@ const fs = require('fs');
1515
const crypto = require('crypto');
1616

1717
const defaults = require('lodash.defaults');
18-
const merge = require('lodash.merge');
18+
const assign = require('lodash.assign');
1919
const get = require('lodash.get');
2020
const each = require('lodash.foreach');
2121
const stripAnsi = require('strip-ansi');
@@ -40,6 +40,7 @@ class BundleTrackerPlugin {
4040
/** @type {Contents} */
4141
this.contents = {
4242
status: 'initialization',
43+
assets: {},
4344
chunks: {},
4445
};
4546
this.name = 'BundleTrackerPlugin';
@@ -61,6 +62,7 @@ class BundleTrackerPlugin {
6162
logTime: false,
6263
relativePath: false,
6364
integrity: false,
65+
indent: 2,
6466
// https://www.w3.org/TR/SRI/#cryptographic-hash-functions
6567
integrityHashes: ['sha256', 'sha384', 'sha512'],
6668
});
@@ -77,10 +79,13 @@ class BundleTrackerPlugin {
7779
* Write bundle tracker stats file
7880
*
7981
* @param {Compiler} compiler
80-
* @param {Contents} contents
82+
* @param {Partial<Contents>} contents
8183
*/
8284
_writeOutput(compiler, contents) {
83-
merge(this.contents, contents);
85+
assign(this.contents, contents, {
86+
assets: assign(this.contents.assets, contents.assets),
87+
chunks: assign(this.contents.chunks, contents.chunks),
88+
});
8489

8590
if (this.options.publicPath) {
8691
this.contents.publicPath = this.options.publicPath;
@@ -121,7 +126,13 @@ class BundleTrackerPlugin {
121126
*/
122127
_handleDone(compiler, stats) {
123128
if (stats.hasErrors()) {
124-
const error = stats.compilation.errors[0];
129+
const findError = compilation => {
130+
if (compilation.errors.length > 0) {
131+
return compilation.errors[0];
132+
}
133+
return compilation.children.find(child => findError(child));
134+
};
135+
const error = findError(stats.compilation);
125136
this._writeOutput(compiler, {
126137
status: 'error',
127138
error: get(error, 'name', 'unknown-error'),
@@ -131,6 +142,7 @@ class BundleTrackerPlugin {
131142
return;
132143
}
133144

145+
/** @type {Contents} */
134146
const output = { status: 'done', assets: {}, chunks: {} };
135147
each(stats.compilation.assets, (file, assetName) => {
136148
const fileInfo = {

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-bundle-tracker",
3-
"version": "1.1.0",
3+
"version": "1.4.0",
44
"description": "Spits out some stats about webpack compilation process to a file",
55
"keywords": [
66
"bundle",
@@ -24,7 +24,7 @@
2424
"test-debug": "node --inspect-brk=0.0.0.0 node_modules/jest/bin/jest --runInBand --env node",
2525
"posttest": "tsc",
2626
"test-watch": "jest --runInBand --env node --watchAll",
27-
"travis": "npm pretest && jest --runInBand --coverage --env node && npm posttest"
27+
"ci": "npm run pretest && jest --runInBand --coverage --env node && npm run posttest"
2828
},
2929
"jest": {
3030
"setupFilesAfterEnv": [
@@ -36,16 +36,13 @@
3636
"lodash.defaults": "^4.2.0",
3737
"lodash.foreach": "^4.5.0",
3838
"lodash.get": "^4.4.2",
39-
"lodash.merge": "^4.6.2",
4039
"strip-ansi": "^6.0.0"
4140
},
4241
"devDependencies": {
4342
"@types/lodash.assign": "^4.2.6",
43+
"@types/lodash.defaults": "^4.2.6",
4444
"@types/lodash.foreach": "^4.5.6",
4545
"@types/lodash.get": "^4.4.6",
46-
"@types/lodash.merge": "^4.6.6",
47-
"@types/lodash.defaults": "^4.2.6",
48-
"@types/mkdirp": "^1.0.1",
4946
"@types/node": "^13.13.52",
5047
"@types/webpack": "^4.41.28",
5148
"@typescript-eslint/eslint-plugin": "^2.34.0",

tests/base.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env jest */
22
'use strict';
33

4+
const fs = require('fs');
45
const zlib = require('zlib');
56
const path = require('path');
67
const rimraf = require('rimraf');
@@ -635,4 +636,70 @@ describe('BundleTrackerPlugin bases tests', () => {
635636
expectWarnings,
636637
);
637638
});
639+
640+
it('correctly merges chunks after multiple runs', done => {
641+
fs.writeFileSync(
642+
path.join(OUTPUT_DIR, 'app1.js'),
643+
`require(${JSON.stringify(path.resolve(__dirname, 'fixtures', 'commons.js'))});`,
644+
);
645+
const compiler = webpack(
646+
{
647+
context: __dirname,
648+
entry: {
649+
app1: path.join(OUTPUT_DIR, 'app1.js'),
650+
app2: path.resolve(__dirname, 'fixtures', 'app2.js'),
651+
},
652+
output: {
653+
path: path.join(OUTPUT_DIR, 'js'),
654+
filename: 'js/[name].js',
655+
publicPath: 'http://localhost:3000/assets/',
656+
},
657+
optimization: {
658+
splitChunks: {
659+
cacheGroups: {
660+
vendors: {
661+
name: 'vendors',
662+
test: /[\\/]node_modules[\\/]/,
663+
priority: -10,
664+
chunks: 'initial',
665+
},
666+
commons: {
667+
name: 'commons',
668+
test: /[\\/]?commons/,
669+
enforce: true,
670+
priority: -20,
671+
chunks: 'all',
672+
reuseExistingChunk: true,
673+
},
674+
default: {
675+
name: 'shared',
676+
reuseExistingChunk: true,
677+
},
678+
},
679+
},
680+
},
681+
plugins: [
682+
new BundleTrackerPlugin({
683+
path: OUTPUT_DIR,
684+
relativePath: true,
685+
includeParents: true,
686+
filename: path.join(OUTPUT_DIR, 'webpack-stats.json'),
687+
}),
688+
],
689+
},
690+
() => {
691+
// Edit app1.js and rerun the compilation
692+
fs.writeFileSync(path.join(OUTPUT_DIR, 'app1.js'), '');
693+
compiler.run(() => {
694+
const trackerStatsContent = fs.readFileSync(path.join(OUTPUT_DIR, 'webpack-stats.json'), 'utf8');
695+
const trackerStats = JSON.parse(trackerStatsContent);
696+
expect(trackerStats.chunks).toMatchObject({
697+
app1: ['js/app1.js'],
698+
app2: ['js/commons.js', 'js/app2.js'],
699+
});
700+
done();
701+
});
702+
},
703+
);
704+
});
638705
});

tests/webpack5.test.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env jest */
22
'use strict';
33

4+
const fs = require('fs');
45
const zlib = require('zlib');
56
const path = require('path');
67
const rimraf = require('rimraf');
@@ -120,7 +121,6 @@ describe('BundleTrackerPlugin bases tests', () => {
120121
],
121122
},
122123
{
123-
status: 'done',
124124
publicPath: 'https://test.org/statics/',
125125
status: 'done',
126126
chunks: {
@@ -636,4 +636,70 @@ describe('BundleTrackerPlugin bases tests', () => {
636636
expectWarnings,
637637
);
638638
});
639+
640+
it('correctly merges chunks after multiple runs', done => {
641+
fs.writeFileSync(
642+
path.join(OUTPUT_DIR, 'app1.js'),
643+
`require(${JSON.stringify(path.resolve(__dirname, 'fixtures', 'commons.js'))});`,
644+
);
645+
const compiler = webpack5(
646+
{
647+
context: __dirname,
648+
entry: {
649+
app1: path.join(OUTPUT_DIR, 'app1.js'),
650+
app2: path.resolve(__dirname, 'fixtures', 'app2.js'),
651+
},
652+
output: {
653+
path: path.join(OUTPUT_DIR, 'js'),
654+
filename: 'js/[name].js',
655+
publicPath: 'http://localhost:3000/assets/',
656+
},
657+
optimization: {
658+
splitChunks: {
659+
cacheGroups: {
660+
vendors: {
661+
name: 'vendors',
662+
test: /[\\/]node_modules[\\/]/,
663+
priority: -10,
664+
chunks: 'initial',
665+
},
666+
commons: {
667+
name: 'commons',
668+
test: /[\\/]?commons/,
669+
enforce: true,
670+
priority: -20,
671+
chunks: 'all',
672+
reuseExistingChunk: true,
673+
},
674+
default: {
675+
name: 'shared',
676+
reuseExistingChunk: true,
677+
},
678+
},
679+
},
680+
},
681+
plugins: [
682+
new BundleTrackerPlugin({
683+
path: OUTPUT_DIR,
684+
relativePath: true,
685+
includeParents: true,
686+
filename: path.join(OUTPUT_DIR, 'webpack-stats.json'),
687+
}),
688+
],
689+
},
690+
() => {
691+
// Edit app1.js and rerun the compilation
692+
fs.writeFileSync(path.join(OUTPUT_DIR, 'app1.js'), '');
693+
compiler.run(() => {
694+
const trackerStatsContent = fs.readFileSync(path.join(OUTPUT_DIR, 'webpack-stats.json'), 'utf8');
695+
const trackerStats = JSON.parse(trackerStatsContent);
696+
expect(trackerStats.chunks).toMatchObject({
697+
app1: ['js/app1.js'],
698+
app2: ['js/commons.js', 'js/app2.js'],
699+
});
700+
done();
701+
});
702+
},
703+
);
704+
});
639705
});

typings.d.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ declare namespace BundleTrackerPlugin {
3333
*/
3434
relativePath?: boolean;
3535
/**
36-
* Indent JSON ouput file
36+
* Indent JSON output file
3737
*/
3838
indent?: number;
3939
/**
@@ -60,17 +60,28 @@ declare namespace BundleTrackerPlugin {
6060
* Error message when webpack has failure from compilation
6161
*/
6262
message?: string;
63+
/**
64+
* File information
65+
*/
66+
assets: {
67+
[name: string]: {
68+
name: string;
69+
integrity?: string;
70+
publicPath?: string;
71+
path: string;
72+
};
73+
};
6374
/**
6475
* List of chunks builded
6576
*/
66-
chunks?: {
77+
chunks: {
6778
[name: string]: [
6879
{
6980
name: string;
7081
publicPath?: string;
7182
path: string;
72-
}
73-
]
83+
},
84+
];
7485
};
7586
/**
7687
* Public path of chunks

0 commit comments

Comments
 (0)