|
5 | 5 |
|
6 | 6 | "use strict"; |
7 | 7 |
|
8 | | -const { SyncHook, SyncWaterfallHook } = require("tapable"); |
| 8 | +const { SyncBailHook, SyncHook, SyncWaterfallHook } = require("tapable"); |
9 | 9 | const { |
10 | 10 | CachedSource, |
11 | 11 | ConcatSource, |
@@ -93,6 +93,7 @@ const CssParser = require("./CssParser"); |
93 | 93 | * @typedef {object} CompilationHooks |
94 | 94 | * @property {SyncWaterfallHook<[Source, Module, ChunkRenderContext]>} renderModulePackage |
95 | 95 | * @property {SyncHook<[Chunk, Hash, ChunkHashContext]>} chunkHash |
| 96 | + * @property {SyncBailHook<[Chunk, Module[], Compilation], Module[] | undefined | void>} orderModules called for each CSS source type (CSS_IMPORT_TYPE, CSS_TYPE) with the chunk's modules pre-sorted by full module name; return an ordered `Module[]` to override the default import-order topological sort, or return `undefined` to keep the default |
96 | 97 | */ |
97 | 98 |
|
98 | 99 | /** |
@@ -160,7 +161,8 @@ class CssModulesPlugin { |
160 | 161 | "module", |
161 | 162 | "renderContext" |
162 | 163 | ]), |
163 | | - chunkHash: new SyncHook(["chunk", "hash", "context"]) |
| 164 | + chunkHash: new SyncHook(["chunk", "hash", "context"]), |
| 165 | + orderModules: new SyncBailHook(["chunk", "modules", "compilation"]) |
164 | 166 | }; |
165 | 167 | compilationHooksMap.set(compilation, hooks); |
166 | 168 | } |
@@ -869,24 +871,33 @@ class CssModulesPlugin { |
869 | 871 | /** @type {string | undefined} */ |
870 | 872 | let charset; |
871 | 873 |
|
| 874 | + const hooks = CssModulesPlugin.getCompilationHooks(compilation); |
| 875 | + |
| 876 | + /** |
| 877 | + * @param {Iterable<Module> | undefined} iter modules pre-sorted by full module name |
| 878 | + * @returns {Module[]} ordered modules |
| 879 | + */ |
| 880 | + const orderModules = (iter) => { |
| 881 | + const modules = iter ? [...iter] : []; |
| 882 | + const result = hooks.orderModules.call(chunk, modules, compilation); |
| 883 | + if (result !== undefined) return result; |
| 884 | + return this.getModulesInOrder(chunk, modules, compilation); |
| 885 | + }; |
| 886 | + |
872 | 887 | return /** @type {CssModule[]} */ ([ |
873 | | - ...this.getModulesInOrder( |
874 | | - chunk, |
| 888 | + ...orderModules( |
875 | 889 | chunkGraph.getOrderedChunkModulesIterableBySourceType( |
876 | 890 | chunk, |
877 | 891 | CSS_IMPORT_TYPE, |
878 | 892 | compareModulesByFullName(compilation.compiler) |
879 | | - ), |
880 | | - compilation |
| 893 | + ) |
881 | 894 | ), |
882 | | - ...this.getModulesInOrder( |
883 | | - chunk, |
| 895 | + ...orderModules( |
884 | 896 | chunkGraph.getOrderedChunkModulesIterableBySourceType( |
885 | 897 | chunk, |
886 | 898 | CSS_TYPE, |
887 | 899 | compareModulesByFullName(compilation.compiler) |
888 | | - ), |
889 | | - compilation |
| 900 | + ) |
890 | 901 | ).map((module) => { |
891 | 902 | if ( |
892 | 903 | typeof (/** @type {BuildInfo} */ (module.buildInfo).charset) !== |
|
0 commit comments