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

Commit 1963f85

Browse files
committed
refactor: improve module importing
1 parent c357565 commit 1963f85

File tree

11 files changed

+142
-104
lines changed

11 files changed

+142
-104
lines changed

cli.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ async function main() {
8686
return
8787
}
8888

89-
// proxy https://deno.land/x/aleph on localhost
90-
const v = Deno.env.get('ALEPH_DEV_PORT')
91-
if (v !== undefined && /^\d+$/.test(v)) {
92-
const { localProxy } = await import('./server/localproxy.ts')
93-
localProxy(parseInt(v))
94-
}
95-
96-
// sets log level
89+
// set log level
9790
const l = options.L || options['log-level']
9891
if (util.isNEString(l)) {
9992
log.setLevel(l.toLowerCase() as LevelNames)
10093
}
10194

95+
// proxy https://deno.land/x/aleph on localhost
96+
const v = Deno.env.get('ALEPH_DEV')
97+
if (v !== undefined) {
98+
const { localProxy } = await import('./server/localproxy.ts')
99+
localProxy(Deno.cwd(), 2020)
100+
}
101+
102102
// check working dir
103103
const workingDir = resolve(String(args[0] || '.'))
104104
if (!existsDirSync(workingDir)) {

framework/react/init.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { dirname } from 'https://deno.land/[email protected]/path/mod.ts'
2-
import {
3-
getAlephPkgUri,
4-
getRelativePath,
5-
toLocalUrl
6-
} from '../../server/helper.ts'
2+
import { getAlephPkgUri, getRelativePath, toLocalUrl } from '../../server/helper.ts'
73
import util from '../../shared/util.ts'
84
import type { ServerApplication } from '../../types.ts'
95

plugins/css.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
1-
import CleanCSS from 'https://esm.sh/[email protected]?no-check'
2-
import PostCSS, { AcceptedPlugin } from 'https://esm.sh/[email protected]'
1+
import type { Plugin, PluginCreator } from 'https://esm.sh/[email protected]'
32
import { join } from 'https://deno.land/[email protected]/path/mod.ts'
43
import { existsFileSync } from '../shared/fs.ts'
54
import util from '../shared/util.ts'
65
import type { LoaderPlugin } from '../types.ts'
76

8-
const cleanCSS = new CleanCSS({ compatibility: '*' /* Internet Explorer 10+ */ })
7+
const postcssVersion = '8.2.8'
98
const productionOnlyPostcssPlugins = ['autoprefixer']
9+
const decoder = new TextDecoder()
1010

11-
type Options = {
11+
export type AcceptedPlugin = string | [string, any] | Plugin | PluginCreator<any>
12+
13+
export type Options = {
1214
postcss?: {
13-
plugins: (string | [string, any] | AcceptedPlugin)[]
15+
plugins: AcceptedPlugin[]
1416
}
1517
}
1618

1719
export default (options?: Options): LoaderPlugin => {
18-
const decoder = new TextDecoder()
1920
let pcssProcessor: any = null
21+
let cleanCSS: any = null
22+
let isProd: any = null
2023

2124
return {
2225
name: 'css-loader',
2326
type: 'loader',
2427
test: /\.p?css$/i,
2528
acceptHMR: true,
2629
async transform({ url, content }) {
30+
if (isProd === null) {
31+
isProd = Deno.env.get('BUILD_MODE') === 'production'
32+
}
33+
if (isProd) {
34+
const { default: CleanCSS } = await import('https://esm.sh/[email protected]?no-check')
35+
cleanCSS = new CleanCSS({ compatibility: '*' /* Internet Explorer 10+ */ })
36+
}
2737
if (pcssProcessor == null) {
2838
pcssProcessor = await initPostCSSProcessor(options)
2939
}
3040
const { content: pcss } = await pcssProcessor.process(decoder.decode(content)).async()
31-
const css = Deno.env.get('BUILD_MODE') === 'production' ? cleanCSS.minify(pcss).styles : pcss
41+
const css = isProd ? cleanCSS.minify(pcss).styles : pcss
3242
if (url.startsWith('#inline-style-')) {
3343
return {
3444
code: css,
@@ -48,6 +58,8 @@ export default (options?: Options): LoaderPlugin => {
4858
}
4959

5060
async function initPostCSSProcessor(options?: Options) {
61+
const { default: PostCSS } = await import(`https://esm.sh/postcss@${postcssVersion}`)
62+
5163
if (options?.postcss) {
5264
return PostCSS(await loadPostcssPlugins(options.postcss.plugins))
5365
}
@@ -74,7 +86,7 @@ async function initPostCSSProcessor(options?: Options) {
7486
return PostCSS(await loadPostcssPlugins(['autoprefixer']))
7587
}
7688

77-
async function loadPostcssPlugins(plugins: (string | [string, any] | AcceptedPlugin)[]) {
89+
async function loadPostcssPlugins(plugins: AcceptedPlugin[]) {
7890
const isDev = Deno.env.get('BUILD_MODE') === 'development'
7991
return await Promise.all(plugins.filter(p => {
8092
if (isDev) {
@@ -98,10 +110,10 @@ async function loadPostcssPlugins(plugins: (string | [string, any] | AcceptedPlu
98110
}
99111

100112
async function importPostcssPluginByName(name: string) {
101-
const { default: Plugin } = await import(`https://esm.sh/${name}?external=postcss@8.2.4&no-check`)
113+
const { default: Plugin } = await import(`https://esm.sh/${name}?external=postcss@${postcssVersion}&no-check`)
102114
return Plugin
103115
}
104116

105-
function isPostcssConfig(v: any): v is { plugins: (string | [string, any] | AcceptedPlugin)[] } {
117+
function isPostcssConfig(v: any): v is { plugins: AcceptedPlugin[] } {
106118
return util.isPlainObject(v) && util.isArray(v.plugins)
107119
}

plugins/sass.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
import { Options, Result } from 'https://esm.sh/[email protected]'
1+
import type { Options, Result } from 'https://esm.sh/[email protected]'
22
import type { LoaderPlugin } from '../types.ts'
33

4-
let renderSync: null | ((options: Options) => Result) = null
4+
type Sass = { renderSync(options: Options): Result }
55

6-
export default (opts?: Options): LoaderPlugin => ({
7-
name: 'sass-loader',
8-
type: 'loader',
9-
test: /\.(sass|scss)$/i,
10-
acceptHMR: true,
11-
async transform({ content, url }) {
12-
if (!('userAgent' in window.navigator)) {
13-
Object.assign(window.navigator, { userAgent: `Deno/${Deno.version.deno}` })
14-
}
15-
if (renderSync === null) {
16-
const sass = await import('https://esm.sh/[email protected]')
17-
renderSync = sass.renderSync
18-
}
19-
const { css, map } = renderSync({
20-
indentedSyntax: url.endsWith('.sass'),
21-
...opts,
22-
file: url,
23-
data: (new TextDecoder).decode(content),
24-
sourceMap: true
25-
})
26-
return {
27-
code: (new TextDecoder).decode(css),
28-
type: 'css',
29-
map: map ? (new TextDecoder).decode(map) : undefined,
6+
export default (opts?: Options): LoaderPlugin => {
7+
let sass: Sass | null = null
8+
9+
return {
10+
name: 'sass-loader',
11+
type: 'loader',
12+
test: /\.(sass|scss)$/i,
13+
acceptHMR: true,
14+
async transform({ content, url }) {
15+
if (!('userAgent' in window.navigator)) {
16+
Object.assign(window.navigator, { userAgent: `Deno/${Deno.version.deno}` })
17+
}
18+
if (sass === null) {
19+
sass = await import('https://esm.sh/[email protected]')
20+
}
21+
const { css, map } = sass.renderSync({
22+
indentedSyntax: url.endsWith('.sass'),
23+
...opts,
24+
file: url,
25+
data: (new TextDecoder).decode(content),
26+
sourceMap: true
27+
})
28+
return {
29+
code: (new TextDecoder).decode(css),
30+
type: 'css',
31+
map: map ? (new TextDecoder).decode(map) : undefined,
32+
}
3033
}
3134
}
32-
})
35+
}

server/api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { BufReader, BufWriter } from 'https://deno.land/[email protected]/io/bufio.ts'
22
import type { MultipartFormData } from 'https://deno.land/[email protected]/mime/multipart.ts'
33
import { MultipartReader } from 'https://deno.land/[email protected]/mime/multipart.ts'
4-
import * as brotli from 'https://deno.land/x/[email protected]/mod.ts'
5-
import { gzipEncode } from 'https://deno.land/x/[email protected]/mod.ts'
4+
import { compress as brotli } from 'https://deno.land/x/[email protected]/mod.ts'
5+
import { gzipEncode as gzip } from 'https://deno.land/x/[email protected]/mod.ts'
66
import log from '../shared/log.ts'
77
import type { APIRequest, ServerRequest, ServerResponse } from '../types.ts'
88

@@ -160,11 +160,11 @@ export class Request implements APIRequest {
160160
if (this.headers.get('accept-encoding')?.includes('br')) {
161161
this.#resp.headers.set('Vary', 'Origin')
162162
this.#resp.headers.set('Content-Encoding', 'br')
163-
body = brotli.compress(body)
163+
body = brotli(body)
164164
} else if (this.headers.get('accept-encoding')?.includes('gzip')) {
165165
this.#resp.headers.set('Vary', 'Origin')
166166
this.#resp.headers.set('Content-Encoding', 'gzip')
167-
body = gzipEncode(body)
167+
body = gzip(body)
168168
}
169169
}
170170
this.#resp.headers.set('Date', (new Date).toUTCString())

0 commit comments

Comments
 (0)