-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fine-grained npm_install & yarn_install deps
- Loading branch information
1 parent
d0a29cd
commit 46d7daf
Showing
34 changed files
with
1,728 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") | ||
|
||
# Test what happens when we depend on the catch-all "node_modules" rule rather | ||
# than declare our dependencies on individual npm packages. | ||
# This is the legacy behavior, so it also proves our backwards-compatibility | ||
# story. | ||
jasmine_node_test( | ||
name = "test", | ||
srcs = [ | ||
"coarse.spec.js", | ||
"common.spec.js", | ||
], | ||
node_modules = "@npm//:node_modules", | ||
) | ||
|
||
# Test what happens when only certain NPM packages are in our dependencies. | ||
# These packages and their dependencies are copied to the execroot, but | ||
# the rest are not. | ||
jasmine_node_test( | ||
name = "fine_grained_test", | ||
srcs = [ | ||
"common.spec.js", | ||
"fine.spec.js", | ||
], | ||
deps = [ | ||
"@npm//:jasmine", | ||
"@npm//:typescript", | ||
# Note, test-b depends on [email protected] which should get | ||
# hoisted to node_modules/test-b/node_modules/test-a | ||
"@npm//:@gregmagolan/test-b", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Fine-grained dependencies | ||
|
||
Declaring the entire `node_modules` directory as an input to every nodejs action | ||
has performance problems. When using local sandboxing, every file is set up in | ||
the kernel container for the sandboxed disk, this is slow on Mac. With remote | ||
execution, we guarantee these files all need to be copied to the worker machine. | ||
|
||
Instead, we can declare individual npm packages as dependencies, e.g.: | ||
``` | ||
nodejs_binary( | ||
name = "fast", | ||
data = ["@my_npm_packages//:jasmine"] | ||
) | ||
``` | ||
|
||
and only the contents of `node_modules/jasmine/` will be copied to workers. | ||
|
||
See design doc: | ||
https://docs.google.com/document/d/1BmQfTKhKMIsd27YKzWIhU7on-OVgRTSnUi1EHtHcceo/edit# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# BEGIN BORING BOILERPLATE | ||
workspace(name = "bazel_managed_deps") | ||
local_repository( | ||
name = "build_bazel_rules_nodejs", | ||
path = "../..", | ||
) | ||
load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dependencies") | ||
rules_nodejs_dependencies() | ||
|
||
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install") | ||
node_repositories() | ||
# END BORING BOILERPLATE | ||
|
||
# This runs yarn install, then our generate_build_file.js to create BUILD files | ||
# inside the resulting node_modules directory. | ||
# The name "npm" here means the resulting modules are referenced like | ||
# @npm//:jasmine | ||
yarn_install( | ||
name = "npm", | ||
package_json = "//:package.json", | ||
yarn_lock = "//:yarn.lock", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
describe('dependencies', () => { | ||
it('it should resolve [email protected] which is under node_modules/test-a', () => { | ||
const testA = require('@gregmagolan/test-a'); | ||
expect(testA).toEqual('test-a-0.0.2'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const ts = require('typescript'); | ||
|
||
describe('dependencies', () => { | ||
it('should get the typescript library', () => { | ||
expect(ts.version).toBe('3.0.1'); | ||
}); | ||
|
||
it(`should resolve transitive dependencies | ||
Note that jasmine-core is not listed in our deps[] | ||
but it is a transitive dependency of jasmine, which is in our deps.`, | ||
() => { | ||
require('jasmine-core'); | ||
}); | ||
|
||
it(`should resolve @gregmagolan/test-b to version 0.0.2 with a @gregmagolan/test-a dependency of 0.0.1 | ||
Note that @gregmagolan/[email protected] is an explicit devDependency of this project, | ||
so we are really testing that test-b will get the version it depends on, not | ||
the hoisted one.`, | ||
() => { | ||
const testB = require('@gregmagolan/test-b'); | ||
expect(testB).toEqual('test-b-0.0.2/test-a-0.0.1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
describe('dependencies', () => { | ||
it('should fail to resolve test-a since it has not been specified as a fine-grained direct dependency', | ||
() => { | ||
try { | ||
require('@gregmagolan/test-a'); | ||
expect(true).toBe(false); | ||
} catch (err) { | ||
expect(err.code).toBe('MODULE_NOT_FOUND'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "bazel_managed_deps", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"jasmine": "^3.2.0", | ||
"typescript": "^3.0.1" | ||
}, | ||
"dependencies": { | ||
"@gregmagolan/test-a": "^0.0.2", | ||
"@gregmagolan/test-b": "^0.0.2" | ||
} | ||
} |
Oops, something went wrong.