168 lines
6.0 KiB
JavaScript
168 lines
6.0 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.fixBinaryPath = exports.initModulePaths = exports.runByHBuilderX = exports.isInHBuilderX = void 0;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const module_1 = __importDefault(require("module"));
|
|
const url_1 = require("url");
|
|
const uni_shared_1 = require("@dcloudio/uni-shared");
|
|
const resolve_1 = require("../resolve");
|
|
const utils_1 = require("../utils");
|
|
const utils_2 = require("./utils");
|
|
var utils_3 = require("./utils");
|
|
Object.defineProperty(exports, "isInHBuilderX", { enumerable: true, get: function () { return utils_3.isInHBuilderX; } });
|
|
exports.runByHBuilderX = (0, uni_shared_1.once)(() => {
|
|
return ((!!process.env.HX_PLUGIN_PATHS || !!process.env.UNI_HBUILDERX_PLUGINS) &&
|
|
(!!process.env.RUN_BY_HBUILDERX || !!process.env.HX_Version));
|
|
});
|
|
/**
|
|
* 增加 node_modules
|
|
*/
|
|
const initializedModulePaths = new Set();
|
|
function getPackageName(specifier) {
|
|
if (!specifier ||
|
|
specifier.startsWith('.') ||
|
|
specifier.startsWith('/') ||
|
|
specifier.startsWith('\\') ||
|
|
specifier.startsWith('#') ||
|
|
specifier.includes(':') ||
|
|
/[\\%?#]/.test(specifier)) {
|
|
return;
|
|
}
|
|
const segments = specifier.split('/');
|
|
if (segments.some((segment) => !segment || segment === '.' || segment === '..') ||
|
|
(specifier.startsWith('@') &&
|
|
(segments.length < 2 || segments[0].length === 1))) {
|
|
return;
|
|
}
|
|
const packageName = specifier.startsWith('@')
|
|
? segments.slice(0, 2).join('/')
|
|
: segments[0];
|
|
return packageName;
|
|
}
|
|
function canFallbackToNodeModules(specifier, error, nodeModulesPath) {
|
|
const code = error?.code;
|
|
if (code !== 'ERR_MODULE_NOT_FOUND') {
|
|
return false;
|
|
}
|
|
const packageName = getPackageName(specifier);
|
|
return (!!packageName && fs_1.default.existsSync(path_1.default.resolve(nodeModulesPath, packageName)));
|
|
}
|
|
function initModulePaths() {
|
|
if (!(0, utils_2.isInHBuilderX)()) {
|
|
return;
|
|
}
|
|
const nodeModulesPath = path_1.default.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules');
|
|
if (initializedModulePaths.has(nodeModulesPath)) {
|
|
return;
|
|
}
|
|
const parentURL = (0, url_1.pathToFileURL)(path_1.default.resolve(process.env.UNI_CLI_CONTEXT, 'package.json')).href;
|
|
const moduleApi = module_1.default;
|
|
const Module = module.constructor.length > 1 ? module.constructor : module_1.default;
|
|
const oldNodeModulePaths = Module._nodeModulePaths;
|
|
Module._nodeModulePaths = function (from) {
|
|
const paths = oldNodeModulePaths.call(this, from);
|
|
if (!paths.includes(nodeModulesPath)) {
|
|
paths.push(nodeModulesPath);
|
|
}
|
|
return paths;
|
|
};
|
|
if (moduleApi.registerHooks) {
|
|
moduleApi.registerHooks({
|
|
resolve(specifier, context, nextResolve) {
|
|
try {
|
|
return nextResolve(specifier, context);
|
|
}
|
|
catch (error) {
|
|
if (!canFallbackToNodeModules(specifier, error, nodeModulesPath)) {
|
|
throw error;
|
|
}
|
|
return nextResolve(specifier, { ...context, parentURL });
|
|
}
|
|
},
|
|
});
|
|
initializedModulePaths.add(nodeModulesPath);
|
|
return;
|
|
}
|
|
// Node 的 ESM 解析不会使用 _nodeModulePaths,需要单独回退到 HBuilderX 内置依赖。
|
|
const register = moduleApi.register;
|
|
if (register) {
|
|
const nodeModulesURL = (0, url_1.pathToFileURL)(nodeModulesPath + path_1.default.sep).href;
|
|
const loader = `
|
|
import { existsSync } from 'node:fs'
|
|
|
|
const parentURL = ${JSON.stringify(parentURL)}
|
|
const nodeModulesURL = ${JSON.stringify(nodeModulesURL)}
|
|
|
|
export async function resolve(specifier, context, nextResolve) {
|
|
try {
|
|
return await nextResolve(specifier, context)
|
|
} catch (error) {
|
|
if (
|
|
error?.code !== 'ERR_MODULE_NOT_FOUND' ||
|
|
!specifier ||
|
|
specifier.startsWith('.') ||
|
|
specifier.startsWith('/') ||
|
|
specifier.startsWith('\\\\') ||
|
|
specifier.startsWith('#') ||
|
|
specifier.includes(':') ||
|
|
/[\\\\%?#]/.test(specifier)
|
|
) {
|
|
throw error
|
|
}
|
|
const segments = specifier.split('/')
|
|
if (
|
|
segments.some(
|
|
(segment) => !segment || segment === '.' || segment === '..'
|
|
) ||
|
|
(specifier.startsWith('@') &&
|
|
(segments.length < 2 || segments[0].length === 1))
|
|
) {
|
|
throw error
|
|
}
|
|
const packageName = specifier.startsWith('@')
|
|
? segments.slice(0, 2).join('/')
|
|
: segments[0]
|
|
if (!existsSync(new URL(packageName + '/', nodeModulesURL))) {
|
|
throw error
|
|
}
|
|
return nextResolve(specifier, { ...context, parentURL })
|
|
}
|
|
}
|
|
`;
|
|
register(`data:text/javascript,${encodeURIComponent(loader)}`, (0, url_1.pathToFileURL)(process.env.UNI_CLI_CONTEXT + path_1.default.sep));
|
|
}
|
|
initializedModulePaths.add(nodeModulesPath);
|
|
}
|
|
exports.initModulePaths = initModulePaths;
|
|
function resolveEsbuildModule(name) {
|
|
try {
|
|
return path_1.default.dirname(require.resolve(name + '/package.json', {
|
|
paths: [path_1.default.dirname((0, resolve_1.resolveBuiltIn)('esbuild/package.json'))],
|
|
}));
|
|
}
|
|
catch (e) { }
|
|
return '';
|
|
}
|
|
function fixBinaryPath() {
|
|
// cli 工程在 HBuilderX 中运行
|
|
if (!(0, utils_2.isInHBuilderX)() && (0, exports.runByHBuilderX)()) {
|
|
if (utils_1.isWindows) {
|
|
const win64 = resolveEsbuildModule('esbuild-windows-64');
|
|
if (win64) {
|
|
process.env.ESBUILD_BINARY_PATH = path_1.default.join(win64, 'esbuild.exe');
|
|
}
|
|
}
|
|
else {
|
|
const arm64 = resolveEsbuildModule('esbuild-darwin-arm64');
|
|
if (arm64) {
|
|
process.env.ESBUILD_BINARY_PATH = path_1.default.join(arm64, 'bin/esbuild');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
exports.fixBinaryPath = fixBinaryPath;
|