@plugjs/tsrun 0.5.76 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +10 -18
- package/dist/cli.mjs.map +3 -3
- package/dist/loader-commonjs.cjs +1 -1
- package/dist/loader-commonjs.cjs.map +2 -2
- package/dist/loader-module.mjs +1 -1
- package/dist/loader-module.mjs.map +2 -2
- package/dist/parser.mjs +5 -13
- package/dist/parser.mjs.map +3 -3
- package/dist/tsrun.mjs +13 -20
- package/dist/tsrun.mjs.map +3 -3
- package/package.json +9 -6
- package/src/loader-shared.ts +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/loader-commonjs.cts", "../src/loader-shared.ts"],
|
|
4
|
-
"sourcesContent": ["// NodeJS dependencies\nimport _module from 'node:module'\nimport _path from 'node:path'\n\nimport {\n CJS,\n ESM,\n esbTranpile,\n logMessage,\n moduleType,\n throwError,\n} from './loader-shared'\n\n/* ========================================================================== *\n * CJS VERSION *\n * ========================================================================== */\n\n/** The extension handler type, loading CJS modules */\ntype ExtensionHandler = (module: NodeJS.Module, filename: string) => void\n\n/* Add the `_compile(...)` method to NodeJS' `Module` interface */\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace NodeJS {\n interface Module {\n _compile: (contents: string, filename: string) => void\n }\n }\n}\n\n/**\n * Add the `_extensions[...]` and `resolveFilename(...)` members to the\n * definition of `node:module`.\n */\ndeclare module 'node:module' {\n const _extensions: Record<`.${string}`, ExtensionHandler>\n function _resolveFilename(\n request: string,\n parent: _module | undefined,\n isMain: boolean,\n options?: any,\n ): string\n}\n\n/* ========================================================================== */\n\nconst _type = moduleType(CJS)\n\nconst loader: ExtensionHandler = (module, filename): void => {\n logMessage(CJS, `Attempting to load \"${filename}\"`)\n\n /* Figure our the extension (\".ts\" or \".cts\")... */\n const ext = _path.extname(filename)\n\n /* If the file is a \".ts\", we need to figure out the default type */\n if (ext === '.ts') {\n /* If the _default_ module type is CJS then load as such! */\n if (_type === ESM) {\n throwError(CJS, `Must use import to load ES Module: ${filename}`, { code: 'ERR_REQUIRE_ESM' })\n }\n } else if (ext !== '.cts') {\n throwError(CJS, `Unsupported filename \"${filename}\"`)\n }\n\n const source = esbTranpile(filename, CJS)\n\n /* Let node do its thing, but wrap any error it throws */\n try {\n module._compile(source, filename)\n } catch (cause) {\n console.error(`Error compiling module \"${filename}\"`, cause)\n }\n}\n\n/**\n * Replace _module._resolveFilename with our own.\n *\n * This is a _HACK BEYOND REDEMPTION_ and I'm ashamed of even _thinking_ about\n * it, but, well, it makes things work.\n *\n * TypeScript allows us to import \"./foo.js\", and internally resolves this to\n * \"./foo.ts\" (yeah, nice, right?) and while we normally wouldn't want to deal\n * with this kind of stuff, the \"node16\" module resolution mode _forces_ us to\n * use this syntax.\n *\n * And we _need_ the \"node16\" module resolution to properly consume \"export\n * conditions\" from other packages. Since ESBuild's plugins only work in async\n * mode, changing those import statements on the fly is out of the question, so\n * we need to hack our way into Node's own resolver.\n *\n * See my post: https://twitter.com/ianosh/status/1559484168685379590\n * ESBuild related fix: https://github.com/evanw/esbuild/commit/0cdc005e3d1c765a084f206741bc4bff78e30ec4\n */\nconst _oldResolveFilename = _module._resolveFilename\n_module._resolveFilename = function(\n request: string,\n parent: _module | undefined,\n ...args: [ isMain: boolean, options: any ]\n): any {\n try {\n /* First call the old _resolveFilename to see what Node thinks */\n return _oldResolveFilename.call(this, request, parent, ...args)\n } catch (error: any) {\n /* If the error was anything but \"MODULE_NOT_FOUND\" bail out */\n if (error.code !== 'MODULE_NOT_FOUND') throw error\n\n /* Check if the \"request\" ends with \".js\", \".mjs\" or \".cjs\" */\n const match = request.match(/(.*)(\\.[mc]?js$)/)\n\n /*\n * If the file matches our extension, _and_ we have a parent, we simply\n * try with a new extension (e.g. \".js\" becomes \".ts\")...\n */\n if (parent && match) {\n const [ , name, ext ] = match\n const tsrequest = name + ext!.replace('js', 'ts')\n try {\n const result = _oldResolveFilename.call(this, tsrequest, parent, ...args)\n logMessage(CJS, `Resolution for \"${request}\" intercepted as \"${tsrequest}`)\n return result\n } catch {\n throw error // throw the _original_ error in this case\n }\n }\n\n /* We have no parent, or we don't match our extension, throw! */\n throw error\n }\n}\n\n/* ========================================================================== */\n\n/* Remember to load our loader for .TS/.CTS as CommonJS modules */\n_module._extensions['.ts'] = _module._extensions['.cts'] = loader\nlogMessage(CJS, 'TypeScript loader for CommonJS loaded')\n", "/* eslint-disable @typescript-eslint/no-unsafe-function-type */\n/* ========================================================================== *\n * HACK BEYOND REDEMPTION: TRANSPILE .ts FILES (the esm loader) *\n * -------------------------------------------------------------------------- *\n * Use ESBuild to quickly transpile TypeScript files into JavaScript. *\n * *\n * The plan as it stands is as follows: *\n * - `.mts` files always get transpiled to ESM modules *\n * - `.cts` files always get transpiled to CJS modules *\n * - `.ts` files are treanspiled according to what's in `package.json` *\n * *\n * Additionally, when transpiling to ESM modules, we can't rely on the magic *\n * that Node's `require(...)` call uses to figure out which file to import. *\n * We need to _actually verify_ on disk what's the correct file to import. *\n * *\n * This is a single module, only available as ESM, and it will _both_ behave *\n * as a NodeJS' loader, _and_ inject the CJS extension handlers (hack) found *\n * in the `_extensions` of `node:module` (same as `require.extensions`). *\n * ========================================================================== */\n\n// NodeJS dependencies\nimport _fs from 'node:fs'\nimport _path from 'node:path'\nimport _util from 'node:util'\n\n// ESBuild is the only external dependency\nimport _esbuild from 'esbuild'\n\n/* ========================================================================== *\n * CONSTANTS AND TYPES *\n * ========================================================================== */\n\n/** Supported types from `package.json` */\nexport type Type = 'commonjs' | 'module'\n/** Constant identifying a `commonjs` module */\nexport const CJS = 'commonjs' as const\n/** Constant identifying an ESM `module` */\nexport const ESM = 'module' as const\n\n/* ========================================================================== *\n * DEBUGGING AND ERRORS *\n * ========================================================================== */\n\n/** Setup debugging */\nconst _debugLog = _util.debuglog('plug:ts-loader')\nconst _debug = _debugLog.enabled\n\n/** Emit some logs if `DEBUG_TS_LOADER` is set to `true` */\nexport function logMessage(mode: Type, arg: string, ...args: any []): void {\n if (! _debug) return\n\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n _debugLog(`[${t}] ${arg}`, ...args)\n}\n\n/** Fail miserably */\nexport function throwError(\n mode: Type,\n message: string,\n options: { start?: Function, code?: string, cause?: any } = {},\n): never {\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n const prefix = `[ts-loader|${t}|pid=${process.pid}]`\n\n const { start = throwError, ...extra } = options\n const error = new Error(`${prefix} ${message}`)\n Error.captureStackTrace(error, start)\n Object.assign(error, extra)\n\n throw error\n}\n\n/* ========================================================================== *\n * MODULE TYPES AND FORCING TYPE *\n * ========================================================================== */\n\n/**\n * Determine the current module type to transpile .TS files as looking at the\n * `__TS_LOADER_FORCE_TYPE` environment variable (used by PlugJS and CLI) or,\n * if unspecified, looking at the `type` field in the `package.json` file.\n */\nexport function moduleType(mode: Type): Type {\n if (process.env.__TS_LOADER_FORCE_TYPE) {\n const type = process.env.__TS_LOADER_FORCE_TYPE\n if ((type === CJS) || (type === ESM)) {\n logMessage(mode, `Forcing type to \"${type}\" from environment`)\n return type\n } else {\n throwError(mode, `Invalid type \"${process.env.__TS_LOADER_FORCE_TYPE}\"`)\n }\n }\n\n const _findType = (directory: string): Type => {\n const packageFile = _path.join(directory, 'package.json')\n try {\n const packageData = _fs.readFileSync(packageFile, 'utf-8')\n const packageJson = JSON.parse(packageData)\n const packageType = packageJson.type\n switch (packageType) {\n case undefined:\n logMessage(mode, `File \"${packageFile}\" does not declare a default type`)\n return CJS\n\n case CJS:\n case ESM:\n logMessage(mode, `File \"${packageFile}\" declares type as \"${CJS}\"`)\n return packageType\n\n default:\n logMessage(mode, `File \"${packageFile}\" specifies unknown type \"${packageType}\"`)\n return CJS\n }\n } catch (cause: any) {\n if ((cause.code !== 'ENOENT') && (cause.code !== 'EISDIR')) {\n throwError(mode, `Unable to read or parse \"${packageFile}\"`, { cause, start: _findType })\n }\n }\n\n const parent = _path.dirname(directory)\n if (directory !== parent) return _findType(directory)\n\n logMessage(mode, `Type defaulted to \"${CJS}\"`)\n return CJS\n }\n\n return _findType(process.cwd())\n}\n\n/* ========================================================================== *\n * ESBUILD HELPERS *\n * ========================================================================== */\n\n/**\n * Take an ESBuild `BuildResult` or `BuildFailure` (they both have arrays\n * of `Message` in both `warnings` and `errors`), format them and print them\n * out nicely. Then fail if any error was detected.\n */\nfunction _esbReport(\n kind: 'error' | 'warning',\n messages: _esbuild.Message[] = [],\n): void {\n const output = process.stderr\n const options = { color: !!output.isTTY, terminalWidth: output.columns || 80 }\n\n const array = _esbuild.formatMessagesSync(messages, { kind, ...options })\n array.forEach((message) => output.write(`${message}\\n`))\n}\n\n/**\n * Transpile with ESBuild\n */\nexport function esbTranpile(filename: string, type: Type): string {\n logMessage(type, `Transpiling \"${filename}\" as \"${type}\"`)\n\n const [ format, __fileurl ] = type === ESM ?\n [ 'esm', 'import.meta.url' ] as const :\n [ 'cjs', '__filename' ] as const\n\n /* ESbuild options */\n const options: _esbuild.TransformOptions = {\n sourcefile: filename, // the original filename we're parsing\n format, // what are we actually transpiling to???\n loader: 'ts', // the format is always \"typescript\"\n sourcemap: 'inline', // always inline source maps\n sourcesContent: false, // do not include sources content in sourcemap\n platform: 'node', // d'oh! :-)\n minifyWhitespace: true, // https://github.com/evanw/esbuild/releases/tag/v0.16.14\n logLevel: 'silent', // catching those in our _esbReport below\n target: `node${process.versions['node']}`, // target _this_ version\n define: { __fileurl }, // from \"globals.d.ts\"\n }\n\n /* Emit a line on the console when loading in debug mode */\n if (_debug) {\n if (format === 'esm') {\n options.banner = `;(await import('node:util')).debuglog('plug:ts-loader')('[esm] Loaded \"%s\"', ${__fileurl});`\n } else if (format === 'cjs') {\n options.banner = `;require('node:util').debuglog('plug:ts-loader')('[cjs] Loaded \"%s\"', ${__fileurl});`\n }\n }\n\n /* Transpile our TypeScript file into some JavaScript stuff */\n let result\n try {\n const source = _fs.readFileSync(filename, 'utf-8')\n result = _esbuild.transformSync(source, options)\n } catch (cause: any) {\n _esbReport('error', (cause as _esbuild.TransformFailure).errors)\n _esbReport('warning', (cause as _esbuild.TransformFailure).warnings)\n throwError(type, `ESBuild error transpiling \"${filename}\"`, { cause, start: esbTranpile })\n }\n\n /* Log transpile warnings if debugging */\n if (_debug) _esbReport('warning', result.warnings)\n\n /* Done! */\n return result.code\n}\n\n\n/* ========================================================================== *\n * UTILITIES *\n * ========================================================================== */\n\n/* Returns a boolean indicating whether the specified file exists or not */\nexport function isFile(path: string): boolean {\n try {\n return _fs.statSync(path).isFile()\n } catch {\n return false\n }\n}\n\n/* Returns a boolean indicating whether the specified directory exists or not */\nexport function isDirectory(path: string): boolean {\n try {\n return _fs.statSync(path).isDirectory()\n } catch {\n return false\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AACA,yBAAoB;AACpB,IAAAA,oBAAkB;;;ACmBlB,qBAAgB;AAChB,uBAAkB;AAClB,uBAAkB;AAGlB,qBAAqB;AASd,IAAM,MAAM;AAEZ,IAAM,MAAM;AAOnB,IAAM,YAAY,iBAAAC,QAAM,SAAS,gBAAgB;AACjD,IAAM,SAAS,UAAU;AAGlB,SAAS,WAAW,MAAY,QAAgB,MAAoB;AACzE,MAAI,CAAE,OAAQ;AAEd,QAAM,IAAI,SAAS,MAAM,QAAQ,SAAS,MAAM,QAAQ;AACxD,YAAU,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,IAAI;AACpC;AAGO,SAAS,WACZ,MACA,SACA,UAA4D,CAAC,GACxD;AACP,QAAM,IAAI,SAAS,MAAM,QAAQ,SAAS,MAAM,QAAQ;AACxD,QAAM,SAAS,cAAc,CAAC,QAAQ,QAAQ,GAAG;AAEjD,QAAM,EAAE,QAAQ,YAAY,GAAG,MAAM,IAAI;AACzC,QAAM,QAAQ,IAAI,MAAM,GAAG,MAAM,IAAI,OAAO,EAAE;AAC9C,QAAM,kBAAkB,OAAO,KAAK;AACpC,SAAO,OAAO,OAAO,KAAK;AAE1B,QAAM;AACR;AAWO,SAAS,WAAW,MAAkB;AAC3C,MAAI,QAAQ,IAAI,wBAAwB;AACtC,UAAM,OAAO,QAAQ,IAAI;AACzB,QAAK,SAAS,OAAS,SAAS,KAAM;AACpC,iBAAW,MAAM,oBAAoB,IAAI,oBAAoB;AAC7D,aAAO;AAAA,IACT,OAAO;AACL,iBAAW,MAAM,iBAAiB,QAAQ,IAAI,sBAAsB,GAAG;AAAA,IACzE;AAAA,EACF;AAEA,QAAM,YAAY,CAAC,cAA4B;AAC7C,UAAM,cAAc,iBAAAC,QAAM,KAAK,WAAW,cAAc;AACxD,QAAI;AACF,YAAM,cAAc,eAAAC,QAAI,aAAa,aAAa,OAAO;AACzD,YAAM,cAAc,KAAK,MAAM,WAAW;AAC1C,YAAM,cAAc,YAAY;AAChC,cAAQ,aAAa;AAAA,QACnB,KAAK;AACH,qBAAW,MAAM,SAAS,WAAW,mCAAmC;AACxE,iBAAO;AAAA,QAET,KAAK;AAAA,QACL,KAAK;AACH,qBAAW,MAAM,SAAS,WAAW,uBAAuB,GAAG,GAAG;AAClE,iBAAO;AAAA,QAET;AACE,qBAAW,MAAM,SAAS,WAAW,6BAA6B,WAAW,GAAG;AAChF,iBAAO;AAAA,MACX;AAAA,IACF,SAAS,OAAY;AACnB,UAAK,MAAM,SAAS,YAAc,MAAM,SAAS,UAAW;AAC1D,mBAAW,MAAM,4BAA4B,WAAW,KAAK,EAAE,OAAO,OAAO,UAAU,CAAC;AAAA,MAC1F;AAAA,IACF;AAEA,UAAM,SAAS,iBAAAD,QAAM,QAAQ,SAAS;AACtC,QAAI,cAAc,OAAQ,QAAO,UAAU,
|
|
4
|
+
"sourcesContent": ["// NodeJS dependencies\nimport _module from 'node:module'\nimport _path from 'node:path'\n\nimport {\n CJS,\n ESM,\n esbTranpile,\n logMessage,\n moduleType,\n throwError,\n} from './loader-shared'\n\n/* ========================================================================== *\n * CJS VERSION *\n * ========================================================================== */\n\n/** The extension handler type, loading CJS modules */\ntype ExtensionHandler = (module: NodeJS.Module, filename: string) => void\n\n/* Add the `_compile(...)` method to NodeJS' `Module` interface */\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace NodeJS {\n interface Module {\n _compile: (contents: string, filename: string) => void\n }\n }\n}\n\n/**\n * Add the `_extensions[...]` and `resolveFilename(...)` members to the\n * definition of `node:module`.\n */\ndeclare module 'node:module' {\n const _extensions: Record<`.${string}`, ExtensionHandler>\n function _resolveFilename(\n request: string,\n parent: _module | undefined,\n isMain: boolean,\n options?: any,\n ): string\n}\n\n/* ========================================================================== */\n\nconst _type = moduleType(CJS)\n\nconst loader: ExtensionHandler = (module, filename): void => {\n logMessage(CJS, `Attempting to load \"${filename}\"`)\n\n /* Figure our the extension (\".ts\" or \".cts\")... */\n const ext = _path.extname(filename)\n\n /* If the file is a \".ts\", we need to figure out the default type */\n if (ext === '.ts') {\n /* If the _default_ module type is CJS then load as such! */\n if (_type === ESM) {\n throwError(CJS, `Must use import to load ES Module: ${filename}`, { code: 'ERR_REQUIRE_ESM' })\n }\n } else if (ext !== '.cts') {\n throwError(CJS, `Unsupported filename \"${filename}\"`)\n }\n\n const source = esbTranpile(filename, CJS)\n\n /* Let node do its thing, but wrap any error it throws */\n try {\n module._compile(source, filename)\n } catch (cause) {\n console.error(`Error compiling module \"${filename}\"`, cause)\n }\n}\n\n/**\n * Replace _module._resolveFilename with our own.\n *\n * This is a _HACK BEYOND REDEMPTION_ and I'm ashamed of even _thinking_ about\n * it, but, well, it makes things work.\n *\n * TypeScript allows us to import \"./foo.js\", and internally resolves this to\n * \"./foo.ts\" (yeah, nice, right?) and while we normally wouldn't want to deal\n * with this kind of stuff, the \"node16\" module resolution mode _forces_ us to\n * use this syntax.\n *\n * And we _need_ the \"node16\" module resolution to properly consume \"export\n * conditions\" from other packages. Since ESBuild's plugins only work in async\n * mode, changing those import statements on the fly is out of the question, so\n * we need to hack our way into Node's own resolver.\n *\n * See my post: https://twitter.com/ianosh/status/1559484168685379590\n * ESBuild related fix: https://github.com/evanw/esbuild/commit/0cdc005e3d1c765a084f206741bc4bff78e30ec4\n */\nconst _oldResolveFilename = _module._resolveFilename\n_module._resolveFilename = function(\n request: string,\n parent: _module | undefined,\n ...args: [ isMain: boolean, options: any ]\n): any {\n try {\n /* First call the old _resolveFilename to see what Node thinks */\n return _oldResolveFilename.call(this, request, parent, ...args)\n } catch (error: any) {\n /* If the error was anything but \"MODULE_NOT_FOUND\" bail out */\n if (error.code !== 'MODULE_NOT_FOUND') throw error\n\n /* Check if the \"request\" ends with \".js\", \".mjs\" or \".cjs\" */\n const match = request.match(/(.*)(\\.[mc]?js$)/)\n\n /*\n * If the file matches our extension, _and_ we have a parent, we simply\n * try with a new extension (e.g. \".js\" becomes \".ts\")...\n */\n if (parent && match) {\n const [ , name, ext ] = match\n const tsrequest = name + ext!.replace('js', 'ts')\n try {\n const result = _oldResolveFilename.call(this, tsrequest, parent, ...args)\n logMessage(CJS, `Resolution for \"${request}\" intercepted as \"${tsrequest}`)\n return result\n } catch {\n throw error // throw the _original_ error in this case\n }\n }\n\n /* We have no parent, or we don't match our extension, throw! */\n throw error\n }\n}\n\n/* ========================================================================== */\n\n/* Remember to load our loader for .TS/.CTS as CommonJS modules */\n_module._extensions['.ts'] = _module._extensions['.cts'] = loader\nlogMessage(CJS, 'TypeScript loader for CommonJS loaded')\n", "/* eslint-disable @typescript-eslint/no-unsafe-function-type */\n/* ========================================================================== *\n * HACK BEYOND REDEMPTION: TRANSPILE .ts FILES (the esm loader) *\n * -------------------------------------------------------------------------- *\n * Use ESBuild to quickly transpile TypeScript files into JavaScript. *\n * *\n * The plan as it stands is as follows: *\n * - `.mts` files always get transpiled to ESM modules *\n * - `.cts` files always get transpiled to CJS modules *\n * - `.ts` files are treanspiled according to what's in `package.json` *\n * *\n * Additionally, when transpiling to ESM modules, we can't rely on the magic *\n * that Node's `require(...)` call uses to figure out which file to import. *\n * We need to _actually verify_ on disk what's the correct file to import. *\n * *\n * This is a single module, only available as ESM, and it will _both_ behave *\n * as a NodeJS' loader, _and_ inject the CJS extension handlers (hack) found *\n * in the `_extensions` of `node:module` (same as `require.extensions`). *\n * ========================================================================== */\n\n// NodeJS dependencies\nimport _fs from 'node:fs'\nimport _path from 'node:path'\nimport _util from 'node:util'\n\n// ESBuild is the only external dependency\nimport _esbuild from 'esbuild'\n\n/* ========================================================================== *\n * CONSTANTS AND TYPES *\n * ========================================================================== */\n\n/** Supported types from `package.json` */\nexport type Type = 'commonjs' | 'module'\n/** Constant identifying a `commonjs` module */\nexport const CJS = 'commonjs' as const\n/** Constant identifying an ESM `module` */\nexport const ESM = 'module' as const\n\n/* ========================================================================== *\n * DEBUGGING AND ERRORS *\n * ========================================================================== */\n\n/** Setup debugging */\nconst _debugLog = _util.debuglog('plug:ts-loader')\nconst _debug = _debugLog.enabled\n\n/** Emit some logs if `DEBUG_TS_LOADER` is set to `true` */\nexport function logMessage(mode: Type, arg: string, ...args: any []): void {\n if (! _debug) return\n\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n _debugLog(`[${t}] ${arg}`, ...args)\n}\n\n/** Fail miserably */\nexport function throwError(\n mode: Type,\n message: string,\n options: { start?: Function, code?: string, cause?: any } = {},\n): never {\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n const prefix = `[ts-loader|${t}|pid=${process.pid}]`\n\n const { start = throwError, ...extra } = options\n const error = new Error(`${prefix} ${message}`)\n Error.captureStackTrace(error, start)\n Object.assign(error, extra)\n\n throw error\n}\n\n/* ========================================================================== *\n * MODULE TYPES AND FORCING TYPE *\n * ========================================================================== */\n\n/**\n * Determine the current module type to transpile .TS files as looking at the\n * `__TS_LOADER_FORCE_TYPE` environment variable (used by PlugJS and CLI) or,\n * if unspecified, looking at the `type` field in the `package.json` file.\n */\nexport function moduleType(mode: Type): Type {\n if (process.env.__TS_LOADER_FORCE_TYPE) {\n const type = process.env.__TS_LOADER_FORCE_TYPE\n if ((type === CJS) || (type === ESM)) {\n logMessage(mode, `Forcing type to \"${type}\" from environment`)\n return type\n } else {\n throwError(mode, `Invalid type \"${process.env.__TS_LOADER_FORCE_TYPE}\"`)\n }\n }\n\n const _findType = (directory: string): Type => {\n const packageFile = _path.join(directory, 'package.json')\n try {\n const packageData = _fs.readFileSync(packageFile, 'utf-8')\n const packageJson = JSON.parse(packageData)\n const packageType = packageJson.type\n switch (packageType) {\n case undefined:\n logMessage(mode, `File \"${packageFile}\" does not declare a default type`)\n return CJS\n\n case CJS:\n case ESM:\n logMessage(mode, `File \"${packageFile}\" declares type as \"${CJS}\"`)\n return packageType\n\n default:\n logMessage(mode, `File \"${packageFile}\" specifies unknown type \"${packageType}\"`)\n return CJS\n }\n } catch (cause: any) {\n if ((cause.code !== 'ENOENT') && (cause.code !== 'EISDIR')) {\n throwError(mode, `Unable to read or parse \"${packageFile}\"`, { cause, start: _findType })\n }\n }\n\n const parent = _path.dirname(directory)\n if (directory !== parent) return _findType(parent)\n\n logMessage(mode, `Type defaulted to \"${CJS}\"`)\n return CJS\n }\n\n return _findType(process.cwd())\n}\n\n/* ========================================================================== *\n * ESBUILD HELPERS *\n * ========================================================================== */\n\n/**\n * Take an ESBuild `BuildResult` or `BuildFailure` (they both have arrays\n * of `Message` in both `warnings` and `errors`), format them and print them\n * out nicely. Then fail if any error was detected.\n */\nfunction _esbReport(\n kind: 'error' | 'warning',\n messages: _esbuild.Message[] = [],\n): void {\n const output = process.stderr\n const options = { color: !!output.isTTY, terminalWidth: output.columns || 80 }\n\n const array = _esbuild.formatMessagesSync(messages, { kind, ...options })\n array.forEach((message) => output.write(`${message}\\n`))\n}\n\n/**\n * Transpile with ESBuild\n */\nexport function esbTranpile(filename: string, type: Type): string {\n logMessage(type, `Transpiling \"${filename}\" as \"${type}\"`)\n\n const [ format, __fileurl ] = type === ESM ?\n [ 'esm', 'import.meta.url' ] as const :\n [ 'cjs', '__filename' ] as const\n\n /* ESbuild options */\n const options: _esbuild.TransformOptions = {\n sourcefile: filename, // the original filename we're parsing\n format, // what are we actually transpiling to???\n loader: 'ts', // the format is always \"typescript\"\n sourcemap: 'inline', // always inline source maps\n sourcesContent: false, // do not include sources content in sourcemap\n platform: 'node', // d'oh! :-)\n minifyWhitespace: true, // https://github.com/evanw/esbuild/releases/tag/v0.16.14\n logLevel: 'silent', // catching those in our _esbReport below\n target: `node${process.versions['node']}`, // target _this_ version\n define: { __fileurl }, // from \"globals.d.ts\"\n }\n\n /* Emit a line on the console when loading in debug mode */\n if (_debug) {\n if (format === 'esm') {\n options.banner = `;(await import('node:util')).debuglog('plug:ts-loader')('[esm] Loaded \"%s\"', ${__fileurl});`\n } else if (format === 'cjs') {\n options.banner = `;require('node:util').debuglog('plug:ts-loader')('[cjs] Loaded \"%s\"', ${__fileurl});`\n }\n }\n\n /* Transpile our TypeScript file into some JavaScript stuff */\n let result\n try {\n const source = _fs.readFileSync(filename, 'utf-8')\n result = _esbuild.transformSync(source, options)\n } catch (cause: any) {\n _esbReport('error', (cause as _esbuild.TransformFailure).errors)\n _esbReport('warning', (cause as _esbuild.TransformFailure).warnings)\n throwError(type, `ESBuild error transpiling \"${filename}\"`, { cause, start: esbTranpile })\n }\n\n /* Log transpile warnings if debugging */\n if (_debug) _esbReport('warning', result.warnings)\n\n /* Done! */\n return result.code\n}\n\n\n/* ========================================================================== *\n * UTILITIES *\n * ========================================================================== */\n\n/* Returns a boolean indicating whether the specified file exists or not */\nexport function isFile(path: string): boolean {\n try {\n return _fs.statSync(path).isFile()\n } catch {\n return false\n }\n}\n\n/* Returns a boolean indicating whether the specified directory exists or not */\nexport function isDirectory(path: string): boolean {\n try {\n return _fs.statSync(path).isDirectory()\n } catch {\n return false\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AACA,yBAAoB;AACpB,IAAAA,oBAAkB;;;ACmBlB,qBAAgB;AACh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E,QAAQ;AACpC,mBAAAA,QAAQ,mBAAmB,SACvB,SACA,WACG,MACA;AACL,MAAI;AAEF,WAAO,oBAAoB,KAAK,MAAM,SAAS,QAAQ,GAAG,IAAI;AAAA,EAChE,SAAS,OAAY;AAEnB,QAAI,MAAM,SAAS,mBAAoB,OAAM;AAG7C,UAAM,QAAQ,QAAQ,MAAM,kBAAkB;AAM9C,QAAI,UAAU,OAAO;AACnB,YAAM,CAAE,EAAE,MAAM,GAAI,IAAI;AACxB,YAAM,YAAY,OAAO,IAAK,QAAQ,MAAM,IAAI;AAChD,UAAI;AACF,cAAM,SAAS,oBAAoB,KAAK,MAAM,WAAW,QAAQ,GAAG,IAAI;AACxE,mBAAW,KAAK,mBAAmB,OAAO,qBAAqB,SAAS,EAAE;AAC1E,eAAO;AAAA,MACT,QAAQ;AACN,cAAM;AAAA,MACR;AAAA,IACF;AAGA,UAAM;AAAA,EACR;AACF;AAKA,mBAAAA,QAAQ,YAAY,KAAK,IAAI,mBAAAA,QAAQ,YAAY,MAAM,IAAI;AAC3D,WAAW,KAAK,uCAAuC;",
|
|
6
6
|
"names": ["import_node_path", "_util", "_path", "_fs", "_esbuild", "module", "_path", "_module"]
|
|
7
7
|
}
|
package/dist/loader-module.mjs
CHANGED
|
@@ -59,7 +59,7 @@ function moduleType(mode) {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
const parent = _path.dirname(directory);
|
|
62
|
-
if (directory !== parent) return _findType(
|
|
62
|
+
if (directory !== parent) return _findType(parent);
|
|
63
63
|
logMessage(mode, `Type defaulted to "${CJS}"`);
|
|
64
64
|
return CJS;
|
|
65
65
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/loader-module.mts", "../src/loader-shared.ts"],
|
|
4
|
-
"sourcesContent": ["import _path from 'node:path'\nimport _url from 'node:url'\n\nimport {\n CJS,\n ESM,\n esbTranpile,\n isDirectory,\n isFile,\n logMessage,\n moduleType,\n} from './loader-shared'\n\n/* ========================================================================== *\n * ESM VERSION *\n * ========================================================================== */\n\n/** The formats that can be handled by NodeJS' loader */\ntype Format = 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm'\n\n/* ========================================================================== */\n\n/** The type identifying a NodeJS' loader `resolve` hook. */\ntype ResolveHook = (\n /** Whatever was requested to be imported (module, relative file, ...). */\n specifier: string,\n /** Context information around this `resolve` hook call. */\n context: ResolveContext,\n /** The subsequent resolve hook in the chain, or the Node.js default one. */\n nextResolve: ResolveNext,\n) => ResolveResult | Promise<ResolveResult>\n\n/** Context information around a `resolve` hook call. */\ninterface ResolveContext {\n importAssertions: object\n /** Export conditions of the relevant `package.json`. */\n conditions: string[]\n /** The module importing this one, or undefined if this is the entry point. */\n parentURL?: string | undefined\n}\n\n/** The subsequent resolve hook in the chain, or the Node.js default one. */\ntype ResolveNext = (specifier: string, context: ResolveContext) => ResolveResult | Promise<ResolveResult>\n\n/** A type describing the required results from a `resolve` hook */\ninterface ResolveResult {\n /** The absolute URL to which this input resolves. */\n url: string\n /** A format hint to the `load` hook (it might be ignored). */\n format?: Format | null | undefined\n /** A signal that this hook intends to terminate the chain of resolve hooks. */\n shortCircuit?: boolean | undefined\n}\n\n/* ========================================================================== */\n\n/** The type identifying a NodeJS' loader `load` hook. */\ntype LoadHook = (\n /** The URL returned by the resolve chain. */\n url: string,\n /** Context information around this `load` hook call. */\n context: LoadContext,\n /** The subsequent load hook in the chain, or the Node.js default one. */\n nextLoad: LoadNext,\n) => LoadResult | Promise<LoadResult>\n\n/** Context information around a `load` hook call. */\ninterface LoadContext {\n importAssertions: object\n /** Export conditions of the relevant `package.json` */\n conditions: string[]\n /** The format hint from the `resolve` hook. */\n format?: ResolveResult['format']\n}\n\n/** The subsequent load hook in the chain, or the Node.js default one. */\ntype LoadNext = (url: string, context: LoadContext) => LoadResult | Promise<LoadResult>\n\n/** A type describing the required results from a `resolve` hook */\ntype LoadResult = {\n /** The format of the code being loaded. */\n format: Format\n /** A signal that this hook intends to terminate the chain of load hooks. */\n shortCircuit?: boolean | undefined\n} & ({\n format: 'builtin' | 'commonjs'\n /** When the source is `builtin` or `commonjs` no source must be returned */\n source?: never | undefined\n} | {\n format: 'json' | 'module'\n /** When the source is `json` or `module` the source can include strings */\n source: string | ArrayBuffer | NodeJS.TypedArray\n} | {\n format: 'wasm'\n /** When the source is `wasm` the source must not be a string */\n source: ArrayBuffer | NodeJS.TypedArray\n})\n\n/* ========================================================================== */\n\nconst _type = moduleType(CJS)\n\n/**\n * Our main `resolve` hook: here we need to check for a couple of options\n * when importing \"\"\n */\nexport const resolve: ResolveHook = (specifier, context, nextResolve): ResolveResult | Promise<ResolveResult> => {\n logMessage(ESM, `Resolving \"${specifier}\" from \"${context.parentURL}\"`)\n\n /* We only resolve relative paths (\"./xxx\" or \"../xxx\") */\n if (! specifier.match(/^\\.\\.?\\//)) return nextResolve(specifier, context)\n\n /* We only resolve if we _do_ have a parent URL and it's a file */\n const parentURL = context.parentURL\n if (! parentURL) return nextResolve(specifier, context)\n if (! parentURL.startsWith('file:')) return nextResolve(specifier, context)\n\n /* We only resolve here if the importer is a \".ts\" or \".mts\" file */\n if (! parentURL.match(/\\.m?ts$/)) return nextResolve(specifier, context)\n\n /* The resolved URL is the specifier resolved against the parent */\n const url = new URL(specifier, parentURL).href\n const path = _url.fileURLToPath(url)\n\n /*\n * Here we are sure that:\n *\n * 1) we are resolving a local path (not a module)\n * 2) the importer is a file, ending with \".ts\" or \".mts\"\n *\n * Now we can check if \"import 'foo'\" resolves to:\n *\n * 1) directly to a file, e.g. \"import './foo.js'\" or \"import './foo.mts'\"\n * 2) import a \"pseudo-JS file\", e.g. \"import './foo.js'\" becomes \"import './foo.ts'\"\n * 3) imports a file without extension as if it were \"import './foo.ts'\"\n * 4) imports a directory as in \"import './foo/index.ts'\"\n *\n * We resolve the _final_ specifier that will be passed to the next resolver\n * for further potential resolution accordingly.\n *\n * We start with the easiest case: is this a real file on the disk?\n */\n if (isFile(path)) {\n logMessage(ESM, `Positive match for \"${specifier}\" as \"${path}\" (1)`)\n return nextResolve(specifier, context) // straight on\n }\n\n /*\n * TypeScript allows us to import \"./foo.js\", and internally resolves this to\n * \"./foo.ts\" (yeah, nice, right?) and while we normally wouldn't want to deal\n * with this kind of stuff, the \"node16\" module resolution mode _forces_ us to\n * use this syntax.\n */\n const match = specifier.match(/(.*)(\\.[mc]?js$)/)\n if (match) {\n const [ , base, ext ] = match\n const tsspecifier = base + ext!.replace('js', 'ts')\n const tsurl = new URL(tsspecifier, parentURL).href\n const tspath = _url.fileURLToPath(tsurl)\n\n if (isFile(tspath)) {\n logMessage(ESM, `Positive match for \"${specifier}\" as \"${tspath}\" (2)`)\n return nextResolve(tsspecifier, context) // straight on\n }\n }\n\n /* Check if the import is actually a file with a \".ts\" extension */\n if (isFile(`${path}.ts`)) {\n logMessage(ESM, `Positive match for \"${specifier}.ts\" as \"${path}.ts\" (3)`)\n return nextResolve(`${specifier}.ts`, context)\n }\n\n /* If the file is a directory, then see if we have an \"index.ts\" in there */\n if (isDirectory(path)) {\n const file = _path.resolve(path, 'index.ts') // resolve, as path is absolute\n if (isFile(file)) {\n logMessage(ESM, `Positive match for \"${specifier}\" as \"${file}\" (4)`)\n const spec = _url.pathToFileURL(file).pathname\n return nextResolve(spec, context)\n }\n }\n\n /* There's really nothing else we can do */\n return nextResolve(specifier, context)\n}\n\n/** Our main `load` hook */\nexport const load: LoadHook = (url, context, nextLoad): LoadResult | Promise<LoadResult> => {\n logMessage(ESM, `Attempting to load \"${url}\"`)\n\n /* We only load from disk, so ignore everything else */\n if (! url.startsWith('file:')) return nextLoad(url, context)\n\n /* Figure our the extension (especially \".ts\", \".mts\" or \".cts\")... */\n const ext = url.match(/\\.[cm]?ts$/)?.[0]\n\n /* Quick and easy bail-outs for non-TS or \".cts\" (always `commonjs`) */\n if (! ext) return nextLoad(url, context)\n\n if (ext === '.cts') {\n logMessage(ESM, `Switching type from \"module\" to \"commonjs\" for \"${url}\"`)\n logMessage(ESM, 'Please note that named import WILL NOT WORK in this case, as Node.js performs a')\n logMessage(ESM, 'static analisys on the CommonJS source code, and this file is transpiled from.')\n logMessage(ESM, 'TypeScript to CommonJS dynamically.')\n return { format: CJS, shortCircuit: true }\n }\n\n /* Convert the url into a file name, any error gets ignored */\n const filename = _url.fileURLToPath(url)\n\n /* If the file is a \".ts\", we need to figure out the default type */\n if (ext === '.ts') {\n if (_type === CJS) {\n logMessage(ESM, `Switching type from \"module\" to \"commonjs\" for \"${url}\"`)\n logMessage(ESM, 'Please note that named import WILL NOT WORK in this case, as Node.js performs a')\n logMessage(ESM, 'static analisys on the CommonJS source code, and this file is transpiled from.')\n logMessage(ESM, 'TypeScript to CommonJS dynamically.')\n return { format: CJS, shortCircuit: true }\n }\n }\n\n /* Transpile with ESBuild and return our source code */\n const source = esbTranpile(filename, ESM)\n return { source, format: ESM, shortCircuit: true }\n}\n\n/* ========================================================================== */\n\n/* Simply output the fact that we were loaded */\nlogMessage(ESM, 'TypeScript loader for ES Modules loaded')\n", "/* eslint-disable @typescript-eslint/no-unsafe-function-type */\n/* ========================================================================== *\n * HACK BEYOND REDEMPTION: TRANSPILE .ts FILES (the esm loader) *\n * -------------------------------------------------------------------------- *\n * Use ESBuild to quickly transpile TypeScript files into JavaScript. *\n * *\n * The plan as it stands is as follows: *\n * - `.mts` files always get transpiled to ESM modules *\n * - `.cts` files always get transpiled to CJS modules *\n * - `.ts` files are treanspiled according to what's in `package.json` *\n * *\n * Additionally, when transpiling to ESM modules, we can't rely on the magic *\n * that Node's `require(...)` call uses to figure out which file to import. *\n * We need to _actually verify_ on disk what's the correct file to import. *\n * *\n * This is a single module, only available as ESM, and it will _both_ behave *\n * as a NodeJS' loader, _and_ inject the CJS extension handlers (hack) found *\n * in the `_extensions` of `node:module` (same as `require.extensions`). *\n * ========================================================================== */\n\n// NodeJS dependencies\nimport _fs from 'node:fs'\nimport _path from 'node:path'\nimport _util from 'node:util'\n\n// ESBuild is the only external dependency\nimport _esbuild from 'esbuild'\n\n/* ========================================================================== *\n * CONSTANTS AND TYPES *\n * ========================================================================== */\n\n/** Supported types from `package.json` */\nexport type Type = 'commonjs' | 'module'\n/** Constant identifying a `commonjs` module */\nexport const CJS = 'commonjs' as const\n/** Constant identifying an ESM `module` */\nexport const ESM = 'module' as const\n\n/* ========================================================================== *\n * DEBUGGING AND ERRORS *\n * ========================================================================== */\n\n/** Setup debugging */\nconst _debugLog = _util.debuglog('plug:ts-loader')\nconst _debug = _debugLog.enabled\n\n/** Emit some logs if `DEBUG_TS_LOADER` is set to `true` */\nexport function logMessage(mode: Type, arg: string, ...args: any []): void {\n if (! _debug) return\n\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n _debugLog(`[${t}] ${arg}`, ...args)\n}\n\n/** Fail miserably */\nexport function throwError(\n mode: Type,\n message: string,\n options: { start?: Function, code?: string, cause?: any } = {},\n): never {\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n const prefix = `[ts-loader|${t}|pid=${process.pid}]`\n\n const { start = throwError, ...extra } = options\n const error = new Error(`${prefix} ${message}`)\n Error.captureStackTrace(error, start)\n Object.assign(error, extra)\n\n throw error\n}\n\n/* ========================================================================== *\n * MODULE TYPES AND FORCING TYPE *\n * ========================================================================== */\n\n/**\n * Determine the current module type to transpile .TS files as looking at the\n * `__TS_LOADER_FORCE_TYPE` environment variable (used by PlugJS and CLI) or,\n * if unspecified, looking at the `type` field in the `package.json` file.\n */\nexport function moduleType(mode: Type): Type {\n if (process.env.__TS_LOADER_FORCE_TYPE) {\n const type = process.env.__TS_LOADER_FORCE_TYPE\n if ((type === CJS) || (type === ESM)) {\n logMessage(mode, `Forcing type to \"${type}\" from environment`)\n return type\n } else {\n throwError(mode, `Invalid type \"${process.env.__TS_LOADER_FORCE_TYPE}\"`)\n }\n }\n\n const _findType = (directory: string): Type => {\n const packageFile = _path.join(directory, 'package.json')\n try {\n const packageData = _fs.readFileSync(packageFile, 'utf-8')\n const packageJson = JSON.parse(packageData)\n const packageType = packageJson.type\n switch (packageType) {\n case undefined:\n logMessage(mode, `File \"${packageFile}\" does not declare a default type`)\n return CJS\n\n case CJS:\n case ESM:\n logMessage(mode, `File \"${packageFile}\" declares type as \"${CJS}\"`)\n return packageType\n\n default:\n logMessage(mode, `File \"${packageFile}\" specifies unknown type \"${packageType}\"`)\n return CJS\n }\n } catch (cause: any) {\n if ((cause.code !== 'ENOENT') && (cause.code !== 'EISDIR')) {\n throwError(mode, `Unable to read or parse \"${packageFile}\"`, { cause, start: _findType })\n }\n }\n\n const parent = _path.dirname(directory)\n if (directory !== parent) return _findType(directory)\n\n logMessage(mode, `Type defaulted to \"${CJS}\"`)\n return CJS\n }\n\n return _findType(process.cwd())\n}\n\n/* ========================================================================== *\n * ESBUILD HELPERS *\n * ========================================================================== */\n\n/**\n * Take an ESBuild `BuildResult` or `BuildFailure` (they both have arrays\n * of `Message` in both `warnings` and `errors`), format them and print them\n * out nicely. Then fail if any error was detected.\n */\nfunction _esbReport(\n kind: 'error' | 'warning',\n messages: _esbuild.Message[] = [],\n): void {\n const output = process.stderr\n const options = { color: !!output.isTTY, terminalWidth: output.columns || 80 }\n\n const array = _esbuild.formatMessagesSync(messages, { kind, ...options })\n array.forEach((message) => output.write(`${message}\\n`))\n}\n\n/**\n * Transpile with ESBuild\n */\nexport function esbTranpile(filename: string, type: Type): string {\n logMessage(type, `Transpiling \"${filename}\" as \"${type}\"`)\n\n const [ format, __fileurl ] = type === ESM ?\n [ 'esm', 'import.meta.url' ] as const :\n [ 'cjs', '__filename' ] as const\n\n /* ESbuild options */\n const options: _esbuild.TransformOptions = {\n sourcefile: filename, // the original filename we're parsing\n format, // what are we actually transpiling to???\n loader: 'ts', // the format is always \"typescript\"\n sourcemap: 'inline', // always inline source maps\n sourcesContent: false, // do not include sources content in sourcemap\n platform: 'node', // d'oh! :-)\n minifyWhitespace: true, // https://github.com/evanw/esbuild/releases/tag/v0.16.14\n logLevel: 'silent', // catching those in our _esbReport below\n target: `node${process.versions['node']}`, // target _this_ version\n define: { __fileurl }, // from \"globals.d.ts\"\n }\n\n /* Emit a line on the console when loading in debug mode */\n if (_debug) {\n if (format === 'esm') {\n options.banner = `;(await import('node:util')).debuglog('plug:ts-loader')('[esm] Loaded \"%s\"', ${__fileurl});`\n } else if (format === 'cjs') {\n options.banner = `;require('node:util').debuglog('plug:ts-loader')('[cjs] Loaded \"%s\"', ${__fileurl});`\n }\n }\n\n /* Transpile our TypeScript file into some JavaScript stuff */\n let result\n try {\n const source = _fs.readFileSync(filename, 'utf-8')\n result = _esbuild.transformSync(source, options)\n } catch (cause: any) {\n _esbReport('error', (cause as _esbuild.TransformFailure).errors)\n _esbReport('warning', (cause as _esbuild.TransformFailure).warnings)\n throwError(type, `ESBuild error transpiling \"${filename}\"`, { cause, start: esbTranpile })\n }\n\n /* Log transpile warnings if debugging */\n if (_debug) _esbReport('warning', result.warnings)\n\n /* Done! */\n return result.code\n}\n\n\n/* ========================================================================== *\n * UTILITIES *\n * ========================================================================== */\n\n/* Returns a boolean indicating whether the specified file exists or not */\nexport function isFile(path: string): boolean {\n try {\n return _fs.statSync(path).isFile()\n } catch {\n return false\n }\n}\n\n/* Returns a boolean indicating whether the specified directory exists or not */\nexport function isDirectory(path: string): boolean {\n try {\n return _fs.statSync(path).isDirectory()\n } catch {\n return false\n }\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,OAAOA,YAAW;AAClB,OAAO,UAAU;;;ACoBjB,OAAO,SAAS;AAChB,OAAO,WAAW;AAClB,OAAO,WAAW;AAGlB,OAAO,cAAc;AASd,IAAM,MAAM;AAEZ,IAAM,MAAM;AAOnB,IAAM,YAAY,MAAM,SAAS,gBAAgB;AACjD,IAAM,SAAS,UAAU;AAGlB,SAAS,WAAW,MAAY,QAAgB,MAAoB;AACzE,MAAI,CAAE,OAAQ;AAEd,QAAM,IAAI,SAAS,MAAM,QAAQ,SAAS,MAAM,QAAQ;AACxD,YAAU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,WAAW,KAAK,EAAE,OAAO,OAAO,UAAU,CAAC;AAAA,MAC1F;AAAA,IACF;AAEA,UAAM,SAAS,MAAM,QAAQ,SAAS;AACtC,QAAI,cAAc,OAAQ,QAAO,UAAU,
|
|
4
|
+
"sourcesContent": ["import _path from 'node:path'\nimport _url from 'node:url'\n\nimport {\n CJS,\n ESM,\n esbTranpile,\n isDirectory,\n isFile,\n logMessage,\n moduleType,\n} from './loader-shared'\n\n/* ========================================================================== *\n * ESM VERSION *\n * ========================================================================== */\n\n/** The formats that can be handled by NodeJS' loader */\ntype Format = 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm'\n\n/* ========================================================================== */\n\n/** The type identifying a NodeJS' loader `resolve` hook. */\ntype ResolveHook = (\n /** Whatever was requested to be imported (module, relative file, ...). */\n specifier: string,\n /** Context information around this `resolve` hook call. */\n context: ResolveContext,\n /** The subsequent resolve hook in the chain, or the Node.js default one. */\n nextResolve: ResolveNext,\n) => ResolveResult | Promise<ResolveResult>\n\n/** Context information around a `resolve` hook call. */\ninterface ResolveContext {\n importAssertions: object\n /** Export conditions of the relevant `package.json`. */\n conditions: string[]\n /** The module importing this one, or undefined if this is the entry point. */\n parentURL?: string | undefined\n}\n\n/** The subsequent resolve hook in the chain, or the Node.js default one. */\ntype ResolveNext = (specifier: string, context: ResolveContext) => ResolveResult | Promise<ResolveResult>\n\n/** A type describing the required results from a `resolve` hook */\ninterface ResolveResult {\n /** The absolute URL to which this input resolves. */\n url: string\n /** A format hint to the `load` hook (it might be ignored). */\n format?: Format | null | undefined\n /** A signal that this hook intends to terminate the chain of resolve hooks. */\n shortCircuit?: boolean | undefined\n}\n\n/* ========================================================================== */\n\n/** The type identifying a NodeJS' loader `load` hook. */\ntype LoadHook = (\n /** The URL returned by the resolve chain. */\n url: string,\n /** Context information around this `load` hook call. */\n context: LoadContext,\n /** The subsequent load hook in the chain, or the Node.js default one. */\n nextLoad: LoadNext,\n) => LoadResult | Promise<LoadResult>\n\n/** Context information around a `load` hook call. */\ninterface LoadContext {\n importAssertions: object\n /** Export conditions of the relevant `package.json` */\n conditions: string[]\n /** The format hint from the `resolve` hook. */\n format?: ResolveResult['format']\n}\n\n/** The subsequent load hook in the chain, or the Node.js default one. */\ntype LoadNext = (url: string, context: LoadContext) => LoadResult | Promise<LoadResult>\n\n/** A type describing the required results from a `resolve` hook */\ntype LoadResult = {\n /** The format of the code being loaded. */\n format: Format\n /** A signal that this hook intends to terminate the chain of load hooks. */\n shortCircuit?: boolean | undefined\n} & ({\n format: 'builtin' | 'commonjs'\n /** When the source is `builtin` or `commonjs` no source must be returned */\n source?: never | undefined\n} | {\n format: 'json' | 'module'\n /** When the source is `json` or `module` the source can include strings */\n source: string | ArrayBuffer | NodeJS.TypedArray\n} | {\n format: 'wasm'\n /** When the source is `wasm` the source must not be a string */\n source: ArrayBuffer | NodeJS.TypedArray\n})\n\n/* ========================================================================== */\n\nconst _type = moduleType(CJS)\n\n/**\n * Our main `resolve` hook: here we need to check for a couple of options\n * when importing \"\"\n */\nexport const resolve: ResolveHook = (specifier, context, nextResolve): ResolveResult | Promise<ResolveResult> => {\n logMessage(ESM, `Resolving \"${specifier}\" from \"${context.parentURL}\"`)\n\n /* We only resolve relative paths (\"./xxx\" or \"../xxx\") */\n if (! specifier.match(/^\\.\\.?\\//)) return nextResolve(specifier, context)\n\n /* We only resolve if we _do_ have a parent URL and it's a file */\n const parentURL = context.parentURL\n if (! parentURL) return nextResolve(specifier, context)\n if (! parentURL.startsWith('file:')) return nextResolve(specifier, context)\n\n /* We only resolve here if the importer is a \".ts\" or \".mts\" file */\n if (! parentURL.match(/\\.m?ts$/)) return nextResolve(specifier, context)\n\n /* The resolved URL is the specifier resolved against the parent */\n const url = new URL(specifier, parentURL).href\n const path = _url.fileURLToPath(url)\n\n /*\n * Here we are sure that:\n *\n * 1) we are resolving a local path (not a module)\n * 2) the importer is a file, ending with \".ts\" or \".mts\"\n *\n * Now we can check if \"import 'foo'\" resolves to:\n *\n * 1) directly to a file, e.g. \"import './foo.js'\" or \"import './foo.mts'\"\n * 2) import a \"pseudo-JS file\", e.g. \"import './foo.js'\" becomes \"import './foo.ts'\"\n * 3) imports a file without extension as if it were \"import './foo.ts'\"\n * 4) imports a directory as in \"import './foo/index.ts'\"\n *\n * We resolve the _final_ specifier that will be passed to the next resolver\n * for further potential resolution accordingly.\n *\n * We start with the easiest case: is this a real file on the disk?\n */\n if (isFile(path)) {\n logMessage(ESM, `Positive match for \"${specifier}\" as \"${path}\" (1)`)\n return nextResolve(specifier, context) // straight on\n }\n\n /*\n * TypeScript allows us to import \"./foo.js\", and internally resolves this to\n * \"./foo.ts\" (yeah, nice, right?) and while we normally wouldn't want to deal\n * with this kind of stuff, the \"node16\" module resolution mode _forces_ us to\n * use this syntax.\n */\n const match = specifier.match(/(.*)(\\.[mc]?js$)/)\n if (match) {\n const [ , base, ext ] = match\n const tsspecifier = base + ext!.replace('js', 'ts')\n const tsurl = new URL(tsspecifier, parentURL).href\n const tspath = _url.fileURLToPath(tsurl)\n\n if (isFile(tspath)) {\n logMessage(ESM, `Positive match for \"${specifier}\" as \"${tspath}\" (2)`)\n return nextResolve(tsspecifier, context) // straight on\n }\n }\n\n /* Check if the import is actually a file with a \".ts\" extension */\n if (isFile(`${path}.ts`)) {\n logMessage(ESM, `Positive match for \"${specifier}.ts\" as \"${path}.ts\" (3)`)\n return nextResolve(`${specifier}.ts`, context)\n }\n\n /* If the file is a directory, then see if we have an \"index.ts\" in there */\n if (isDirectory(path)) {\n const file = _path.resolve(path, 'index.ts') // resolve, as path is absolute\n if (isFile(file)) {\n logMessage(ESM, `Positive match for \"${specifier}\" as \"${file}\" (4)`)\n const spec = _url.pathToFileURL(file).pathname\n return nextResolve(spec, context)\n }\n }\n\n /* There's really nothing else we can do */\n return nextResolve(specifier, context)\n}\n\n/** Our main `load` hook */\nexport const load: LoadHook = (url, context, nextLoad): LoadResult | Promise<LoadResult> => {\n logMessage(ESM, `Attempting to load \"${url}\"`)\n\n /* We only load from disk, so ignore everything else */\n if (! url.startsWith('file:')) return nextLoad(url, context)\n\n /* Figure our the extension (especially \".ts\", \".mts\" or \".cts\")... */\n const ext = url.match(/\\.[cm]?ts$/)?.[0]\n\n /* Quick and easy bail-outs for non-TS or \".cts\" (always `commonjs`) */\n if (! ext) return nextLoad(url, context)\n\n if (ext === '.cts') {\n logMessage(ESM, `Switching type from \"module\" to \"commonjs\" for \"${url}\"`)\n logMessage(ESM, 'Please note that named import WILL NOT WORK in this case, as Node.js performs a')\n logMessage(ESM, 'static analisys on the CommonJS source code, and this file is transpiled from.')\n logMessage(ESM, 'TypeScript to CommonJS dynamically.')\n return { format: CJS, shortCircuit: true }\n }\n\n /* Convert the url into a file name, any error gets ignored */\n const filename = _url.fileURLToPath(url)\n\n /* If the file is a \".ts\", we need to figure out the default type */\n if (ext === '.ts') {\n if (_type === CJS) {\n logMessage(ESM, `Switching type from \"module\" to \"commonjs\" for \"${url}\"`)\n logMessage(ESM, 'Please note that named import WILL NOT WORK in this case, as Node.js performs a')\n logMessage(ESM, 'static analisys on the CommonJS source code, and this file is transpiled from.')\n logMessage(ESM, 'TypeScript to CommonJS dynamically.')\n return { format: CJS, shortCircuit: true }\n }\n }\n\n /* Transpile with ESBuild and return our source code */\n const source = esbTranpile(filename, ESM)\n return { source, format: ESM, shortCircuit: true }\n}\n\n/* ========================================================================== */\n\n/* Simply output the fact that we were loaded */\nlogMessage(ESM, 'TypeScript loader for ES Modules loaded')\n", "/* eslint-disable @typescript-eslint/no-unsafe-function-type */\n/* ========================================================================== *\n * HACK BEYOND REDEMPTION: TRANSPILE .ts FILES (the esm loader) *\n * -------------------------------------------------------------------------- *\n * Use ESBuild to quickly transpile TypeScript files into JavaScript. *\n * *\n * The plan as it stands is as follows: *\n * - `.mts` files always get transpiled to ESM modules *\n * - `.cts` files always get transpiled to CJS modules *\n * - `.ts` files are treanspiled according to what's in `package.json` *\n * *\n * Additionally, when transpiling to ESM modules, we can't rely on the magic *\n * that Node's `require(...)` call uses to figure out which file to import. *\n * We need to _actually verify_ on disk what's the correct file to import. *\n * *\n * This is a single module, only available as ESM, and it will _both_ behave *\n * as a NodeJS' loader, _and_ inject the CJS extension handlers (hack) found *\n * in the `_extensions` of `node:module` (same as `require.extensions`). *\n * ========================================================================== */\n\n// NodeJS dependencies\nimport _fs from 'node:fs'\nimport _path from 'node:path'\nimport _util from 'node:util'\n\n// ESBuild is the only external dependency\nimport _esbuild from 'esbuild'\n\n/* ========================================================================== *\n * CONSTANTS AND TYPES *\n * ========================================================================== */\n\n/** Supported types from `package.json` */\nexport type Type = 'commonjs' | 'module'\n/** Constant identifying a `commonjs` module */\nexport const CJS = 'commonjs' as const\n/** Constant identifying an ESM `module` */\nexport const ESM = 'module' as const\n\n/* ========================================================================== *\n * DEBUGGING AND ERRORS *\n * ========================================================================== */\n\n/** Setup debugging */\nconst _debugLog = _util.debuglog('plug:ts-loader')\nconst _debug = _debugLog.enabled\n\n/** Emit some logs if `DEBUG_TS_LOADER` is set to `true` */\nexport function logMessage(mode: Type, arg: string, ...args: any []): void {\n if (! _debug) return\n\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n _debugLog(`[${t}] ${arg}`, ...args)\n}\n\n/** Fail miserably */\nexport function throwError(\n mode: Type,\n message: string,\n options: { start?: Function, code?: string, cause?: any } = {},\n): never {\n const t = mode === ESM ? 'esm' : mode === CJS ? 'cjs' : '---'\n const prefix = `[ts-loader|${t}|pid=${process.pid}]`\n\n const { start = throwError, ...extra } = options\n const error = new Error(`${prefix} ${message}`)\n Error.captureStackTrace(error, start)\n Object.assign(error, extra)\n\n throw error\n}\n\n/* ========================================================================== *\n * MODULE TYPES AND FORCING TYPE *\n * ========================================================================== */\n\n/**\n * Determine the current module type to transpile .TS files as looking at the\n * `__TS_LOADER_FORCE_TYPE` environment variable (used by PlugJS and CLI) or,\n * if unspecified, looking at the `type` field in the `package.json` file.\n */\nexport function moduleType(mode: Type): Type {\n if (process.env.__TS_LOADER_FORCE_TYPE) {\n const type = process.env.__TS_LOADER_FORCE_TYPE\n if ((type === CJS) || (type === ESM)) {\n logMessage(mode, `Forcing type to \"${type}\" from environment`)\n return type\n } else {\n throwError(mode, `Invalid type \"${process.env.__TS_LOADER_FORCE_TYPE}\"`)\n }\n }\n\n const _findType = (directory: string): Type => {\n const packageFile = _path.join(directory, 'package.json')\n try {\n const packageData = _fs.readFileSync(packageFile, 'utf-8')\n const packageJson = JSON.parse(packageData)\n const packageType = packageJson.type\n switch (packageType) {\n case undefined:\n logMessage(mode, `File \"${packageFile}\" does not declare a default type`)\n return CJS\n\n case CJS:\n case ESM:\n logMessage(mode, `File \"${packageFile}\" declares type as \"${CJS}\"`)\n return packageType\n\n default:\n logMessage(mode, `File \"${packageFile}\" specifies unknown type \"${packageType}\"`)\n return CJS\n }\n } catch (cause: any) {\n if ((cause.code !== 'ENOENT') && (cause.code !== 'EISDIR')) {\n throwError(mode, `Unable to read or parse \"${packageFile}\"`, { cause, start: _findType })\n }\n }\n\n const parent = _path.dirname(directory)\n if (directory !== parent) return _findType(parent)\n\n logMessage(mode, `Type defaulted to \"${CJS}\"`)\n return CJS\n }\n\n return _findType(process.cwd())\n}\n\n/* ========================================================================== *\n * ESBUILD HELPERS *\n * ========================================================================== */\n\n/**\n * Take an ESBuild `BuildResult` or `BuildFailure` (they both have arrays\n * of `Message` in both `warnings` and `errors`), format them and print them\n * out nicely. Then fail if any error was detected.\n */\nfunction _esbReport(\n kind: 'error' | 'warning',\n messages: _esbuild.Message[] = [],\n): void {\n const output = process.stderr\n const options = { color: !!output.isTTY, terminalWidth: output.columns || 80 }\n\n const array = _esbuild.formatMessagesSync(messages, { kind, ...options })\n array.forEach((message) => output.write(`${message}\\n`))\n}\n\n/**\n * Transpile with ESBuild\n */\nexport function esbTranpile(filename: string, type: Type): string {\n logMessage(type, `Transpiling \"${filename}\" as \"${type}\"`)\n\n const [ format, __fileurl ] = type === ESM ?\n [ 'esm', 'import.meta.url' ] as const :\n [ 'cjs', '__filename' ] as const\n\n /* ESbuild options */\n const options: _esbuild.TransformOptions = {\n sourcefile: filename, // the original filename we're parsing\n format, // what are we actually transpiling to???\n loader: 'ts', // the format is always \"typescript\"\n sourcemap: 'inline', // always inline source maps\n sourcesContent: false, // do not include sources content in sourcemap\n platform: 'node', // d'oh! :-)\n minifyWhitespace: true, // https://github.com/evanw/esbuild/releases/tag/v0.16.14\n logLevel: 'silent', // catching those in our _esbReport below\n target: `node${process.versions['node']}`, // target _this_ version\n define: { __fileurl }, // from \"globals.d.ts\"\n }\n\n /* Emit a line on the console when loading in debug mode */\n if (_debug) {\n if (format === 'esm') {\n options.banner = `;(await import('node:util')).debuglog('plug:ts-loader')('[esm] Loaded \"%s\"', ${__fileurl});`\n } else if (format === 'cjs') {\n options.banner = `;require('node:util').debuglog('plug:ts-loader')('[cjs] Loaded \"%s\"', ${__fileurl});`\n }\n }\n\n /* Transpile our TypeScript file into some JavaScript stuff */\n let result\n try {\n const source = _fs.readFileSync(filename, 'utf-8')\n result = _esbuild.transformSync(source, options)\n } catch (cause: any) {\n _esbReport('error', (cause as _esbuild.TransformFailure).errors)\n _esbReport('warning', (cause as _esbuild.TransformFailure).warnings)\n throwError(type, `ESBuild error transpiling \"${filename}\"`, { cause, start: esbTranpile })\n }\n\n /* Log transpile warnings if debugging */\n if (_debug) _esbReport('warning', result.warnings)\n\n /* Done! */\n return result.code\n}\n\n\n/* ========================================================================== *\n * UTILITIES *\n * ========================================================================== */\n\n/* Returns a boolean indicating whether the specified file exists or not */\nexport function isFile(path: string): boolean {\n try {\n return _fs.statSync(path).isFile()\n } catch {\n return false\n }\n}\n\n/* Returns a boolean indicating whether the specified directory exists or not */\nexport function isDirectory(path: string): boolean {\n try {\n return _fs.statSync(path).isDirectory()\n } catch {\n return false\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAOA,YAAW;AAClB,OAAO,UAAU;;;ACoBjB,OAAO,SAAS;AAChB,OAAO,WAAW;AAClB,OAAO,WAAW;AAGlB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I,CAAE,IAAK,QAAO,SAAS,KAAK,OAAO;AAEvC,MAAI,QAAQ,QAAQ;AAClB,eAAW,KAAK,mDAAmD,GAAG,GAAG;AACzE,eAAW,KAAK,iFAAiF;AACjG,eAAW,KAAK,gFAAgF;AAChG,eAAW,KAAK,qCAAqC;AACrD,WAAO,EAAE,QAAQ,KAAK,cAAc,KAAK;AAAA,EAC3C;AAGA,QAAM,WAAW,KAAK,cAAc,GAAG;AAGvC,MAAI,QAAQ,OAAO;AACjB,QAAI,UAAU,KAAK;AACjB,iBAAW,KAAK,mDAAmD,GAAG,GAAG;AACzE,iBAAW,KAAK,iFAAiF;AACjG,iBAAW,KAAK,gFAAgF;AAChG,iBAAW,KAAK,qCAAqC;AACrD,aAAO,EAAE,QAAQ,KAAK,cAAc,KAAK;AAAA,IAC3C;AAAA,EACF;AAGA,QAAM,SAAS,YAAY,UAAU,GAAG;AACxC,SAAO,EAAE,QAAQ,QAAQ,KAAK,cAAc,KAAK;AACnD;AAKA,WAAW,KAAK,yCAAyC;",
|
|
6
6
|
"names": ["_path", "_path"]
|
|
7
7
|
}
|
package/dist/parser.mjs
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
-
});
|
|
7
|
-
|
|
8
1
|
// node_modules/yargs-parser/build/lib/index.js
|
|
9
2
|
import { format } from "util";
|
|
10
3
|
import { normalize, resolve } from "path";
|
|
@@ -946,10 +939,11 @@ function stripQuotes(val) {
|
|
|
946
939
|
|
|
947
940
|
// node_modules/yargs-parser/build/lib/index.js
|
|
948
941
|
import { readFileSync } from "fs";
|
|
942
|
+
import { createRequire } from "node:module";
|
|
949
943
|
var _a;
|
|
950
944
|
var _b;
|
|
951
945
|
var _c;
|
|
952
|
-
var minNodeVersion = process && process.env && process.env.YARGS_MIN_NODE_VERSION ? Number(process.env.YARGS_MIN_NODE_VERSION) :
|
|
946
|
+
var minNodeVersion = process && process.env && process.env.YARGS_MIN_NODE_VERSION ? Number(process.env.YARGS_MIN_NODE_VERSION) : 20;
|
|
953
947
|
var nodeVersion = (_b = (_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node) !== null && _b !== void 0 ? _b : (_c = process === null || process === void 0 ? void 0 : process.version) === null || _c === void 0 ? void 0 : _c.slice(1);
|
|
954
948
|
if (nodeVersion) {
|
|
955
949
|
const major = Number(nodeVersion.match(/^([^.]+)/)[1]);
|
|
@@ -958,6 +952,7 @@ if (nodeVersion) {
|
|
|
958
952
|
}
|
|
959
953
|
}
|
|
960
954
|
var env = process ? process.env : {};
|
|
955
|
+
var require2 = createRequire ? createRequire(import.meta.url) : void 0;
|
|
961
956
|
var parser = new YargsParser({
|
|
962
957
|
cwd: process.cwd,
|
|
963
958
|
env: () => {
|
|
@@ -966,11 +961,9 @@ var parser = new YargsParser({
|
|
|
966
961
|
format,
|
|
967
962
|
normalize,
|
|
968
963
|
resolve,
|
|
969
|
-
// TODO: figure out a way to combine ESM and CJS coverage, such that
|
|
970
|
-
// we can exercise all the lines below:
|
|
971
964
|
require: (path) => {
|
|
972
|
-
if (typeof
|
|
973
|
-
return
|
|
965
|
+
if (typeof require2 !== "undefined") {
|
|
966
|
+
return require2(path);
|
|
974
967
|
} else if (path.match(/\.json$/)) {
|
|
975
968
|
return JSON.parse(readFileSync(path, "utf8"));
|
|
976
969
|
} else {
|
|
@@ -1010,7 +1003,6 @@ yargs-parser/build/lib/yargs-parser.js:
|
|
|
1010
1003
|
yargs-parser/build/lib/index.js:
|
|
1011
1004
|
(**
|
|
1012
1005
|
* @fileoverview Main entrypoint for libraries using yargs-parser in Node.js
|
|
1013
|
-
* CJS and ESM environments.
|
|
1014
1006
|
*
|
|
1015
1007
|
* @license
|
|
1016
1008
|
* Copyright (c) 2016, Contributors
|