Skip to content

Commit 9792a77

Browse files
authored
Merge pull request #25 from crashmax-dev/feat/override-esbuild-options
feat: overriding default esbuild transform options
2 parents 1215858 + 7630802 commit 9792a77

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

src/helpers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { transformWithEsbuild } from 'vite'
22
import { grants } from './constants.js'
33
import type { Grants, Transform } from './types.js'
4+
import type { EsbuildTransformOptions } from 'vite'
45

56
export function removeDuplicates(arr: any): any[] {
67
return [...new Set(Array.isArray(arr) ? arr : arr ? [arr] : [])]
78
}
89

9-
export async function transform({
10-
minify,
11-
file,
12-
name,
13-
loader
14-
}: Transform): Promise<string> {
10+
export async function transform(
11+
{ minify, file, name, loader }: Transform,
12+
transformOptions?: EsbuildTransformOptions
13+
): Promise<string> {
1514
const { code } = await transformWithEsbuild(file, name, {
1615
minify,
1716
loader,
1817
sourcemap: false,
19-
legalComments: 'none'
18+
legalComments: 'none',
19+
...transformOptions
2020
})
2121

2222
return code

src/index.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,15 @@ export default function UserscriptPlugin(
148148
try {
149149
let source = readFileSync(outPath, 'utf8')
150150
source = source.replace(styleTemplate, `${css.inject()}`)
151-
source = await transform({
152-
minify: !isBuildWatch,
153-
file: source,
154-
name: fileName,
155-
loader: 'js'
156-
})
151+
source = await transform(
152+
{
153+
minify: !isBuildWatch,
154+
file: source,
155+
name: fileName,
156+
loader: 'js'
157+
},
158+
config.esbuildTransformOptions
159+
)
157160

158161
config.header.grant = removeDuplicates(
159162
isBuildWatch
@@ -164,12 +167,15 @@ export default function UserscriptPlugin(
164167
if (isBuildWatch) {
165168
const wsFile = readFileSync(resolve(pluginDir, 'ws.js'), 'utf8')
166169

167-
const wsScript = await transform({
168-
minify: !isBuildWatch,
169-
file: wsFile.replace('__WS__', `ws://localhost:${port}`),
170-
name: wsPath,
171-
loader: 'js'
172-
})
170+
const wsScript = await transform(
171+
{
172+
minify: !isBuildWatch,
173+
file: wsFile.replace('__WS__', `ws://localhost:${port}`),
174+
name: wsPath,
175+
loader: 'js'
176+
},
177+
config.esbuildTransformOptions
178+
)
173179

174180
writeFileSync(wsPath, wsScript)
175181
writeFileSync(

src/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GM, GMwindow } from './constants.js'
2+
import type { EsbuildTransformOptions } from 'vite'
23

34
export interface Transform {
45
minify: boolean
@@ -217,4 +218,20 @@ export interface UserscriptPluginConfig {
217218
* Server config.
218219
*/
219220
server?: ServerConfig
221+
222+
/**
223+
* Override default esbuild transform options.
224+
*
225+
* @default
226+
* ```json
227+
* {
228+
* "minify": true,
229+
* "legalComments": "none"
230+
* }
231+
* ```
232+
*/
233+
esbuildTransformOptions?: Omit<
234+
EsbuildTransformOptions,
235+
'format' | 'target' | 'loader' | 'sourcemap'
236+
>
220237
}

0 commit comments

Comments
 (0)