diff --git a/rollup.config.mjs b/rollup.config.mjs index 75deee5d..447027e4 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,95 +1,61 @@ import nodeResolve from "@rollup/plugin-node-resolve"; import typescript from "@rollup/plugin-typescript"; -import { dirname, resolve } from "node:path"; +import { dirname, join, parse, resolve } from "node:path"; import url from "node:url"; const isWatching = !!process.env.ROLLUP_WATCH; -const external = ["ws", "@elgato/schemas/streamdeck/plugins"]; - -const output = { - banner: `/**! +const banner = `/**! * @author Elgato * @module elgato/streamdeck * @license MIT * @copyright Copyright (c) Corsair Memory Inc. - */`, - sourcemap: isWatching, - sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => { - return url.pathToFileURL(resolve(dirname(sourcemapPath), relativeSourcePath)).href; - }, -}; - -/** - * Generates a wrapped DTS file. - * @param {string} index File path to the index.d.ts file. - * @returns The wrapped DTS file. - */ -function dts(index) { - return `import streamDeck from "${index}"; - -export * from "${index}"; -export default streamDeck;`; -} + */`; /** - * Rollup configuration. + * Defines a rollup configuration for the specified input and output. + * @param {string} input Path to the input TypeScript file, with "src/" omitted. + * @param {string} output Path to the desired output JavaScript file, with "dist/" omitted. + * @returns Rollup configuration. */ -export default [ - /** - * Main build. - */ - { - input: "src/plugin/index.ts", +function defineConfig(input, output) { + return { + input: join("src", input), output: { - ...output, - file: `dist/index.js`, - }, - external, - plugins: [ - typescript({ - tsconfig: "src/plugin/tsconfig.build.json", - mapRoot: isWatching ? "./" : undefined, - }), - nodeResolve(), - { - name: "emit-dts", - generateBundle() { - this.emitFile({ - fileName: "index.d.ts", - source: dts("../types/plugin"), - type: "asset", - }); - }, + banner, + file: join("dist", output), + sourcemap: isWatching, + sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => { + return url.pathToFileURL(resolve(dirname(sourcemapPath), relativeSourcePath)).href; }, - ], - }, - - /** - * Browser build. - */ - { - input: "src/ui/index.ts", - output: { - ...output, - file: `dist/browser.js`, }, - external, + external: ["ws", "@elgato/schemas/streamdeck/plugins"], plugins: [ typescript({ - tsconfig: "src/ui/tsconfig.build.json", + tsconfig: join("src", dirname(input), "tsconfig.build.json"), mapRoot: isWatching ? "./" : undefined, }), nodeResolve(), { name: "emit-dts", generateBundle() { + const types = `"../types/${dirname(input)}"`; this.emitFile({ - fileName: "browser.d.ts", - source: dts("../types/ui"), + fileName: `${parse(output).name}.d.ts`, type: "asset", + source: `${banner} +import streamDeck from ${types}; + +export * from ${types}; +export default streamDeck; +`, }); }, }, ], - }, -]; + }; +} + +/** + * Rollup configuration. + */ +export default [defineConfig("plugin/index.ts", "index.js"), defineConfig("ui/index.ts", "browser.js")];