@@ -19,44 +19,18 @@ const BOWER_COMPONENTS_PATTERN = `${path.sep}bower_components${path.sep}`
19
19
/** @internal */
20
20
export const ELECTRON_COMPILE_SHIM_FILENAME = "__shim.js"
21
21
22
- function extractPathAfterLastNodeModules ( src : string , file : string ) {
23
- const srcComponents = src . split ( path . sep )
24
- const lastNodeModulesIndex = srcComponents . lastIndexOf ( "node_modules" )
25
-
26
- if ( lastNodeModulesIndex === - 1 || lastNodeModulesIndex === srcComponents . length - 1 ) {
27
- return ""
28
- }
29
-
30
- const pathAfterNodeModules = srcComponents . slice ( lastNodeModulesIndex + 1 ) . join ( path . sep )
31
-
32
- const matchIndex = file . indexOf ( pathAfterNodeModules )
33
-
34
- if ( matchIndex === - 1 ) {
35
- return ""
36
- }
37
-
38
- const remainingPathStartIndex = matchIndex + pathAfterNodeModules . length
39
- if ( remainingPathStartIndex >= file . length ) {
40
- return ""
41
- }
42
-
43
- const remainingPath = file . substring ( remainingPathStartIndex ) . trim ( )
44
- return remainingPath . startsWith ( path . sep ) ? remainingPath . substring ( 1 ) : remainingPath
45
- }
46
-
47
22
export function getDestinationPath ( file : string , fileSet : ResolvedFileSet ) {
48
23
if ( file === fileSet . src ) {
49
24
return fileSet . destination
50
- } else {
51
- const src = fileSet . src
52
- const dest = fileSet . destination
53
- if ( file . length > src . length && file . startsWith ( src ) && file [ src . length ] === path . sep ) {
54
- return dest + file . substring ( src . length )
55
- } else {
56
- const e = extractPathAfterLastNodeModules ( src , file )
57
- return path . join ( dest , e )
58
- }
59
25
}
26
+
27
+ const src = fileSet . src
28
+ const dest = fileSet . destination
29
+ // get node_modules path relative to src and then append to dest
30
+ if ( file . startsWith ( src ) ) {
31
+ return path . join ( dest , path . relative ( src , file ) )
32
+ }
33
+ return dest
60
34
}
61
35
62
36
export async function copyAppFiles ( fileSet : ResolvedFileSet , packager : Packager , transformer : FileTransformer ) {
@@ -214,28 +188,21 @@ export async function computeNodeModuleFileSets(platformPackager: PlatformPackag
214
188
// serial execution because copyNodeModules is concurrent and so, no need to increase queue/pressure
215
189
const result = new Array < ResolvedFileSet > ( )
216
190
let index = 0
191
+ const NODE_MODULES = "node_modules"
217
192
for ( const info of deps ) {
218
- const source = platformPackager . info . appDir + path . sep + "node_modules" + path . sep + info . name
219
- // const source = platformPackager.info.appDir
220
- const destination = getDestinationPath ( source , { src : mainMatcher . from , destination : mainMatcher . to , files : [ ] , metadata : null as any } )
221
-
222
- // use main matcher patterns, so, user can exclude some files in such hoisted node modules
223
- // source here includes node_modules, but pattern base should be without because users expect that pattern "!node_modules/loot-core/src{,/**/*}" will work
224
- const matcher = new FileMatcher ( path . dirname ( platformPackager . info . appDir + path . sep + "node_modules" ) , destination , mainMatcher . macroExpander , mainMatcher . patterns )
193
+ const source = info . dir
194
+ const destination = path . join ( mainMatcher . to , NODE_MODULES , info . name )
195
+ // use main matcher patterns, so, user can exclude some files !node_modules/xxxx
196
+ const matcher = new FileMatcher ( path . dirname ( path . dirname ( source ) ) , destination , mainMatcher . macroExpander , mainMatcher . patterns )
225
197
const copier = new NodeModuleCopyHelper ( matcher , platformPackager . info )
226
198
const files = await copier . collectNodeModules ( info , nodeModuleExcludedExts )
227
199
result [ index ++ ] = validateFileSet ( { src : source , destination, files, metadata : copier . metadata } )
228
200
229
201
if ( info . conflictDependency ) {
230
202
for ( const dep of info . conflictDependency ) {
231
- const source = platformPackager . info . appDir + path . sep + "node_modules" + path . sep + info . name + path . sep + "node_modules" + path . sep + dep . name
232
- const destination = getDestinationPath ( source , { src : mainMatcher . from , destination : mainMatcher . to , files : [ ] , metadata : null as any } )
233
- const matcher = new FileMatcher (
234
- path . dirname ( platformPackager . info . appDir + path . sep + "node_modules" + path . sep + info . name + path . sep + "node_modules" ) ,
235
- destination ,
236
- mainMatcher . macroExpander ,
237
- mainMatcher . patterns
238
- )
203
+ const source = dep . dir
204
+ const destination = path . join ( mainMatcher . to , NODE_MODULES , info . name , NODE_MODULES , dep . name )
205
+ const matcher = new FileMatcher ( path . dirname ( path . dirname ( source ) ) , destination , mainMatcher . macroExpander , mainMatcher . patterns )
239
206
const copier = new NodeModuleCopyHelper ( matcher , platformPackager . info )
240
207
result [ index ++ ] = validateFileSet ( { src : source , destination, files : await copier . collectNodeModules ( dep , nodeModuleExcludedExts ) , metadata : copier . metadata } )
241
208
}
0 commit comments