All files / bundle / ts / bundle.ts

97.96% Branches 48/49
100.00% Functions 3/3
100.00% Lines 77/77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
 
 
 
 
 
 
 
x6
x6
x6
x6
x6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x6
x22
x22
x2
x2
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x22
x21
x22
x20
x20
x22
x1
x22
x22
 
x22
x22
x22
x19
x19
x22
x2
x1
x1
x1
x1
x2
x2
x22
x20
x20
x21
x22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x6
x2
x2
x2
x2
x6
x4
x4
x2
x6
x1
x1
x1
x1
x1
x2
x2
x2
x2


















































I

































































































































/**
 * Bundle and transpile TypeScript to JavaScript.
 * @module
 */

// Imports
import type { Nullable } from "@libs/typing"
import { denoLoaderPlugin, denoResolverPlugin } from "@luca/esbuild-deno-loader"
import { encodeBase64 } from "@std/encoding/base64"
import { minify as terser } from "terser"
import { fromFileUrl } from "@std/path/from-file-url"
import { delay } from "@std/async/delay"

/**
 * Bundle and transpile TypeScript to JavaScript.
 *
 * Minification can be either:
 * - `terser` for advanced minification through {@link https://terser.org | Terser}
 * - `basic` for basic minification through {@link https://github.com/evanw/esbuild | esbuild}
 *
 * A banner option can be provided to prepend a comment to the output, which can be useful for licensing information.
 *
 * Use the `shadow` option to replace the local URLs (using `file://` scheme) with a shadow url to avoid exposing real paths.
 *
 * ```ts ignore
 * // From file
 * import { bundle } from "./bundle.ts"
 * console.log(await bundle(new URL(import.meta.url)))
 * ```
 * ```ts ignore
 * // From file and config
 * import { bundle } from "./bundle.ts"
 * console.log(await bundle(new URL(import.meta.url), { config: new URL("deno.jsonc", import.meta.url) }))
 * ```
 * ```ts
 * // From string
 * import { bundle } from "./bundle.ts"
 * console.log(await bundle(`console.log("Hello world")`))
 * ```
 */
export async function bundle(input: URL | string, { builder = "binary", minify = "terser", format = "esm", debug = false, banner = "", shadow = true, config, lockfile, exports, raw, overrides } = {} as options): Promise<string> {
  const esbuild = builder === "wasm" ? await import("../vendored/esbuild/wasm.js") : await import("esbuild")
  if (builder === "wasm") {
    await esbuild.initialize({ worker: false })
  }
  const url = input instanceof URL ? input : new URL(`data:application/typescript;base64,${encodeBase64(input)}`)
  let code = ""
  try {
    const { outputFiles: [{ text: output }] } = await esbuild.build({
      plugins: [
        overrides?.imports ? overridesImports({ imports: overrides.imports }) : null,
        denoResolverPlugin({ configPath: config ? fromFileUrl(config) : undefined }),
        denoLoaderPlugin({ configPath: config ? fromFileUrl(config) : undefined, lockPath: lockfile ? fromFileUrl(lockfile) : undefined }),
      ].filter((plugin): plugin is Plugin => Boolean(plugin)),
      entryPoints: [url.href],
      format,
      globalName: exports,
      write: false,
      minify: minify === "basic",
      target: "esnext",
      treeShaking: true,
      sourcemap: debug ? "inline" : false,
      sourcesContent: debug,
      bundle: true,
      logLevel: "silent",
      ...raw,
    })
    code = output
    if (minify) {
      code = code.trim()
    }
  } catch (error) {
    throw new TypeError(`Failed to bundle ts:\n${(error as Error).message}`)
  } finally {
    await esbuild.stop()
    // TODO(@lowlighter): remove after https://github.com/evanw/esbuild/pull/3701
    await delay(500)
  }
  if (minify === "terser") {
    code = await terser(code, { format: { comments: false }, module: true, sourceMap: debug ? { url: "inline" } : false }).then((response) => response.code!)
  }
  if (banner) {
    if (banner.includes("\n")) {
      banner = `/**\n${banner.split("\n").map((line) => ` * ${line}`).join("\n")}\n */`
    } else {
      banner = `// ${banner}`
    }
    code = `${banner}\n${code}`
  }
  if (shadow) {
    code = code.replaceAll(/(["'])(?<scheme>file:\/\/).*?\/(?<name>[A-Za-z0-9_]+\.(?:ts|js|mjs))\1/g, "'$<scheme>/shadow/$<name>'")
  }
  return code
}

/** Bundle options. */
export type options = {
  /**
   * The builder version to use.
   *
   * It can be either:
   * - `binary` to use the binary runtime (faster but requires permissions to run)
   * - `wasm` to use the WASM runtime (slower but more portable)
   */
  builder?: "binary" | "wasm"
  /**
   * Minify the output.
   *
   * It can be either:
   * - `false` to disable minification
   * - `basic` for basic minification using {@link https://esbuild.github.io | esbuild}
   * - `terser` for advanced minification through {@link https://terser.org | Terser}
   */
  minify?: false | "basic" | "terser"
  /** Output format. */
  format?: "esm" | "iife"
  /** Global exports. */
  exports?: string
  /** Enable debug and source map. */
  debug?: boolean
  /**
   * Path to the config file.
   *
   * It is advised to leave this option empty as the deno plugin now resolves the configuration automatically.
   */
  config?: URL
  /** Path to the lockfile. */
  lockfile?: URL
  /**
   * Banner to prepend to the output, useful for licensing information.
   *
   * It is automatically formatted as a comment.
   */
  banner?: string
  /** Replace local URLs with shadow URLs. */
  shadow?: boolean
  /**
   * Raw options to esbuild.
   *
   * These options can also be used to override the default behavior of the bundler.
   * Note that these options are excluded from the breaking changes policy.
   *
   * @see {@link https://esbuild.github.io/api/#build-api | esbuild} for more information.
   */
  raw?: Record<PropertyKey, unknown>
  /** Overrides. */
  overrides?: {
    /**
     * Override imports.
     *
     * Can be used to replace imports with custom values before deno resolution.
     * The key is the matching import path (as it appears in the source code) and the value is what it should be replaced with.
     */
    imports?: Record<string, string>
  }
}

/** Esbuild plugin. */
type Plugin = {
  name: string
  setup: (build: { onResolve: (options: { filter: RegExp }, callback: (args: { path: string }) => Nullable<{ path: string; namespace: string }>) => void }) => void
}

/** Override imports. */
function overridesImports(options: { imports: NonNullable<NonNullable<options["overrides"]>["imports"]> }): Plugin {
  return ({
    name: "libs-bundler-overrides-imports",
    setup(build) {
      build.onResolve({ filter: /.*/ }, (args) => {
        if (!(args.path in options.imports)) {
          return null
        }
        const url = new URL(options.imports[args.path])
        if (url.protocol === "file:") {
          return { path: fromFileUrl(url), namespace: "file" }
        }
        const namespace = url.protocol.slice(0, -1)
        const path = url.href.slice(namespace.length + 1)
        return { path, namespace }
      })
    },
  }) as Plugin
}