{"version":3,"file":"minify-html.cjs","names":["lib"],"sources":["../src/minify-html.ts"],"sourcesContent":["import type { MinifyOptions } from './lib/index_bg';\nimport * as lib from './lib/index_bg';\n\nconst { __wbg_set_wasm, minify: wasmMinify, ...__wbg } = lib;\n\nconst imports = { './index_bg.js': __wbg };\n\nexport type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;\n\nexport type SyncInitInput = WebAssembly.Module;\n\nasync function loadWasm(input: InitInput, imports: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource> {\n  let source: Response | BufferSource | WebAssembly.Module = input;\n\n  if (\n    typeof source === 'string' ||\n    (typeof Request === 'function' && source instanceof Request) ||\n    (typeof URL === 'function' && source instanceof URL)\n  ) {\n    source = await fetch(source);\n  }\n\n  if (typeof Response === 'function' && source instanceof Response) {\n    if (typeof WebAssembly.instantiateStreaming === 'function') {\n      try {\n        return await WebAssembly.instantiateStreaming(source, imports);\n      } catch (e) {\n        if (source.headers.get('Content-Type') !== 'application/wasm') {\n          console.warn(\n            '`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\\n',\n            e,\n          );\n        }\n      }\n    }\n\n    const bytes = await source.arrayBuffer();\n    return await WebAssembly.instantiate(bytes, imports);\n  }\n\n  const instantiated = (await WebAssembly.instantiate(source, imports)) as WebAssembly.Instance | WebAssembly.WebAssemblyInstantiatedSource;\n\n  if (instantiated instanceof WebAssembly.Instance) {\n    return { instance: instantiated, module: source as WebAssembly.Module };\n  }\n\n  return instantiated;\n}\n\nasync function initAsync(input: InitInput | Promise<InitInput>) {\n  const { instance } = await loadWasm(await input, imports);\n  __wbg_set_wasm(instance.exports);\n}\n\nfunction initSync(input: SyncInitInput) {\n  const instance = new WebAssembly.Instance(input, imports);\n  __wbg_set_wasm(instance.exports);\n}\n\n/** Initializes minify-html asynchronously */\nexport async function initMinifyHTML(input: InitInput | Promise<InitInput>): Promise<void> {\n  if (initMinifyHTML.initialized) {\n    throw new Error('(@cf-wasm/minify-html): Function already called. The `initMinifyHTML()` function can be used only once.');\n  }\n  if (!input) {\n    throw new Error('(@cf-wasm/minify-html): Argument `input` is not valid.');\n  }\n  initMinifyHTML.initialized = true;\n  initMinifyHTML.promise = (async () => {\n    await initAsync(input);\n    initMinifyHTML.ready = true;\n  })();\n  return initMinifyHTML.promise;\n}\n\n/** Initializes minify-html synchronously */\ninitMinifyHTML.sync = (input: SyncInitInput): void => {\n  if (initMinifyHTML.initialized) {\n    throw new Error('(@cf-wasm/minify-html): Function already called. The `initMinifyHTML()` function can be used only once.');\n  }\n  if (!input) {\n    throw new Error('(@cf-wasm/minify-html): Argument `input` is not valid.');\n  }\n  initMinifyHTML.initialized = true;\n  initSync(input);\n  initMinifyHTML.promise = Promise.resolve(undefined);\n  initMinifyHTML.ready = true;\n};\n\ninitMinifyHTML.promise = null as Promise<void> | null;\n/** Indicates whether minify-html is initialized */\ninitMinifyHTML.initialized = false;\n/** Indicates whether minify-html is ready */\ninitMinifyHTML.ready = false;\n\n/** Ensures minify-html is ready */\ninitMinifyHTML.ensure = async () => {\n  if (!initMinifyHTML.promise) {\n    throw new Error('(@cf-wasm/minify-html): Function not called. Call `initMinifyHTML()` function first.');\n  }\n  return initMinifyHTML.promise;\n};\n\n/** Minifies UTF-8 HTML code, represented as an array of bytes.\n *\n * @param code A slice of bytes representing the source code to minify.\n * @param options Configuration object to adjust minification approach.\n *\n * @example\n * ```ts\n * import { minify, type MinifyOptions } from \"@cf-wasm/minify/workerd\";\n *\n * const encoder = new TextEncoder();\n * const decoder = new TextDecoder();\n *\n * const code = encoder.encode(\"<p>  Hello, world!  </p>\");\n * const options: MinifyOptions = {\n *   keep_comments: true\n * };\n *\n * const minified = decoder.decode(minify(code, options)); // minified === \"<p>Hello, world!\"\n * ```\n */\nexport function minify(code: Uint8Array, options: MinifyOptions = {}) {\n  if (!initMinifyHTML.ready) {\n    if (initMinifyHTML.initialized) {\n      throw new Error(\n        '(@cf-wasm/minify-html): minify-html is not yet ready while `initMinifyHTML()` function was called. Use `minify.async()` async method instead to ensure minify-html is ready.',\n      );\n    }\n    throw new Error('(@cf-wasm/minify-html): minify-html is not yet initialized. Call `initMinifyHTML()` function first.');\n  }\n\n  return wasmMinify(code, options);\n}\n\nminify.async = async (code: Uint8Array, options: MinifyOptions = {}) => {\n  await initMinifyHTML.ensure();\n  return wasmMinify(code, options);\n};\n\nexport type { MinifyOptions };\n"],"mappings":";AAGA,MAAM,EAAE,gBAAgB,QAAQ,YAAY,GAAG,uCAAUA,CAAAA;AAEzD,MAAM,UAAU,EAAE,iBAAiB,OAAO;AAM1C,eAAe,SAAS,OAAkB,SAAkF;CAC1H,IAAI,SAAuD;AAE3D,KACE,OAAO,WAAW,YACjB,OAAO,YAAY,cAAc,kBAAkB,WACnD,OAAO,QAAQ,cAAc,kBAAkB,IAEhD,UAAS,MAAM,MAAM,OAAO;AAG9B,KAAI,OAAO,aAAa,cAAc,kBAAkB,UAAU;AAChE,MAAI,OAAO,YAAY,yBAAyB,WAC9C,KAAI;AACF,UAAO,MAAM,YAAY,qBAAqB,QAAQ,QAAQ;WACvD,GAAG;AACV,OAAI,OAAO,QAAQ,IAAI,eAAe,KAAK,mBACzC,SAAQ,KACN,qMACA,EACD;;EAKP,MAAM,QAAQ,MAAM,OAAO,aAAa;AACxC,SAAO,MAAM,YAAY,YAAY,OAAO,QAAQ;;CAGtD,MAAM,eAAgB,MAAM,YAAY,YAAY,QAAQ,QAAQ;AAEpE,KAAI,wBAAwB,YAAY,SACtC,QAAO;EAAE,UAAU;EAAc,QAAQ;EAA8B;AAGzE,QAAO;;AAGT,eAAe,UAAU,OAAuC;CAC9D,MAAM,EAAE,aAAa,MAAM,SAAS,MAAM,OAAO,QAAQ;AACzD,gBAAe,SAAS,QAAQ;;AAGlC,SAAS,SAAS,OAAsB;AAEtC,gBAAe,IADM,YAAY,SAAS,OAAO,QAC1B,CAAC,QAAQ;;;AAIlC,eAA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