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

Commit 2341f96

Browse files
author
Wenjie Xia
committed
refactor: improve plugin typings
1 parent acec179 commit 2341f96

File tree

6 files changed

+37
-22
lines changed

6 files changed

+37
-22
lines changed

plugins/sass.ts

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

3-
const pluginFactory = (opts: Options = {}) => ({
4+
const pluginFactory = (opts: Options = {}): Plugin => ({
5+
type: 'loader',
46
name: 'sass-loader',
57
test: /.(sass|scss)$/,
68
acceptHMR: true,
@@ -19,10 +21,10 @@ const pluginFactory = (opts: Options = {}) => ({
1921
}
2022
}
2123
})
22-
2324
const defaultPlugin = pluginFactory()
2425

25-
pluginFactory.displayName = defaultPlugin.name
26+
// make the `pluginFactory` as a plugin
27+
pluginFactory.type = defaultPlugin.type
2628
pluginFactory.test = defaultPlugin.test
2729
pluginFactory.acceptHMR = defaultPlugin.acceptHMR
2830
pluginFactory.transform = defaultPlugin.transform

plugins/sass_test.ts

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

4-
Deno.test('project scss loader plugin', () => {
5-
const { code, loader } = plugin.transform(
4+
Deno.test('project scss loader plugin', async () => {
5+
const { code, loader } = await plugin.transform(
66
(new TextEncoder).encode('$someVar: 123px; .some-selector { width: $someVar; }'),
77
'test.scss'
88
)
@@ -13,13 +13,13 @@ Deno.test('project scss loader plugin', () => {
1313
assertEquals(loader, 'css')
1414
})
1515

16-
Deno.test('project sass loader plugin', () => {
17-
let ret = plugin.transform(
16+
Deno.test('project sass loader plugin', async () => {
17+
let ret = await plugin.transform(
1818
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
1919
'test.sass'
2020
)
2121
assertEquals(ret.code, '.some-selector {\n width: 123px;\n}')
22-
ret = plugin({ indentType: 'tab', indentWidth: 2 }).transform(
22+
ret = await plugin({ indentType: 'tab', indentWidth: 2 }).transform(
2323
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
2424
'test.sass'
2525
)

plugins/wasm.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
export default {
1+
import type { Plugin } from '../types.ts'
2+
3+
const wasmLoader: Plugin = {
4+
type: 'loader',
25
name: 'wasm-loader',
36
test: /.wasm$/,
47
transform(content: Uint8Array, path: string) {
@@ -13,3 +16,5 @@ export default {
1316
}
1417
}
1518
}
19+
20+
export default wasmLoader

plugins/wasm_test.ts

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

44
Deno.test('project wasm loader plugin', async () => {
55
const wasmCode = new Uint8Array([
@@ -10,7 +10,7 @@ Deno.test('project wasm loader plugin', async () => {
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 } = plugin.transform(wasmCode, '42.wasm')
13+
const { code, loader } = await plugin.transform(wasmCode, '42.wasm')
1414
const jsfile = (await Deno.makeTempFile()) + '.js'
1515
await Deno.writeTextFile(jsfile, code)
1616
const { default: wasm } = await import('file://' + jsfile)

server/app.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,10 @@ export class Appliaction {
938938
} else {
939939
for (const plugin of this.config.plugins) {
940940
if (plugin.type === 'loader' && plugin.test.test(url)) {
941-
const { code, type = 'js' } = await plugin.transform(sourceContent, url)
941+
const { code, loader: pLoader = 'js' } = await plugin.transform(sourceContent, url)
942942
sourceCode = code
943-
loader = type
944-
mod.loader = type
943+
loader = pLoader
944+
mod.loader = pLoader
945945
break
946946
}
947947
}
@@ -1000,8 +1000,8 @@ export class Appliaction {
10001000
if (type !== 'css') {
10011001
for (const plugin of this.config.plugins) {
10021002
if (plugin.type === 'loader' && plugin.test.test(`${key}.${type}`)) {
1003-
const { code, type: _type } = await plugin.transform((new TextEncoder).encode(tpl), url)
1004-
if (_type === 'css') {
1003+
const { code, loader } = await plugin.transform((new TextEncoder).encode(tpl), url)
1004+
if (loader === 'css') {
10051005
tpl = code
10061006
type = 'css'
10071007
}

types.ts

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

3+
type TransformRet = {
4+
code: string,
5+
map?: string,
6+
loader?: 'js' | 'ts' | 'jsx' | 'tsx' | 'css' // default is 'js'
7+
}
8+
39
export type LoaderPlugin = {
410
/* `type` defines the plugin type */
511
type: 'loader'
12+
/** `name` gives the plugin a name. */
13+
name: string
614
/** `test` matches the import url. */
715
test: RegExp
816
/** `acceptHMR` enables the HMR. */
917
acceptHMR?: boolean
1018
/** `transform` transforms the source content. */
11-
transform(content: Uint8Array, url: string): Promise<{ code: string, map?: string, type?: 'js' | 'ts' | 'jsx' | 'tsx' | 'css' }>
19+
transform(content: Uint8Array, url: string): TransformRet | Promise<TransformRet>
1220
}
1321

1422
/**
@@ -29,7 +37,7 @@ export type SSROptions = {
2937
}
3038

3139
/**
32-
* The Config for Aleph.js application.
40+
* The Config for **Aleph.js** application.
3341
*/
3442
export type Config = {
3543
/** `framework` to run your application (default is 'react'). */
@@ -155,6 +163,6 @@ export type FormFile = {
155163
}
156164

157165
/**
158-
* ES Import map.
166+
* The ES Import maps.
159167
*/
160168
export type ImportMap = Record<string, ReadonlyArray<string>>

0 commit comments

Comments
 (0)