Skip to content

Commit

Permalink
fix: fix built-in-module-map generator
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed May 29, 2022
1 parent d744a43 commit 65e1179
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "ava",
"prebuild": "pnpm run clean",
"build": "pnpm run prebuild && pnpm run rollup",
"build:built_in_module_map": "ts-node script/generate-built-in-module-map.ts",
"build:built_in_module_map": "ts-node --esm script/generate-built-in-module-map.ts",
"prewatch": "pnpm run clean",
"watch": "pnpm run prewatch && pnpm run rollup:watch",
"rollup": "rollup -c rollup.config.js",
Expand Down
35 changes: 18 additions & 17 deletions script/generate-built-in-module-map.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Module from "module";
import {join} from "path";
import path from "crosspath";
import {existsSync, mkdirSync, writeFileSync} from "fs";
import {format} from "prettier";

import prettierConfig from "../prettier.config.js";
import {format, type RequiredOptions} from "prettier";
import prettierConfig from "@wessberg/prettier-config" assert {type: "json"};

const IGNORED_MODULE_NAMES = new Set([
"_http_agent",
Expand Down Expand Up @@ -46,7 +45,7 @@ function generateBuiltInModuleInnerContents(): string {
return str;
}

function generateBuiltInModule(): string {
async function generateBuiltInModule(): Promise<string> {
return format(
`\
/* eslint-disable */
Expand All @@ -71,21 +70,21 @@ export function isBuiltInModule (moduleName: string): moduleName is BuiltInModul
}
export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
${generateBuiltInModuleMapInnerContents()}
${await generateBuiltInModuleMapInnerContents()}
};
`,
{
...prettierConfig,
...(prettierConfig as Partial<RequiredOptions>),
parser: "typescript"
}
);
}

function generateBuiltInModuleMapInnerContents(): string {
async function generateBuiltInModuleMapInnerContents(): Promise<string> {
let str = "";
for (const moduleName of Module.builtinModules) {
if (IGNORED_MODULE_NAMES.has(moduleName)) continue;
str += `${generateBuiltInModuleMapContents(moduleName)},\n`;
str += `${await generateBuiltInModuleMapContents(moduleName)},\n`;
}
return str;
}
Expand All @@ -95,21 +94,23 @@ function filterAndFormatModuleKeys(keys: string[]): string {
return `[${filteredKeys.map(filteredKey => `"${filteredKey}"`).join(",")}]`;
}

function generateBuiltInModuleMapContents(moduleName: string): string {
// eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires
const loadedModule = require(moduleName);
async function generateBuiltInModuleMapContents(moduleName: string): Promise<string> {
let loadedModule = await import(moduleName);
loadedModule ="default" in loadedModule ? loadedModule.default : loadedModule;

return `\
${moduleName.includes("/") ? `"${moduleName}"` : moduleName}: {
namedExports: new Set(${typeof loadedModule === "function" ? "[]" : filterAndFormatModuleKeys(Object.keys(loadedModule))}),
hasDefaultExport: true
}`;
}

const DESTINATION_DIR = join(__dirname, "../src/transformer/built-in");
const DESTINATION = join(DESTINATION_DIR, "built-in-module-map.ts");
const CURRENT_DIR = path.dirname(import.meta.url.replace(/file:\/{2,3}/, ``));
const DESTINATION_DIR = path.join(CURRENT_DIR, "../src/transformer/built-in");
const DESTINATION = path.join(DESTINATION_DIR, "built-in-module-map.ts");

if (!existsSync(DESTINATION_DIR)) {
mkdirSync(DESTINATION_DIR);
if (!existsSync(path.native.normalize(DESTINATION_DIR))) {
mkdirSync(path.native.normalize(DESTINATION_DIR));
}

writeFileSync(DESTINATION, generateBuiltInModule());
writeFileSync(path.native.normalize(DESTINATION), await generateBuiltInModule());
36 changes: 26 additions & 10 deletions src/transformer/built-in/built-in-module-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const BUILT_IN_MODULE = new Set([
"punycode",
"querystring",
"readline",
"readline/promises",
"repl",
"stream",
"stream/consumers",
Expand Down Expand Up @@ -78,7 +79,7 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
hasDefaultExport: true
},
async_hooks: {
namedExports: new Set(["AsyncLocalStorage", "createHook", "executionAsyncId", "triggerAsyncId", "executionAsyncResource", "AsyncResource"]),
namedExports: new Set(["AsyncLocalStorage", "createHook", "executionAsyncId", "triggerAsyncId", "executionAsyncResource", "asyncWrapProviders", "AsyncResource"]),
hasDefaultExport: true
},
buffer: {
Expand Down Expand Up @@ -311,6 +312,8 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"UV_FS_O_FILEMAP",
"O_TRUNC",
"O_APPEND",
"S_IRUSR",
"S_IWUSR",
"F_OK",
"R_OK",
"W_OK",
Expand Down Expand Up @@ -378,7 +381,6 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"DH_NOT_SUITABLE_GENERATOR",
"ALPN_ENABLED",
"RSA_PKCS1_PADDING",
"RSA_SSLV23_PADDING",
"RSA_NO_PADDING",
"RSA_PKCS1_OAEP_PADDING",
"RSA_X931_PADDING",
Expand All @@ -393,7 +395,8 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"TLS1_3_VERSION",
"POINT_CONVERSION_COMPRESSED",
"POINT_CONVERSION_UNCOMPRESSED",
"POINT_CONVERSION_HYBRID"
"POINT_CONVERSION_HYBRID",
"defaultCipherList"
]),
hasDefaultExport: true
},
Expand Down Expand Up @@ -462,7 +465,9 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"X509Certificate",
"secureHeapUsed",
"constants",
"webcrypto"
"webcrypto",
"subtle",
"getRandomValues"
]),
hasDefaultExport: true
},
Expand Down Expand Up @@ -843,6 +848,7 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"PerformanceMeasure",
"PerformanceObserver",
"PerformanceObserverEntryList",
"PerformanceResourceTiming",
"monitorEventLoopDelay",
"createHistogram",
"performance",
Expand All @@ -863,12 +869,14 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"config",
"dlopen",
"uptime",
"getActiveResourcesInfo",
"reallyExit",
"cpuUsage",
"resourceUsage",
"memoryUsage",
"kill",
"exit",
"hrtime",
"openStdin",
"allowedNodeEnvironmentFlags",
"assert",
Expand All @@ -892,8 +900,9 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"ppid",
"execPath",
"debugPort",
"hrtime",
"argv0",
"exitCode",
"report",
"setSourceMapsEnabled",
"mainModule",
"emit"
Expand All @@ -909,7 +918,11 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
hasDefaultExport: true
},
readline: {
namedExports: new Set(["Interface", "clearLine", "clearScreenDown", "createInterface", "cursorTo", "emitKeypressEvents", "moveCursor"]),
namedExports: new Set(["Interface", "clearLine", "clearScreenDown", "createInterface", "cursorTo", "emitKeypressEvents", "moveCursor", "promises"]),
hasDefaultExport: true
},
"readline/promises": {
namedExports: new Set(["Interface", "Readline", "createInterface"]),
hasDefaultExport: true
},
repl: {
Expand Down Expand Up @@ -944,7 +957,9 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"ByteLengthQueuingStrategy",
"CountQueuingStrategy",
"TextEncoderStream",
"TextDecoderStream"
"TextDecoderStream",
"CompressionStream",
"DecompressionStream"
]),
hasDefaultExport: true
},
Expand All @@ -957,7 +972,7 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
hasDefaultExport: true
},
"timers/promises": {
namedExports: new Set(["setTimeout", "setImmediate", "setInterval"]),
namedExports: new Set(["setTimeout", "setImmediate", "setInterval", "scheduler"]),
hasDefaultExport: true
},
tls: {
Expand All @@ -978,7 +993,6 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"Server",
"createServer",
"connect",
"parseCertString",
"createSecurePair"
]),
hasDefaultExport: true
Expand Down Expand Up @@ -1038,6 +1052,7 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"isPrimitive",
"log",
"promisify",
"stripVTControlCharacters",
"toUSVString",
"TextDecoder",
"TextEncoder",
Expand Down Expand Up @@ -1108,7 +1123,8 @@ export const BUILT_IN_MODULE_MAP: BuiltInModuleMap = {
"takeCoverage",
"stopCoverage",
"serialize",
"writeHeapSnapshot"
"writeHeapSnapshot",
"promiseHooks"
]),
hasDefaultExport: true
},
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@wessberg/ts-config",
"include": ["src/**/*.*", "test/**/*.*"],
"include": ["src/**/*.*", "script/**/*.*", "test/**/*.*"],
"compilerOptions": {
"module": "esnext",
"target": "es2019",
Expand Down

0 comments on commit 65e1179

Please sign in to comment.