Skip to content

Commit

Permalink
chore: merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 19, 2023
1 parent 86a5e05 commit b59312d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
8 changes: 3 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Expand Down Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions src/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '*',
Expand All @@ -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)))
)
}
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 8 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +17,7 @@ import {
isEmptyObject,
normalizeDirPath,
reloadPage,
removeOldJsFiles,
validateOptions,
} from './helper/utils'
import { build, buildAll, esbuildTypescript } from './helper/build'
Expand Down Expand Up @@ -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,
})
Expand All @@ -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()

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -313,7 +304,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
s.update(
node.sourceCodeLocation!.startOffset,
node.sourceCodeLocation!.endOffset,
`<${SCRIPT_TAG} ${attrs}></${SCRIPT_TAG}>`,
`<script ${attrs}></script>`,
)
} else {
s.remove(node.sourceCodeLocation!.startOffset, node.sourceCodeLocation!.endOffset)
Expand Down Expand Up @@ -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', {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/inject-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function injectScripts(scripts: Scripts) {
...s,
tag: 'script',
attrs: {
crossorigin: true,
...s.attrs,
[VPPT_DATA_ATTR]: 'true',
},
Expand Down

0 comments on commit b59312d

Please sign in to comment.