From fdaf99e2ca6070c870606715fc689c548fa6125c Mon Sep 17 00:00:00 2001 From: Shigma Date: Fri, 2 Feb 2024 04:34:57 +0800 Subject: [PATCH] fix(console): fix import edge cases --- packages/client/client/index.ts | 1 + plugins/config/client/components/utils.ts | 3 +-- plugins/console/src/node/index.ts | 32 +++++++++++++---------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/client/client/index.ts b/packages/client/client/index.ts index 6eeb738d..9816652a 100644 --- a/packages/client/client/index.ts +++ b/packages/client/client/index.ts @@ -20,6 +20,7 @@ export * from './components' export * from './context' export * from './data' export { Service } from './utils' +export { ScopeStatus } from 'cordis' export default install diff --git a/plugins/config/client/components/utils.ts b/plugins/config/client/components/utils.ts index 2eef6911..11f27156 100644 --- a/plugins/config/client/components/utils.ts +++ b/plugins/config/client/components/utils.ts @@ -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 diff --git a/plugins/console/src/node/index.ts b/plugins/console/src/node/index.ts index bf59c21a..21794dfa 100644 --- a/plugins/console/src/node/index.ts +++ b/plugins/console/src/node/index.ts @@ -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 } @@ -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) {