Skip to content

feat: overriding default esbuild transform options #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { transformWithEsbuild } from 'vite'
import { grants } from './constants.js'
import type { Grants, Transform } from './types.js'
import type { EsbuildTransformOptions } from 'vite'

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

export async function transform({
minify,
file,
name,
loader
}: Transform): Promise<string> {
export async function transform(
{ minify, file, name, loader }: Transform,
transformOptions?: EsbuildTransformOptions
): Promise<string> {
const { code } = await transformWithEsbuild(file, name, {
minify,
loader,
sourcemap: false,
legalComments: 'none'
legalComments: 'none',
...transformOptions
})

return code
Expand Down
30 changes: 18 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ export default function UserscriptPlugin(
try {
let source = readFileSync(outPath, 'utf8')
source = source.replace(styleTemplate, `${css.inject()}`)
source = await transform({
minify: !isBuildWatch,
file: source,
name: fileName,
loader: 'js'
})
source = await transform(
{
minify: !isBuildWatch,
file: source,
name: fileName,
loader: 'js'
},
config.esbuildTransformOptions
)

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

const wsScript = await transform({
minify: !isBuildWatch,
file: wsFile.replace('__WS__', `ws://localhost:${port}`),
name: wsPath,
loader: 'js'
})
const wsScript = await transform(
{
minify: !isBuildWatch,
file: wsFile.replace('__WS__', `ws://localhost:${port}`),
name: wsPath,
loader: 'js'
},
config.esbuildTransformOptions
)

writeFileSync(wsPath, wsScript)
writeFileSync(
Expand Down
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GM, GMwindow } from './constants.js'
import type { EsbuildTransformOptions } from 'vite'

export interface Transform {
minify: boolean
Expand Down Expand Up @@ -217,4 +218,20 @@ export interface UserscriptPluginConfig {
* Server config.
*/
server?: ServerConfig

/**
* Override default esbuild transform options.
*
* @default
* ```json
* {
* "minify": true,
* "legalComments": "none"
* }
* ```
*/
esbuildTransformOptions?: Omit<
EsbuildTransformOptions,
'format' | 'target' | 'loader' | 'sourcemap'
>
}