-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathplugin.test.ts
More file actions
115 lines (106 loc) · 3.27 KB
/
plugin.test.ts
File metadata and controls
115 lines (106 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import path from "node:path";
import { transformFileSync } from "@babel/core";
import { plugin } from "./plugin.js";
import { setupTempDirectory } from "../test-utils.js";
import { getLibraryName } from "../path-utils.js";
describe("plugin", () => {
it("transforms require calls, regardless", (context) => {
const tempDirectoryPath = setupTempDirectory(context, {
"package.json": `{ "name": "my-package" }`,
"addon-1.apple.node/addon-1.node":
"// This is supposed to be a binary file",
"addon-2.apple.node/addon-2.node":
"// This is supposed to be a binary file",
"addon-1.js": `
const addon = require('./addon-1.node');
console.log(addon);
`,
"addon-2.js": `
const addon = require('./addon-2.node');
console.log(addon);
`,
"sub-directory/addon-1.js": `
const addon = require('../addon-1.node');
console.log(addon);
`,
"addon-1-bindings.js": `
const addon = require('bindings')('addon-1');
console.log(addon);
`,
"require-js-file.js": `
const addon = require('./addon-1.js');
console.log(addon);
`,
});
const ADDON_1_REQUIRE_ARG = getLibraryName(
path.join(tempDirectoryPath, "addon-1"),
{ stripPathSuffix: false }
);
const ADDON_2_REQUIRE_ARG = getLibraryName(
path.join(tempDirectoryPath, "addon-2"),
{ stripPathSuffix: false }
);
{
const result = transformFileSync(
path.join(tempDirectoryPath, "./addon-1.js"),
{ plugins: [[plugin, { stripPathSuffix: false }]] }
);
assert(result);
const { code } = result;
assert(
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
`Unexpected code: ${code}`
);
}
{
const result = transformFileSync(
path.join(tempDirectoryPath, "./addon-2.js"),
{ plugins: [[plugin, { naming: "hash" }]] }
);
assert(result);
const { code } = result;
assert(
code && code.includes(`requireNodeAddon("${ADDON_2_REQUIRE_ARG}")`),
`Unexpected code: ${code}`
);
}
{
const result = transformFileSync(
path.join(tempDirectoryPath, "./sub-directory/addon-1.js"),
{ plugins: [[plugin, { naming: "hash" }]] }
);
assert(result);
const { code } = result;
assert(
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
`Unexpected code: ${code}`
);
}
{
const result = transformFileSync(
path.join(tempDirectoryPath, "./addon-1-bindings.js"),
{ plugins: [[plugin, { naming: "hash" }]] }
);
assert(result);
const { code } = result;
assert(
code && code.includes(`requireNodeAddon("${ADDON_1_REQUIRE_ARG}")`),
`Unexpected code: ${code}`
);
}
{
const result = transformFileSync(
path.join(tempDirectoryPath, "./require-js-file.js"),
{ plugins: [[plugin, { naming: "hash" }]] }
);
assert(result);
const { code } = result;
assert(
code && !code.includes(`requireNodeAddon`),
`Unexpected code: ${code}`
);
}
});
});