From fcdad694358b9882f7d3ab4998d9c1c9458f3f13 Mon Sep 17 00:00:00 2001 From: Felipe Batista da Silva Date: Thu, 28 Mar 2024 16:41:21 +0100 Subject: [PATCH 1/2] 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 https://github.com/django-webpack/django-webpack-loader/issues/343 --- lib/index.js | 6 +++ tests/fixtures/appWithAssetResources.js | 9 +++++ tests/fixtures/assets/resources/test.txt | 1 + tests/webpack5.test.js | 47 ++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 tests/fixtures/appWithAssetResources.js create mode 100644 tests/fixtures/assets/resources/test.txt diff --git a/lib/index.js b/lib/index.js index 7694509..4e4c2d4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 => { diff --git a/tests/fixtures/appWithAssetResources.js b/tests/fixtures/appWithAssetResources.js new file mode 100644 index 0000000..cc810ee --- /dev/null +++ b/tests/fixtures/appWithAssetResources.js @@ -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"); +}); diff --git a/tests/fixtures/assets/resources/test.txt b/tests/fixtures/assets/resources/test.txt new file mode 100644 index 0000000..802992c --- /dev/null +++ b/tests/fixtures/assets/resources/test.txt @@ -0,0 +1 @@ +Hello world diff --git a/tests/webpack5.test.js b/tests/webpack5.test.js index f32ea1d..aa8e695 100644 --- a/tests/webpack5.test.js +++ b/tests/webpack5.test.js @@ -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'), From 3ffd7ce0749e4a5aa7e654e6bd6a139892be84ee Mon Sep 17 00:00:00 2001 From: Felipe Batista da Silva Date: Tue, 2 Apr 2024 16:42:41 +0200 Subject: [PATCH 2/2] fix chunk name --- tests/webpack5.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/webpack5.test.js b/tests/webpack5.test.js index aa8e695..e4870a0 100644 --- a/tests/webpack5.test.js +++ b/tests/webpack5.test.js @@ -810,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();