Skip to content

Commit 340dc1a

Browse files
committed
Don't allow slashes on filename
1 parent 051d882 commit 340dc1a

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ npm install --save-dev webpack-bundle-tracker
1414
This project is compatible with NodeJS versions 12 and up.
1515

1616
## Migrating from version 1.x.y to 2.x.y
17-
Starting on version 2.0.0, when creating a new instance of `BundleTracker`, the usage of the `path` parameter has been fixed and it's now being used to generate the output path for the stats file, together with the `filename` parameter. On version 2.0.0, if the `path` parameter is ommited from the constuctor call, it will attempt to place the stats file at the `output.path` directory (if also ommited, will use `'.'` as a fallback). To avoid that, when migrating, double-check if the file placement is as expected. The usage of these parameters is documented [here](#usage) and [here](#options).
17+
Starting on version 2.0.0, when creating a new instance of `BundleTracker`, the usage of the `path` parameter has been fixed and it's now being used to generate the output path for the stats file, together with the `filename` parameter. On version 2.0.0, if the `path` parameter is ommited from the constuctor call, it will attempt to place the stats file at the `output.path` directory (if also ommited, will use `'.'` as a fallback). Also, version 2.0.0 doesn't allow sub-directories to be included on the `filename`, only allowing to include them on the `path` param. To avoid those issues, when migrating, double-check if the file placement is as expected. The usage of these parameters is documented [here](#usage) and [here](#options).
1818

1919
## Usage
2020

2121
```javascript
22+
var path = require('path');
2223
var BundleTracker = require('webpack-bundle-tracker');
2324
module.exports = {
2425
context: __dirname,
@@ -27,15 +28,15 @@ module.exports = {
2728
},
2829

2930
output: {
30-
path: require('path').resolve('./assets/bundles/'),
31+
path: path.resolve('./assets/bundles/'),
3132
filename: '[name]-[hash].js',
3233
publicPath: 'http://localhost:3000/assets/bundles/',
3334
},
3435

3536
plugins: [
3637
new BundleTracker({
37-
path: __dirname,
38-
filename: 'assets/webpack-stats.json',
38+
path: path.join(__dirname, 'assets'),
39+
filename: 'webpack-stats.json',
3940
}),
4041
],
4142
};

lib/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ class BundleTrackerPlugin {
8484
integrityHashes: ['sha256', 'sha384', 'sha512'],
8585
});
8686

87+
if (this.options.filename?.includes('/')) {
88+
throw Error(
89+
"The `filename` shouldn't include a `/`. Please use the `path` parameter to " +
90+
"build the directory path and use `filename` only for the file's name itself.\n" +
91+
'TIP: you can use `path.join` to build the directory path in your config file.',
92+
);
93+
}
94+
8795
// Set output directories
8896
this.outputChunkDir = path.resolve(get(compiler.options, 'output.path', process.cwd()));
8997
// @ts-ignore: TS2345 this.options.path can't be undefined here because we set a default value above

tests/base.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ describe('BundleTrackerPlugin bases tests', () => {
185185
);
186186
});
187187

188-
it('It should create intermdiate directory if filename option is set with intermdiate directory', done => {
188+
it('It should create intermdiate directory if path option is set with intermdiate directory', done => {
189189
const expectErrors = null;
190190
const expectWarnings = getWebpack4WarningMessage();
191191

192-
const filename = 'data/stats.json';
192+
const filename = 'stats.json';
193193

194194
testPlugin(
195195
webpack,
@@ -203,7 +203,7 @@ describe('BundleTrackerPlugin bases tests', () => {
203203
},
204204
plugins: [
205205
new BundleTrackerPlugin({
206-
path: OUTPUT_DIR,
206+
path: path.join(OUTPUT_DIR, 'data'),
207207
filename,
208208
}),
209209
],
@@ -221,7 +221,7 @@ describe('BundleTrackerPlugin bases tests', () => {
221221
},
222222
},
223223
},
224-
filename,
224+
`data/${filename}`,
225225
done,
226226
expectErrors,
227227
expectWarnings,

tests/webpack5.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ describe('BundleTrackerPlugin bases tests', () => {
185185
);
186186
});
187187

188-
it('It should create intermdiate directory if filename option is set with intermdiate directory', done => {
188+
it('It should create intermdiate directory if path option is set with intermdiate directory', done => {
189189
const expectErrors = null;
190190
const expectWarnings = getWebpack5WarningMessage();
191191

192-
const filename = 'data/stats.json';
192+
const filename = 'stats.json';
193193

194194
testPlugin(
195195
webpack5,
@@ -203,7 +203,7 @@ describe('BundleTrackerPlugin bases tests', () => {
203203
},
204204
plugins: [
205205
new BundleTrackerPlugin({
206-
path: OUTPUT_DIR,
206+
path: path.join(OUTPUT_DIR, 'data'),
207207
filename,
208208
}),
209209
],
@@ -221,7 +221,7 @@ describe('BundleTrackerPlugin bases tests', () => {
221221
},
222222
},
223223
},
224-
filename,
224+
`data/${filename}`,
225225
done,
226226
expectErrors,
227227
expectWarnings,

0 commit comments

Comments
 (0)