diff --git a/.eslintrc b/.eslintrc index 0015c597..57b89a9a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -11,7 +11,8 @@ "ignorePatterns": [ ".github/", "dist/", - "node_modules/" + "node_modules/", + "types/" ], "parser": "@typescript-eslint/parser", "plugins": [ diff --git a/.gitignore b/.gitignore index 20b545e6..cdeec038 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ node_modules/ coverage/ # Build output -dist/ \ No newline at end of file +dist/ +types/ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a0537c84..fa01889d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,6 @@ "jest-websocket-mock": "^2.5.0", "prettier": "^3.0.3", "rollup": "^4.0.2", - "rollup-plugin-dts": "^6.1.0", "typescript": "^5.2.2" }, "engines": { @@ -7470,40 +7469,6 @@ "fsevents": "~2.3.2" } }, - "node_modules/rollup-plugin-dts": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/rollup-plugin-dts/-/rollup-plugin-dts-6.1.0.tgz", - "integrity": "sha512-ijSCPICkRMDKDLBK9torss07+8dl9UpY9z1N/zTeA1cIqdzMlpkV3MOOC7zukyvQfDyxa1s3Dl2+DeiP/G6DOw==", - "dev": true, - "dependencies": { - "magic-string": "^0.30.4" - }, - "engines": { - "node": ">=16" - }, - "funding": { - "url": "https://github.com/sponsors/Swatinem" - }, - "optionalDependencies": { - "@babel/code-frame": "^7.22.13" - }, - "peerDependencies": { - "rollup": "^3.29.4 || ^4", - "typescript": "^4.5 || ^5.0" - } - }, - "node_modules/rollup-plugin-dts/node_modules/magic-string": { - "version": "0.30.4", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.4.tgz", - "integrity": "sha512-Q/TKtsC5BPm0kGqgBIF9oXAs/xEf2vRKiIB4wCRQTJOQIByZ1d+NnUOotvJOvNpi5RNIgVOMC3pOuaP1ZTDlVg==", - "dev": true, - "dependencies": { - "@jridgewell/sourcemap-codec": "^1.4.15" - }, - "engines": { - "node": ">=12" - } - }, "node_modules/run-applescript": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/run-applescript/-/run-applescript-5.0.0.tgz", diff --git a/package.json b/package.json index 79220715..37434f09 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ }, "files": [ "./dist/*.js", - "./dist/*.d.ts" + "./dist/*.d.ts", + "./types/**/*d.ts" ], "exports": { ".": { @@ -24,8 +25,9 @@ } }, "scripts": { - "build": "rm -rf ./dist && rollup --config rollup.config.ts --configPlugin typescript", - "watch": "rollup --config rollup.config.ts --configPlugin typescript --watch", + "build": "rm -rf ./dist && rollup --config rollup.config.mjs && npm run types", + "watch": "rollup --config rollup.config.mjs --watch --watch.onEnd=\"npm run types\"", + "types": "rm -rf ./types && mkdir types && cp -r ./dist/types ./", "lint": "eslint . --ext .ts --max-warnings 0", "lint:fix": "prettier --config .prettierrc src/**/*.ts --write", "preversion": "npm run build && npm test && npm run lint", @@ -76,7 +78,6 @@ "jest-websocket-mock": "^2.5.0", "prettier": "^3.0.3", "rollup": "^4.0.2", - "rollup-plugin-dts": "^6.1.0", "typescript": "^5.2.2" }, "dependencies": { diff --git a/rollup.config.mjs b/rollup.config.mjs new file mode 100644 index 00000000..8f5df4cf --- /dev/null +++ b/rollup.config.mjs @@ -0,0 +1,95 @@ +import nodeResolve from "@rollup/plugin-node-resolve"; +import typescript from "@rollup/plugin-typescript"; +import { dirname, 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: `/**! + * @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. + */ +export default [ + /** + * Main build. + */ + { + input: "src/plugin/index.ts", + 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" + }); + } + } + ] + }, + + /** + * Browser build. + */ + { + input: "src/ui/index.ts", + output: { + ...output, + file: `dist/browser.js` + }, + external, + plugins: [ + typescript({ + tsconfig: "src/ui/tsconfig.build.json", + mapRoot: isWatching ? "./" : undefined + }), + nodeResolve(), + { + name: "emit-dts", + generateBundle() { + this.emitFile({ + fileName: "browser.d.ts", + source: dts("../types/ui"), + type: "asset" + }); + } + } + ] + } +]; diff --git a/rollup.config.ts b/rollup.config.ts deleted file mode 100644 index 4aca106b..00000000 --- a/rollup.config.ts +++ /dev/null @@ -1,91 +0,0 @@ -import nodeResolve from "@rollup/plugin-node-resolve"; -import typescript from "@rollup/plugin-typescript"; -import { dirname, join, parse, resolve } from "node:path"; -import url from "node:url"; -import { NormalizedOutputOptions, OutputBundle, RollupOptions } from "rollup"; -import dts from "rollup-plugin-dts"; - -const isWatching = !!process.env.ROLLUP_WATCH; -const banner = `/**! - * @author Elgato - * @module elgato/streamdeck - * @license MIT - * @copyright Copyright (c) Corsair Memory Inc. - */`; - -/** - * Gets the rollup configuration for the specified {@link input}. - * @param input Partial path to the input. - * @param output Name of the output file. - * @returns Rollup configuration for the specified input. - */ -function getOptions(input: string, output: string): RollupOptions[] { - const outputFileWithoutExtension = join("dist", `${parse(output).name}`); - - return [ - /** - * Main build. - */ - { - input, - output: { - file: `${outputFileWithoutExtension}.js`, - banner, - sourcemap: isWatching, - sourcemapPathTransform: (relativeSourcePath: string, sourcemapPath: string): string => { - return url.pathToFileURL(resolve(dirname(sourcemapPath), relativeSourcePath)).href; - } - }, - external: ["ws", "@elgato/schemas/streamdeck/plugins"], - plugins: [ - typescript({ - tsconfig: join(dirname(input), "tsconfig.json"), - mapRoot: isWatching ? "./" : undefined - }), - nodeResolve() - ] - }, - /** - * TypeScript declarations. - */ - { - input, - output: { - file: `${outputFileWithoutExtension}.d.ts` - }, - external: ["@elgato/schemas/streamdeck/plugins"], - plugins: [ - dts(), - { - name: "NameChecker", - generateBundle: function (options: NormalizedOutputOptions, bundle: OutputBundle): void { - // Search each file for variable names that resemble possible duplicates, e.g. ActionEvent$1, Event$1. - const warnings = new Set( - Object.values(bundle).reduce((names, file) => ("code" in file ? [...names, ...(file.code.match(/[A-Za-z0-9\-_]+?\$\d/gm) || [])] : names), []) - ); - - // And warn for each possible duplicate. - if (warnings.size) { - warnings.forEach((value) => this.warn(`Type was renamed "${value}"`)); - } - } - } - ] - } - ]; -} - -/** - * Provides declarations for Stream Deck API related types. - */ -const api = { - input: "src/api/index.ts", - output: { - banner, - file: "dist/api.d.ts" - }, - external: ["@elgato/schemas/streamdeck/plugins"], - plugins: [dts()] -}; - -export default [...getOptions("src/plugin/index.ts", "index.js"), ...getOptions("src/ui/index.ts", "browser.js"), api]; diff --git a/src/plugin/tsconfig.json b/src/plugin/tsconfig.build.json similarity index 59% rename from src/plugin/tsconfig.json rename to src/plugin/tsconfig.build.json index b5cf254e..27f3a3b3 100644 --- a/src/plugin/tsconfig.json +++ b/src/plugin/tsconfig.build.json @@ -1,5 +1,9 @@ { "extends": "../../tsconfig.json", + "compilerOptions": { + "declaration": true, + "declarationDir": "../../dist/types" + }, "exclude": [ "../../tests", "../ui/", diff --git a/src/ui/tsconfig.build.json b/src/ui/tsconfig.build.json new file mode 100644 index 00000000..7f7f9b4c --- /dev/null +++ b/src/ui/tsconfig.build.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "declaration": true, + "declarationDir": "../../dist/types" + }, + "exclude": [ + "**/__mocks__/", + "**/__tests__/" + ] +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 1bf0b6a8..ad8d9439 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,6 @@ }, "include": [ "src/", - "rollup.config.ts", "tests/" ], "exclude": [