@@ -144,7 +144,7 @@ export async function main() {
144
144
// find all packages (including packages in nested node_modules)
145
145
const pkgs : Dep [ ] = [ ] ;
146
146
147
- await findPackagesAndPush ( pkgs , 'node_modules' , deps ) ;
147
+ await findPackagesAndPush ( pkgs , nodeModulesFolder ( ) , deps ) ;
148
148
149
149
// Sort the files to ensure the order
150
150
pkgs . sort ( compareDep ) ;
@@ -162,6 +162,12 @@ export async function main() {
162
162
await writeFile ( '.bazelignore' , `node_modules\n${ config . workspace_rerooted_path } ` ) ;
163
163
}
164
164
165
+ function nodeModulesFolder ( ) : string {
166
+ return config . exports_directories_only ?
167
+ `${ config . workspace_rerooted_package_json_dir } /node_modules` :
168
+ 'node_modules' ;
169
+ }
170
+
165
171
/**
166
172
* Generates all build files
167
173
*/
@@ -206,7 +212,7 @@ function flattenDependencies(pkgs: Dep[]) {
206
212
async function generateRootBuildFile ( pkgs : Dep [ ] ) {
207
213
let buildFile = config . exports_directories_only ?
208
214
printRootExportsDirectories ( pkgs ) :
209
- printRoot ( pkgs ) ;
215
+ printRootExportsAllFiles ( pkgs ) ;
210
216
211
217
// Add the manual build file contents if they exists
212
218
try {
@@ -219,15 +225,27 @@ async function generateRootBuildFile(pkgs: Dep[]) {
219
225
await writeFile ( 'BUILD.bazel' , buildFile ) ;
220
226
}
221
227
228
+
222
229
function printRootExportsDirectories ( pkgs : Dep [ ] ) {
223
230
let filegroupsStarlark = '' ;
224
- pkgs . forEach ( pkg => filegroupsStarlark += `filegroup(
225
- name = "${ pkg . _dir . replace ( "/" , "_" ) } __source_directory",
226
- srcs = ["node_modules/${ pkg . _dir } "],
227
- visibility = ["@${ config . workspace } //:__subpackages__"],
231
+ pkgs . forEach ( pkg => {
232
+ filegroupsStarlark += `
233
+ copy_file(
234
+ name = "node_modules/${ pkg . _dir } ",
235
+ src = "${ config . workspace_rerooted_package_json_dir } /node_modules/${ pkg . _dir } ",
236
+ is_directory = True,
237
+ out = "node_modules/${ pkg . _dir } ",
238
+ visibility = ["//visibility:public"],
228
239
)
229
-
230
- ` ) ;
240
+ js_library(
241
+ name = "${ pkg . _dir . replace ( "/" , "_" ) } __contents",
242
+ package_name = "${ pkg . _dir } ",
243
+ package_path = "${ config . package_path } ",
244
+ strip_prefix = "node_modules/${ pkg . _dir } ",
245
+ srcs = [":node_modules/${ pkg . _dir } "],
246
+ visibility = ["//:__subpackages__"],
247
+ )
248
+ ` } ) ;
231
249
232
250
let depsStarlark = '' ;
233
251
if ( pkgs . length ) {
@@ -240,7 +258,11 @@ if (pkgs.length) {
240
258
241
259
const result =
242
260
generateBuildFileHeader ( ) + `load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
261
+ load("@build_bazel_rules_nodejs//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")
243
262
263
+ # To support remote-execution, we must create a tree artifacts from the source directories.
264
+ # We make the output node_modules/pkg_dir so that we get a free node_modules
265
+ # tree in bazel-out and runfiles for this external repository.
244
266
${ filegroupsStarlark }
245
267
246
268
# The node_modules directory in one catch-all js_library
@@ -251,7 +273,7 @@ js_library(
251
273
return result
252
274
}
253
275
254
- function printRoot ( pkgs : Dep [ ] ) {
276
+ function printRootExportsAllFiles ( pkgs : Dep [ ] ) {
255
277
let pkgFilesStarlark = '' ;
256
278
if ( pkgs . length ) {
257
279
let list = '' ;
@@ -312,8 +334,10 @@ async function generatePackageBuildFiles(pkg: Dep) {
312
334
if ( pkg . _files . includes ( 'BUILD' ) ) buildFilePath = 'BUILD' ;
313
335
if ( pkg . _files . includes ( 'BUILD.bazel' ) ) buildFilePath = 'BUILD.bazel' ;
314
336
337
+ const nodeModules = nodeModulesFolder ( ) ;
338
+
315
339
// Recreate the pkg dir inside the node_modules folder
316
- const nodeModulesPkgDir = `node_modules /${ pkg . _dir } ` ;
340
+ const nodeModulesPkgDir = `${ nodeModules } /${ pkg . _dir } ` ;
317
341
// Check if the current package dep dir is a symlink (which happens when we
318
342
// install a node_module with link:)
319
343
const isPkgDirASymlink = await fs . lstat ( nodeModulesPkgDir )
@@ -334,10 +358,10 @@ async function generatePackageBuildFiles(pkg: Dep) {
334
358
// The following won't be used in a symlink build file case
335
359
let buildFile = config . exports_directories_only ?
336
360
printPackageExportsDirectories ( pkg ) :
337
- printPackage ( pkg ) ;
361
+ printPackageLegacy ( pkg ) ;
338
362
if ( buildFilePath ) {
339
363
buildFile = buildFile + '\n' +
340
- await fs . readFile ( path . join ( 'node_modules' , pkg . _dir , buildFilePath ) , 'utf-8' ) ;
364
+ await fs . readFile ( path . join ( nodeModules , pkg . _dir , buildFilePath ) , 'utf-8' ) ;
341
365
} else {
342
366
buildFilePath = 'BUILD.bazel' ;
343
367
}
@@ -377,7 +401,7 @@ async function generatePackageBuildFiles(pkg: Dep) {
377
401
if ( basenameUc === '_BUILD' || basenameUc === '_BUILD.BAZEL' ) {
378
402
destFile = path . posix . join ( path . dirname ( destFile ) , basename . substr ( 1 ) ) ;
379
403
}
380
- const src = path . posix . join ( 'node_modules' , pkg . _dir , file ) ;
404
+ const src = path . posix . join ( nodeModules , pkg . _dir , file ) ;
381
405
await prev ;
382
406
await mkdirp ( path . dirname ( destFile ) ) ;
383
407
await fs . copyFile ( src , destFile ) ;
@@ -452,6 +476,7 @@ load("@build_bazel_rules_nodejs//internal/copy_repository:copy_repository.bzl",
452
476
// Copy all files for this workspace to a folder under _workspaces
453
477
// to restore the Bazel files which have be renamed from the npm package
454
478
const workspaceSourcePath = path . posix . join ( '_workspaces' , workspace ) ;
479
+ const nodeModules = nodeModulesFolder ( ) ;
455
480
await mkdirp ( workspaceSourcePath ) ;
456
481
await Promise . all ( pkg . _files . map ( async ( file ) => {
457
482
if ( / ^ n o d e _ m o d u l e s [ / \\ ] / . test ( file ) ) {
@@ -471,7 +496,7 @@ load("@build_bazel_rules_nodejs//internal/copy_repository:copy_repository.bzl",
471
496
if ( basenameUc === '_BUILD' || basenameUc === '_BUILD.BAZEL' ) {
472
497
destFile = path . posix . join ( path . dirname ( destFile ) , basename . substr ( 1 ) ) ;
473
498
}
474
- const src = path . posix . join ( 'node_modules' , pkg . _dir , file ) ;
499
+ const src = path . posix . join ( nodeModules , pkg . _dir , file ) ;
475
500
const dest = path . posix . join ( workspaceSourcePath , destFile ) ;
476
501
await mkdirp ( path . dirname ( dest ) ) ;
477
502
await fs . copyFile ( src , dest ) ;
@@ -517,7 +542,7 @@ async function generateScopeBuildFiles(scope: string, pkgs: Dep[]) {
517
542
518
543
let buildFile = config . exports_directories_only ?
519
544
printScopeExportsDirectories ( scope , deps ) :
520
- printScope ( scope , deps ) ;
545
+ printScopeLegacy ( scope , deps ) ;
521
546
await writeFile ( path . posix . join ( scope , 'BUILD.bazel' ) , buildFile ) ;
522
547
}
523
548
@@ -676,7 +701,7 @@ async function findPackagesAndPush(pkgs: Dep[], p: string, dependencies: Set<str
676
701
* Finds and returns an array of all package scopes in node_modules.
677
702
*/
678
703
async function findScopes ( ) {
679
- const p = 'node_modules' ;
704
+ const p = nodeModulesFolder ( ) ;
680
705
if ( ! await isDirectory ( p ) ) {
681
706
return [ ] ;
682
707
}
@@ -688,8 +713,8 @@ async function findScopes() {
688
713
if ( ! f . startsWith ( '@' ) ) return ;
689
714
f = path . posix . join ( p , f ) ;
690
715
if ( await isDirectory ( f ) ) {
691
- // strip 'node_modules/ ' from filename
692
- return f . substring ( 'node_modules/' . length ) ;
716
+ // strip leading 'node_modules' from filename
717
+ return f . substring ( p . length + 1 ) ;
693
718
}
694
719
} )
695
720
) )
@@ -709,10 +734,10 @@ export async function parsePackage(p: string, dependencies: Set<string> = new Se
709
734
const pkg = ( await isFile ( packageJson ) ) ?
710
735
JSON . parse ( stripBom ( await fs . readFile ( packageJson , { encoding : 'utf8' } ) ) ) :
711
736
{ version : '0.0.0' } ;
712
-
737
+
713
738
// Trim the leading node_modules from the path and
714
739
// assign to _dir for future use
715
- pkg . _dir = p . substring ( 'node_modules/' . length ) ;
740
+ pkg . _dir = p . substring ( nodeModulesFolder ( ) . length + 1 ) ;
716
741
717
742
// Stash the package directory name for future use
718
743
pkg . _name = pkg . _dir . split ( '/' ) . pop ( ) ;
@@ -1049,64 +1074,27 @@ function printPackageExportsDirectories(pkg: Dep) {
1049
1074
// Flattened list of direct and transitive dependencies hoisted to root by the package manager
1050
1075
const deps = [ pkg ] . concat ( pkg . _dependencies . filter ( dep => dep !== pkg && ! dep . _isNested ) ) ;
1051
1076
const depsStarlark =
1052
- deps . map ( dep => `"//${ dep . _dir } : ${ dep . _name } __contents",` ) . join ( '\n ' ) ;
1077
+ deps . map ( dep => `"//: ${ dep . _dir . replace ( "/" , "_" ) } __contents",` ) . join ( '\n ' ) ;
1053
1078
1054
1079
return `load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
1055
- load("@build_bazel_rules_nodejs//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")
1056
1080
1057
1081
# Generated targets for npm package "${ pkg . _dir } "
1058
1082
${ printJson ( pkg ) }
1059
1083
1060
- # To support remote-execution, we must create a tree artifact from the source directory
1061
- copy_file(
1062
- name = "directory",
1063
- src = "@${ config . workspace } //:${ pkg . _dir . replace ( "/" , "_" ) } __source_directory",
1064
- is_directory = True,
1065
- out = "tree",
1066
- )
1067
-
1068
1084
# The primary target for this package for use in rule deps
1069
1085
js_library(
1070
1086
name = "${ pkg . _name } ",
1071
1087
deps = [
1072
1088
${ depsStarlark }
1073
1089
],
1074
1090
)
1075
-
1076
- # Target is used as dep for main targets to prevent circular dependencies errors
1077
- js_library(
1078
- name = "contents",
1079
- package_name = "${ pkg . _dir } ",
1080
- package_path = "${ config . package_path } ",
1081
- strip_prefix = "tree",
1082
- srcs = [":directory"],
1083
- visibility = ["//:__subpackages__"],
1084
- )
1085
-
1086
- # For ts_library backward compat which uses @npm//typescript:__files
1087
- alias(
1088
- name = "${ pkg . _name } __files",
1089
- actual = "directory",
1090
- )
1091
-
1092
- # For ts_library backward compat which uses @npm//typescript:__files
1093
- alias(
1094
- name = "${ pkg . _name } __contents",
1095
- actual = "contents",
1096
- )
1097
-
1098
- # For ts_library backward compat which uses @npm//typescript:typescript__typings
1099
- alias(
1100
- name = "${ pkg . _name } __typings",
1101
- actual = "contents",
1102
- )
1103
1091
` ;
1104
1092
}
1105
1093
1106
1094
/**
1107
1095
* Given a pkg, return the skylark `js_library` targets for the package.
1108
1096
*/
1109
- function printPackage ( pkg : Dep ) {
1097
+ function printPackageLegacy ( pkg : Dep ) {
1110
1098
function starlarkFiles ( attr : string , files : string [ ] , comment : string = '' ) {
1111
1099
return `
1112
1100
${ comment ? comment + '\n ' : '' } ${ attr } = [
@@ -1316,7 +1304,7 @@ export function printPackageBin(pkg: Dep) {
1316
1304
1317
1305
for ( const [ name , path ] of executables . entries ( ) ) {
1318
1306
const entryPoint = config . exports_directories_only ?
1319
- `{ "@${ config . workspace } //${ pkg . _dir } :directory ": "${ path } " }` :
1307
+ `{ "@${ config . workspace } //:node_modules/ ${ pkg . _dir } ": "${ path } " }` :
1320
1308
`"@${ config . workspace } //:node_modules/${ pkg . _dir } /${ path } "` ;
1321
1309
result += `# Wire up the \`bin\` entry \`${ name } \`
1322
1310
nodejs_binary(
@@ -1346,7 +1334,7 @@ export function printIndexBzl(pkg: Dep) {
1346
1334
1347
1335
for ( const [ name , path ] of executables . entries ( ) ) {
1348
1336
const entryPoint = config . exports_directories_only ?
1349
- `{ "@${ config . workspace } //${ pkg . _dir } :directory ": "${ path } " }` :
1337
+ `{ "@${ config . workspace } //:node_modules/ ${ pkg . _dir } ": "${ path } " }` :
1350
1338
`"@${ config . workspace } //:node_modules/${ pkg . _dir } /${ path } "` ;
1351
1339
result = `${ result }
1352
1340
@@ -1391,7 +1379,7 @@ type Dep = {
1391
1379
/**
1392
1380
* Given a scope, return the skylark `js_library` target for the scope.
1393
1381
*/
1394
- function printScope ( scope : string , deps : Dep [ ] ) {
1382
+ function printScopeLegacy ( scope : string , deps : Dep [ ] ) {
1395
1383
let pkgFilesStarlark = '' ;
1396
1384
if ( deps . length ) {
1397
1385
const list = deps . map ( dep => `"//${ dep . _dir } :${ dep . _name } __files",` ) . join ( '\n ' ) ;
0 commit comments