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

Commit 9a209f2

Browse files
author
Wenjie Xia
committed
refactor(plugin): update LoaderPlugin interface
1 parent 9073538 commit 9a209f2

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

plugins/sass.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import type { LoaderPlugin } from '../types.ts'
33

44
const pluginFactory = (opts: Options = {}): LoaderPlugin => ({
55
type: 'loader',
6-
loader: 'css',
76
test: /.(sass|scss)$/,
87
acceptHMR: true,
9-
precompile(content: Uint8Array, path: string) {
8+
transform(content: Uint8Array, path: string) {
109
const ret = renderSync({
1110
indentedSyntax: path.endsWith('.sass'),
1211
...opts,
@@ -17,15 +16,15 @@ const pluginFactory = (opts: Options = {}): LoaderPlugin => ({
1716
return {
1817
code: (new TextDecoder).decode(ret.css),
1918
map: ret.map ? (new TextDecoder).decode(ret.map) : undefined,
19+
format: 'css',
2020
}
2121
}
2222
})
2323

2424
// make the `pluginFactory` function as a plugin
2525
const defaultPlugin = pluginFactory()
26-
pluginFactory.loader = defaultPlugin.loader
2726
pluginFactory.test = defaultPlugin.test
2827
pluginFactory.acceptHMR = defaultPlugin.acceptHMR
29-
pluginFactory.precompile = defaultPlugin.precompile
28+
pluginFactory.transform = defaultPlugin.transform
3029

3130
export default pluginFactory

plugins/sass_test.ts

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

44
Deno.test('project scss loader plugin', async () => {
5-
const { code } = await plugin.precompile!(
5+
const { code, format } = await plugin.transform(
66
(new TextEncoder).encode('$someVar: 123px; .some-selector { width: $someVar; }'),
77
'test.scss'
88
)
99
assertEquals(plugin.test.test('test.sass'), true)
1010
assertEquals(plugin.test.test('test.scss'), true)
11-
assertEquals(plugin.loader, 'css')
1211
assertEquals(plugin.acceptHMR, true)
1312
assertEquals(code, '.some-selector {\n width: 123px;\n}')
13+
assertEquals(format, 'css')
1414
})
1515

1616
Deno.test('project sass loader plugin', async () => {
17-
let ret = await plugin.precompile!(
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 = await plugin({ indentType: 'tab', indentWidth: 2 }).precompile!(
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
)
2626
assertEquals(ret.code, '.some-selector {\n\t\twidth: 123px;\n}')
27+
assertEquals(ret.format, 'css')
2728
})

plugins/wasm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import type { LoaderPlugin } from '../types.ts'
22

33
const wasmLoader: LoaderPlugin = {
44
type: 'loader',
5-
loader: 'js',
65
test: /.wasm$/,
7-
precompile: (content: Uint8Array, path: string) => ({
6+
transform: (content: Uint8Array, path: string) => ({
87
code: `
98
const wasmBytes = new Uint8Array([${content.join(',')}])
109
const wasmModule = new WebAssembly.Module(wasmBytes)
1110
const { exports } = new WebAssembly.Instance(wasmModule)
1211
export default exports
1312
`,
13+
format: 'js'
1414
})
1515
}
1616

plugins/wasm_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ 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 } = await plugin.precompile!(wasmBytes, '42.wasm')
13+
const { code, format } = 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)
1717
assertEquals(plugin.test.test('test.wasm'), true)
18-
assertEquals(plugin.loader, 'js')
18+
assertEquals(format, 'js')
1919
assertEquals(wasm.main(), 42)
2020
})

types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { AcceptedPlugin, bufio } from './deps.ts'
66
export type TransformResult = {
77
code: string,
88
map?: string,
9+
format?: 'js' | 'ts' | 'jsx' | 'tsx' | 'css',
910
}
1011

1112
/**
@@ -14,14 +15,12 @@ export type TransformResult = {
1415
export type LoaderPlugin = {
1516
/** `type` specifies the plugin type. */
1617
type: 'loader'
17-
/** `loader` specifies the loader. */
18-
loader: 'js' | 'ts' | 'jsx' | 'tsx' | 'css'
1918
/** `test` matches the import url. */
2019
test: RegExp
2120
/** `acceptHMR` enables the HMR. */
2221
acceptHMR?: boolean
2322
/** `transform` transforms the source content. */
24-
precompile?(content: Uint8Array, url: string): TransformResult | Promise<TransformResult>
23+
transform(content: Uint8Array, url: string): TransformResult | Promise<TransformResult>
2524
}
2625

2726
/**

0 commit comments

Comments
 (0)