1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const {
|
6 | ArrayPrototypeForEach,
|
7 | ObjectDefineProperty,
|
8 | ObjectPrototypeHasOwnProperty,
|
9 | SafeSet,
|
10 | StringPrototypeIncludes,
|
11 | StringPrototypeStartsWith,
|
12 | } = require('./node-primordials');
|
13 |
|
14 | const { getOptionValue } = require('./node-options');
|
15 | const userConditions = getOptionValue('--conditions');
|
16 |
|
17 | const noAddons = getOptionValue('--no-addons');
|
18 | const addonConditions = noAddons ? [] : ['node-addons'];
|
19 |
|
20 |
|
21 | const cjsConditions = new SafeSet([
|
22 | 'require',
|
23 | 'node',
|
24 | ...addonConditions,
|
25 | ...userConditions,
|
26 | ]);
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function addBuiltinLibsToObject(object, dummyModuleName) {
|
34 |
|
35 | const Module = require('module').Module;
|
36 | const { builtinModules } = Module;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | const dummyModule = new Module(dummyModuleName);
|
42 |
|
43 | ArrayPrototypeForEach(builtinModules, (name) => {
|
44 |
|
45 |
|
46 | if (StringPrototypeStartsWith(name, '_') ||
|
47 | StringPrototypeIncludes(name, '/') ||
|
48 | ObjectPrototypeHasOwnProperty(object, name)) {
|
49 | return;
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | const setReal = (val) => {
|
58 |
|
59 |
|
60 | delete object[name];
|
61 | object[name] = val;
|
62 | };
|
63 |
|
64 | ObjectDefineProperty(object, name, {
|
65 | get: () => {
|
66 |
|
67 | const lib = (dummyModule.require || require)(name);
|
68 |
|
69 |
|
70 |
|
71 | delete object[name];
|
72 | ObjectDefineProperty(object, name, {
|
73 | get: () => lib,
|
74 | set: setReal,
|
75 | configurable: true,
|
76 | enumerable: false
|
77 | });
|
78 |
|
79 | return lib;
|
80 | },
|
81 | set: setReal,
|
82 | configurable: true,
|
83 | enumerable: false
|
84 | });
|
85 | });
|
86 | }
|
87 |
|
88 | exports.addBuiltinLibsToObject = addBuiltinLibsToObject;
|
89 | exports.cjsConditions = cjsConditions;
|