vite-plugin-cogs-sdk 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # `vite-plugin-cogs-sdk`
2
+
3
+ A Vite plugin to easily setup your project to be a COGS plugin or custom content. It:
4
+
5
+ - In development mode creates an index.html which redirects to the Vite dev server with hot reloading to make development easier.
6
+ - In development and production mode ensures that the `src/cogs-plugin-manifest.js` file is copied into the correct location.
7
+ - Automatically set the dev server to be accessible over the network so Media Masters can access it (can be disabled with the `noServerExpose` option).
8
+
9
+ To use the plugin you need to have a file at `src/cogs-plugin-manifest.js` in CommonJS format.
10
+
11
+ ## Installation
12
+
13
+ Install `vite-plugin-cogs-sdk` as a development dependency using your package manager of choice:
14
+
15
+ ```bash
16
+ npm install --save-dev vite-plugin-cogs-sdk
17
+ pnpm add --save-dev vite-plugin-cogs-sdk
18
+ yarn add --dev vite-plugin-cogs-sdk
19
+ ```
20
+
21
+ You can then add the plugin to your `vite.config.js` file, adding it to the `plugins` array if you already have others:
22
+
23
+ ```javascript
24
+ import { defineConfig } from 'vite';
25
+ import cogsSdkPlugin from 'vite-plugin-cogs-sdk';
26
+
27
+ export default defineConfig({
28
+ plugins: [cogsSdkPlugin()],
29
+ });
30
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ cogsSdkPlugin: () => cogsSdkPlugin,
24
+ default: () => index_default
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_node_fs = require("fs");
28
+ var import_promises = require("fs/promises");
29
+ var import_node_path = require("path");
30
+ var PLUGIN_NAME = "cogs-sdk";
31
+ var devModeIndexHtmlContent = (serverUrl) => {
32
+ const serverUrlNoTrailingSlash = serverUrl.endsWith("/") ? serverUrl.slice(0, -1) : serverUrl;
33
+ return `<html>
34
+ <head>
35
+ <title>Redirecting...</title>
36
+ <script>
37
+ document.addEventListener('DOMContentLoaded', () => {
38
+ const parsedUrl = new URL(document.location.href);
39
+ const pathParams = new URLSearchParams(parsedUrl.searchParams);
40
+ document.location.href = \`${serverUrlNoTrailingSlash}\${parsedUrl.pathname}?\${pathParams.toString()}\`;
41
+ });
42
+ </script>
43
+ </head>
44
+ <body></body>
45
+ </html>`;
46
+ };
47
+ var INDEX_HTML_POLYFILL = ` <!-- COGS SDK Polyfill for global -->
48
+ <script>
49
+ if (global === undefined) {
50
+ var global = window;
51
+ }
52
+ if (module === undefined) {
53
+ var module = {};
54
+ }
55
+ </script>
56
+ </head>`;
57
+ var cogsSdkPlugin = (options = {}) => {
58
+ let config;
59
+ let serverUrl;
60
+ let manifestFilename, manifestFilePath;
61
+ const basePath = process.cwd();
62
+ if (options.manifestFilePath) {
63
+ manifestFilename = (0, import_node_path.basename)(options.manifestFilePath);
64
+ manifestFilePath = (0, import_node_path.resolve)(basePath, options.manifestFilePath);
65
+ } else {
66
+ manifestFilename = "cogs-plugin-manifest.js";
67
+ manifestFilePath = (0, import_node_path.join)(basePath, "src", manifestFilename);
68
+ }
69
+ if (!(0, import_node_fs.existsSync)(manifestFilePath)) {
70
+ throw new Error(`COGS Manifest file not found at ${manifestFilePath}`);
71
+ }
72
+ const generateDevModeOutput = async () => {
73
+ const outDirPath = (0, import_node_path.join)(basePath, config.build.outDir);
74
+ await (0, import_promises.rm)(outDirPath, { force: true, recursive: true });
75
+ await (0, import_promises.mkdir)(outDirPath);
76
+ await (0, import_promises.cp)(manifestFilePath, (0, import_node_path.join)(outDirPath, manifestFilename));
77
+ const publicPath = (0, import_node_path.join)(basePath, "public");
78
+ if ((0, import_node_fs.existsSync)(publicPath)) {
79
+ await (0, import_promises.cp)(publicPath, outDirPath, { recursive: true });
80
+ }
81
+ const indexHtmlContent = devModeIndexHtmlContent(serverUrl);
82
+ await (0, import_promises.writeFile)((0, import_node_path.join)(outDirPath, "index.html"), indexHtmlContent);
83
+ };
84
+ return {
85
+ name: PLUGIN_NAME,
86
+ /**
87
+ * Make sure the server is exposed to all the network so it "just works" in dev mode
88
+ */
89
+ config() {
90
+ if (options.noServerExpose) {
91
+ return {};
92
+ } else {
93
+ return { server: { host: "0.0.0.0" } };
94
+ }
95
+ },
96
+ /**
97
+ * Store the resolved config so we can access it elsewhere in the plugin code
98
+ */
99
+ configResolved(resolvedConfig) {
100
+ config = resolvedConfig;
101
+ },
102
+ /**
103
+ * When the server is configured we add a listener to get the real port value. This is because it might not match the one in the config if
104
+ * that one was not available
105
+ */
106
+ configureServer(server) {
107
+ server.httpServer?.on("listening", () => {
108
+ setImmediate(() => {
109
+ if (server.resolvedUrls?.network && server.resolvedUrls.network.length > 0) {
110
+ serverUrl = server.resolvedUrls.network[0];
111
+ } else if (server.resolvedUrls?.local && server.resolvedUrls.local.length > 0) {
112
+ serverUrl = server.resolvedUrls.local[0];
113
+ } else {
114
+ const address = server.httpServer?.address();
115
+ if (address && typeof address === "object") {
116
+ serverUrl = `http://localhost:${address.port}/`;
117
+ }
118
+ }
119
+ generateDevModeOutput();
120
+ });
121
+ });
122
+ },
123
+ /**
124
+ * When the build starts we detect if we're in "serve" mode (dev mode) and start a dev build output
125
+ */
126
+ async buildStart() {
127
+ if (config.command === "serve") {
128
+ serverUrl = `http://localhost:${config.server.port}/`;
129
+ this.addWatchFile(manifestFilePath);
130
+ await generateDevModeOutput();
131
+ }
132
+ },
133
+ /**
134
+ * When the manifest file changes in dev mode, we need to rebuild the output.
135
+ * This is only used in build mode.
136
+ */
137
+ async watchChange(path) {
138
+ if (path === manifestFilePath) {
139
+ await generateDevModeOutput();
140
+ }
141
+ },
142
+ /**
143
+ * We tell Vite to include the manifest file in the build output as an asset.
144
+ * This is only called in "build" mode when we're bundling for production
145
+ */
146
+ async generateBundle() {
147
+ const source = await (0, import_promises.readFile)(manifestFilePath);
148
+ this.emitFile({
149
+ fileName: manifestFilename,
150
+ source,
151
+ type: "asset"
152
+ });
153
+ },
154
+ /**
155
+ * We need to add a polyfill for `global` and `module` in the HTML file for the COGS SDK to correctly load
156
+ * It is used in both build and serve mode
157
+ */
158
+ transformIndexHtml(html) {
159
+ return html.replace(/<\/head>/, INDEX_HTML_POLYFILL);
160
+ }
161
+ };
162
+ };
163
+ var index_default = cogsSdkPlugin;
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ cogsSdkPlugin
167
+ });
@@ -0,0 +1,15 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface CogsPluginOptions {
4
+ /**
5
+ * Set a custom path to the COGS plugin manifest file
6
+ */
7
+ manifestFilePath?: string;
8
+ /**
9
+ * Set the `true` to not expose the dev server on all networks and only allow connections over `localhost`
10
+ */
11
+ noServerExpose?: boolean;
12
+ }
13
+ declare const cogsSdkPlugin: (options?: CogsPluginOptions) => Plugin;
14
+
15
+ export { type CogsPluginOptions, cogsSdkPlugin, cogsSdkPlugin as default };
@@ -0,0 +1,15 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface CogsPluginOptions {
4
+ /**
5
+ * Set a custom path to the COGS plugin manifest file
6
+ */
7
+ manifestFilePath?: string;
8
+ /**
9
+ * Set the `true` to not expose the dev server on all networks and only allow connections over `localhost`
10
+ */
11
+ noServerExpose?: boolean;
12
+ }
13
+ declare const cogsSdkPlugin: (options?: CogsPluginOptions) => Plugin;
14
+
15
+ export { type CogsPluginOptions, cogsSdkPlugin, cogsSdkPlugin as default };
package/dist/index.js ADDED
@@ -0,0 +1,142 @@
1
+ // src/index.ts
2
+ import { existsSync } from "fs";
3
+ import { cp, mkdir, readFile, rm, writeFile } from "fs/promises";
4
+ import { basename, join, resolve } from "path";
5
+ var PLUGIN_NAME = "cogs-sdk";
6
+ var devModeIndexHtmlContent = (serverUrl) => {
7
+ const serverUrlNoTrailingSlash = serverUrl.endsWith("/") ? serverUrl.slice(0, -1) : serverUrl;
8
+ return `<html>
9
+ <head>
10
+ <title>Redirecting...</title>
11
+ <script>
12
+ document.addEventListener('DOMContentLoaded', () => {
13
+ const parsedUrl = new URL(document.location.href);
14
+ const pathParams = new URLSearchParams(parsedUrl.searchParams);
15
+ document.location.href = \`${serverUrlNoTrailingSlash}\${parsedUrl.pathname}?\${pathParams.toString()}\`;
16
+ });
17
+ </script>
18
+ </head>
19
+ <body></body>
20
+ </html>`;
21
+ };
22
+ var INDEX_HTML_POLYFILL = ` <!-- COGS SDK Polyfill for global -->
23
+ <script>
24
+ if (global === undefined) {
25
+ var global = window;
26
+ }
27
+ if (module === undefined) {
28
+ var module = {};
29
+ }
30
+ </script>
31
+ </head>`;
32
+ var cogsSdkPlugin = (options = {}) => {
33
+ let config;
34
+ let serverUrl;
35
+ let manifestFilename, manifestFilePath;
36
+ const basePath = process.cwd();
37
+ if (options.manifestFilePath) {
38
+ manifestFilename = basename(options.manifestFilePath);
39
+ manifestFilePath = resolve(basePath, options.manifestFilePath);
40
+ } else {
41
+ manifestFilename = "cogs-plugin-manifest.js";
42
+ manifestFilePath = join(basePath, "src", manifestFilename);
43
+ }
44
+ if (!existsSync(manifestFilePath)) {
45
+ throw new Error(`COGS Manifest file not found at ${manifestFilePath}`);
46
+ }
47
+ const generateDevModeOutput = async () => {
48
+ const outDirPath = join(basePath, config.build.outDir);
49
+ await rm(outDirPath, { force: true, recursive: true });
50
+ await mkdir(outDirPath);
51
+ await cp(manifestFilePath, join(outDirPath, manifestFilename));
52
+ const publicPath = join(basePath, "public");
53
+ if (existsSync(publicPath)) {
54
+ await cp(publicPath, outDirPath, { recursive: true });
55
+ }
56
+ const indexHtmlContent = devModeIndexHtmlContent(serverUrl);
57
+ await writeFile(join(outDirPath, "index.html"), indexHtmlContent);
58
+ };
59
+ return {
60
+ name: PLUGIN_NAME,
61
+ /**
62
+ * Make sure the server is exposed to all the network so it "just works" in dev mode
63
+ */
64
+ config() {
65
+ if (options.noServerExpose) {
66
+ return {};
67
+ } else {
68
+ return { server: { host: "0.0.0.0" } };
69
+ }
70
+ },
71
+ /**
72
+ * Store the resolved config so we can access it elsewhere in the plugin code
73
+ */
74
+ configResolved(resolvedConfig) {
75
+ config = resolvedConfig;
76
+ },
77
+ /**
78
+ * When the server is configured we add a listener to get the real port value. This is because it might not match the one in the config if
79
+ * that one was not available
80
+ */
81
+ configureServer(server) {
82
+ server.httpServer?.on("listening", () => {
83
+ setImmediate(() => {
84
+ if (server.resolvedUrls?.network && server.resolvedUrls.network.length > 0) {
85
+ serverUrl = server.resolvedUrls.network[0];
86
+ } else if (server.resolvedUrls?.local && server.resolvedUrls.local.length > 0) {
87
+ serverUrl = server.resolvedUrls.local[0];
88
+ } else {
89
+ const address = server.httpServer?.address();
90
+ if (address && typeof address === "object") {
91
+ serverUrl = `http://localhost:${address.port}/`;
92
+ }
93
+ }
94
+ generateDevModeOutput();
95
+ });
96
+ });
97
+ },
98
+ /**
99
+ * When the build starts we detect if we're in "serve" mode (dev mode) and start a dev build output
100
+ */
101
+ async buildStart() {
102
+ if (config.command === "serve") {
103
+ serverUrl = `http://localhost:${config.server.port}/`;
104
+ this.addWatchFile(manifestFilePath);
105
+ await generateDevModeOutput();
106
+ }
107
+ },
108
+ /**
109
+ * When the manifest file changes in dev mode, we need to rebuild the output.
110
+ * This is only used in build mode.
111
+ */
112
+ async watchChange(path) {
113
+ if (path === manifestFilePath) {
114
+ await generateDevModeOutput();
115
+ }
116
+ },
117
+ /**
118
+ * We tell Vite to include the manifest file in the build output as an asset.
119
+ * This is only called in "build" mode when we're bundling for production
120
+ */
121
+ async generateBundle() {
122
+ const source = await readFile(manifestFilePath);
123
+ this.emitFile({
124
+ fileName: manifestFilename,
125
+ source,
126
+ type: "asset"
127
+ });
128
+ },
129
+ /**
130
+ * We need to add a polyfill for `global` and `module` in the HTML file for the COGS SDK to correctly load
131
+ * It is used in both build and serve mode
132
+ */
133
+ transformIndexHtml(html) {
134
+ return html.replace(/<\/head>/, INDEX_HTML_POLYFILL);
135
+ }
136
+ };
137
+ };
138
+ var index_default = cogsSdkPlugin;
139
+ export {
140
+ cogsSdkPlugin,
141
+ index_default as default
142
+ };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "vite-plugin-cogs-sdk",
3
+ "description": "A Vite plugin to easily setup your project to be a COGS plugin or custom content",
4
+ "author": "Clockwork Dog <info@clockwork.dog>",
5
+ "homepage": "https://github.com/clockwork-dog/cogs-sdk/tree/main/packages/vite-plugin",
6
+ "version": "2.10.0",
7
+ "keywords": [
8
+ "cogs",
9
+ "vite-plugin"
10
+ ],
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/clockwork-dog/cogs-sdk.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/clockwork-dog/cogs-sdk/issues"
18
+ },
19
+ "files": [
20
+ "dist/**/*"
21
+ ],
22
+ "type": "module",
23
+ "exports": {
24
+ ".": {
25
+ "import": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "require": {
30
+ "types": "./dist/index.d.cts",
31
+ "default": "./dist/index.cjs"
32
+ }
33
+ }
34
+ },
35
+ "scripts": {
36
+ "types": "tsc --noEmit",
37
+ "lint": "eslint",
38
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
39
+ "release": "yarn npm publish --access public"
40
+ },
41
+ "peerDependencies": {
42
+ "vite": ">=6.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "@eslint/js": "^9.30.1",
46
+ "eslint": "^9.30.1",
47
+ "eslint-config-prettier": "^10.1.5",
48
+ "eslint-plugin-prettier": "^5.5.1",
49
+ "globals": "^16.3.0",
50
+ "prettier": "^3.6.2",
51
+ "tsup": "^8.5.0",
52
+ "typescript": "~5.8.3",
53
+ "typescript-eslint": "^8.35.1",
54
+ "vite": "^6.0.0"
55
+ }
56
+ }