From b59312d755a550045f35fe6d9ca0ce6dca84294f Mon Sep 17 00:00:00 2001 From: hemengke1997 <23536175@qq.com> Date: Tue, 19 Sep 2023 14:59:44 +0800 Subject: [PATCH] chore: merge master --- package.json | 2 +- pnpm-lock.yaml | 8 +++----- src/helper/AbsCacheProcessor.ts | 3 +-- src/helper/utils.ts | 17 +++++++++++------ src/index.ts | 25 ++++++++----------------- src/plugins/inject-script.ts | 1 + 6 files changed, 25 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index eb5c847..5aa0988 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "vite": "3 || 4" }, "dependencies": { + "debug": "^4.3.4", "esbuild": "^0.19.2", "fs-extra": "^11.1.1", "magic-string": "^0.30.3", @@ -64,7 +65,6 @@ "@types/node": "^20.6.0", "bumpp": "^9.2.0", "conventional-changelog-cli": "^2.2.2", - "debug": "^4.3.4", "eslint": "^8.49.0", "mock-fs": "^5.2.0", "sirv": "^2.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2f34bf0..a926e3c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + debug: + specifier: ^4.3.4 + version: 4.3.4 esbuild: specifier: ^0.19.2 version: 0.19.2 @@ -54,9 +57,6 @@ importers: conventional-changelog-cli: specifier: ^2.2.2 version: registry.npmmirror.com/conventional-changelog-cli@2.2.2 - debug: - specifier: ^4.3.4 - version: 4.3.4 eslint: specifier: ^8.49.0 version: 8.49.0 @@ -2165,7 +2165,6 @@ packages: optional: true dependencies: ms: 2.1.2 - dev: true /deep-eql@4.1.3: resolution: {integrity: sha512-WaEtAOpRA1MQ0eohqZjpGD8zdI0Ovsm8mmFhaDN8dvDZzyoUMcYDnf5Y6iu7HTXxf8JDS23qWa4a+hKCDyOPzw==} @@ -4228,7 +4227,6 @@ packages: /ms@2.1.2: resolution: {integrity: sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==} - dev: true /ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} diff --git a/src/helper/AbsCacheProcessor.ts b/src/helper/AbsCacheProcessor.ts index f9db050..3c3e409 100644 --- a/src/helper/AbsCacheProcessor.ts +++ b/src/helper/AbsCacheProcessor.ts @@ -1,7 +1,6 @@ import { normalizePath } from 'vite' import createDebug from 'debug' import type { ManifestCache } from './ManifestCache' -import { JS_EXT } from './utils' const debug = createDebug('vite-plugin-public-typescript:AbsCacheProcessor ===> ') @@ -57,7 +56,7 @@ export abstract class AbsCacheProcessor { function getOutputPath(p: string, hash?: string) { hash = hash ? `.${hash}` : '' - return normalizePath(`${p}/${tsFileName}${hash}${JS_EXT}`) + return normalizePath(`${p}/${tsFileName}${hash}.js`) } let outputPath = getOutputPath(outputDir) diff --git a/src/helper/utils.ts b/src/helper/utils.ts index 09b8627..f660f2a 100644 --- a/src/helper/utils.ts +++ b/src/helper/utils.ts @@ -11,11 +11,6 @@ import { assert } from './assert' const debug = createDebug('vite-plugin-public-typescript:util ===> ') -export const TS_EXT = '.ts' -export const JSON_EXT = '.json' -export const JS_EXT = '.js' -export const SCRIPT_TAG = 'script' - export function reloadPage(ws: WebSocketServer) { ws.send({ path: '*', @@ -27,7 +22,7 @@ export function isPublicTypescript(args: { filePath: string; inputDir: string; r const { filePath, root, inputDir } = args return ( - path.extname(filePath) === TS_EXT && + path.extname(filePath) === '.ts' && normalizePath(path.resolve(root, inputDir)).endsWith(normalizePath(path.dirname(filePath))) ) } @@ -198,6 +193,16 @@ export async function findAllOldJsFile(args: { publicDir: string; outputDir: str return oldFiles } +export function removeOldJsFiles(oldFiles: string[]) { + if (oldFiles.length) { + for (const f of oldFiles) { + if (fs.existsSync(f)) { + fs.removeSync(f) + } + } + } +} + export function disableManifestHmr(config: ResolvedConfig, manifestPath: string) { if (config.command === 'serve') { const index = config.configFileDependencies.indexOf(manifestPath) diff --git a/src/index.ts b/src/index.ts index 2a69557..0985fdf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,10 +8,6 @@ import fs from 'fs-extra' import createDebug from 'debug' import MagicString from 'magic-string' import { - JSON_EXT, - JS_EXT, - SCRIPT_TAG, - TS_EXT, _isPublicTypescript, addCodeHeader, disableManifestHmr, @@ -21,6 +17,7 @@ import { isEmptyObject, normalizeDirPath, reloadPage, + removeOldJsFiles, validateOptions, } from './helper/utils' import { build, buildAll, esbuildTypescript } from './helper/build' @@ -128,7 +125,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { fs.ensureDirSync(getInputDir(resolvedRoot, opts.inputDir)) - const tsFilesGlob = await glob(getInputDir(resolvedRoot, opts.inputDir, `/*${TS_EXT}`), { + const tsFilesGlob = await glob(getInputDir(resolvedRoot, opts.inputDir, `/*.ts`), { cwd: resolvedRoot, absolute: true, }) @@ -143,7 +140,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { ...opts, }) - cache.setManifestPath(normalizePath(`${globalConfigBuilder.get().absInputDir}/${opts.manifestName}${JSON_EXT}`)) + cache.setManifestPath(normalizePath(`${globalConfigBuilder.get().absInputDir}/${opts.manifestName}.json`)) cache.initCacheFromFile() @@ -153,7 +150,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { debug('cache:', cache.get()) - assert(cache.getManifestPath().includes(JSON_EXT)) + assert(cache.getManifestPath().includes('.json')) }, configureServer(server) { @@ -246,13 +243,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { publicDir: viteConfig.publicDir, tsFileNames, }) - if (oldFiles.length) { - for (const f of oldFiles) { - if (fs.existsSync(f)) { - fs.removeSync(f) - } - } - } + removeOldJsFiles(oldFiles) } await buildAll(tsFilesGlob) @@ -283,7 +274,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { return } // script tags - if (node.nodeName === SCRIPT_TAG) { + if (node.nodeName === 'script') { const { src, vppt } = getScriptInfo(node) if (vppt?.value && src?.value) { @@ -313,7 +304,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { s.update( node.sourceCodeLocation!.startOffset, node.sourceCodeLocation!.endOffset, - `<${SCRIPT_TAG} ${attrs}>`, + ``, ) } else { s.remove(node.sourceCodeLocation!.startOffset, node.sourceCodeLocation!.endOffset) @@ -354,7 +345,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { async configureServer(server) { server.middlewares.use((req, res, next) => { try { - if (req?.url?.startsWith('/') && req?.url?.endsWith(JS_EXT)) { + if (req?.url?.startsWith('/') && req?.url?.endsWith('.js')) { const cacheItem = cache.findCacheItemByPath(req.url) if (cacheItem) { return send(req, res, addCodeHeader(cacheItem._code || ''), 'js', { diff --git a/src/plugins/inject-script.ts b/src/plugins/inject-script.ts index 4ab7521..b5ceab8 100644 --- a/src/plugins/inject-script.ts +++ b/src/plugins/inject-script.ts @@ -13,6 +13,7 @@ export function injectScripts(scripts: Scripts) { ...s, tag: 'script', attrs: { + crossorigin: true, ...s.attrs, [VPPT_DATA_ATTR]: 'true', },