generated from hemengke1997/ts-starter
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
848e26f
commit eaa8688
Showing
7 changed files
with
171 additions
and
91 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("this is haha!!!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"test": "/test.bb9057b2.js" | ||
"test": "/test.e7681e84.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
console.log('this is test') | ||
console.log('this is haha!!!') | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,110 @@ | ||
import { createHash } from 'node:crypto' | ||
import path from 'node:path' | ||
import type { WebSocketServer } from 'vite' | ||
import { normalizePath, transformWithEsbuild } from 'vite' | ||
|
||
import fg from 'fast-glob' | ||
import fs from 'fs-extra' | ||
import type { VitePluginOptions } from '..' | ||
import type { ManifestCache } from './manifestCache' | ||
|
||
export function getContentHash(chunk: string | Uint8Array) { | ||
return createHash('sha256').update(chunk).digest('hex').substring(0, 8) | ||
} | ||
|
||
type BuildOptions = { | ||
filePath: string | ||
publicDir: string | ||
cache: ManifestCache | ||
code?: string | ||
buildLength: number | ||
} & Required<VitePluginOptions> | ||
|
||
export async function build(options: BuildOptions) { | ||
const { filePath, publicDir, transformOptions, outputDir } = options | ||
const code = options.code || fs.readFileSync(filePath, 'utf-8') | ||
const fileName = path.basename(filePath, path.extname(filePath)) | ||
await transformWithEsbuild(code, fileName, { | ||
loader: 'ts', | ||
format: 'esm', | ||
minify: true, | ||
platform: 'browser', | ||
sourcemap: false, | ||
...transformOptions, | ||
}).then(async (res) => { | ||
await deleteOldFiles({ | ||
...options, | ||
publicDir, | ||
fileName, | ||
outputDir, | ||
}) | ||
|
||
await addJsFile({ code: res.code, fileName, ...options }) | ||
}) | ||
} | ||
|
||
type TDeleteFile = { | ||
publicDir: string | ||
outputDir: string | ||
fileName: string | ||
cache: ManifestCache | ||
} & Required<VitePluginOptions> | ||
|
||
export const ts = '.ts' | ||
|
||
export async function deleteOldFiles(args: TDeleteFile) { | ||
const { publicDir, outputDir, fileName, cache, inputDir, manifestName } = args | ||
const oldFiles = fg.sync(normalizePath(path.join(publicDir, `${outputDir}/${fileName}.?(*.)js`))) | ||
// if exits old files | ||
if (oldFiles.length) { | ||
// delete old files | ||
for (const f of oldFiles) { | ||
if (fs.existsSync(f)) { | ||
// and modify manifest | ||
cache.removeCache(fileName) | ||
cache.writeCache(`${inputDir}/${manifestName}.json`) | ||
await fs.remove(f) | ||
} | ||
} | ||
} | ||
} | ||
|
||
type TAddFile = { | ||
code?: string | ||
fileName: string | ||
publicDir: string | ||
cache: ManifestCache | ||
buildLength: number | ||
} & Required<VitePluginOptions> | ||
|
||
let currentBuildTimes = 0 | ||
export async function addJsFile(args: TAddFile) { | ||
const { hash, code = '', outputDir, fileName, publicDir, cache, buildLength, manifestName, inputDir } = args | ||
let outPath = '' | ||
if (hash) { | ||
const hash = getContentHash(code) | ||
outPath = normalizePath(`${outputDir}/${fileName}.${hash}.js`) | ||
} else { | ||
outPath = normalizePath(`${outputDir}/${fileName}.js`) | ||
} | ||
|
||
const fp = normalizePath(path.join(publicDir, outPath)) | ||
await fs.ensureDir(path.dirname(fp)) | ||
await fs.writeFile(fp, code) | ||
cache.setCache({ key: fileName, value: outPath }) | ||
// write cache | ||
currentBuildTimes++ | ||
if (currentBuildTimes >= buildLength) { | ||
cache.writeCache(`${inputDir}/${manifestName}.json`) | ||
} | ||
} | ||
|
||
export function reloadPage(ws: WebSocketServer) { | ||
ws.send({ | ||
type: 'full-reload', | ||
}) | ||
} | ||
|
||
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))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters