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

Commit bce52d9

Browse files
author
Je
committed
refactor: cleanup
1 parent 60783f4 commit bce52d9

File tree

6 files changed

+36
-46
lines changed

6 files changed

+36
-46
lines changed

project.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ export class Project {
517517
Object.assign(this, { buildID: this.mode + '.' + this.config.buildTarget })
518518
// update routing options
519519
this.#routing = new Routing([], this.config.baseUrl, this.config.defaultLocale, this.config.locales)
520-
// import post plugins
520+
// import postcss plugins
521521
await Promise.all(this.config.postcss.plugins.map(async p => {
522522
let name: string
523523
if (typeof p === 'string') {
@@ -676,7 +676,10 @@ export class Project {
676676
break
677677
}
678678
}
679-
return isDep
679+
if (isDep) {
680+
return
681+
}
682+
return this.config.plugins.findIndex(p => p.test.test(path)) > -1
680683
})()
681684
if (validated) {
682685
const moduleID = path.replace(reModuleExt, '.js')
@@ -846,7 +849,6 @@ export class Project {
846849
return this.#modules.get(mod.id)!
847850
}
848851

849-
const { importMap } = this
850852
const name = path.basename(mod.sourceFilePath).replace(reModuleExt, '')
851853
const saveDir = path.join(this.buildDir, path.dirname(mod.sourceFilePath))
852854
const metaFile = path.join(saveDir, `${name}.meta.json`)
@@ -870,6 +872,8 @@ export class Project {
870872

871873
let sourceContent = ''
872874
let shouldCompile = false
875+
let fsync = false
876+
873877
if (options?.sourceCode) {
874878
const sourceHash = getHash(options.sourceCode, true)
875879
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
@@ -879,8 +883,9 @@ export class Project {
879883
}
880884
} else if (mod.isRemote) {
881885
let dlUrl = url
882-
for (const importPath in importMap.imports) {
883-
const alias = importMap.imports[importPath]
886+
const { imports } = this.importMap
887+
for (const importPath in imports) {
888+
const alias = imports[importPath]
884889
if (importPath === url) {
885890
dlUrl = alias
886891
break
@@ -960,8 +965,6 @@ export class Project {
960965
}
961966
}
962967

963-
let fsync = false
964-
965968
// compile source code
966969
if (shouldCompile) {
967970
const t = performance.now()

renderer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import unescape from 'https://esm.sh/lodash/unescape?no-check'
12
import React, { ComponentType, ReactElement } from 'https://esm.sh/react'
23
import { renderToString } from 'https://esm.sh/react-dom/server'
34
import { RouterContext } from './context.ts'

tsc/compile.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import transformImportPathRewrite from './transform-import-path-rewrite.ts'
33
import transformReactJsx from './transform-react-jsx.ts'
44
import transformReactRefresh from './transform-react-refresh.ts'
55
import transformReactUseDenoHook from './transform-react-use-deno-hook.ts'
6-
import { CreatePlainTransformer, CreateTransformer } from './transformer.ts'
76

87
export interface CompileOptions {
98
mode: 'development' | 'production'
@@ -13,14 +12,6 @@ export interface CompileOptions {
1312
signUseDeno: (id: string) => string
1413
}
1514

16-
export function createSourceFile(fileName: string, source: string) {
17-
return ts.createSourceFile(
18-
fileName,
19-
source,
20-
ts.ScriptTarget.ES2015,
21-
)
22-
}
23-
2415
const allowTargets = [
2516
'esnext',
2617
'es2015',
@@ -34,12 +25,12 @@ const allowTargets = [
3425
export function compile(fileName: string, source: string, { mode, target: targetName, rewriteImportPath, reactRefresh, signUseDeno }: CompileOptions) {
3526
const target = allowTargets.indexOf(targetName.toLowerCase())
3627
const transformers: ts.CustomTransformers = { before: [], after: [] }
37-
transformers.before!.push(CreatePlainTransformer(transformReactJsx, { mode, rewriteImportPath }))
38-
transformers.before!.push(CreateTransformer(transformReactUseDenoHook, signUseDeno))
28+
transformers.before!.push(createPlainTransformer(transformReactJsx, { mode, rewriteImportPath }))
29+
transformers.before!.push(createTransformer(transformReactUseDenoHook, signUseDeno))
3930
if (reactRefresh) {
40-
transformers.before!.push(CreateTransformer(transformReactRefresh))
31+
transformers.before!.push(createTransformer(transformReactRefresh))
4132
}
42-
transformers.after!.push(CreatePlainTransformer(transformImportPathRewrite, rewriteImportPath))
33+
transformers.after!.push(createPlainTransformer(transformImportPathRewrite, rewriteImportPath))
4334

4435
return ts.transpileModule(source, {
4536
fileName,
@@ -60,3 +51,22 @@ export function compile(fileName: string, source: string, { mode, target: target
6051
transformers,
6152
})
6253
}
54+
55+
function createPlainTransformer(transform: (sf: ts.SourceFile, node: ts.Node, ...args: any[]) => ts.VisitResult<ts.Node>, ...args: any[]): ts.TransformerFactory<ts.SourceFile> {
56+
function nodeVisitor(ctx: ts.TransformationContext, sf: ts.SourceFile) {
57+
const visitor: ts.Visitor = node => {
58+
const ret = transform(sf, node, ...args)
59+
if (ret != null) {
60+
return ret
61+
}
62+
return ts.visitEachChild(node, visitor, ctx)
63+
}
64+
return visitor
65+
}
66+
67+
return ctx => sf => ts.visitNode(sf, nodeVisitor(ctx, sf))
68+
}
69+
70+
function createTransformer(transform: (ctx: ts.TransformationContext, sf: ts.SourceFile, options?: any) => ts.SourceFile, options?: Record<string, any>): ts.TransformerFactory<ts.SourceFile> {
71+
return ctx => sf => transform(ctx, sf, options)
72+
}

tsc/transform-react-use-deno-hook.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/**
2-
* TypeScript AST Transformer for react refresh.
3-
* @link https://github.com/facebook/react/issues/16604#issuecomment-528663101
4-
* @link https://github.com/facebook/react/blob/master/packages/react-refresh/src/ReactFreshBabelPlugin.js
2+
* TypeScript AST Transformer for useDeno hook.
53
*/
64

75
import ts from 'https://esm.sh/typescript'
@@ -113,8 +111,6 @@ export class RefreshTransformer {
113111
}
114112
}
115113

116-
117-
118114
export default function transformReactUseDenoHook(ctx: ts.TransformationContext, sf: ts.SourceFile, signUseDeno: (id: string) => string): ts.SourceFile {
119115
const t = new RefreshTransformer(sf, signUseDeno)
120116
return t.transform()

tsc/transformer.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface SSROptions {
3333
export interface Plugin {
3434
test: RegExp
3535
resolve?(path: string): { path: string, external?: boolean }
36-
transform?(path: string): { code: string, sourceMap?: string, loader?: 'js' | 'json' | 'css' | 'markdown' }
36+
transform?(path: string): { code: string, sourceMap?: string, loader?: 'js' | 'css' | 'text' }
3737
}
3838

3939
/**

0 commit comments

Comments
 (0)