@@ -4,8 +4,11 @@ import webpack, { version as webpackVersion } from 'webpack';
44
55import validateOptions from 'schema-utils' ;
66
7+ import CssModuleFactory from './CssModuleFactory' ;
8+ import CssDependencyTemplate from './CssDependencyTemplate' ;
79import CssDependency from './CssDependency' ;
810import schema from './plugin-options.json' ;
11+ import { MODULE_TYPE , compareModulesByIdentifier } from './utils' ;
912
1013// webpack 5 exposes the sources property to ensure the right version of webpack-sources is used
1114const { ConcatSource, SourceMapSource, OriginalSource } =
@@ -19,8 +22,6 @@ const {
1922
2023const isWebpack4 = webpackVersion [ 0 ] === '4' ;
2124
22- const MODULE_TYPE = 'css/mini-extract' ;
23-
2425const pluginName = 'mini-css-extract-plugin' ;
2526
2627const REGEXP_CHUNKHASH = / \[ c h u n k h a s h (?: : ( \d + ) ) ? \] / i;
@@ -29,93 +30,6 @@ const REGEXP_NAME = /\[name\]/i;
2930const REGEXP_PLACEHOLDERS = / \[ ( n a m e | i d | c h u n k h a s h ) \] / g;
3031const DEFAULT_FILENAME = '[name].css' ;
3132
32- const compareIds = ( a , b ) => {
33- if ( typeof a !== typeof b ) {
34- return typeof a < typeof b ? - 1 : 1 ;
35- }
36- if ( a < b ) return - 1 ;
37- if ( a > b ) return 1 ;
38- return 0 ;
39- } ;
40-
41- const compareModulesByIdentifier = ( a , b ) => {
42- return compareIds ( a . identifier ( ) , b . identifier ( ) ) ;
43- } ;
44-
45- class CssDependencyTemplate {
46- apply ( ) { }
47- }
48-
49- class CssModule extends webpack . Module {
50- constructor ( dependency ) {
51- super ( MODULE_TYPE , dependency . context ) ;
52-
53- this . id = '' ;
54- this . _identifier = dependency . identifier ;
55- this . _identifierIndex = dependency . identifierIndex ;
56- this . content = dependency . content ;
57- this . media = dependency . media ;
58- this . sourceMap = dependency . sourceMap ;
59- }
60-
61- // no source() so webpack doesn't do add stuff to the bundle
62-
63- size ( ) {
64- return this . content . length ;
65- }
66-
67- identifier ( ) {
68- return `css ${ this . _identifier } ${ this . _identifierIndex } ` ;
69- }
70-
71- readableIdentifier ( requestShortener ) {
72- return `css ${ requestShortener . shorten ( this . _identifier ) } ${
73- this . _identifierIndex ? ` (${ this . _identifierIndex } )` : ''
74- } `;
75- }
76-
77- nameForCondition ( ) {
78- const resource = this . _identifier . split ( '!' ) . pop ( ) ;
79- const idx = resource . indexOf ( '?' ) ;
80-
81- if ( idx >= 0 ) {
82- return resource . substring ( 0 , idx ) ;
83- }
84-
85- return resource ;
86- }
87-
88- updateCacheModule ( module ) {
89- this . content = module . content ;
90- this . media = module . media ;
91- this . sourceMap = module . sourceMap ;
92- }
93-
94- needRebuild ( ) {
95- return true ;
96- }
97-
98- build ( options , compilation , resolver , fileSystem , callback ) {
99- this . buildInfo = { } ;
100- this . buildMeta = { } ;
101- callback ( ) ;
102- }
103-
104- updateHash ( hash , context ) {
105- super . updateHash ( hash , context ) ;
106-
107- hash . update ( this . content ) ;
108- hash . update ( this . media || '' ) ;
109- hash . update ( this . sourceMap ? JSON . stringify ( this . sourceMap ) : '' ) ;
110- }
111- }
112-
113- class CssModuleFactory {
114- create ( { dependencies : [ dependency ] } , callback ) {
115- callback ( null , new CssModule ( dependency ) ) ;
116- }
117- }
118-
11933class MiniCssExtractPlugin {
12034 constructor ( options = { } ) {
12135 validateOptions ( schema , options , 'Mini CSS Extract Plugin' ) ;
0 commit comments