mirror of
https://github.com/whyour/qinglong.git
synced 2026-06-18 19:25:10 +08:00
修复 esm 依赖查询路径
This commit is contained in:
parent
949d956aef
commit
5f2d5bb24a
41
shell/preload/esm-loader.mjs
Normal file
41
shell/preload/esm-loader.mjs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { existsSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const builtinModules = [
|
||||
'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns',
|
||||
'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'process', 'querystring',
|
||||
'readline', 'stream', 'string_decoder', 'timers', 'tls', 'tty', 'url', 'util',
|
||||
'v8', 'zlib',
|
||||
];
|
||||
|
||||
function isBareSpecifier(specifier) {
|
||||
return !specifier.startsWith('.') &&
|
||||
!specifier.startsWith('/') &&
|
||||
!specifier.startsWith('file:') &&
|
||||
!specifier.startsWith('node:') &&
|
||||
!builtinModules.includes(specifier) &&
|
||||
!builtinModules.includes(specifier.split('/')[0]);
|
||||
}
|
||||
|
||||
export function resolve(specifier, context, nextResolve) {
|
||||
if (!isBareSpecifier(specifier)) {
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
|
||||
// 解析优先级:全局 pnpm > 系统全局
|
||||
const bases = [
|
||||
process.env.QL_NODE_GLOBAL_PATH,
|
||||
'/usr/local/lib/node_modules',
|
||||
].filter(Boolean);
|
||||
|
||||
for (const base of bases) {
|
||||
if (existsSync(join(base, specifier))) {
|
||||
return nextResolve(specifier, {
|
||||
...context,
|
||||
parentURL: new URL(`${join(base, specifier)}/`, 'file://').href,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
|
|
@ -4,6 +4,11 @@ const path = require('path');
|
|||
const client = require('./client.js');
|
||||
require(`./env.js`);
|
||||
|
||||
// 注册 ESM loader,使全局安装的包也可通过 import 导入
|
||||
try {
|
||||
Module.register(new URL('esm-loader.mjs', `file://${__dirname}/`).href);
|
||||
} catch (_) {}
|
||||
|
||||
function preferGlobalNodeModules() {
|
||||
const { QL_NODE_GLOBAL_PATH } = process.env;
|
||||
if (!QL_NODE_GLOBAL_PATH || Module._qlGlobalPathPatched) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user