Skip to content

Commit 4816591

Browse files
Add assets source filenames
Asset resources in `webpack-stats.json` file will look like, ```json "assets": { "assets/test-bbf3c94e2a3948c98900.txt": { "name": "assets/test-bbf3c94e2a3948c98900.txt", "path": "/home/user/project-root/assets/test-bbf3c94e2a3948c98900.txt", "publicPath": "http://localhost:3000/assets/test-bbf3c94e2a3948c98900.txt", "sourceFilename": "src/test.txt" } } ``` Related to django-webpack/django-webpack-loader#343
1 parent 97c7ec6 commit 4816591

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ class BundleTrackerPlugin {
192192
fileInfo.path = path.relative(this.outputChunkDir, fileInfo.path);
193193
}
194194

195+
fileInfo.sourceFilename = stats.compilation.assetsInfo.get(assetName).sourceFilename;
196+
195197
output.assets[assetName] = fileInfo;
196198
});
197199
each(stats.compilation.chunkGroups, chunkGroup => {
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
const common = require('./commons');
4+
5+
require('./assets/resources/test.txt')
6+
7+
require(["./shared"], function(shared) {
8+
shared("This is app with asset resources");
9+
});
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world

tests/webpack5.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,53 @@ describe('BundleTrackerPlugin bases tests', () => {
638638
);
639639
});
640640

641+
it("shows original asset's filepath", done => {
642+
const expectErrors = null;
643+
const expectWarnings = getWebpack5WarningMessage();
644+
645+
testPlugin(
646+
webpack5,
647+
{
648+
context: __dirname,
649+
entry: {
650+
appWithAssetResources: path.resolve(__dirname, 'fixtures', 'appWithAssetResources.js'),
651+
},
652+
output: {
653+
assetModuleFilename: 'assets/[name]-[contenthash][ext]',
654+
path: OUTPUT_DIR,
655+
filename: 'js/[name].js',
656+
publicPath: 'http://localhost:3000/assets/',
657+
},
658+
module: {
659+
rules: [{ test: /\.txt$/, type: 'asset/resource' }],
660+
},
661+
plugins: [
662+
new BundleTrackerPlugin({
663+
path: OUTPUT_DIR,
664+
relativePath: true,
665+
includeParents: true,
666+
filename: 'webpack-stats.json',
667+
}),
668+
],
669+
},
670+
{
671+
status: 'done',
672+
assets: {
673+
'assets/test-bbf3c94e2a3948c98900.txt': {
674+
name: 'assets/test-bbf3c94e2a3948c98900.txt',
675+
path: 'assets/test-bbf3c94e2a3948c98900.txt',
676+
publicPath: 'http://localhost:3000/assets/assets/test-bbf3c94e2a3948c98900.txt',
677+
sourceFilename: 'fixtures/assets/resources/test.txt',
678+
},
679+
},
680+
},
681+
'webpack-stats.json',
682+
done,
683+
expectErrors,
684+
expectWarnings,
685+
);
686+
});
687+
641688
it('correctly merges chunks after multiple runs', done => {
642689
fs.writeFileSync(
643690
path.join(OUTPUT_DIR, 'app1.js'),

0 commit comments

Comments
 (0)