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 Apr 3, 2024
1 parent 97c7ec6 commit 2662683
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ class BundleTrackerPlugin {
fileInfo.path = path.relative(this.outputChunkDir, fileInfo.path);
}

if (stats.compilation.assetsInfo) {
fileInfo.sourceFilename = stats.compilation.assetsInfo.get(assetName).sourceFilename;
}

output.assets[assetName] = fileInfo;
});
each(stats.compilation.chunkGroups, chunkGroup => {
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 2662683

Please sign in to comment.