Skip to content

Commit

Permalink
Add assets source filenames
Browse files Browse the repository at this point in the history
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
  • Loading branch information
batistadasilva committed Mar 29, 2024
1 parent 97c7ec6 commit 0223bca
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class BundleTrackerPlugin {

output.assets[assetName] = fileInfo;
});
each(Object.fromEntries(stats.compilation.assetsInfo), ({ sourceFilename }, assetName) => {
if (!sourceFilename) return;

output.assets[assetName].sourceFilename = sourceFilename;
});
each(stats.compilation.chunkGroups, chunkGroup => {
if (!chunkGroup.isInitial()) return;

Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/appWithAssetResources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

const common = require('./commons');

require('./assets/resources/test.txt')

require(["./shared"], function(shared) {
shared("This is app with asset resources");
});
1 change: 1 addition & 0 deletions tests/fixtures/assets/resources/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
47 changes: 47 additions & 0 deletions tests/webpack5.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,53 @@ describe('BundleTrackerPlugin bases tests', () => {
);
});

it("shows original asset's filepath", done => {
const expectErrors = null;
const expectWarnings = getWebpack5WarningMessage();

testPlugin(
webpack5,
{
context: __dirname,
entry: {
appWithAssetResources: path.resolve(__dirname, 'fixtures', 'appWithAssetResources.js'),
},
output: {
assetModuleFilename: 'assets/[name]-[contenthash][ext]',
path: OUTPUT_DIR,
filename: 'js/[name].js',
publicPath: 'http://localhost:3000/assets/',
},
module: {
rules: [{ test: /\.txt$/, type: 'asset/resource' }],
},
plugins: [
new BundleTrackerPlugin({
path: OUTPUT_DIR,
relativePath: true,
includeParents: true,
filename: 'webpack-stats.json',
}),
],
},
{
status: 'done',
assets: {
'assets/test-bbf3c94e2a3948c98900.txt': {
name: 'assets/test-bbf3c94e2a3948c98900.txt',
path: 'assets/test-bbf3c94e2a3948c98900.txt',
publicPath: 'http://localhost:3000/assets/assets/test-bbf3c94e2a3948c98900.txt',
sourceFilename: 'fixtures/assets/resources/test.txt',
},
},
},
'webpack-stats.json',
done,
expectErrors,
expectWarnings,
);
});

it('correctly merges chunks after multiple runs', done => {
fs.writeFileSync(
path.join(OUTPUT_DIR, 'app1.js'),
Expand Down

0 comments on commit 0223bca

Please sign in to comment.