77
88const makeSerializable = require ( "../util/makeSerializable" ) ;
99const CssIcssExportDependency = require ( "./CssIcssExportDependency" ) ;
10+ const CssImportDependency = require ( "./CssImportDependency" ) ;
1011const CssLocalIdentifierDependency = require ( "./CssLocalIdentifierDependency" ) ;
11- const ModuleDependency = require ( "./ModuleDependency" ) ;
1212
1313/** @typedef {import("webpack-sources").ReplaceSource } ReplaceSource */
1414/** @typedef {import("../Dependency") } Dependency */
@@ -18,35 +18,30 @@ const ModuleDependency = require("./ModuleDependency");
1818/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext } ObjectDeserializerContext */
1919/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext } ObjectSerializerContext */
2020
21- class CssIcssImportDependency extends ModuleDependency {
21+ class CssIcssImportDependency extends CssImportDependency {
2222 /**
2323 * Example of dependency:
2424 *
2525 *:import('./style.css') { IMPORTED_NAME: v-primary }
2626 * @param {string } request request request path which needs resolving
27- * @param {string } exportName export name
2827 * @param {Range } range the range of dependency
28+ * @param {"local" | "global" } mode mode of the parsed CSS
29+ * @param {string } exportName export name
2930 */
30- constructor ( request , exportName , range ) {
31- super ( request ) ;
31+ constructor ( request , range , mode , exportName ) {
32+ super ( request , range , mode ) ;
3233 this . exportName = exportName ;
33- this . range = range ;
3434 }
3535
3636 get type ( ) {
3737 return "css :import" ;
3838 }
3939
40- get category ( ) {
41- return "css-import" ;
42- }
43-
4440 /**
4541 * @param {ObjectSerializerContext } context context
4642 */
4743 serialize ( context ) {
4844 const { write } = context ;
49- write ( this . range ) ;
5045 write ( this . exportName ) ;
5146 super . serialize ( context ) ;
5247 }
@@ -56,14 +51,13 @@ class CssIcssImportDependency extends ModuleDependency {
5651 */
5752 deserialize ( context ) {
5853 const { read } = context ;
59- this . range = read ( ) ;
6054 this . exportName = read ( ) ;
6155 super . deserialize ( context ) ;
6256 }
6357}
6458
6559CssIcssImportDependency . Template = class CssIcssImportDependencyTemplate extends (
66- ModuleDependency . Template
60+ CssImportDependency . Template
6761) {
6862 /**
6963 * @param {Dependency } dependency the dependency for which the template should be applied
@@ -79,36 +73,38 @@ CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends
7973 ( templateContext . moduleGraph . getModule ( dep ) ) ;
8074 let value ;
8175
82- for ( const item of module . dependencies ) {
83- if (
84- item instanceof CssLocalIdentifierDependency &&
85- dep . exportName === item . name
86- ) {
87- value = CssLocalIdentifierDependency . Template . getIdentifier (
88- item . prefix ,
89- dep . exportName ,
90- {
91- ...templateContext ,
92- module
93- }
76+ if ( module ) {
77+ for ( const item of module . dependencies ) {
78+ if (
79+ item instanceof CssLocalIdentifierDependency &&
80+ dep . exportName === item . name
81+ ) {
82+ value = CssLocalIdentifierDependency . Template . getIdentifier (
83+ item . prefix ,
84+ dep . exportName ,
85+ {
86+ ...templateContext ,
87+ module
88+ }
89+ ) ;
90+ break ;
91+ } else if (
92+ item instanceof CssIcssExportDependency &&
93+ dep . exportName === item . name
94+ ) {
95+ value = item . value ;
96+ break ;
97+ }
98+ }
99+
100+ if ( ! value ) {
101+ throw new Error (
102+ `Imported '${ dep . exportName } ' name from '${ dep . request } ' not found`
94103 ) ;
95- break ;
96- } else if (
97- item instanceof CssIcssExportDependency &&
98- dep . exportName === item . name
99- ) {
100- value = item . value ;
101- break ;
102104 }
103- }
104105
105- if ( ! value ) {
106- throw new Error (
107- `Imported '${ dep . exportName } ' name from '${ dep . request } ' not found`
108- ) ;
106+ source . replace ( range [ 0 ] , range [ 1 ] , value ) ;
109107 }
110-
111- source . replace ( range [ 0 ] , range [ 1 ] , value ) ;
112108 }
113109} ;
114110
0 commit comments