Skip to content

Commit

Permalink
fix: add babel-optional-chaining in plugin-vue, use checkModuleExist …
Browse files Browse the repository at this point in the history
…replace loadModule
  • Loading branch information
zhangyuang committed Feb 21, 2024
1 parent cdbbeae commit 3f6d451
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
1 change: 1 addition & 0 deletions packages/plugin-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/plugin-proposal-optional-chaining": "^7.16.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.12.13",
"@babel/runtime": "^7.12.13",
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-vue3/src/config/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { join } from 'path'
import { Mode } from 'ssr-types'
import { getCwd, loadConfig, setStyle, loadModuleFromFramework, logErr, getBuildConfig, getDefineEnv, addCommonChain } from 'ssr-common-utils'
import { getCwd, loadConfig, setStyle, loadModuleFromFramework, logErr, getBuildConfig, getDefineEnv, addCommonChain, checkModuleExist } from 'ssr-common-utils'
import * as webpack from 'ssr-webpack4'

import * as WebpackChain from 'webpack-chain'
Expand Down Expand Up @@ -122,7 +122,7 @@ const getBaseConfig = (chain: WebpackChain, isServer: boolean) => {
...(isServer ? define?.server : define?.client),
...define?.base
}])
if (loadModuleFromFramework('element-plus')) {
if (checkModuleExist('element-plus')) {
const { coerce } = require('semver')
if ((coerce(process.version)?.major ?? 0) < 14) {
logErr('Use element-plus auto import require Node.js Version >= v14 for optional chaining')
Expand Down
27 changes: 16 additions & 11 deletions packages/utils/src/server/cwd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const getWebpackSplitCache = () => {
const generateMap: Record<string, string> = require(resolve(getCwd(), './build/generateMap.json'))
const asyncChunkMap = require(resolve(getCwd(), './build/asyncChunkMap.json'))
let maxPriority = Object.keys(asyncChunkMap).length + 1
const splitPriorityMap: Record<string, number|undefined> = {
const splitPriorityMap: Record<string, number | undefined> = {
'common-vendor': maxPriority + 2,
'layout-app~vendor': maxPriority + 1,
'layout-app': maxPriority + 1
Expand Down Expand Up @@ -204,7 +204,7 @@ const cyrb53 = function (str: string, seed = 0) {
return 4294967296 * (2097151 & h2) + (h1 >>> 0)
}

const cryptoAsyncChunkName = (chunks: Array<{name: string}>, asyncChunkMap: Record<string, string[]>) => {
const cryptoAsyncChunkName = (chunks: Array<{ name: string }>, asyncChunkMap: Record<string, string[]>) => {
const arr = chunks.filter(Boolean)
arr.sort((a, b) => a.name > b.name ? -1 : 1) // 保证相同值不同顺序的数组最终的加密结果一致
const allChunksNames = arr.map(item => item.name).join('~')
Expand Down Expand Up @@ -276,16 +276,20 @@ export const debounce = (func: Function, wait: number) => {
timer = setTimeout(func, wait)
}
}
const checkModuleExist = (name: string) => {
try {
loadModuleFromFramework(name)
return true
} catch {
return false
}
}
const loadModuleFromFramework = (path: string) => {
const framework = judgeFramework()
const paths = resolve(getCwd(), `./node_modules/${framework}`)
try {
return require.resolve(path, {
paths: [accessFileSync(paths) ? realpathSync(paths) : paths]
})
} catch (error) {
return ''
}
return require.resolve(path, {
paths: [accessFileSync(paths) ? realpathSync(paths) : paths]
})
}

const processError = (err: any) => {
Expand Down Expand Up @@ -324,7 +328,7 @@ const copyReactContext = async () => {

const execPromisify = promisify(exec)

const stringifyDefine = (obj: {[key: string]: Json}) => {
const stringifyDefine = (obj: { [key: string]: Json }) => {
for (const key in obj) {
const val = obj[key]
if (typeof val === 'string' && val.slice(0, 1) !== '"') {
Expand Down Expand Up @@ -384,5 +388,6 @@ export {
getSplitChunksOptions,
cleanOutClientDir,
checkContainsRev,
getPkgJson
getPkgJson,
checkModuleExist
}
19 changes: 5 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f6d451

Please sign in to comment.