@@ -48,6 +48,7 @@ module.exports = function (content, map) {
48
48
subPackagesMap,
49
49
mainResourceMap,
50
50
additionalAssets,
51
+ toBeRewriteSrcSet,
51
52
} = compilationMpx ;
52
53
53
54
const appRequestpath = mainCompilation . _preparedEntrypoints [ 0 ] . request ;
@@ -90,6 +91,30 @@ module.exports = function (content, map) {
90
91
projectRoot ,
91
92
) ;
92
93
94
+ function retrieveSubPackRoot ( originOutputPath , requestPath ) {
95
+ let subPackageRoot = '' ;
96
+ // node_modules引用的是同一路径,只有子包引用时,这种在小程序引用不合法但对于npm引用合法,会根据源的打包输出路径来决定是否应该在子包打几份输出
97
+ if ( processingSubPackages ) {
98
+ for ( let src in subPackagesMap ) {
99
+ // 分包引用且主包未引用的资源,需打入分包目录中
100
+ if ( originOutputPath . startsWith ( src ) && ! mainResourceMap [ requestPath ] ) {
101
+ subPackageRoot = subPackagesMap [ src ] ;
102
+ break ;
103
+ }
104
+ }
105
+ } else {
106
+ mainResourceMap [ requestPath ] = true ;
107
+ }
108
+ return subPackageRoot ;
109
+ }
110
+
111
+ function genImportOutputFileName ( subPackageRoot , requestPath ) {
112
+ const selfResourceName = path . parse ( requestPath ) . name ;
113
+ const requestHash = hash ( requestPath ) ;
114
+ const outputFilename = toPosix ( path . join ( subPackageRoot , 'wxss' , selfResourceName + requestHash + typeExtMap . styles ) ) ;
115
+ return outputFilename ;
116
+ }
117
+
93
118
if ( sourceMap ) {
94
119
if ( map ) {
95
120
if ( typeof map === 'string' ) {
@@ -154,35 +179,29 @@ module.exports = function (content, map) {
154
179
if ( loaderUtils . isUrlRequest ( url , root ) ) {
155
180
const originalPathWithoutExt = stripExtension ( originFilePath ) ;
156
181
// Injected in child compilation to memorize origin component/page/app and parent css output location
157
- let originOutputName =
182
+ let originOutputName ;
183
+
184
+ // output name which was be rewritten from mpx file should be regenerated
185
+ if ( toBeRewriteSrcSet . has ( originFilePath ) ) {
186
+ const originSubPackageRoot = retrieveSubPackRoot ( originFilePath , originFilePath ) ;
187
+ originOutputName = genImportOutputFileName ( originSubPackageRoot , originFilePath ) ;
188
+ } else {
158
189
// memorized origin
159
- thisCompilation . $originalOutputName
160
- // page
161
- || pagesMap [ originalPathWithoutExt ]
162
- // component
163
- || componentsMap [ originalPathWithoutExt ]
164
- // app
165
- || path . relative ( projectRoot , appRequestpath ) ;
190
+ originOutputName = thisCompilation . $originalOutputName
191
+ // page
192
+ || pagesMap [ originalPathWithoutExt ]
193
+ // component
194
+ || componentsMap [ originalPathWithoutExt ]
195
+ // app
196
+ || path . relative ( projectRoot , appRequestpath ) ;
197
+ }
198
+
166
199
const originOutputPath = path . resolve ( projectRoot , originOutputName ) ;
167
200
168
201
// Part of these codes are copied from extract function
169
202
const requestPath = path . resolve ( path . dirname ( originFilePath ) , url ) ;
170
- const selfResourceName = path . parse ( requestPath ) . name ;
171
- let subPackageRoot = '' ;
172
- // node_modules引用的是同一路径,只有子包引用时,这种在小程序引用不合法但对于npm引用合法,会根据源的打包输出路径来决定是否应该在子包打几份输出
173
- if ( compilationMpx . processingSubPackages ) {
174
- for ( let src in subPackagesMap ) {
175
- // 分包引用且主包未引用的资源,需打入分包目录中
176
- if ( originOutputPath . startsWith ( src ) && ! mainResourceMap [ requestPath ] ) {
177
- subPackageRoot = subPackagesMap [ src ] ;
178
- break ;
179
- }
180
- }
181
- } else {
182
- mainResourceMap [ requestPath ] = true ;
183
- }
184
- const requestHash = hash ( requestPath ) ;
185
- const outputFilename = toPosix ( path . join ( subPackageRoot , 'wxss' , selfResourceName + requestHash + typeExtMap . styles ) ) ;
203
+ const outputSubPackageRoot = retrieveSubPackRoot ( originOutputPath , requestPath ) ;
204
+ const outputFilename = genImportOutputFileName ( outputSubPackageRoot , requestPath ) ;
186
205
187
206
const parentOutputName = thisCompilation . $parentOutputName || originOutputName ;
188
207
@@ -207,7 +226,7 @@ module.exports = function (content, map) {
207
226
new NodeTemplatePlugin ( outputOptions ) ,
208
227
new LibraryTemplatePlugin ( null , 'commonjs2' ) ,
209
228
new NodeTargetPlugin ( ) ,
210
- new SingleEntryPlugin ( this . context , childRequest , childFilename ) ,
229
+ new SingleEntryPlugin ( loaderContext . context , childRequest , childFilename ) ,
211
230
new LimitChunkCountPlugin ( { maxChunks : 1 } )
212
231
] ) ;
213
232
@@ -247,7 +266,7 @@ module.exports = function (content, map) {
247
266
}
248
267
249
268
try {
250
- let text = this . exec ( source , childRequest ) ;
269
+ let text = loaderContext . exec ( source , childRequest ) ;
251
270
if ( Array . isArray ( text ) ) {
252
271
text = text . map ( ( item ) => {
253
272
return item [ 1 ] ;
0 commit comments