Skip to content

Commit

Permalink
fix(console): fix import edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 1, 2024
1 parent 3a5d055 commit fdaf99e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/client/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './components'
export * from './context'
export * from './data'
export { Service } from './utils'
export { ScopeStatus } from 'cordis'

export default install

Expand Down
3 changes: 1 addition & 2 deletions plugins/config/client/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Dict } from 'koishi'
import { computed, ref } from 'vue'
import { ScopeStatus } from 'cordis'
import { router, send, store } from '@koishijs/client'
import { router, ScopeStatus, send, store } from '@koishijs/client'

interface DepInfo {
required: boolean
Expand Down
32 changes: 18 additions & 14 deletions plugins/console/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,8 @@ class NodeConsole extends Console {
}

// we only transform js imports in production mode
let source = await fs.readFile(filename, 'utf8')
let output = ''
let cap: RegExpExecArray
while ((cap = /^(import\b[^'"]+\bfrom\s*)(['"])([^'"]+)\2;/.exec(source))) {
const [stmt, left, quote, path] = cap
output += left + quote + ({
'vue': '../vue.js',
'vue-router': '../vue-router.js',
'@vueuse/core': '../vueuse.js',
'@koishijs/client': '../client.js',
}[path] ?? path) + quote + ';'
source = source.slice(cap.index + stmt.length)
}
return ctx.body = output + source
const source = await fs.readFile(filename, 'utf8')
return ctx.body = await this.transformImport(source)
} else {
return ctx.status = 404
}
Expand All @@ -184,6 +172,22 @@ class NodeConsole extends Console {
})
}

private async transformImport(source: string) {
let output = ''
let cap: RegExpExecArray
while ((cap = /((?:^|;)import\b[^'"]+\bfrom\s*)(['"])([^'"]+)\2;/m.exec(source))) {
const [stmt, left, quote, path] = cap
output += source.slice(0, cap.index) + left + quote + ({
'vue': '../vue.js',
'vue-router': '../vue-router.js',
'@vueuse/core': '../vueuse.js',
'@koishijs/client': '../client.js',
}[path] ?? path) + quote + ';'
source = source.slice(cap.index + stmt.length)
}
return output + source
}

private async transformHtml(template: string) {
const { uiPath, head = [] } = this.config
if (this.vite) {
Expand Down

0 comments on commit fdaf99e

Please sign in to comment.