diff --git a/playground/vite-project/public/a.7df79757.js b/playground/vite-project/public/a.7df79757.js deleted file mode 100644 index cf31b21..0000000 --- a/playground/vite-project/public/a.7df79757.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";(()=>{function r(){return"hello"}function e(t){let o=0,n=c=>{o=c,t.innerHTML=`count is ${o}`};t.addEventListener("click",()=>n(o+1)),n(0)}function u(t,o){return t+o}var l=u(1,2);console.log(l);var s=r();console.log(s);e();})(); diff --git a/playground/vite-project/public/a.9df57b6c.js b/playground/vite-project/public/a.9df57b6c.js new file mode 100644 index 0000000..4ee8509 --- /dev/null +++ b/playground/vite-project/public/a.9df57b6c.js @@ -0,0 +1 @@ +"use strict";(()=>{console.log("this is a");})(); diff --git a/playground/vite-project/public/test.be77f0f2.js b/playground/vite-project/public/test.be77f0f2.js deleted file mode 100644 index 8f0a72e..0000000 --- a/playground/vite-project/public/test.be77f0f2.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";(()=>{console.log("this is haha!!!");})(); diff --git a/playground/vite-project/publicTypescript/a.ts b/playground/vite-project/publicTypescript/a.ts index 16d6654..6bd7e12 100644 --- a/playground/vite-project/publicTypescript/a.ts +++ b/playground/vite-project/publicTypescript/a.ts @@ -1,11 +1,3 @@ -import { hello } from 'lib' -import { other, setupCounter } from '@/other' +console.log('this is a') -const x = other(1, 2) -console.log(x) - -const h = hello() - -console.log(h) - -setupCounter() +export {} diff --git a/playground/vite-project/publicTypescript/custom-manifest.json b/playground/vite-project/publicTypescript/custom-manifest.json index d58f69c..dc07047 100644 --- a/playground/vite-project/publicTypescript/custom-manifest.json +++ b/playground/vite-project/publicTypescript/custom-manifest.json @@ -1,4 +1,3 @@ { - "a": "/a.7df79757.js", - "test": "/test.be77f0f2.js" -} + "a": "/a.9df57b6c.js" +} \ No newline at end of file diff --git a/playground/vite-project/publicTypescript/test.ts b/playground/vite-project/publicTypescript/test.ts deleted file mode 100644 index 41a5754..0000000 --- a/playground/vite-project/publicTypescript/test.ts +++ /dev/null @@ -1,3 +0,0 @@ -console.log('this is haha!!!') - -export {} diff --git a/src/index.ts b/src/index.ts index 4b9fce6..c989749 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import type { PluginOption, ResolvedConfig } from 'vite' import { normalizePath } from 'vite' import fg from 'fast-glob' import type { BuildOptions } from 'esbuild' +import { ensureDirSync } from 'fs-extra' import { addJsFile, build, deleteOldFiles, isPublicTypescript, reloadPage, ts } from './utils' import { ManifestCache } from './utils/manifestCache' @@ -14,7 +15,7 @@ export interface VitePluginOptions { ssrBuild?: boolean | undefined /** * @description input public typescript dir - * @default publicTypescript + * @default 'publicTypescript' */ inputDir?: string /** @@ -29,7 +30,7 @@ export interface VitePluginOptions { esbuildOptions?: BuildOptions | undefined /** * @description manifest fileName - * @default manifest + * @default 'manifest' */ manifestName?: string /** @@ -61,8 +62,12 @@ export function publicTypescript(options: VitePluginOptions): PluginOption { return { name: 'vite:public-typescript', + enforce: 'pre', configResolved(c) { config = c + + ensureDirSync(normalizePath(path.resolve(config.root, `${opts.inputDir}`))) + files = fg.sync(normalizePath(path.resolve(config.root, `${opts.inputDir}/*.ts`)), { cwd: config.root, absolute: true, diff --git a/src/utils/index.ts b/src/utils/index.ts index 9df3a2b..a36bda4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -97,7 +97,7 @@ export async function addJsFile(args: TAddFile) { const fp = normalizePath(path.join(publicDir, outPath)) await fs.ensureDir(path.dirname(fp)) - await fs.writeFile(fp, code) + await fs.writeFile(fp, crlf(code)) cache.setCache({ key: fileName, value: outPath }) // write cache currentBuildTimes++ @@ -115,3 +115,9 @@ export function reloadPage(ws: WebSocketServer) { export function isPublicTypescript({ filePath, root, inputDir }: { filePath: string; root: string; inputDir: string }) { return path.extname(filePath) === ts && normalizePath(filePath).includes(normalizePath(path.resolve(root, inputDir))) } + +export function crlf(text: string) { + const CRLF = '\r\n' + const R_CRLF = /\r\n|\r(?!\n)|\n/g + return text.replace(R_CRLF, CRLF) +} diff --git a/src/utils/manifestCache.ts b/src/utils/manifestCache.ts index e74d7ca..f088596 100644 --- a/src/utils/manifestCache.ts +++ b/src/utils/manifestCache.ts @@ -1,5 +1,6 @@ import path from 'node:path' import fs from 'fs-extra' +import { crlf } from '.' interface CacheType { key: string @@ -40,6 +41,6 @@ export class ManifestCache { .sort() .forEach((k) => (orderdCache[k] = cacheObj[k])) await fs.ensureDir(path.dirname(targetPath)) - await fs.writeFile(targetPath, `${JSON.stringify(orderdCache, null, 2)}`) + await fs.writeFile(targetPath, crlf(`${JSON.stringify(orderdCache, null, 2)}`)) } }