Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit a7a5bd6

Browse files
committed
refactor: clean up
1 parent 5979edd commit a7a5bd6

File tree

3 files changed

+41
-47
lines changed

3 files changed

+41
-47
lines changed

framework/core/routing.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,6 @@ export async function redirect(url: string, replace?: boolean) {
291291
events.emit('popstate', { type: 'popstate', resetScroll: true })
292292
}
293293

294-
export function isModuleURL(url: string) {
295-
for (const ext of moduleExts) {
296-
if (url.endsWith('.' + ext)) {
297-
return true
298-
}
299-
}
300-
return false
301-
}
302-
303294
export function toPagePath(url: string): string {
304295
let pathname = url
305296
for (const ext of moduleExts) {

server/app.ts

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { buildChecksum, transform } from '../compiler/mod.ts'
33
import { colors, createHash, ensureDir, minify, path, walk } from '../deps.ts'
44
import { EventEmitter } from '../framework/core/events.ts'
55
import type { RouteModule } from '../framework/core/routing.ts'
6-
import { createBlankRouterURL, isModuleURL, Routing, toPagePath } from '../framework/core/routing.ts'
6+
import { createBlankRouterURL, Routing, toPagePath } from '../framework/core/routing.ts'
77
import { defaultReactVersion, minDenoVersion, moduleExts, hashShortLength } from '../shared/constants.ts'
88
import { ensureTextFile, existsDirSync, existsFileSync } from '../shared/fs.ts'
99
import log from '../shared/log.ts'
@@ -12,7 +12,7 @@ import type { Config, LoaderPlugin, LoaderTransformResult, ModuleOptions, Router
1212
import { VERSION } from '../version.ts'
1313
import { Bundler } from './bundler.ts'
1414
import { defaultConfig, loadConfig } from './config.ts'
15-
import { clearCompilation, computeHash, createHtml, formatBytesWithColor, getAlephPkgUri, getRelativePath, reFullVersion, reHashJs, reHashResolve, toLocalUrl, trimModuleExt } from './helper.ts'
15+
import { clearCompilation, computeHash, createHtml, formatBytesWithColor, getAlephPkgUri, getRelativePath, isModuleURL, reFullVersion, reHashJs, reHashResolve, toLocalUrl, trimModuleExt } from './helper.ts'
1616

1717
/** A module includes the compilation details. */
1818
export type Module = {
@@ -211,36 +211,7 @@ export class Application implements ServerApplication {
211211
for await (const event of w) {
212212
for (const p of event.paths) {
213213
const url = util.cleanPath(util.trimPrefix(p, this.srcDir))
214-
const validated = () => {
215-
// ignore `.aleph` and output directories
216-
if (url.startsWith('/.aleph/') || url.startsWith(this.config.outputDir)) {
217-
return false
218-
}
219-
220-
// is module
221-
if (isModuleURL(url)) {
222-
if (url.startsWith('/pages/') || url.startsWith('/api/')) {
223-
return true
224-
}
225-
switch (trimModuleExt(url)) {
226-
case '/404':
227-
case '/app':
228-
return true
229-
}
230-
}
231-
232-
// is dep
233-
for (const { deps } of this.#modules.values()) {
234-
if (deps.some(dep => dep.url === url)) {
235-
return true
236-
}
237-
}
238-
239-
// is loaded by plugin
240-
return this.config.plugins.some(p => p.type === 'loader' && p.test.test(url))
241-
}
242-
243-
if (validated()) {
214+
if (this.isScopedModule(url)) {
244215
util.debounceX(url, () => {
245216
if (existsFileSync(p)) {
246217
let type = 'modify'
@@ -298,6 +269,34 @@ export class Application implements ServerApplication {
298269
}
299270
}
300271

272+
private isScopedModule(url: string) {
273+
// is module
274+
if (isModuleURL(url)) {
275+
if (url.startsWith('/pages/') || url.startsWith('/api/')) {
276+
return true
277+
}
278+
switch (trimModuleExt(url)) {
279+
case '/404':
280+
case '/app':
281+
return true
282+
}
283+
}
284+
285+
// is page module by plugin
286+
if (this.config.plugins.some(p => p.type === 'loader' && p.test.test(url) && p.allowPage)) {
287+
return true
288+
}
289+
290+
// is dep
291+
for (const { deps } of this.#modules.values()) {
292+
if (deps.some(dep => dep.url === url)) {
293+
return true
294+
}
295+
}
296+
297+
return false
298+
}
299+
301300
get isDev() {
302301
return this.mode === 'development'
303302
}
@@ -472,12 +471,7 @@ export class Application implements ServerApplication {
472471
['/app', '/404'].includes(trimModuleExt(url))
473472
}
474473
}
475-
for (const plugin of this.config.plugins) {
476-
if (plugin.type === 'loader' && plugin.test.test(url)) {
477-
return plugin.allowPage || plugin.acceptHMR
478-
}
479-
}
480-
return false
474+
return this.config.plugins.some(p => p.type === 'loader' && p.test.test(url) && (p.allowPage || p.acceptHMR))
481475
}
482476

483477
/** inject HMR code */

server/helper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ export function getRelativePath(from: string, to: string): string {
6161
return r
6262
}
6363

64+
export function isModuleURL(url: string) {
65+
for (const ext of moduleExts) {
66+
if (url.endsWith('.' + ext)) {
67+
return true
68+
}
69+
}
70+
return false
71+
}
72+
6473
export function trimModuleExt(url: string) {
6574
for (const ext of moduleExts) {
6675
if (url.endsWith('.' + ext)) {

0 commit comments

Comments
 (0)