{
  "version": 3,
  "sources": ["../../../src/plugins/decorators/plugin.ts"],
  "sourceRoot": "file://",
  "sourcesContent": ["/**\n * @file Plugins - decorators\n * @module mkbuild/plugins/decorators\n */\n\nimport { DECORATOR_REGEX } from '@flex-development/decorator-regex'\nimport { EXT_DTS_REGEX, EXT_TS_REGEX } from '@flex-development/ext-regex'\nimport * as mlly from '@flex-development/mlly'\nimport pathe from '@flex-development/pathe'\nimport * as tscu from '@flex-development/tsconfig-utils'\nimport { DOT, cast, type Nullable } from '@flex-development/tutils'\nimport type {\n  BuildOptions,\n  OnLoadArgs,\n  OnLoadOptions,\n  OnLoadResult,\n  Plugin,\n  PluginBuild\n} from 'esbuild'\nimport type { URL } from 'node:url'\n\n/**\n * Plugin name.\n *\n * @const {string} PLUGIN_NAME\n */\nconst PLUGIN_NAME: string = 'decorators'\n\n/**\n * Returns a plugin that allows esbuild to handle [`emitDecoratorMetadata`][1].\n *\n * [1]: https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata\n *\n * @see https://github.com/evanw/esbuild/issues/257\n *\n * @param {tscu.LoadTsconfigOptions?} [options] - Plugin options\n * @return {Plugin} Decorator metadata plugin\n */\nconst plugin = (options?: tscu.LoadTsconfigOptions): Plugin => {\n  /**\n   * Allows esbuild to handle [`emitDecoratorMetadata`][1].\n   *\n   * [1]: https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata\n   * [2]: https://esbuild.github.io/plugins\n   * [3]: https://esbuild.github.io/api/#build-api\n   *\n   * @param {PluginBuild} build - [esbuild plugin api][2]\n   * @param {BuildOptions} build.initialOptions - [esbuild build api][3] options\n   * @param {PluginBuild['onLoad']} build.onLoad - Function called on file load\n   * @return {Promise<void>} Nothing when complete\n   */\n  const setup = async ({\n    initialOptions,\n    onLoad\n  }: PluginBuild): Promise<void> => {\n    const { absWorkingDir = DOT, tsconfig = 'tsconfig.json' } = initialOptions\n\n    /**\n     * User compiler options.\n     *\n     * @const {tscu.CompilerOptions} compilerOptions\n     */\n    const compilerOptions: tscu.CompilerOptions = tscu.loadCompilerOptions(\n      pathe.resolve(absWorkingDir, tsconfig),\n      options\n    )\n\n    // exit early if decorator metadata should not be emitted\n    if (!compilerOptions.emitDecoratorMetadata) return void 0\n\n    // inline sourcemaps\n    if (compilerOptions.sourceMap) {\n      compilerOptions.sourceMap = false\n      compilerOptions.inlineSources = true\n      compilerOptions.inlineSourceMap = true\n    }\n\n    /**\n     * TypeScript module.\n     *\n     * @const {typeof import('typescript')} ts\n     */\n    const ts: typeof import('typescript') = (await import('typescript')).default\n\n    /**\n     * {@linkcode onLoad} callback options.\n     *\n     * @const {OnLoadOptions}\n     */\n    const opts: OnLoadOptions = { filter: /.*/ }\n\n    // transpile modules containing decorators\n    onLoad(opts, async (args: OnLoadArgs): Promise<Nullable<OnLoadResult>> => {\n      /**\n       * Callback result.\n       *\n       * @var {?OnLoadResult} result\n       */\n      let result: Nullable<OnLoadResult> = null\n\n      // transpile modules, but skip typescript declaration modules\n      if (EXT_TS_REGEX.test(args.path) && !EXT_DTS_REGEX.test(args.path)) {\n        /**\n         * URL of module to load.\n         *\n         * @const {URL} url\n         */\n        const url: URL = mlly.toURL(args.path)\n\n        /**\n         * File content at {@linkcode args.path}.\n         *\n         * @const {string} source\n         */\n        const source: string = cast(await mlly.getSource(url))\n\n        // do nothing if module does not contain decorators\n        DECORATOR_REGEX.lastIndex = 0\n        if (!DECORATOR_REGEX.test(source)) return null\n\n        // transpile module to emit decorator metadata\n        const { outputText: contents } = ts.transpileModule(source, {\n          compilerOptions: tscu.normalizeCompilerOptions(compilerOptions),\n          fileName: args.path\n        })\n\n        // set result to transpiled content, loader, and plugin name\n        result = { contents, loader: 'js', pluginName: PLUGIN_NAME }\n      }\n\n      return result\n    })\n\n    return void 0\n  }\n\n  return { name: PLUGIN_NAME, setup }\n}\n\nexport default plugin\n"],
  "mappings": ";;AAKA,SAAS,uBAAuB;AAChC,SAAS,eAAe,oBAAoB;AAC5C,YAAY,UAAU;AACtB,OAAO,WAAW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