-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (39 loc) · 1.24 KB
/
index.js
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
if (!module.pnpApiPath) {
console.error(
"Error: pnpApi not found. Are you running command with yarn? `yarn node ...`"
);
process.exit(1);
}
let pnpapi = require("pnpapi");
let fs = require("fs");
let pnpResolve = args => {
const path = pnpapi.resolveRequest(args.path, args.resolveDir + "/", {
considerBuiltins: true,
extensions: [".js", ".jsx", ".ts", ".tsx", ".json"],
});
return path ? { path, namespace: "pnp" } : { external: true };
};
module.exports = ({ external = [] } = {}) => {
let externalsSet = new Set(external);
return {
name: "pnp-plugin",
setup(build) {
// Initial resolve if not a relative path
build.onResolve({ filter: /^[^\.\/]/ }, args => {
if (externalsSet.has(args.path)) return { external: true };
return pnpResolve(args);
});
// Subsequent resolves within pnp zip files
build.onResolve({ filter: /.*/, namespace: "pnp" }, pnpResolve);
build.onLoad({ filter: /.*/, namespace: "pnp" }, async args => {
let resolveDir = args.path.match(/(.+\/)/)[1];
let contents = await fs.promises.readFile(args.path, "utf8");
return {
contents,
resolveDir,
loader: "default",
};
});
},
};
};