+2
-31
@@ -63,5 +63,3 @@ /** | ||
| const { Debounce, getPackageGlobHmrFiles } = require('./util.js'); | ||
| const globHmrFiles = getPackageGlobHmrFiles(); | ||
| const { Debounce } = require('./util.js'); | ||
| const { hookServices, unblocks } = require('./hmr-hook.js'); | ||
@@ -79,22 +77,3 @@ hookServices(); | ||
| }); | ||
| }, 500); | ||
| const debouncedRerunFunc = new Debounce(() => { | ||
| unblocks().then(() => { | ||
| // noinspection JSUnresolvedReference | ||
| for (const cacheName in Module._cache) { | ||
| // noinspection JSUnresolvedReference | ||
| delete Module._cache[cacheName]; | ||
| } | ||
| for (const parentFilename in relationModuleFilenames) { | ||
| delete relationModuleFilenames[parentFilename]; | ||
| } | ||
| aliveFilenames.splice(0, aliveFilenames.length); | ||
| try { | ||
| aliveFilenames.push(pathname); | ||
| require(pathname); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| }); | ||
| }); | ||
| }, 200); | ||
@@ -111,9 +90,1 @@ /** | ||
| }); | ||
| chokidarWatcher.on('add', freshFilename => { | ||
| const newGlobHmrFiles = getPackageGlobHmrFiles(); | ||
| if (!globHmrFiles.includes(freshFilename) && newGlobHmrFiles.includes(freshFilename)) { | ||
| globHmrFiles.push(freshFilename); | ||
| debouncedRerunFunc.call(); | ||
| } | ||
| }); |
@@ -6,9 +6,16 @@ import { getHookHttpUrl, getHookNetUrl } from './util.js'; | ||
| if (context.conditions.includes('import')) { | ||
| if (specifier === 'net') { | ||
| specifier = getHookNetUrl; | ||
| } else if (specifier === 'http') { | ||
| specifier = getHookHttpUrl; | ||
| /** | ||
| * @type {string} | ||
| */ | ||
| let newSpecifier = specifier; | ||
| if (newSpecifier.startsWith('node:')) { | ||
| newSpecifier = newSpecifier.slice('node:'.length); | ||
| } | ||
| if (newSpecifier === 'net') { | ||
| specifier = getHookNetUrl(); | ||
| } else if (newSpecifier === 'http') { | ||
| specifier = getHookHttpUrl(); | ||
| } | ||
| } | ||
| return defaultResolve(specifier, context, defaultResolve); | ||
| } |
+14
-34
| const url = require('url'); | ||
| const path = require('path'); | ||
| const loaders = ['./hmr.mjs', './loaders/loader-block.mjs']; | ||
| const getLoaderUrl = loader => url.pathToFileURL(require.resolve(loader)).toString(); | ||
| const loaders = [ | ||
| ['--require', './hmr-proxy.cjs'], | ||
| ['--loader', './loaders/loader-block.mjs'] | ||
| ]; | ||
| const getLoaderUrl = (includeType, pathname) => { | ||
| if (includeType === '--require') { | ||
| return path.join(__dirname, pathname); | ||
| } else { | ||
| return url.pathToFileURL(require.resolve(pathname)).toString(); | ||
| } | ||
| }; | ||
@@ -9,3 +18,4 @@ const getLoaderArgs = () => { | ||
| for (const loader of loaders) { | ||
| args.push('--loader', getLoaderUrl(loader)); | ||
| const [includeType, pathname] = loader; | ||
| args.push(includeType, getLoaderUrl(includeType, pathname)); | ||
| } | ||
@@ -17,34 +27,6 @@ return args; | ||
| const newArgs = [...oldArgs]; | ||
| const index = newArgs.findIndex(arg => arg === '--loader'); | ||
| if (index !== -1) { | ||
| newArgs.splice(index, 0, ...getLoaderArgs()); | ||
| } | ||
| newArgs.splice(0, 0, ...getLoaderArgs()); | ||
| return newArgs; | ||
| }; | ||
| const getPackageGlobHmrFiles = (cwd = undefined) => { | ||
| if (!cwd) { | ||
| cwd = process.cwd(); | ||
| } | ||
| const packageJson = path.join(cwd, 'package.json'); | ||
| /** | ||
| * @type {Record<string, any>} | ||
| */ | ||
| const packageJsonObj = require(packageJson); | ||
| /** | ||
| * @type {string[]} | ||
| */ | ||
| const hmrGlobs = packageJsonObj.hmrGlobs || []; | ||
| const hmrFiles = []; | ||
| if (hmrGlobs.length) { | ||
| const { globSync } = require('glob'); | ||
| const globFiles = globSync(hmrGlobs, { cwd, ignore: 'node_modules/**' }); | ||
| for (const globFile of globFiles) { | ||
| const fullPath = path.join(cwd, globFile); | ||
| hmrFiles.push(fullPath); | ||
| } | ||
| } | ||
| return hmrFiles; | ||
| }; | ||
| class Debounce { | ||
@@ -72,3 +54,1 @@ constructor(func, delay) { | ||
| module.exports.getLoaderArgs = getLoaderArgs; | ||
| module.exports.getPackageGlobHmrFiles = getPackageGlobHmrFiles; |
+2
-3
| { | ||
| "name": "tsxx", | ||
| "version": "1.0.13", | ||
| "version": "1.0.14", | ||
| "description": "tsx 增强", | ||
| "scripts": { | ||
| "build": "node build.mjs && cp -r src/* dist/", | ||
| "build": "node build.mjs", | ||
| "lint": "prettier --write .", | ||
@@ -20,5 +20,4 @@ "test": "vitest" | ||
| "chokidar": "^3.5.3", | ||
| "glob": "^10.3.4", | ||
| "tsx": "^3.12.8" | ||
| } | ||
| } |
| import { test, expect } from 'vitest'; | ||
| import { hookServices, unblocks } from './hmr-hook.js'; | ||
| test('auto unblocks', async () => { | ||
| hookServices(); | ||
| const http = require('http'); | ||
| const server = http.createServer((_: any, res: any) => { | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('Hello World\n'); | ||
| }); | ||
| const port = 3007; | ||
| const count = await new Promise(resolve => { | ||
| server.listen(port, () => { | ||
| console.log(`Server running at http://localhost:${port}`); | ||
| setInterval(async () => { | ||
| resolve(await unblocks()); | ||
| }, 100); | ||
| }); | ||
| }); | ||
| expect(count).toBe(1); | ||
| }); |
| import('./hmr-proxy.cjs'); |
| { | ||
| "hmrGlobs": [ | ||
| "src/**/*.js", | ||
| "src/**/*.ts" | ||
| ] | ||
| } |
| import { test, expect } from 'vitest'; | ||
| // @ts-ignore | ||
| import { getSpawnArgs, Debounce, getLoaderArgs, getPackageGlobHmrFiles } from './util.js'; | ||
| import * as path from 'path'; | ||
| test('get package glob hmr files', () => { | ||
| let cwd = path.join(process.cwd(), 'src/test/glob'); | ||
| const files = getPackageGlobHmrFiles(cwd); | ||
| expect(files).toEqual([ | ||
| path.join(cwd, 'src/demo03.ts'), | ||
| path.join(cwd, 'src/demo02.js'), | ||
| path.join(cwd, 'src/demo01.js') | ||
| ]); | ||
| }); | ||
| test('get new spawn args', () => { | ||
| const loaderArgs = getLoaderArgs(); | ||
| const newArgs = getSpawnArgs(['---', 'xxx', '--loader', 'someJsFile.js']); | ||
| expect(newArgs).toEqual(['---', 'xxx', ...loaderArgs, '--loader', 'someJsFile.js']); | ||
| }); | ||
| test('debounce', () => { | ||
| const num = { | ||
| count: 1 | ||
| }; | ||
| const call = new Debounce(() => { | ||
| num.count++; | ||
| }, 100); | ||
| call.call(); | ||
| call.call(); | ||
| call.call(); | ||
| expect(num.count).toBe(1); | ||
| }); |
2
-33.33%3
-40%6
-14.29%7116
-29.3%9
-43.75%236
-27.38%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed