Skip to content

Commit

Permalink
fix: 修复esbuildregister失效
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 15, 2021
1 parent 03a1e94 commit 05a00b1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/cli-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"url": "https://github.com/xus-code/cli/issues"
},
"dependencies": {
"@babel/register": "^7.13.0",
"@types/debug": "^4.1.5",
"@types/yargs-parser": "^20.2.0",
"@xus/babel-preset": "^0.1.9",
Expand All @@ -37,13 +36,14 @@
"debug": "^4.3.1",
"deepmerge": "^4.2.2",
"enquirer": "^2.3.6",
"esbuild-register": "^2.2.0",
"esbuild": "^0.9.2",
"execa": "^5.0.0",
"fast-glob": "^3.2.5",
"joi": "^17.4.0",
"lodash": "^4.17.21",
"log-symbols": "^4.0.0",
"ora": "^5.3.0",
"pirates": "^4.0.1",
"resolve": "^1.20.0",
"rimraf": "^3.0.2",
"semver": "^7.3.4",
Expand Down
14 changes: 4 additions & 10 deletions packages/cli-shared/src/EsbuildRegister/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createDebug, lodash, winPath } from '..'
import { register } from 'esbuild-register/dist/node'
import { register } from './register'

const debug = createDebug('xus:shared:EsbuildRegister')
export class EsbuildRegister {
private only: Record<string, string[]> = {}
Expand All @@ -19,15 +20,8 @@ export class EsbuildRegister {
}, [])
.map(winPath)
)
only.forEach((file) => {
register({
sourcefile: file,
sourcemap: false,
extensions: ['.js', '.jsx', '.ts', '.tsx'],
format: 'cjs',
target: 'es2019'
})
})

register(only)
}
}

Expand Down
38 changes: 38 additions & 0 deletions packages/cli-shared/src/EsbuildRegister/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path from 'path'
import { transformSync, Loader } from 'esbuild'
import { addHook } from 'pirates'

const files = new Set<string>()

function compile(code: string, filename: string) {
const match = new RegExp(`(${[...files].join('|')})`)
if (!match.test(filename)) {
return code
}
const { code: js, warnings } = transformSync(code, {
sourcefile: filename,
sourcemap: false,
target: 'node12.0.0',
format: 'cjs',
loader: path.extname(filename).slice(1) as Loader
})
if (warnings && warnings.length > 0) {
for (const warning of warnings) {
console.log(warning.location)
console.log(warning.text)
}
}
return js
}

let revert: any

export function register(file: string[]) {
file.forEach((f) => files.add(f))
if (!revert) {
revert = addHook(compile, {
exts: ['.js', '.jsx', '.ts', '.tsx'],
ignoreNodeModules: false
})
}
}
12 changes: 6 additions & 6 deletions packages/core/src/manager/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ export class ConfigManager {
private loadUserConfig() {
// 1. load userConfig
const path = this.service.PathManager.userConfigPath
const userConfig =
path === ' '
? loadModule<IProjectConfig>(path, (err) => {
logger.error(`user config load failed, ${err}`)
})
: {}
let userConfig = {}
if (path) {
userConfig = loadModule<IProjectConfig>(path, (err) => {
logger.error(`user config load failed, ${err}`)
})
}

logger.debug(`user config: `)
logger.debug(userConfig)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/manager/PathManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isLernaPkg, lookUpFile } from '@xus/cli-shared'
import { CONFIG_FILES } from '../constants'

export class PathManager {
private configPath!: string
private configPath!: string | undefined
get cwd(): string {
return process.cwd()
}
Expand All @@ -15,7 +15,7 @@ export class PathManager {

get userConfigPath() {
if (!this.configPath) {
this.configPath = lookUpFile(this.cwd, CONFIG_FILES, true) || ' '
this.configPath = lookUpFile(this.cwd, CONFIG_FILES, true) || undefined
}
return this.configPath
}
Expand Down

0 comments on commit 05a00b1

Please sign in to comment.