Skip to content

Commit

Permalink
perf: fix CRLF warning on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Nov 14, 2022
1 parent edeeae7 commit cab1e1f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 22 deletions.
1 change: 0 additions & 1 deletion playground/vite-project/public/a.7df79757.js

This file was deleted.

1 change: 1 addition & 0 deletions playground/vite-project/public/a.9df57b6c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";(()=>{console.log("this is a");})();
1 change: 0 additions & 1 deletion playground/vite-project/public/test.be77f0f2.js

This file was deleted.

12 changes: 2 additions & 10 deletions playground/vite-project/publicTypescript/a.ts
Original file line number Diff line number Diff line change
@@ -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 {}
5 changes: 2 additions & 3 deletions playground/vite-project/publicTypescript/custom-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"a": "/a.7df79757.js",
"test": "/test.be77f0f2.js"
}
"a": "/a.9df57b6c.js"
}
3 changes: 0 additions & 3 deletions playground/vite-project/publicTypescript/test.ts

This file was deleted.

9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -14,7 +15,7 @@ export interface VitePluginOptions {
ssrBuild?: boolean | undefined
/**
* @description input public typescript dir
* @default publicTypescript
* @default 'publicTypescript'
*/
inputDir?: string
/**
Expand All @@ -29,7 +30,7 @@ export interface VitePluginOptions {
esbuildOptions?: BuildOptions | undefined
/**
* @description manifest fileName
* @default manifest
* @default 'manifest'
*/
manifestName?: string
/**
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion src/utils/manifestCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import fs from 'fs-extra'
import { crlf } from '.'

interface CacheType {
key: string
Expand Down Expand Up @@ -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)}`))
}
}

0 comments on commit cab1e1f

Please sign in to comment.