Skip to content

Commit d407a2e

Browse files
MivrsimoViso
andauthored
feat: add replace_package module extension tag (aspect-build#2289)
### Changes are visible to end-users: yes - Searched for relevant documentation and updated as needed: yes - Breaking change (forces users to change their own code or config): yes - Suggested release notes appear below: yes Add `npm_replace_package(package, replacement)` bzlmod API. ### Test plan - Updated old test case --------- Co-authored-by: Simeon Visotskiy <[email protected]>
1 parent fcd460f commit d407a2e

28 files changed

+444
-35
lines changed

docs/npm_translate_lock.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
utils_module
3+
lodash_module

e2e/npm_translate_lock_replace_packages/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ js_test(
1515
entry_point = "main.js",
1616
)
1717

18+
js_test(
19+
name = "utils_test",
20+
data = [
21+
"@utils_replacement_module//:test_lib",
22+
],
23+
entry_point = "@utils_replacement_module//:test.mjs",
24+
no_copy_to_bin = ["@utils_replacement_module//:test.mjs"],
25+
)
26+
1827
npm_package(
1928
name = "npm-pkg",
2029
srcs = [

e2e/npm_translate_lock_replace_packages/MODULE.bazel

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,10 @@ local_path_override(
1414
path = "../..",
1515
)
1616

17-
npm = use_extension(
18-
"@aspect_rules_js//npm:extensions.bzl",
19-
"npm",
20-
dev_dependency = True,
21-
)
22-
npm.npm_translate_lock(
23-
name = "npm",
24-
npmrc = "//:.npmrc",
25-
pnpm_lock = "//:pnpm-lock.yaml",
26-
replace_packages = {
27-
"[email protected]": "@chalk_501//:pkg",
28-
},
29-
verify_node_modules_ignored = "//:.bazelignore",
17+
bazel_dep(name = "lodash_replacement_module", version = "0.0.0")
18+
local_path_override(
19+
module_name = "lodash_replacement_module",
20+
path = "lodash_module",
3021
)
3122

3223
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
@@ -39,4 +30,29 @@ http_archive(
3930
url = "https://registry.npmjs.org/chalk/-/chalk-5.0.1.tgz",
4031
)
4132

33+
bazel_dep(name = "utils_replacement_module", version = "0.0.0")
34+
local_path_override(
35+
module_name = "utils_replacement_module",
36+
path = "utils_module",
37+
)
38+
39+
npm = use_extension(
40+
"@aspect_rules_js//npm:extensions.bzl",
41+
"npm",
42+
dev_dependency = True,
43+
)
44+
npm.npm_translate_lock(
45+
name = "npm",
46+
npmrc = "//:.npmrc",
47+
pnpm_lock = "//:pnpm-lock.yaml",
48+
verify_node_modules_ignored = "//:.bazelignore",
49+
)
50+
npm.npm_replace_package(
51+
package = "[email protected]",
52+
replacement = "@chalk_501//:pkg",
53+
)
54+
npm.npm_replace_package(
55+
package = "[email protected]",
56+
replacement = "@lodash_replacement_module//:lodash_replacement",
57+
)
4258
use_repo(npm, "npm")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
2+
3+
# This module provides a lodash replacement package
4+
npm_package(
5+
name = "lodash_replacement",
6+
srcs = [
7+
"lodash.js",
8+
"package.json",
9+
],
10+
visibility = ["//visibility:public"],
11+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module(
2+
name = "lodash_replacement_module",
3+
version = "0.0.0",
4+
compatibility_level = 1,
5+
)
6+
7+
bazel_dep(name = "aspect_rules_js", version = "0.0.0")
8+
local_path_override(
9+
module_name = "aspect_rules_js",
10+
path = "../../..",
11+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Simple lodash replacement - version 4.17.20 equivalent
2+
export default {
3+
uniq: function(array) {
4+
return [...new Set(array)];
5+
},
6+
version: "4.17.20"
7+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "lodash",
3+
"version": "4.17.20",
4+
"type": "module",
5+
"main": "lodash.js"
6+
}
Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
import chalk from 'chalk'
2+
import lodash from 'lodash'
23
import { readFileSync } from 'fs'
34

4-
const packageJsonDep = JSON.parse(readFileSync('./package.json', 'utf-8'))
5-
.dependencies['chalk']
6-
if (packageJsonDep !== '5.3.0') {
5+
const packageJson = JSON.parse(readFileSync('./package.json', 'utf-8'))
6+
7+
// Test chalk replacement
8+
const chalkPackageJsonDep = packageJson.dependencies['chalk']
9+
if (chalkPackageJsonDep !== '5.3.0') {
710
throw new Error(
8-
`Expected chalk version 5.3.0 declared in package.json, but got ${pkgDep}`
11+
`Expected chalk version 5.3.0 declared in package.json, but got ${chalkPackageJsonDep}`
912
)
1013
}
1114

12-
const actualDep = JSON.parse(
15+
const chalkActualDep = JSON.parse(
1316
readFileSync('./node_modules/chalk/package.json', 'utf-8')
1417
).version
15-
if (actualDep !== '5.0.1') {
18+
if (chalkActualDep !== '5.0.1') {
19+
throw new Error(
20+
`Expected chalk to be replaced with version 5.0.1, but got ${chalkActualDep}`
21+
)
22+
}
23+
24+
// Test lodash replacement
25+
const lodashPackageJsonDep = packageJson.dependencies['lodash']
26+
if (lodashPackageJsonDep !== '4.17.21') {
27+
throw new Error(
28+
`Expected lodash version 4.17.21 declared in package.json, but got ${lodashPackageJsonDep}`
29+
)
30+
}
31+
32+
const lodashActualDep = JSON.parse(
33+
readFileSync('./node_modules/lodash/package.json', 'utf-8')
34+
).version
35+
if (lodashActualDep !== '4.17.20') {
1636
throw new Error(
17-
`Expected chalk to be replaced with version 5.0.1, but got ${actualDep}`
37+
`Expected lodash to be replaced with version 4.17.20, but got ${lodashActualDep}`
1838
)
1939
}
2040

21-
console.log(chalk.blue(`Hello world! The meaning of life is... 42`))
41+
// Test that both packages work functionally
42+
const testArray = [1, 2, 2, 3, 3, 3]
43+
const uniqueArray = lodash.uniq(testArray)
44+
if (uniqueArray.length !== 3) {
45+
throw new Error(`Expected lodash.uniq to work, but got array length ${uniqueArray.length}`)
46+
}
47+

e2e/npm_translate_lock_replace_packages/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"private": true,
33
"type": "module",
44
"dependencies": {
5-
"chalk": "5.3.0"
5+
"chalk": "5.3.0",
6+
"lodash": "4.17.21"
67
},
78
"pnpm": {
89
"onlyBuiltDependencies": []

0 commit comments

Comments
 (0)