From 65e685937194070d7a73f8f18e8eaa17b0cdd0ef Mon Sep 17 00:00:00 2001 From: Felipe Batista da Silva Date: Thu, 28 Mar 2024 16:41:21 +0100 Subject: [PATCH] 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 | 5 +++ tests/fixtures/appWithAssetResources.js | 9 +++++ tests/fixtures/assets/resources/test.txt | 1 + tests/webpack5.test.js | 47 ++++++++++++++++++++++++ 4 files changed, 62 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..2124bc2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; 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'),