@mica-js/widget 0.1.1 → 0.2.1

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.
@@ -0,0 +1,14 @@
1
+ declare module '*?audio-worklet' {
2
+ /** URL of the self-contained `AudioWorkletProcessor` module. */
3
+ const url: string;
4
+ export default url;
5
+ }
6
+
7
+ declare module '*?audio-worklet-code' {
8
+ /**
9
+ * Self-contained, minified `AudioWorkletProcessor` script, ready to inline into a module that
10
+ * loads it from a Blob URL at runtime.
11
+ */
12
+ const code: string;
13
+ export default code;
14
+ }
package/dist/vite.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ /// <reference path="./virtual-runtime.d.ts" />
2
+ import { Plugin as Plugin_2 } from 'vite';
3
+ import { SandboxAlias } from '@mica-js/sandbox/vite';
4
+
5
+ /**
6
+ * Consumer plugin for the two worklet queries.
7
+ *
8
+ * `import url from './worklet.ts?audio-worklet'` bundles the ordinary module into one
9
+ * self-contained `AudioWorkletProcessor` script and resolves the import to its URL: development
10
+ * turns the bundle into a Blob URL, a build emits it as an asset and returns the URL the bundler
11
+ * generated for that asset, so the library never pins a root-absolute path.
12
+ *
13
+ * `import code from './worklet.ts?audio-worklet-code'` resolves to the script text instead. A
14
+ * package uses it to inline its own processor, so consumers need neither the query nor a published
15
+ * `*.worklet.js` asset. `@mica-js/widget` ships its processor this way; the queries stay public for
16
+ * projects that author their own worklet module.
17
+ */
18
+ export declare function micaWidgetPlugins(options?: WidgetPluginOptions): Plugin_2[];
19
+
20
+ /**
21
+ * File name of the worklet asset a *consumer-authored* `?audio-worklet` import emits. `base: './'`
22
+ * makes Vite reference it relative to the importing module.
23
+ */
24
+ export declare const WIDGET_AUDIO_WORKLET_FILE_NAME = "widget-audio.worklet.js";
25
+
26
+ export declare interface WidgetPluginOptions {
27
+ /**
28
+ * Aliases applied while bundling the audio worklet. The worklet usually imports the RPC core, so
29
+ * the entry must resolve workspace sources the same way the sandbox worker bundle does.
30
+ */
31
+ readonly aliases?: readonly SandboxAlias[];
32
+ }
33
+
34
+ export { }
package/dist/vite.js ADDED
@@ -0,0 +1,129 @@
1
+ import { build } from "rolldown";
2
+ //#region src/build/bundle-script.ts
3
+ /**
4
+ * Bundles one entry into a single classic script: IIFE output, no code splitting, every dependency
5
+ * inlined. A Worker and an `AudioWorkletProcessor` both run such a file without a module loader, so
6
+ * the result is asserted to be self-contained before it is published.
7
+ */
8
+ async function bundleSelfContainedScript(entry, options) {
9
+ const alias = {};
10
+ for (const item of options.aliases) alias[item.find] = item.replacement;
11
+ const result = await build({
12
+ input: entry,
13
+ external: [],
14
+ resolve: {
15
+ conditionNames: [
16
+ "source",
17
+ "import",
18
+ "browser",
19
+ "default"
20
+ ],
21
+ alias
22
+ },
23
+ output: {
24
+ format: "iife",
25
+ codeSplitting: false,
26
+ name: "MicaWidgetStandalone",
27
+ minify: options.minify
28
+ },
29
+ write: false
30
+ });
31
+ const chunk = (Array.isArray(result) ? result : [result]).flatMap((output) => output.output).find((item) => item.type === "chunk");
32
+ if (chunk === void 0 || chunk.type !== "chunk") throw new Error(`@mica-js/widget: ${options.label} produced no bundle for "${entry}"`);
33
+ return chunk.code;
34
+ }
35
+ //#endregion
36
+ //#region src/build/vite-plugin.ts
37
+ var AUDIO_WORKLET_QUERY = "?audio-worklet";
38
+ var AUDIO_WORKLET_CODE_QUERY = "?audio-worklet-code";
39
+ var AUDIO_WORKLET_PREFIX = "\0mica-widget-audio-worklet:";
40
+ var AUDIO_WORKLET_CODE_PREFIX = "\0mica-widget-audio-worklet-code:";
41
+ /**
42
+ * File name of the worklet asset a *consumer-authored* `?audio-worklet` import emits. `base: './'`
43
+ * makes Vite reference it relative to the importing module.
44
+ */
45
+ var WIDGET_AUDIO_WORKLET_FILE_NAME = "widget-audio.worklet.js";
46
+ /**
47
+ * Bundles one worklet entry, once per entry and build, into a single minified classic script.
48
+ *
49
+ * `?audio-worklet` turns the result into the URL an `AudioContext.addModule()` call loads, and
50
+ * `?audio-worklet-code` hands the script text to a package that inlines it into its own entry - the
51
+ * two queries share the bundle, so a package never ships the same processor twice.
52
+ */
53
+ function audioWorkletPlugin(explicitAliases) {
54
+ const bundles = /* @__PURE__ */ new Map();
55
+ let aliases = explicitAliases;
56
+ let command = "serve";
57
+ let assetReference;
58
+ return {
59
+ name: "mica-widget:audio-worklet",
60
+ enforce: "pre",
61
+ configResolved(config) {
62
+ command = config.command;
63
+ aliases = [...(config.resolve.alias ?? []).flatMap((alias) => typeof alias.find === "string" ? [{
64
+ find: alias.find,
65
+ replacement: alias.replacement
66
+ }] : []), ...explicitAliases];
67
+ },
68
+ async resolveId(id, importer) {
69
+ const query = id.endsWith(AUDIO_WORKLET_CODE_QUERY) ? AUDIO_WORKLET_CODE_QUERY : id.endsWith(AUDIO_WORKLET_QUERY) ? AUDIO_WORKLET_QUERY : void 0;
70
+ if (query === void 0) return null;
71
+ const resolved = await this.resolve(id.slice(0, -query.length), importer, { skipSelf: true });
72
+ if (!resolved) return null;
73
+ return query === AUDIO_WORKLET_CODE_QUERY ? `${AUDIO_WORKLET_CODE_PREFIX}${resolved.id}` : `${AUDIO_WORKLET_PREFIX}${resolved.id}`;
74
+ },
75
+ async load(id) {
76
+ const isCode = id.startsWith(AUDIO_WORKLET_CODE_PREFIX);
77
+ if (!isCode && !id.startsWith(AUDIO_WORKLET_PREFIX)) return null;
78
+ const entry = id.slice((isCode ? AUDIO_WORKLET_CODE_PREFIX : AUDIO_WORKLET_PREFIX).length);
79
+ let bundled = bundles.get(entry);
80
+ if (!bundled) {
81
+ bundled = bundleWorklet(entry, aliases, command === "build");
82
+ bundles.set(entry, bundled);
83
+ }
84
+ const source = await bundled;
85
+ if (isCode) return `export default ${JSON.stringify(source)};`;
86
+ if (command === "build") {
87
+ assetReference ??= this.emitFile({
88
+ type: "asset",
89
+ name: WIDGET_AUDIO_WORKLET_FILE_NAME,
90
+ source
91
+ });
92
+ return `export default import.meta.ROLLUP_FILE_URL_${assetReference};`;
93
+ }
94
+ return `export default URL.createObjectURL(new Blob([${JSON.stringify(source)}], { type: 'text/javascript' }));`;
95
+ }
96
+ };
97
+ }
98
+ /**
99
+ * Bundles the worklet as one classic script. Minified IIFE output keeps the script small and makes
100
+ * the self-containment assertion meaningful: a worklet is loaded through `addModule()`, which must
101
+ * not need to resolve anything else.
102
+ */
103
+ async function bundleWorklet(entry, aliases, minify) {
104
+ return await bundleSelfContainedScript(entry, {
105
+ aliases,
106
+ minify,
107
+ label: WIDGET_AUDIO_WORKLET_FILE_NAME
108
+ });
109
+ }
110
+ /**
111
+ * Consumer plugin for the two worklet queries.
112
+ *
113
+ * `import url from './worklet.ts?audio-worklet'` bundles the ordinary module into one
114
+ * self-contained `AudioWorkletProcessor` script and resolves the import to its URL: development
115
+ * turns the bundle into a Blob URL, a build emits it as an asset and returns the URL the bundler
116
+ * generated for that asset, so the library never pins a root-absolute path.
117
+ *
118
+ * `import code from './worklet.ts?audio-worklet-code'` resolves to the script text instead. A
119
+ * package uses it to inline its own processor, so consumers need neither the query nor a published
120
+ * `*.worklet.js` asset. `@mica-js/widget` ships its processor this way; the queries stay public for
121
+ * projects that author their own worklet module.
122
+ */
123
+ function micaWidgetPlugins(options = {}) {
124
+ return [audioWorkletPlugin(options.aliases ?? [])];
125
+ }
126
+ //#endregion
127
+ export { WIDGET_AUDIO_WORKLET_FILE_NAME, micaWidgetPlugins };
128
+
129
+ //# sourceMappingURL=vite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.js","names":[],"sources":["../src/build/bundle-script.ts","../src/build/vite-plugin.ts"],"sourcesContent":["import { build } from 'rolldown';\nimport type { SandboxAlias } from '@mica-js/sandbox/vite';\n\n/** One entry bundled into a single classic script. */\nexport interface SelfContainedScriptOptions {\n /** Workspace aliases applied while bundling, so the artifact never resolves a stale `dist`. */\n readonly aliases: readonly SandboxAlias[];\n /** Minify the bundle. Published standalone scripts are always minified. */\n readonly minify: boolean;\n /** Artifact name reported when the bundle still contains module syntax. */\n readonly label: string;\n}\n\n/**\n * Bundles one entry into a single classic script: IIFE output, no code splitting, every dependency\n * inlined. A Worker and an `AudioWorkletProcessor` both run such a file without a module loader, so\n * the result is asserted to be self-contained before it is published.\n */\nexport async function bundleSelfContainedScript(\n entry: string,\n options: SelfContainedScriptOptions,\n): Promise<string> {\n const alias: Record<string, string> = {};\n for (const item of options.aliases) alias[item.find] = item.replacement;\n const result = await build({\n input: entry,\n external: [],\n resolve: { conditionNames: ['source', 'import', 'browser', 'default'], alias },\n output: {\n format: 'iife',\n codeSplitting: false,\n name: 'MicaWidgetStandalone',\n minify: options.minify,\n },\n write: false,\n });\n const outputs = Array.isArray(result) ? result : [result];\n const chunk = outputs.flatMap((output) => output.output).find((item) => item.type === 'chunk');\n if (chunk === undefined || chunk.type !== 'chunk') {\n throw new Error(`@mica-js/widget: ${options.label} produced no bundle for \"${entry}\"`);\n }\n return chunk.code;\n}\n","import type { Plugin } from 'vite';\nimport type { SandboxAlias } from '@mica-js/sandbox/vite';\nimport { bundleSelfContainedScript } from './bundle-script';\n\nconst AUDIO_WORKLET_QUERY = '?audio-worklet';\nconst AUDIO_WORKLET_CODE_QUERY = '?audio-worklet-code';\nconst AUDIO_WORKLET_PREFIX = '\\0mica-widget-audio-worklet:';\nconst AUDIO_WORKLET_CODE_PREFIX = '\\0mica-widget-audio-worklet-code:';\n\n/**\n * File name of the worklet asset a *consumer-authored* `?audio-worklet` import emits. `base: './'`\n * makes Vite reference it relative to the importing module.\n */\nexport const WIDGET_AUDIO_WORKLET_FILE_NAME = 'widget-audio.worklet.js';\n\nexport interface WidgetPluginOptions {\n /**\n * Aliases applied while bundling the audio worklet. The worklet usually imports the RPC core, so\n * the entry must resolve workspace sources the same way the sandbox worker bundle does.\n */\n readonly aliases?: readonly SandboxAlias[];\n}\n\n/**\n * Bundles one worklet entry, once per entry and build, into a single minified classic script.\n *\n * `?audio-worklet` turns the result into the URL an `AudioContext.addModule()` call loads, and\n * `?audio-worklet-code` hands the script text to a package that inlines it into its own entry - the\n * two queries share the bundle, so a package never ships the same processor twice.\n */\nfunction audioWorkletPlugin(explicitAliases: readonly SandboxAlias[]): Plugin {\n const bundles = new Map<string, Promise<string>>();\n let aliases: readonly SandboxAlias[] = explicitAliases;\n let command: 'serve' | 'build' = 'serve';\n let assetReference: string | undefined;\n\n return {\n name: 'mica-widget:audio-worklet',\n enforce: 'pre',\n configResolved(config) {\n command = config.command;\n const resolved = (config.resolve.alias ?? []).flatMap((alias) =>\n typeof alias.find === 'string'\n ? [{ find: alias.find, replacement: alias.replacement }]\n : [],\n );\n aliases = [...resolved, ...explicitAliases];\n },\n async resolveId(id, importer) {\n const query = id.endsWith(AUDIO_WORKLET_CODE_QUERY)\n ? AUDIO_WORKLET_CODE_QUERY\n : id.endsWith(AUDIO_WORKLET_QUERY)\n ? AUDIO_WORKLET_QUERY\n : undefined;\n if (query === undefined) return null;\n const resolved = await this.resolve(id.slice(0, -query.length), importer, {\n skipSelf: true,\n });\n if (!resolved) return null;\n return query === AUDIO_WORKLET_CODE_QUERY\n ? `${AUDIO_WORKLET_CODE_PREFIX}${resolved.id}`\n : `${AUDIO_WORKLET_PREFIX}${resolved.id}`;\n },\n async load(id) {\n const isCode = id.startsWith(AUDIO_WORKLET_CODE_PREFIX);\n if (!isCode && !id.startsWith(AUDIO_WORKLET_PREFIX)) return null;\n const entry = id.slice((isCode ? AUDIO_WORKLET_CODE_PREFIX : AUDIO_WORKLET_PREFIX).length);\n let bundled = bundles.get(entry);\n if (!bundled) {\n bundled = bundleWorklet(entry, aliases, command === 'build');\n bundles.set(entry, bundled);\n }\n const source = await bundled;\n if (isCode) return `export default ${JSON.stringify(source)};`;\n if (command === 'build') {\n assetReference ??= this.emitFile({\n type: 'asset',\n name: WIDGET_AUDIO_WORKLET_FILE_NAME,\n source,\n });\n return `export default import.meta.ROLLUP_FILE_URL_${assetReference};`;\n }\n return `export default URL.createObjectURL(new Blob([${JSON.stringify(source)}], { type: 'text/javascript' }));`;\n },\n };\n}\n\n/**\n * Bundles the worklet as one classic script. Minified IIFE output keeps the script small and makes\n * the self-containment assertion meaningful: a worklet is loaded through `addModule()`, which must\n * not need to resolve anything else.\n */\nasync function bundleWorklet(\n entry: string,\n aliases: readonly SandboxAlias[],\n minify: boolean,\n): Promise<string> {\n const script = await bundleSelfContainedScript(entry, {\n aliases,\n minify,\n label: WIDGET_AUDIO_WORKLET_FILE_NAME,\n });\n return script;\n}\n\n/**\n * Consumer plugin for the two worklet queries.\n *\n * `import url from './worklet.ts?audio-worklet'` bundles the ordinary module into one\n * self-contained `AudioWorkletProcessor` script and resolves the import to its URL: development\n * turns the bundle into a Blob URL, a build emits it as an asset and returns the URL the bundler\n * generated for that asset, so the library never pins a root-absolute path.\n *\n * `import code from './worklet.ts?audio-worklet-code'` resolves to the script text instead. A\n * package uses it to inline its own processor, so consumers need neither the query nor a published\n * `*.worklet.js` asset. `@mica-js/widget` ships its processor this way; the queries stay public for\n * projects that author their own worklet module.\n */\nexport function micaWidgetPlugins(options: WidgetPluginOptions = {}): Plugin[] {\n return [audioWorkletPlugin(options.aliases ?? [])];\n}\n"],"mappings":";;;;;;;AAkBA,eAAsB,0BACpB,OACA,SACiB;CACjB,MAAM,QAAgC,CAAC;CACvC,KAAK,MAAM,QAAQ,QAAQ,SAAS,MAAM,KAAK,QAAQ,KAAK;CAC5D,MAAM,SAAS,MAAM,MAAM;EACzB,OAAO;EACP,UAAU,CAAC;EACX,SAAS;GAAE,gBAAgB;IAAC;IAAU;IAAU;IAAW;GAAS;GAAG;EAAM;EAC7E,QAAQ;GACN,QAAQ;GACR,eAAe;GACf,MAAM;GACN,QAAQ,QAAQ;EAClB;EACA,OAAO;CACT,CAAC;CAED,MAAM,SADU,MAAM,QAAQ,MAAM,IAAI,SAAS,CAAC,MAAM,EAAA,CAClC,SAAS,WAAW,OAAO,MAAM,CAAC,CAAC,MAAM,SAAS,KAAK,SAAS,OAAO;CAC7F,IAAI,UAAU,KAAA,KAAa,MAAM,SAAS,SACxC,MAAM,I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AAS,YAAY,OAAO;IAC3D,QAAQ,IAAI,OAAO,OAAO;GAC5B;GACA,MAAM,SAAS,MAAM;GACrB,IAAI,QAAQ,OAAO,kBAAkB,KAAK,UAAU,MAAM,EAAE;GAC5D,IAAI,YAAY,SAAS;IACvB,mBAAmB,KAAK,SAAS;KAC/B,MAAM;KACN,MAAM;KACN;IACF,CAAC;IACD,OAAO,8CAA8C,eAAe;GACtE;GACA,OAAO,gDAAgD,KAAK,UAAU,MAAM,EAAE;EAChF;CACF;AACF;;;;;;AAOA,eAAe,cACb,OACA,SACA,QACiB;CAMjB,OAAO,MALc,0BAA0B,OAAO;EACpD;EACA;EACA,OAAO;CACT,CAAC;AAEH;;;;;;;;;;;;;;AAeA,SAAgB,kBAAkB,UAA+B,CAAC,GAAa;CAC7E,OAAO,CAAC,mBAAmB,QAAQ,WAAW,CAAC,CAAC,CAAC;AACnD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mica-js/widget",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Mica widget contract and runtime: definitions, pure-data package loading, and the sandboxed WidgetHost",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -14,6 +14,10 @@
14
14
  ".": {
15
15
  "types": "./dist/index.d.ts",
16
16
  "import": "./dist/index.js"
17
+ },
18
+ "./vite": {
19
+ "types": "./dist/vite.d.ts",
20
+ "import": "./dist/vite.js"
17
21
  }
18
22
  },
19
23
  "files": [
@@ -23,11 +27,15 @@
23
27
  "access": "public"
24
28
  },
25
29
  "dependencies": {
26
- "@mica-js/core": "0.3.0",
27
- "@mica-js/sandbox": "0.2.0"
30
+ "@mica-js/core": "0.5.0",
31
+ "@mica-js/sandbox": "0.3.0",
32
+ "@mica-js/simulator": "0.4.0",
33
+ "rolldown": "^1.2.14"
34
+ },
35
+ "peerDependencies": {
36
+ "vite": "^8.0.0"
28
37
  },
29
38
  "devDependencies": {
30
- "rolldown": "^1.2.14",
31
39
  "typescript": "^6.0.3",
32
40
  "vite": "^8.3.4",
33
41
  "vite-plugin-dts": "^5.1.2",