Skip to content

Commit

Permalink
refactor: reduce hard-coding
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 19, 2023
1 parent 1e539fe commit 86a5e05
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

## 功能

- 运行时和构建时,把指定文件夹中的`typescript`文件编译为`javascript`,供浏览器直接使用
- 运行时和构建时,把指定文件夹中的 `typescript` 文件编译为 `javascript`,供浏览器直接使用
- 输出带有 `hash` 的js文件,无需担心缓存
- 自定义编译选项,指定目标浏览器范围,无需担心兼容性
- 支持vite环境变量
- 支持 `vite` 环境变量
- 支持 `HMR`
- 生产可用
- 支持不同的输出方式(内存模式和文件模式)

## 安装

Expand Down
3 changes: 2 additions & 1 deletion src/helper/AbsCacheProcessor.ts
Original file line number Diff line number Diff line change
@@ -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 ===> ')

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -310,7 +313,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
s.update(
node.sourceCodeLocation!.startOffset,
node.sourceCodeLocation!.endOffset,
`<script ${attrs}></script>`,
`<${SCRIPT_TAG} ${attrs}></${SCRIPT_TAG}>`,
)
} else {
s.remove(node.sourceCodeLocation!.startOffset, node.sourceCodeLocation!.endOffset)
Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit 86a5e05

Please sign in to comment.