提交 8e5e2215 编写于 作者: fxy060608's avatar fxy060608

chore: add resolve

上级 04ca6594
......@@ -44,6 +44,7 @@
"postcss-import": "^14.0.2",
"postcss-load-config": "^3.1.0",
"postcss-selector-parser": "^6.0.6",
"resolve": "^1.20.0",
"rollup-plugin-copy": "^3.4.0",
"tapable": "^2.2.0",
"xregexp": "3.1.0"
......
......@@ -16,6 +16,7 @@ export * from './preprocess'
export * from './postcss'
export * from './filter'
export * from './esbuild'
export * from './resolve'
export { M } from './messages'
......
import { extend } from '@vue/shared'
import { ComponentJson, PageWindowOptions, UsingComponents } from './types'
import { removeExt, relativeFile, normalizeNodeModules } from '../../utils'
import { removeExt, normalizeNodeModules } from '../../utils'
import { relativeFile } from '../../resolve'
let appJsonCache: Record<string, any> = {}
const jsonFilesCache = new Map<string, string>()
......
import fs from 'fs'
import path from 'path'
import debug from 'debug'
import resolve from 'resolve'
import { once } from '@dcloudio/uni-shared'
import { normalizePath } from './utils'
const DEFAULT_EXTENSIONS = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
function resolveWithSymlinks(id: string, basedir: string): string {
return resolve.sync(id, {
basedir,
extensions: DEFAULT_EXTENSIONS,
// necessary to work with pnpm
preserveSymlinks: true,
})
}
export function relativeFile(from: string, to: string) {
const relativePath = normalizePath(path.relative(path.dirname(from), to))
return relativePath.startsWith('.') ? relativePath : './' + relativePath
}
export const resolveMainPathOnce = once((inputDir: string) => {
const mainTsPath = path.resolve(inputDir, 'main.ts')
if (fs.existsSync(mainTsPath)) {
return normalizePath(mainTsPath)
}
return normalizePath(path.resolve(inputDir, 'main.js'))
})
const ownerModules = ['@dcloudio/uni-app', '@dcloudio/vite-plugin-uni']
const paths: string[] = []
function resolveNodeModulePath(modulePath: string) {
const nodeModulesPaths: string[] = []
const nodeModulesPath = path.join(modulePath, 'node_modules')
if (fs.existsSync(nodeModulesPath)) {
nodeModulesPaths.push(nodeModulesPath)
}
const index = modulePath.lastIndexOf('node_modules')
if (index > -1) {
nodeModulesPaths.push(
path.join(modulePath.substr(0, index), 'node_modules')
)
}
return nodeModulesPaths
}
function initPaths() {
const cliContext = process.env.UNI_CLI_CONTEXT
if (cliContext) {
const pathSet = new Set<string>()
pathSet.add(path.join(cliContext, 'node_modules'))
;[`@dcloudio/uni-` + process.env.UNI_PLATFORM, ...ownerModules].forEach(
(ownerModule) => {
let pkgPath: string = ''
try {
pkgPath = require.resolve(ownerModule + '/package.json', {
paths: [cliContext],
})
} catch (e) {}
if (pkgPath) {
resolveNodeModulePath(path.dirname(pkgPath)).forEach(
(nodeModulePath) => {
pathSet.add(nodeModulePath)
}
)
}
}
)
paths.push(...pathSet)
debug('uni-paths')(paths)
}
}
export function getBuiltInPaths() {
if (!paths.length) {
initPaths()
}
return paths
}
export function resolveBuiltIn(path: string) {
return require.resolve(path, { paths: getBuiltInPaths() })
}
let componentsLibPath: string = ''
export function resolveComponentsLibPath() {
if (!componentsLibPath) {
componentsLibPath = path.join(
resolveWithSymlinks(
'@dcloudio/uni-components/package.json',
process.env.UNI_INPUT_DIR
),
'../lib'
)
}
return componentsLibPath
}
......@@ -2,10 +2,8 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import { camelize, capitalize } from '@vue/shared'
import { once } from '@dcloudio/uni-shared'
export { default as hash } from 'hash-sum'
import type { SFCTemplateCompileOptions } from '@vue/compiler-sfc'
import debug from 'debug'
import { PAGE_EXTNAME, PAGE_EXTNAME_APP } from './constants'
import {
......@@ -14,14 +12,17 @@ import {
RootNode,
TemplateChildNode,
} from '@vue/compiler-core'
export let isRunningWithYarnPnp: boolean
try {
isRunningWithYarnPnp = Boolean(require('pnpapi'))
} catch {}
export const isWindows = os.platform() === 'win32'
export function normalizePath(id: string): string {
return isWindows ? id.replace(/\\/g, '/') : id
}
export function relativeFile(from: string, to: string) {
return normalizePath(path.relative(path.dirname(from), to))
}
export function checkElementNodeTag(
node: RootNode | TemplateChildNode | null | undefined,
tag: string
......@@ -29,82 +30,6 @@ export function checkElementNodeTag(
return !!node && node.type === NodeTypes.ELEMENT && node.tag === tag
}
export const resolveMainPathOnce = once((inputDir: string) => {
const mainTsPath = path.resolve(inputDir, 'main.ts')
if (fs.existsSync(mainTsPath)) {
return normalizePath(mainTsPath)
}
return normalizePath(path.resolve(inputDir, 'main.js'))
})
let componentsLibPath: string = ''
export function resolveComponentsLibPath() {
if (!componentsLibPath) {
componentsLibPath = path.resolve(
resolveBuiltIn('@dcloudio/uni-components/package.json'),
'../lib'
)
}
return componentsLibPath
}
const ownerModules = ['@dcloudio/uni-app', '@dcloudio/vite-plugin-uni']
const paths: string[] = []
function resolveNodeModulePath(modulePath: string) {
const nodeModulesPaths: string[] = []
const nodeModulesPath = path.join(modulePath, 'node_modules')
if (fs.existsSync(nodeModulesPath)) {
nodeModulesPaths.push(nodeModulesPath)
}
const index = modulePath.lastIndexOf('node_modules')
if (index > -1) {
nodeModulesPaths.push(
path.join(modulePath.substr(0, index), 'node_modules')
)
}
return nodeModulesPaths
}
function initPaths() {
const cliContext = process.env.UNI_CLI_CONTEXT
if (cliContext) {
const pathSet = new Set<string>()
pathSet.add(path.join(cliContext, 'node_modules'))
;[`@dcloudio/uni-` + process.env.UNI_PLATFORM, ...ownerModules].forEach(
(ownerModule) => {
let pkgPath: string = ''
try {
pkgPath = require.resolve(ownerModule + '/package.json', {
paths: [cliContext],
})
} catch (e) {}
if (pkgPath) {
resolveNodeModulePath(path.dirname(pkgPath)).forEach(
(nodeModulePath) => {
pathSet.add(nodeModulePath)
}
)
}
}
)
paths.push(...pathSet)
debug('uni-paths')(paths)
}
}
export function getBuiltInPaths() {
if (!paths.length) {
initPaths()
}
return paths
}
export function resolveBuiltIn(path: string) {
return require.resolve(path, { paths: getBuiltInPaths() })
}
export function normalizeIdentifier(str: string) {
return capitalize(camelize(str.replace(/\//g, '-')))
}
......
......@@ -375,6 +375,7 @@ importers:
postcss-import: ^14.0.2
postcss-load-config: ^3.1.0
postcss-selector-parser: ^6.0.6
resolve: ^1.20.0
rollup-plugin-copy: ^3.4.0
tapable: ^2.2.0
xregexp: 3.1.0
......@@ -405,6 +406,7 @@ importers:
postcss-import: 14.0.2_postcss@8.3.11
postcss-load-config: 3.1.0
postcss-selector-parser: 6.0.6
resolve: 1.20.0
rollup-plugin-copy: 3.4.0
tapable: 2.2.1
xregexp: 3.1.0
......
......@@ -19,7 +19,7 @@ const pkgs = {
next: '4.0.2',
},
'vue-i18n': {
next: '9.1.7',
next: '9.1.9',
},
vite: {
latest: '2.6.14',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册