Skip to content

Commit

Permalink
Merge pull request #125 from hostnfly/assets-source-filenames
Browse files Browse the repository at this point in the history
Add assets source filenames
  • Loading branch information
fjsj authored Apr 3, 2024
2 parents 97c7ec6 + 3ffd7ce commit b0a7c27
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ class BundleTrackerPlugin {
fileInfo.path = path.relative(this.outputChunkDir, fileInfo.path);
}

// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
if (stats.compilation.assetsInfo) {
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
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
49 changes: 48 additions & 1 deletion 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 Expand Up @@ -763,7 +810,7 @@ describe('BundleTrackerPlugin bases tests', () => {
const assetsKeys = toPairs(stats.assets).map(pair => pair[0]);
const chunksKeys = toPairs(stats.chunks).map(pair => pair[0]);

expect(assetsKeys).toEqual(['css/appA.css', 'js/862.js', 'js/appA.js', 'js/appZ.js', 'js/commons.js']);
expect(assetsKeys).toEqual(['css/appA.css', 'js/75.js', 'js/appA.js', 'js/appZ.js', 'js/commons.js']);
expect(chunksKeys).toEqual(['appA', 'appZ']);

done();
Expand Down

0 comments on commit b0a7c27

Please sign in to comment.