240 lines
9.4 KiB
JavaScript
240 lines
9.4 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.resolveComponentsLibDirs = exports.resolveComponentsLibPath = exports.resolveVueI18nAlias = exports.resolvePiniaAlias = exports.resolvePiniaDependencies = exports.resolveProjectPinia = exports.resolvePinia = exports.resolveVueI18nDependencies = exports.resolveProjectVueI18n = exports.resolveVueI18n = exports.resolveVueI18nRuntime = exports.resolveBuiltIn = exports.getBuiltInPaths = exports.resolveMainPathOnce = exports.relativeFile = exports.resolveMainUtsName = exports.requireResolve = void 0;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const debug_1 = __importDefault(require("debug"));
|
|
const resolve_1 = __importDefault(require("resolve"));
|
|
const uni_shared_1 = require("@dcloudio/uni-shared");
|
|
const utils_1 = require("./utils");
|
|
const env_1 = require("./hbx/env");
|
|
const constants_1 = require("./constants");
|
|
function requireResolve(filename, basedir) {
|
|
return resolveWithSymlinks(filename, basedir);
|
|
}
|
|
exports.requireResolve = requireResolve;
|
|
function resolveWithSymlinks(id, basedir) {
|
|
return resolve_1.default.sync(id, {
|
|
basedir,
|
|
extensions: process.env.UNI_APP_X === 'true' ? constants_1.uni_app_x_extensions : constants_1.extensions,
|
|
// necessary to work with pnpm
|
|
preserveSymlinks: true,
|
|
});
|
|
}
|
|
function resolveMainUtsName() {
|
|
if (process.env.UNI_COMPILE_TARGET === 'ext-api') {
|
|
return process.env.UNI_COMPILE_EXT_API_UVUE_ENTRY || 'main.uts';
|
|
}
|
|
return 'main.uts';
|
|
}
|
|
exports.resolveMainUtsName = resolveMainUtsName;
|
|
function relativeFile(from, to) {
|
|
const relativePath = (0, utils_1.normalizePath)(path_1.default.relative(path_1.default.dirname(from), to));
|
|
return relativePath.startsWith('.') ? relativePath : './' + relativePath;
|
|
}
|
|
exports.relativeFile = relativeFile;
|
|
exports.resolveMainPathOnce = (0, uni_shared_1.once)((inputDir) => {
|
|
if (process.env.UNI_APP_X === 'true') {
|
|
const mainUTSPath = path_1.default.resolve(inputDir, resolveMainUtsName());
|
|
if (fs_1.default.existsSync(mainUTSPath)) {
|
|
return (0, utils_1.normalizePath)(mainUTSPath);
|
|
}
|
|
}
|
|
const mainTsPath = path_1.default.resolve(inputDir, 'main.ts');
|
|
if (fs_1.default.existsSync(mainTsPath)) {
|
|
return (0, utils_1.normalizePath)(mainTsPath);
|
|
}
|
|
if (process.env.UNI_APP_X === 'true' &&
|
|
process.env.UNI_UTS_PLATFORM === 'app-android' &&
|
|
process.env.UNI_APP_X_DOM2 !== 'true') {
|
|
throw new Error('uni-app x Android VDOM 模式仅支持 main.uts 或 main.ts 入口文件');
|
|
}
|
|
return (0, utils_1.normalizePath)(path_1.default.resolve(inputDir, 'main.js'));
|
|
});
|
|
const ownerModules = [
|
|
'@dcloudio/uni-app',
|
|
'@dcloudio/vite-plugin-uni',
|
|
'@dcloudio/uni-cli-shared',
|
|
];
|
|
const paths = [];
|
|
function resolveNodeModulePath(modulePath) {
|
|
const nodeModulesPaths = [];
|
|
const nodeModulesPath = path_1.default.join(modulePath, 'node_modules');
|
|
if (fs_1.default.existsSync(nodeModulesPath)) {
|
|
nodeModulesPaths.push(nodeModulesPath);
|
|
}
|
|
const index = modulePath.lastIndexOf('node_modules');
|
|
if (index > -1) {
|
|
nodeModulesPaths.push(path_1.default.join(modulePath.slice(0, index), 'node_modules'));
|
|
}
|
|
return nodeModulesPaths;
|
|
}
|
|
function initPaths() {
|
|
const cliContext = process.env.UNI_CLI_CONTEXT || process.cwd();
|
|
if (cliContext) {
|
|
const pathSet = new Set();
|
|
pathSet.add(path_1.default.join(cliContext, 'node_modules'));
|
|
if (!(0, env_1.isInHBuilderX)()) {
|
|
;
|
|
[`@dcloudio/uni-` + process.env.UNI_PLATFORM, ...ownerModules].forEach((ownerModule) => {
|
|
let pkgPath = '';
|
|
try {
|
|
pkgPath = require.resolve(ownerModule + '/package.json', {
|
|
paths: [cliContext],
|
|
});
|
|
}
|
|
catch (e) { }
|
|
if (pkgPath) {
|
|
resolveNodeModulePath(path_1.default.dirname(pkgPath)).forEach((nodeModulePath) => {
|
|
pathSet.add(nodeModulePath);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
paths.push(...pathSet);
|
|
(0, debug_1.default)('uni-paths')(paths);
|
|
}
|
|
}
|
|
function getBuiltInPaths() {
|
|
if (!paths.length) {
|
|
initPaths();
|
|
}
|
|
return paths;
|
|
}
|
|
exports.getBuiltInPaths = getBuiltInPaths;
|
|
function resolveBuiltIn(module) {
|
|
if (process.env.UNI_COMPILE_TARGET === 'ext-api' &&
|
|
process.env.UNI_APP_NEXT_WORKSPACE &&
|
|
module.startsWith('@dcloudio/')) {
|
|
return path_1.default.resolve(process.env.UNI_APP_NEXT_WORKSPACE, 'packages', module.replace('@dcloudio/', ''));
|
|
}
|
|
return require.resolve(module, { paths: getBuiltInPaths() });
|
|
}
|
|
exports.resolveBuiltIn = resolveBuiltIn;
|
|
function resolveVueI18nRuntime() {
|
|
return path_1.default.resolve(__dirname, process.env.UNI_APP_X === 'true'
|
|
? '../lib/dom2/vue-i18n/dist/vue-i18n.runtime.esm-bundler.js'
|
|
: '../lib/vue-i18n/dist/vue-i18n.runtime.esm-bundler.js');
|
|
}
|
|
exports.resolveVueI18nRuntime = resolveVueI18nRuntime;
|
|
function resolveVueI18n() {
|
|
return path_1.default.resolve(__dirname, '../lib/dom2/vue-i18n/dist/vue-i18n.esm-bundler.js');
|
|
}
|
|
exports.resolveVueI18n = resolveVueI18n;
|
|
function resolveProjectModule(module) {
|
|
const inputDir = process.env.UNI_INPUT_DIR;
|
|
if (!inputDir) {
|
|
return;
|
|
}
|
|
try {
|
|
return resolveWithSymlinks(`${module}/package.json`, inputDir);
|
|
}
|
|
catch (e) { }
|
|
}
|
|
function resolveProjectVueI18n() {
|
|
return resolveProjectModule('vue-i18n');
|
|
}
|
|
exports.resolveProjectVueI18n = resolveProjectVueI18n;
|
|
function resolveDevtoolsApi(libDir) {
|
|
// uni-app x 暂不支持 @vue/devtools-api,统一指向兼容性空实现
|
|
return path_1.default.resolve(libDir, '@vue/devtools-api/dist/index.js');
|
|
}
|
|
function resolveVueI18nDependencies() {
|
|
if (process.env.UNI_APP_X !== 'true' ||
|
|
resolveProjectVueI18n() !== undefined) {
|
|
return {};
|
|
}
|
|
const libDir = path_1.default.resolve(__dirname, '../lib/dom2');
|
|
return {
|
|
'@intlify/core-base': path_1.default.resolve(libDir, '@intlify/core-base/dist/core-base.mjs'),
|
|
'@intlify/message-compiler': path_1.default.resolve(libDir, '@intlify/message-compiler/dist/message-compiler.mjs'),
|
|
'@intlify/shared': path_1.default.resolve(libDir, '@intlify/shared/dist/shared.mjs'),
|
|
'@vue/devtools-api': resolveDevtoolsApi(libDir),
|
|
};
|
|
}
|
|
exports.resolveVueI18nDependencies = resolveVueI18nDependencies;
|
|
function resolvePinia() {
|
|
return path_1.default.resolve(__dirname, '../lib/dom2/pinia/dist/pinia.mjs');
|
|
}
|
|
exports.resolvePinia = resolvePinia;
|
|
function resolveProjectPinia() {
|
|
return resolveProjectModule('pinia');
|
|
}
|
|
exports.resolveProjectPinia = resolveProjectPinia;
|
|
function resolvePiniaDependencies() {
|
|
if (process.env.UNI_APP_X !== 'true' || resolveProjectPinia() !== undefined) {
|
|
return {};
|
|
}
|
|
return {
|
|
'@vue/devtools-api': resolveDevtoolsApi(path_1.default.resolve(__dirname, '../lib/dom2')),
|
|
};
|
|
}
|
|
exports.resolvePiniaDependencies = resolvePiniaDependencies;
|
|
function resolvePiniaAlias() {
|
|
if (process.env.UNI_APP_X !== 'true' || resolveProjectPinia() !== undefined) {
|
|
return {};
|
|
}
|
|
return {
|
|
...resolvePiniaDependencies(),
|
|
'pinia/package.json': path_1.default.resolve(__dirname, '../lib/dom2/pinia/package.json'),
|
|
pinia: resolvePinia(),
|
|
};
|
|
}
|
|
exports.resolvePiniaAlias = resolvePiniaAlias;
|
|
function resolveVueI18nAlias() {
|
|
if (process.env.UNI_APP_X === 'true' &&
|
|
resolveProjectVueI18n() !== undefined) {
|
|
return {};
|
|
}
|
|
return {
|
|
...resolveVueI18nDependencies(),
|
|
...(process.env.UNI_APP_X === 'true'
|
|
? {
|
|
'vue-i18n/package.json': path_1.default.resolve(__dirname, '../lib/dom2/vue-i18n/package.json'),
|
|
}
|
|
: {}),
|
|
'vue-i18n/runtime': resolveVueI18nRuntime(),
|
|
'vue-i18n': process.env.UNI_APP_X === 'true'
|
|
? resolveVueI18n()
|
|
: resolveVueI18nRuntime(),
|
|
};
|
|
}
|
|
exports.resolveVueI18nAlias = resolveVueI18nAlias;
|
|
let componentsLibPath = '';
|
|
function resolveComponentsLibPath() {
|
|
if (!componentsLibPath) {
|
|
const dir = process.env.UNI_APP_X_DOM2 === 'true'
|
|
? '../lib-x-vapor'
|
|
: process.env.UNI_APP_X === 'true'
|
|
? '../lib-x'
|
|
: '../lib';
|
|
if ((0, env_1.isInHBuilderX)()) {
|
|
componentsLibPath = path_1.default.join(resolveBuiltIn('@dcloudio/uni-components/package.json'), dir);
|
|
}
|
|
else {
|
|
try {
|
|
componentsLibPath = path_1.default.join(resolveWithSymlinks('@dcloudio/uni-components/package.json', process.env.UNI_INPUT_DIR), dir);
|
|
}
|
|
catch (e) {
|
|
try {
|
|
componentsLibPath = path_1.default.join(resolveWithSymlinks('@dcloudio/uni-components/package.json', process.cwd()), dir);
|
|
}
|
|
catch (e) {
|
|
console.log(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return componentsLibPath;
|
|
}
|
|
exports.resolveComponentsLibPath = resolveComponentsLibPath;
|
|
function resolveComponentsLibDirs() {
|
|
return process.env.UNI_COMPILE_TARGET === 'ext-api'
|
|
? []
|
|
: [resolveComponentsLibPath()];
|
|
}
|
|
exports.resolveComponentsLibDirs = resolveComponentsLibDirs;
|