Skip to content

Commit 65e6859

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 65e6859

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

lib/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ class BundleTrackerPlugin {
194194

195195
output.assets[assetName] = fileInfo;
196196
});
197+
each(Object.fromEntries(stats.compilation.assetsInfo), ({ sourceFilename }, assetName) => {
198+
if (!sourceFilename) return;
199+
200+
output.assets[assetName].sourceFilename = sourceFilename;
201+
});
197202
each(stats.compilation.chunkGroups, chunkGroup => {
198203
if (!chunkGroup.isInitial()) return;
199204

+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)