diff --git a/.vscode/settings.json b/.vscode/settings.json index 02ccaec..6277672 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,11 +1,12 @@ { - "typescript.tsdk": "node_modules/typescript/lib", - "editor.formatOnSave": true, - "eslint.format.enable": true, - "editor.defaultFormatter": "dbaeumer.vscode-eslint", - "typescript.format.enable": false, - "javascript.format.enable": false, - "[typescript]": { - "editor.defaultFormatter": "dbaeumer.vscode-eslint" - } + "typescript.tsdk": "node_modules/typescript/lib", + "editor.formatOnSave": true, + "eslint.format.enable": true, + "editor.defaultFormatter": "dbaeumer.vscode-eslint", + "typescript.format.enable": false, + "javascript.format.enable": false, + "[typescript]": { + "editor.defaultFormatter": "rvest.vs-code-prettier-eslint" + }, + "nuxt.isNuxtApp": false } diff --git a/examples/nest/src/app.service.ts b/examples/nest/src/app.service.ts index 4e8938f..9e8dda7 100644 --- a/examples/nest/src/app.service.ts +++ b/examples/nest/src/app.service.ts @@ -4,6 +4,6 @@ import { str } from './str'; @Injectable() export class AppService { getHello(): string { - return `change me to see updates! ${str} from a new file,..`; + return `change To see updates! ${str} from a new file,..`; } } diff --git a/examples/nest/src/vite-env.d.ts b/examples/nest/src/vite-env.d.ts new file mode 100644 index 0000000..11f02fe --- /dev/null +++ b/examples/nest/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/examples/nest/tsconfig.json b/examples/nest/tsconfig.json index a489f42..a3a6690 100644 --- a/examples/nest/tsconfig.json +++ b/examples/nest/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "module": "commonjs", + "module": "ESNext", + "moduleResolution": "node", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, diff --git a/packages/vite-plugin-node/package.json b/packages/vite-plugin-node/package.json index 33f8bed..c7730e9 100644 --- a/packages/vite-plugin-node/package.json +++ b/packages/vite-plugin-node/package.json @@ -34,6 +34,7 @@ "dependencies": { "@rollup/pluginutils": "^4.1.1", "chalk": "^4.1.2", + "colors": "^1.4.0", "debug": "^4.3.2" }, "peerDependencies": { diff --git a/packages/vite-plugin-node/src/rollup-plugin-swc.ts b/packages/vite-plugin-node/src/rollup-plugin-swc.ts index 940be5b..a8a3fa9 100644 --- a/packages/vite-plugin-node/src/rollup-plugin-swc.ts +++ b/packages/vite-plugin-node/src/rollup-plugin-swc.ts @@ -1,9 +1,9 @@ import { createFilter } from '@rollup/pluginutils'; import type { Compiler, Options } from '@swc/core'; import type { Plugin } from 'vite'; -import { cleanUrl } from './utils'; +import { SwcFileUpdate, cleanUrl } from './utils'; -export function RollupPluginSwc(options: Options): Plugin { +export function RollupPluginSwc(options: Options, appPath: string): Plugin { let swc: Compiler; // todo: load swc/tsconfig from config files const config: Options = { @@ -19,7 +19,7 @@ export function RollupPluginSwc(options: Options): Plugin { if (filter(id) || filter(cleanUrl(id))) { if (!swc) swc = await import('@swc/core'); - + SwcFileUpdate(id, appPath); const result = await swc.transform(code, { ...config, filename: id, diff --git a/packages/vite-plugin-node/src/utils.ts b/packages/vite-plugin-node/src/utils.ts index 1469242..780579d 100644 --- a/packages/vite-plugin-node/src/utils.ts +++ b/packages/vite-plugin-node/src/utils.ts @@ -1,3 +1,6 @@ +import path from 'path'; +import 'colors'; + /** * modified from vitejs source code, just to make the console output looks vite-like */ @@ -16,7 +19,7 @@ export const cleanUrl = (url: string) => url.replace(hashRE, '').replace(queryRE, ''); export function isObject(item: any): item is object { - return (item && typeof item === 'object' && !Array.isArray(item)); + return item && typeof item === 'object' && !Array.isArray(item); } export default function mergeDeep(target: object, source: object) { @@ -40,3 +43,26 @@ export default function mergeDeep(target: object, source: object) { } return output; } +const CacheFileMap: Record = {}; + +export function SwcFileUpdate(FileId: string, appPath: string) { + console.clear(); + + let rootPath = ''; + FileId.split(path.sep).forEach((spath) => { + appPath.split('/').forEach((apath) => { + if (apath === spath && rootPath === '') + rootPath = apath; + }); + }); + + const FileNameIndex = FileId.split(path.sep).indexOf(rootPath); + const FilePath = path.join(...FileId.split(path.sep).slice(FileNameIndex)); + if (CacheFileMap[FilePath] === undefined) + CacheFileMap[FilePath] = 0; + else CacheFileMap[FilePath] += 1; + + CacheFileMap[FilePath] !== 0 + /* eslint-disable no-console */ + && console.log(`${FilePath} ✖️ [ ${CacheFileMap[FilePath]} ] updated`.yellow); +} diff --git a/packages/vite-plugin-node/src/vite-plugin-node.ts b/packages/vite-plugin-node/src/vite-plugin-node.ts index 5b54fc0..11c9035 100644 --- a/packages/vite-plugin-node/src/vite-plugin-node.ts +++ b/packages/vite-plugin-node/src/vite-plugin-node.ts @@ -6,22 +6,25 @@ import { PLUGIN_NAME } from '.'; import type { VitePluginNodeConfig } from '.'; export function VitePluginNode(cfg: VitePluginNodeConfig): Plugin[] { - const swcOptions = mergeDeep({ - module: { - type: 'es6', - }, - jsc: { - target: 'es2019', - parser: { - syntax: 'typescript', - decorators: true, + const swcOptions = mergeDeep( + { + module: { + type: 'es6', }, - transform: { - legacyDecorator: true, - decoratorMetadata: true, + jsc: { + target: 'es2019', + parser: { + syntax: 'typescript', + decorators: true, + }, + transform: { + legacyDecorator: true, + decoratorMetadata: true, + }, }, }, - }, cfg.swcOptions ?? {}); + cfg.swcOptions ?? {}, + ); const config: VitePluginNodeConfig = { appPath: cfg.appPath, @@ -36,7 +39,9 @@ export function VitePluginNode(cfg: VitePluginNodeConfig): Plugin[] { { name: PLUGIN_NAME, config: () => { - const plugincConfig: UserConfig & { VitePluginNodeConfig: VitePluginNodeConfig } = { + const plugincConfig: UserConfig & { + VitePluginNodeConfig: VitePluginNodeConfig + } = { build: { ssr: config.appPath, rollupOptions: { @@ -49,9 +54,7 @@ export function VitePluginNode(cfg: VitePluginNodeConfig): Plugin[] { optimizeDeps: { // Vite does not work well with optionnal dependencies, // mark them as ignored for now - exclude: [ - '@swc/core', - ], + exclude: ['@swc/core'], }, VitePluginNodeConfig: config, }; @@ -69,7 +72,7 @@ export function VitePluginNode(cfg: VitePluginNodeConfig): Plugin[] { if (config.tsCompiler === 'swc') { plugins.push({ - ...RollupPluginSwc(config.swcOptions!), + ...RollupPluginSwc(config.swcOptions!, cfg.appPath), }); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3ed5198..08c16aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -195,6 +195,7 @@ importers: '@types/node': ^16.10.2 '@types/rx': ^4.1.2 chalk: ^4.1.2 + colors: ^1.4.0 debug: ^4.3.2 express: ^4.17.1 fastify: ^3.22.0 @@ -203,10 +204,11 @@ importers: npm-run-all: ^4.1.5 reflect-metadata: ^0.1.13 rxjs: ^7.5.5 - vite: ^4.0.3 + vite: ^4.0.0 dependencies: '@rollup/pluginutils': 4.2.1 chalk: 4.1.2 + colors: 1.4.0 debug: 4.3.4 devDependencies: '@marblejs/core': 4.0.3_fp-ts@2.12.1+rxjs@7.5.5 @@ -3504,8 +3506,6 @@ packages: resolution: {integrity: sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==} engines: {node: '>=0.1.90'} requiresBuild: true - dev: true - optional: true /combined-stream/1.0.8: resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==}