{"version":3,"file":"index.cjs","names":["path","watcher: ReturnType<typeof createWatcher> | undefined"],"sources":["../index.ts"],"sourcesContent":["import path from 'node:path';\nimport chokidar from 'chokidar';\nimport { rolldown } from 'rolldown';\nimport type { RolldownOptions } from 'rolldown';\n\ndeclare module 'karma' {\n\tinterface ConfigOptions {\n\t\trolldownPreprocessor?: Omit<RolldownOptions, 'input'> | undefined;\n\t}\n}\n\nconst hasNullByte = (string: string) => string.includes('\\u0000');\n\nconst createWatcher = (emitter: any) => {\n\tconst files = new Map();\n\tconst watch = chokidar.watch([]);\n\n\tconst refreshFile = (filePath: string) => {\n\t\t/**\n\t\t * Had to go diving for this one...\n\t\t * Not exactly Karma’s public facing API,\n\t\t * but appears to get the job done =).\n\t\t */\n\t\tconst isPOSIX = path.sep === '/';\n\t\tfilePath = isPOSIX ? filePath : filePath.replace(/\\\\/g, '/');\n\t\temitter._fileList.changeFile(filePath, true);\n\t};\n\n\tconst handleChange = (path: string) => {\n\t\tfor (const [entry, dependencies] of files.entries()) {\n\t\t\tif (entry === path || dependencies.includes(path)) {\n\t\t\t\treturn refreshFile(entry);\n\t\t\t}\n\t\t}\n\t};\n\n\t// watch.on('change', debounce(handleChange, 150))\n\twatch.on('change', handleChange);\n\n\treturn {\n\t\tadd(entry: string, dependencies: string[]) {\n\t\t\tif (!hasNullByte(entry)) {\n\t\t\t\tconst filteredDependencies = dependencies.filter((path) => !hasNullByte(path));\n\t\t\t\tfiles.set(entry, filteredDependencies);\n\t\t\t\twatch.add([entry, ...filteredDependencies]);\n\t\t\t}\n\t\t}\n\t};\n};\n\nconst createPreprocessor = (preconfig: any, config: any, emitter: any, logger: any) => {\n\tconst log = logger.create('preprocessor.rolldown');\n\n\tlet watcher: ReturnType<typeof createWatcher> | undefined;\n\tif (!config.singleRun && config.autoWatch) {\n\t\twatcher = createWatcher(emitter);\n\t}\n\n\treturn async (\n\t\toriginal: string,\n\t\tfile: any,\n\t\tdone: (error: Error | null, code: string | null) => void\n\t) => {\n\t\tconst originalPath = file.originalPath;\n\t\tconst location = path.relative(config.basePath, originalPath);\n\t\ttry {\n\t\t\tconst options = {\n\t\t\t\t...config.rolldownPreprocessor,\n\t\t\t\t...preconfig.options,\n\t\t\t\tinput: originalPath\n\t\t\t};\n\n\t\t\tlog.info('Generating bundle for ./%s', location);\n\t\t\tconst bundle = await rolldown(options);\n\t\t\tconst { output } = await bundle.generate();\n\n\t\t\tif (watcher) {\n\t\t\t\tconst watchFiles = await bundle.watchFiles;\n\t\t\t\twatcher.add(originalPath, watchFiles);\n\t\t\t}\n\n\t\t\tawait bundle.close();\n\n\t\t\tfor (const result of output) {\n\t\t\t\tif (result.type !== 'asset') {\n\t\t\t\t\tconst { code, map, facadeModuleId, fileName } = result;\n\n\t\t\t\t\t/**\n\t\t\t\t\t * Processors that have alternate source file extensions\n\t\t\t\t\t * must make sure to use the file name output by rolldown.\n\t\t\t\t\t */\n\t\t\t\t\tif (facadeModuleId && !hasNullByte(facadeModuleId)) {\n\t\t\t\t\t\tconst { dir } = path.parse(originalPath);\n\t\t\t\t\t\tfile.path = path.posix.join(dir, fileName);\n\t\t\t\t\t}\n\n\t\t\t\t\tfile.sourceMap = map;\n\n\t\t\t\t\tconst processed =\n\t\t\t\t\t\toptions.output.sourcemap === 'inline' && map\n\t\t\t\t\t\t\t? `${code}\\n//# sourceMappingURL=${map.toUrl()}\\n`\n\t\t\t\t\t\t\t: code;\n\n\t\t\t\t\treturn done(null, processed);\n\t\t\t\t}\n\t\t\t}\n\t\t\tlog.warn('Nothing was processed.');\n\t\t\tdone(null, original);\n\t\t} catch (error) {\n\t\t\t// https://github.com/jlmakes/karma-rollup-preprocessor/pull/77/files\n\t\t\tif (error instanceof Error) {\n\t\t\t\tlog.error(\n\t\t\t\t\t'Failed to process ./%s\\n%s\\n%s\\n',\n\t\t\t\t\tlocation,\n\t\t\t\t\terror.message || '\\n',\n\t\t\t\t\terror.stack\n\t\t\t\t);\n\t\t\t}\n\t\t\tdone(error as null, null);\n\t\t}\n\t};\n};\n\nexport default {\n\t'preprocessor:rolldown': [\n\t\t'factory',\n\t\t((factory: any) => {\n\t\t\tfactory.$inject = ['args', 'config', 'emitter', 'logger'];\n\t\t\treturn factory;\n\t\t})(createPreprocessor)\n\t]\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM,eAAe,WAAmB,OAAO,SAAS,KAAS;AAEjE,MAAM,iBAAiB,YAAiB;CACvC,MAAM,wBAAQ,IAAI,KAAK;CACvB,MAAM,QAAQ,iBAAS,MAAM,EAAE,CAAC;CAEhC,MAAM,eAAe,aAAqB;AAOzC,aADgBA,kBAAK,QAAQ,MACR,WAAW,SAAS,QAAQ,OAAO,IAAI;AAC5D,UAAQ,UAAU,WAAW,UAAU,KAAK;;CAG7C,MAAM,gBAAgB,WAAiB;AACtC,OAAK,MAAM,CAAC,OAAO,iBAAiB,MAAM,SAAS,CAClD,KAAI,UAAUA,UAAQ,aAAa,SAASA,OAAK,CAChD,QAAO,YAAY,MAAM;;AAM5B,OAAM,GAAG,UAAU,aAAa;AAEhC,QAAO,EACN,IAAI,OAAe,cAAwB;AAC1C,MAAI,CAAC,YAAY,MAAM,EAAE;GACxB,MAAM,uBAAuB,aAAa,QAAQ,WAAS,CAAC,YAAYA,OAAK,CAAC;AAC9E,SAAM,IAAI,OAAO,qBAAqB;AACtC,SAAM,IAAI,CAAC,OAAO,GAAG,qBAAqB,CAAC;;IAG7C;;AAGF,MAAM,sBAAsB,WAAgB,QAAa,SAAc,WAAgB;CACtF,MAAM,MAAM,OAAO,OAAO,wBAAwB;CAElD,IAAIC;AACJ,KAAI,CAAC,OAAO,aAAa,OAAO,UAC/B,WAAU,cAAc,QAAQ;AAGjC,QAAO,OACN,UACA,MACA,SACI;EACJ,MAAM,eAAe,KAAK;EAC1B,MAAM,WAAWD,kBAAK,SAAS,OAAO,UAAU,aAAa;AAC7D,MAAI;GACH,MAAM,UAAU;IACf,GAAG,OAAO;IACV,GAAG,UAA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