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

Commit 393cb5c

Browse files
author
Wenjie Xia
committed
refactor: clean up
1 parent 43403a0 commit 393cb5c

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

cli.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,39 @@ Options:
3636
async function main() {
3737
// parse deno args
3838
const args: Array<string> = []
39-
const argOptions: Record<string, string | boolean> = {}
39+
const flags: Record<string, string | boolean> = {}
4040
for (let i = 0; i < Deno.args.length; i++) {
4141
const arg = Deno.args[i]
4242
if (arg.startsWith('-')) {
4343
if (arg.includes('=')) {
4444
const [key, value] = arg.replace(/^-+/, '').split('=', 2)
45-
argOptions[key] = value
45+
flags[key] = value
4646
} else {
4747
const key = arg.replace(/^-+/, '')
4848
const nextArg = Deno.args[i + 1]
4949
if (nextArg && !nextArg.startsWith('-')) {
50-
argOptions[key] = nextArg
50+
flags[key] = nextArg
5151
i++
5252
} else {
53-
argOptions[key] = true
53+
flags[key] = true
5454
}
5555
}
5656
} else {
5757
args.push(arg)
5858
}
5959
}
6060

61-
// get command, default is 'dev'
6261
const hasCommand = args.length > 0 && args[0] in commands
6362
const command = (hasCommand ? String(args.shift()) : 'dev') as keyof typeof commands
6463

6564
// prints aleph.js version
66-
if (argOptions.v && command != 'upgrade') {
65+
if (flags.v && command != 'upgrade') {
6766
console.log(`aleph.js v${VERSION}`)
6867
Deno.exit(0)
6968
}
7069

7170
// prints aleph.js and deno version
72-
if (argOptions.version && command != 'upgrade') {
71+
if (flags.version && command != 'upgrade') {
7372
const { deno, v8, typescript } = Deno.version
7473
console.log(`aleph.js ${VERSION}`)
7574
console.log(`deno ${deno}`)
@@ -79,7 +78,7 @@ async function main() {
7978
}
8079

8180
// prints help message
82-
if (argOptions.h || argOptions.help) {
81+
if (flags.h || flags.help) {
8382
if (hasCommand) {
8483
import(`./cli/${command}.ts`).then(({ helpMessage }) => {
8584
console.log(commands[command])
@@ -96,7 +95,7 @@ async function main() {
9695
}
9796

9897
// sets log level
99-
const l = argOptions.L || argOptions['log-level']
98+
const l = flags.L || flags['log-level']
10099
if (util.isNEString(l)) {
101100
log.setLevel(l)
102101
}
@@ -163,14 +162,14 @@ async function main() {
163162

164163
const { default: cmd } = await import(`./cli/${command}.ts`)
165164
if (command === 'upgrade') {
166-
await cmd(argOptions.v || argOptions.version || args[0] || 'latest')
165+
await cmd(flags.v || flags.version || args[0] || 'latest')
167166
return
168167
}
169168
const appDir = path.resolve(args[0] || '.')
170169
if (command !== 'init' && !existsDirSync(appDir)) {
171170
log.fatal('No such directory:', appDir)
172171
}
173-
await cmd(appDir, argOptions)
172+
await cmd(appDir, flags)
174173
}
175174

176175
if (import.meta.main) {

deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// deno.land/std
1+
// std
22
export { Untar } from 'https://deno.land/[email protected]/archive/tar.ts'
33
export * as bytes from 'https://deno.land/[email protected]/bytes/mod.ts'
44
export * as base64 from 'https://deno.land/[email protected]/encoding/base64.ts'

plugins/sass.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Options, renderSync } from 'https://esm.sh/[email protected]'
2-
import type { Plugin } from '../types.ts'
2+
import type { LoaderPlugin } from '../types.ts'
33

4-
const pluginFactory = (opts: Options = {}): Plugin => ({
4+
const pluginFactory = (opts: Options = {}): LoaderPlugin => ({
55
type: 'loader',
66
name: 'sass-loader',
77
test: /.(sass|scss)$/,

plugins/wasm.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import type { Plugin } from '../types.ts'
1+
import type { LoaderPlugin } from '../types.ts'
22

3-
const wasmLoader: Plugin = {
3+
const wasmLoader: LoaderPlugin = {
44
type: 'loader',
55
name: 'wasm-loader',
66
test: /.wasm$/,
7-
transform(content: Uint8Array, path: string) {
8-
return {
9-
code: `
10-
const wasmCode = new Uint8Array([${content.join(',')}])
11-
const wasmModule = new WebAssembly.Module(wasmCode)
12-
const { exports } = new WebAssembly.Instance(wasmModule)
13-
export default exports
14-
`,
15-
loader: 'js'
16-
}
17-
}
7+
transform: (content: Uint8Array, path: string) => ({
8+
code: `
9+
const wasmBytes = new Uint8Array([${content.join(',')}])
10+
const wasmModule = new WebAssembly.Module(wasmBytes)
11+
const { exports } = new WebAssembly.Instance(wasmModule)
12+
export default exports
13+
`,
14+
loader: 'js'
15+
})
1816
}
1917

2018
export default wasmLoader

plugins/wasm_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
22
import plugin from './wasm.ts'
33

44
Deno.test('project wasm loader plugin', async () => {
5-
const wasmCode = new Uint8Array([
5+
const wasmBytes = new Uint8Array([
66
0, 97, 115, 109, 1, 0, 0, 0, 1, 133, 128, 128, 128, 0, 1, 96, 0, 1, 127,
77
3, 130, 128, 128, 128, 0, 1, 0, 4, 132, 128, 128, 128, 0, 1, 112, 0, 0,
88
5, 131, 128, 128, 128, 0, 1, 0, 1, 6, 129, 128, 128, 128, 0, 0, 7, 145,
99
128, 128, 128, 0, 2, 6, 109, 101, 109, 111, 114, 121, 2, 0, 4, 109, 97,
1010
105, 110, 0, 0, 10, 138, 128, 128, 128, 0, 1, 132, 128, 128, 128, 0, 0,
1111
65, 42, 11
1212
])
13-
const { code, loader } = await plugin.transform(wasmCode, '42.wasm')
13+
const { code, loader } = await plugin.transform(wasmBytes, '42.wasm')
1414
const jsfile = (await Deno.makeTempFile()) + '.js'
1515
await Deno.writeTextFile(jsfile, code)
1616
const { default: wasm } = await import('file://' + jsfile)

types.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import type { AcceptedPlugin, bufio } from './deps.ts'
22

3-
type TransformRet = {
3+
/**
4+
* The loader plugin transform result.
5+
*/
6+
type TransformResult = {
7+
loader: 'js' | 'ts' | 'jsx' | 'tsx' | 'css' // default is 'js'
48
code: string,
59
map?: string,
6-
loader?: 'js' | 'ts' | 'jsx' | 'tsx' | 'css' // default is 'js'
710
}
811

12+
/**
13+
* A loader plugin to transform the source media.
14+
*/
915
export type LoaderPlugin = {
10-
/* `type` defines the plugin type */
16+
/** `type` defines the plugin type. */
1117
type: 'loader'
1218
/** `name` gives the plugin a name. */
1319
name: string
@@ -16,7 +22,7 @@ export type LoaderPlugin = {
1622
/** `acceptHMR` enables the HMR. */
1723
acceptHMR?: boolean
1824
/** `transform` transforms the source content. */
19-
transform(content: Uint8Array, url: string): TransformRet | Promise<TransformRet>
25+
transform(content: Uint8Array, url: string): TransformResult | Promise<TransformResult>
2026
}
2127

2228
/**

0 commit comments

Comments
 (0)