diff --git a/README.md b/README.md index e0c1f8d..2421af2 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,12 @@ ## 功能 -- 运行时和构建时,把指定文件夹中的`typescript`文件编译为`javascript`,供浏览器直接使用 +- 运行时和构建时,把指定文件夹中的 `typescript` 文件编译为 `javascript`,供浏览器直接使用 - 输出带有 `hash` 的js文件,无需担心缓存 - 自定义编译选项,指定目标浏览器范围,无需担心兼容性 -- 支持vite环境变量 +- 支持 `vite` 环境变量 - 支持 `HMR` -- 生产可用 +- 支持不同的输出方式(内存模式和文件模式) ## 安装 diff --git a/src/helper/AbsCacheProcessor.ts b/src/helper/AbsCacheProcessor.ts index 3c3e409..f9db050 100644 --- a/src/helper/AbsCacheProcessor.ts +++ b/src/helper/AbsCacheProcessor.ts @@ -1,6 +1,7 @@ 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 ===> ') @@ -56,7 +57,7 @@ export abstract class AbsCacheProcessor { function getOutputPath(p: string, hash?: string) { hash = hash ? `.${hash}` : '' - return normalizePath(`${p}/${tsFileName}${hash}.js`) + return normalizePath(`${p}/${tsFileName}${hash}${JS_EXT}`) } let outputPath = getOutputPath(outputDir) diff --git a/src/helper/utils.ts b/src/helper/utils.ts index 118cac5..09b8627 100644 --- a/src/helper/utils.ts +++ b/src/helper/utils.ts @@ -12,6 +12,9 @@ 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({ diff --git a/src/index.ts b/src/index.ts index 95778c7..2a69557 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,9 @@ 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, @@ -140,7 +143,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { ...opts, }) - cache.setManifestPath(normalizePath(`${globalConfigBuilder.get().absInputDir}/${opts.manifestName}.json`)) + cache.setManifestPath(normalizePath(`${globalConfigBuilder.get().absInputDir}/${opts.manifestName}${JSON_EXT}`)) cache.initCacheFromFile() @@ -150,7 +153,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { debug('cache:', cache.get()) - assert(cache.getManifestPath().includes('.json')) + assert(cache.getManifestPath().includes(JSON_EXT)) }, configureServer(server) { @@ -280,7 +283,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { return } // script tags - if (node.nodeName === 'script') { + if (node.nodeName === SCRIPT_TAG) { const { src, vppt } = getScriptInfo(node) if (vppt?.value && src?.value) { @@ -310,7 +313,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) @@ -351,7 +354,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { async configureServer(server) { server.middlewares.use((req, res, next) => { try { - if (req?.url?.endsWith('.js') && req.url.startsWith('/')) { + if (req?.url?.startsWith('/') && req?.url?.endsWith(JS_EXT)) { const cacheItem = cache.findCacheItemByPath(req.url) if (cacheItem) { return send(req, res, addCodeHeader(cacheItem._code || ''), 'js', {