From cab1e1ff9dbf7e2806c470274257cd011816e726 Mon Sep 17 00:00:00 2001 From: hemengke1997 <49073282+hemengke1997@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:48:17 +0800 Subject: [PATCH] perf: fix CRLF warning on windows --- playground/vite-project/public/a.7df79757.js | 1 - playground/vite-project/public/a.9df57b6c.js | 1 + playground/vite-project/public/test.be77f0f2.js | 1 - playground/vite-project/publicTypescript/a.ts | 12 ++---------- .../publicTypescript/custom-manifest.json | 5 ++--- playground/vite-project/publicTypescript/test.ts | 3 --- src/index.ts | 9 +++++++-- src/utils/index.ts | 8 +++++++- src/utils/manifestCache.ts | 3 ++- 9 files changed, 21 insertions(+), 22 deletions(-) delete mode 100644 playground/vite-project/public/a.7df79757.js create mode 100644 playground/vite-project/public/a.9df57b6c.js delete mode 100644 playground/vite-project/public/test.be77f0f2.js delete mode 100644 playground/vite-project/publicTypescript/test.ts 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)}`)) } }