@sdeverywhere/plugin-worker 0.2.3 → 0.2.5

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/dist/index.cjs CHANGED
@@ -34,50 +34,9 @@ var import_vite = require("vite");
34
34
  // src/vite-config.ts
35
35
  var import_path = require("path");
36
36
  var import_url = require("url");
37
-
38
- // src/var-names.ts
39
- function sdeNameForVensimName(name) {
40
- return "_" + name.trim().replace(/"/g, "_").replace(/\s+!$/g, "!").replace(/\s/g, "_").replace(/,/g, "_").replace(/-/g, "_").replace(/\./g, "_").replace(/\$/g, "_").replace(/'/g, "_").replace(/&/g, "_").replace(/%/g, "_").replace(/\//g, "_").replace(/\|/g, "_").toLowerCase();
41
- }
42
- function sdeNameForVensimVarName(varName) {
43
- const m = varName.match(/([^[]+)(?:\[([^\]]+)\])?/);
44
- if (!m) {
45
- throw new Error(`Invalid Vensim name: ${varName}`);
46
- }
47
- let id = sdeNameForVensimName(m[1]);
48
- if (m[2]) {
49
- const subscripts = m[2].split(",").map((x) => sdeNameForVensimName(x));
50
- id += `[${subscripts.join("][")}]`;
51
- }
52
- return id;
53
- }
54
-
55
- // src/vite-config.ts
56
37
  var __filename2 = (0, import_url.fileURLToPath)(importMetaUrl);
57
38
  var __dirname = (0, import_path.dirname)(__filename2);
58
- function injectModelSpec(modelSpec) {
59
- const outputVarIds = modelSpec.outputs.map((o) => sdeNameForVensimVarName(o.varName));
60
- const moduleSrc = `
61
- export const numInputs = ${modelSpec.inputs.length};
62
- export const outputVarIds = ${JSON.stringify(outputVarIds)};
63
- `;
64
- const virtualModuleId = "virtual:model-spec";
65
- const resolvedVirtualModuleId = "\0" + virtualModuleId;
66
- return {
67
- name: "vite-plugin-virtual-custom",
68
- resolveId(id) {
69
- if (id === virtualModuleId) {
70
- return resolvedVirtualModuleId;
71
- }
72
- },
73
- load(id) {
74
- if (id === resolvedVirtualModuleId) {
75
- return moduleSrc;
76
- }
77
- }
78
- };
79
- }
80
- function createViteConfig(stagedModelDir, modelJsFile, modelSpec, outputFile) {
39
+ function createViteConfig(stagedModelDir, modelJsFile, outputFile) {
81
40
  const root = stagedModelDir;
82
41
  const entry = (0, import_path.resolve)(__dirname, "..", "template-worker", "worker.js");
83
42
  return {
@@ -93,10 +52,10 @@ function createViteConfig(stagedModelDir, modelJsFile, modelSpec, outputFile) {
93
52
  // Configure path aliases
94
53
  resolve: {
95
54
  alias: [
96
- // In the template, we use `@_wasm_` as an alias for the JS-wrapped Wasm
97
- // file generated by Emscripten
55
+ // In the template, we use `@_generatedModelFile_` as an alias for the model
56
+ // file containing the generated JS or Wasm model
98
57
  {
99
- find: "@_wasm_",
58
+ find: "@_generatedModelFile_",
100
59
  replacement: (0, import_path.resolve)(stagedModelDir, modelJsFile)
101
60
  }
102
61
  ],
@@ -110,10 +69,6 @@ function createViteConfig(stagedModelDir, modelJsFile, modelSpec, outputFile) {
110
69
  // be safe to use this workaround for a while.
111
70
  browserField: false
112
71
  },
113
- plugins: [
114
- // Use a virtual module plugin to inject the model spec values
115
- injectModelSpec(modelSpec)
116
- ],
117
72
  build: {
118
73
  // Write output file to the `staged/model` directory; note that this path is
119
74
  // relative to the bundle `root` directory
@@ -144,13 +99,13 @@ var WorkerPlugin = class {
144
99
  constructor(options) {
145
100
  this.options = options;
146
101
  }
147
- async postGenerate(context, modelSpec) {
102
+ async postGenerate(context) {
148
103
  const log = context.log;
149
104
  log("info", "Building worker");
150
105
  const prepDir = context.config.prepDir;
151
106
  const srcDir = "model";
152
107
  const stagedModelDir = (0, import_path2.join)(prepDir, "staged", srcDir);
153
- const inModelJsFile = "wasm-model.js";
108
+ const inModelJsFile = "generated-model.js";
154
109
  const outWorkerJsFile = "worker.js";
155
110
  const outputPaths = this.options?.outputPaths || [(0, import_path2.join)(prepDir, outWorkerJsFile)];
156
111
  for (const outputPath of outputPaths) {
@@ -158,7 +113,7 @@ var WorkerPlugin = class {
158
113
  const dstFile = (0, import_path2.basename)(outputPath);
159
114
  context.prepareStagedFile(srcDir, outWorkerJsFile, dstDir, dstFile);
160
115
  }
161
- const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, modelSpec, outWorkerJsFile);
116
+ const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, outWorkerJsFile);
162
117
  await (0, import_vite.build)(viteConfig);
163
118
  return true;
164
119
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../../../node_modules/.pnpm/tsup@7.2.0_typescript@5.2.2/node_modules/tsup/assets/cjs_shims.js","../src/plugin.ts","../src/vite-config.ts","../src/var-names.ts"],"sourcesContent":["// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nexport type { WorkerPluginOptions } from './options'\nexport { workerPlugin } from './plugin'\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL('file:' + __filename).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { basename, dirname, join as joinPath } from 'path'\n\nimport { build } from 'vite'\n\nimport type { BuildContext, ModelSpec, Plugin } from '@sdeverywhere/build'\n\nimport type { WorkerPluginOptions } from './options'\nimport { createViteConfig } from './vite-config'\n\nexport function workerPlugin(options?: WorkerPluginOptions): Plugin {\n return new WorkerPlugin(options)\n}\n\nclass WorkerPlugin implements Plugin {\n constructor(private readonly options?: WorkerPluginOptions) {}\n\n async postGenerate(context: BuildContext, modelSpec: ModelSpec): Promise<boolean> {\n const log = context.log\n log('info', 'Building worker')\n\n // Locate the input (model JS/Wasm) file in the staged directory. Note\n // that this relies on the `plugin-wasm` package writing a file called\n // `wasm-model.js` to the `staged/model` directory.\n const prepDir = context.config.prepDir\n const srcDir = 'model'\n const stagedModelDir = joinPath(prepDir, 'staged', srcDir)\n const inModelJsFile = 'wasm-model.js'\n const outWorkerJsFile = 'worker.js'\n\n // If `outputPaths` is undefined, write the `worker.js` to the prep dir\n const outputPaths = this.options?.outputPaths || [joinPath(prepDir, outWorkerJsFile)]\n\n // Add staged file entries; this will cause the generated worker to be copied\n // to the configured output paths during the \"copy staged files\" step\n // TODO: This assumes that other plugins that use the generated worker files\n // will run after the \"copy staged files\" step. There might be use cases\n // where a plugin needs access to these in `postGenerate`, in which case the\n // files will not have been copied already. Need to reconsider the ordering\n // of plugins and the \"copy staged files\" step(s).\n for (const outputPath of outputPaths) {\n const dstDir = dirname(outputPath)\n const dstFile = basename(outputPath)\n context.prepareStagedFile(srcDir, outWorkerJsFile, dstDir, dstFile)\n }\n\n // Build the worker and write generated file to the `staged/model` directory\n const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, modelSpec, outWorkerJsFile)\n await build(viteConfig)\n\n // log('info', 'Done!')\n\n return true\n }\n}\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { dirname, resolve as resolvePath } from 'path'\nimport { fileURLToPath } from 'url'\n\nimport type { InlineConfig, Plugin as VitePlugin } from 'vite'\n\nimport type { ModelSpec } from '@sdeverywhere/build'\n\nimport { sdeNameForVensimVarName } from './var-names'\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = dirname(__filename)\n\n/**\n * This is a virtual module plugin used to inject model-specific configuration\n * values into the generated worker bundle.\n *\n * This follows the \"Virtual Modules Convention\" described here:\n * https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention\n *\n * TODO: This could be simplified by using `vite-plugin-virtual` but that\n * doesn't seem to be working correctly in an ESM setting\n */\nfunction injectModelSpec(modelSpec: ModelSpec): VitePlugin {\n const outputVarIds = modelSpec.outputs.map(o => sdeNameForVensimVarName(o.varName))\n\n const moduleSrc = `\nexport const numInputs = ${modelSpec.inputs.length};\nexport const outputVarIds = ${JSON.stringify(outputVarIds)};\n`\n\n const virtualModuleId = 'virtual:model-spec'\n const resolvedVirtualModuleId = '\\0' + virtualModuleId\n\n return {\n name: 'vite-plugin-virtual-custom',\n resolveId(id: string) {\n if (id === virtualModuleId) {\n return resolvedVirtualModuleId\n }\n },\n load(id: string) {\n if (id === resolvedVirtualModuleId) {\n return moduleSrc\n }\n }\n }\n}\n\n/**\n * Create a Vite `InlineConfig` that can be used to build a complete\n * worker bundle that can run the generated Wasm model in a separate\n * Web Worker or Node worker thread.\n *\n * @param stagedModelDir The `staged` directory under the `sde-prep` directory.\n * @param modelJsFile The name of the JS file containing the embedded Wasm.\n * @param modelSpec The model spec generated earlier in the build process.\n * @param outputFile The name of the generated worker JS file.\n * @return An `InlineConfig` instance that can be passed to Vite's `build` function.\n */\nexport function createViteConfig(\n stagedModelDir: string,\n modelJsFile: string,\n modelSpec: ModelSpec,\n outputFile: string\n): InlineConfig {\n // Use `staged/model` as the root directory for the worker build\n const root = stagedModelDir\n\n // Use `template-worker/worker.js` from this package as the entry file\n const entry = resolvePath(__dirname, '..', 'template-worker', 'worker.js')\n\n return {\n // Don't use an external config file\n configFile: false,\n\n // Use the root directory configured above\n root,\n\n // Don't clear the screen in dev mode so that we can see builder output\n clearScreen: false,\n\n // Disable vite output by default\n // TODO: Re-enable logging if `--verbose` option is used?\n logLevel: 'silent',\n\n // Configure path aliases\n resolve: {\n alias: [\n // In the template, we use `@_wasm_` as an alias for the JS-wrapped Wasm\n // file generated by Emscripten\n {\n find: '@_wasm_',\n replacement: resolvePath(stagedModelDir, modelJsFile)\n }\n ],\n\n // XXX: Prevent Vite from using the `browser` section of `threads/package.json`\n // since we want to force the use of the general module (under dist) that chooses\n // the correct implementation (Web Worker vs worker_threads) at runtime. This\n // gets the job done, but is fragile because it applies to all dependencies even\n // though we really only need this workaround for the threads package. Fortunately\n // the worker template is very simple (only depends on `@sdeverywhere/runtime-async`,\n // which in turn only depends on `@sdeverywhere/runtime` and `threads`, so we should\n // be safe to use this workaround for a while.\n browserField: false\n },\n\n plugins: [\n // Use a virtual module plugin to inject the model spec values\n injectModelSpec(modelSpec)\n ],\n\n build: {\n // Write output file to the `staged/model` directory; note that this path is\n // relative to the bundle `root` directory\n outDir: '.',\n emptyOutDir: false,\n\n lib: {\n entry,\n name: 'worker',\n formats: ['iife'],\n fileName: () => outputFile\n },\n\n rollupOptions: {\n onwarn: (warning, warn) => {\n // XXX: Suppress \"Use of eval is strongly discouraged\" warnings that are\n // triggered by use of the following pattern in threads.js:\n // eval(\"require\")(\"worker_threads\")\n // It would be nice to avoid use of `eval` there, but it's not critical for\n // our use case so we will suppress the warnings for now\n if (warning.code !== 'EVAL') {\n warn(warning)\n }\n }\n }\n }\n }\n}\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\n/**\n * Helper function that converts a Vensim variable or subscript name\n * into a valid C identifier as used by SDE.\n * TODO: Import helper function from `compile` package instead\n */\nfunction sdeNameForVensimName(name: string): string {\n return (\n '_' +\n name\n .trim()\n .replace(/\"/g, '_')\n .replace(/\\s+!$/g, '!')\n .replace(/\\s/g, '_')\n .replace(/,/g, '_')\n .replace(/-/g, '_')\n .replace(/\\./g, '_')\n .replace(/\\$/g, '_')\n .replace(/'/g, '_')\n .replace(/&/g, '_')\n .replace(/%/g, '_')\n .replace(/\\//g, '_')\n .replace(/\\|/g, '_')\n .toLowerCase()\n )\n}\n\n/**\n * Helper function that converts a Vensim variable name (possibly containing\n * subscripts) into a valid C identifier as used by SDE.\n * TODO: Import helper function from `compile` package instead\n */\nexport function sdeNameForVensimVarName(varName: string): string {\n const m = varName.match(/([^[]+)(?:\\[([^\\]]+)\\])?/)\n if (!m) {\n throw new Error(`Invalid Vensim name: ${varName}`)\n }\n let id = sdeNameForVensimName(m[1])\n if (m[2]) {\n const subscripts = m[2].split(',').map(x => sdeNameForVensimName(x))\n id += `[${subscripts.join('][')}]`\n }\n\n return id\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAA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,YAA6B,SAA+B;AAA/B;AAAA,EAAgC;AAAA,EAE7D,MAAM,aAAa,SAAuB,WAAwC;AAChF,UAAM,MAAM,QAAQ;AACpB,QAAI,QAAQ,iBAAiB;AAK7B,UAAM,UAAU,QAAQ,OAAO;AAC/B,UAAM,SAAS;AACf,UAAM,qBAAiB,aAAAC,MAAS,SAAS,UAAU,MAAM;AACzD,UAAM,gBAAgB;AACtB,UAAM,kBAAkB;AAGxB,UAAM,cAAc,KAAK,SAAS,eAAe,KAAC,aAAAA,MAAS,SAAS,eAAe,CAAC;AASpF,eAAW,cAAc,aAAa;AACpC,YAAM,aAAS,sBAAQ,UAAU;AACjC,YAAM,cAAU,uBAAS,UAAU;AACnC,cAAQ,kBAAkB,QAAQ,iBAAiB,QAAQ,OAAO;AAAA,IACpE;AAGA,UAAM,aAAa,iBAAiB,gBAAgB,eAAe,WAAW,eAAe;AAC7F,cAAM,mBAAM,UAAU;AAItB,WAAO;AAAA,EACT;AACF;","names":["import_path","__filename","resolvePath","joinPath"]}
1
+ {"version":3,"sources":["../src/index.ts","../../../node_modules/.pnpm/tsup@7.2.0_typescript@5.2.2/node_modules/tsup/assets/cjs_shims.js","../src/plugin.ts","../src/vite-config.ts"],"sourcesContent":["// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nexport type { WorkerPluginOptions } from './options'\nexport { workerPlugin } from './plugin'\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL('file:' + __filename).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { basename, dirname, join as joinPath } from 'path'\n\nimport { build } from 'vite'\n\nimport type { BuildContext, Plugin } from '@sdeverywhere/build'\n\nimport type { WorkerPluginOptions } from './options'\nimport { createViteConfig } from './vite-config'\n\nexport function workerPlugin(options?: WorkerPluginOptions): Plugin {\n return new WorkerPlugin(options)\n}\n\nclass WorkerPlugin implements Plugin {\n constructor(private readonly options?: WorkerPluginOptions) {}\n\n async postGenerate(context: BuildContext): Promise<boolean> {\n const log = context.log\n log('info', 'Building worker')\n\n // Locate the generated model file in the staged directory. Note that this\n // relies on the `build` package writing the JS model (or the `plugin-wasm`\n // package writing the Wasm model) to a file called `generated-model.js`\n // in the `staged/model` directory.\n // TODO: Allow for customizing the file name/path\n const prepDir = context.config.prepDir\n const srcDir = 'model'\n const stagedModelDir = joinPath(prepDir, 'staged', srcDir)\n const inModelJsFile = 'generated-model.js'\n const outWorkerJsFile = 'worker.js'\n\n // If `outputPaths` is undefined, write the `worker.js` to the prep dir\n const outputPaths = this.options?.outputPaths || [joinPath(prepDir, outWorkerJsFile)]\n\n // Add staged file entries; this will cause the generated worker to be copied\n // to the configured output paths during the \"copy staged files\" step\n // TODO: This assumes that other plugins that use the generated worker files\n // will run after the \"copy staged files\" step. There might be use cases\n // where a plugin needs access to these in `postGenerate`, in which case the\n // files will not have been copied already. Need to reconsider the ordering\n // of plugins and the \"copy staged files\" step(s).\n for (const outputPath of outputPaths) {\n const dstDir = dirname(outputPath)\n const dstFile = basename(outputPath)\n context.prepareStagedFile(srcDir, outWorkerJsFile, dstDir, dstFile)\n }\n\n // Build the worker and write generated file to the `staged/model` directory\n const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, outWorkerJsFile)\n await build(viteConfig)\n\n // log('info', 'Done!')\n\n return true\n }\n}\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { dirname, resolve as resolvePath } from 'path'\nimport { fileURLToPath } from 'url'\n\nimport type { InlineConfig } from 'vite'\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = dirname(__filename)\n\n/**\n * Create a Vite `InlineConfig` that can be used to build a complete\n * worker bundle that can run the generated Wasm model in a separate\n * Web Worker or Node worker thread.\n *\n * @param stagedModelDir The `staged` directory under the `sde-prep` directory.\n * @param modelJsFile The name of the JS file containing the generated JS or Wasm model.\n * @param outputFile The name of the generated worker JS file.\n * @return An `InlineConfig` instance that can be passed to Vite's `build` function.\n */\nexport function createViteConfig(stagedModelDir: string, modelJsFile: string, outputFile: string): InlineConfig {\n // Use `staged/model` as the root directory for the worker build\n const root = stagedModelDir\n\n // Use `template-worker/worker.js` from this package as the entry file\n const entry = resolvePath(__dirname, '..', 'template-worker', 'worker.js')\n\n return {\n // Don't use an external config file\n configFile: false,\n\n // Use the root directory configured above\n root,\n\n // Don't clear the screen in dev mode so that we can see builder output\n clearScreen: false,\n\n // Disable vite output by default\n // TODO: Re-enable logging if `--verbose` option is used?\n logLevel: 'silent',\n\n // Configure path aliases\n resolve: {\n alias: [\n // In the template, we use `@_generatedModelFile_` as an alias for the model\n // file containing the generated JS or Wasm model\n {\n find: '@_generatedModelFile_',\n replacement: resolvePath(stagedModelDir, modelJsFile)\n }\n ],\n\n // XXX: Prevent Vite from using the `browser` section of `threads/package.json`\n // since we want to force the use of the general module (under dist) that chooses\n // the correct implementation (Web Worker vs worker_threads) at runtime. This\n // gets the job done, but is fragile because it applies to all dependencies even\n // though we really only need this workaround for the threads package. Fortunately\n // the worker template is very simple (only depends on `@sdeverywhere/runtime-async`,\n // which in turn only depends on `@sdeverywhere/runtime` and `threads`, so we should\n // be safe to use this workaround for a while.\n browserField: false\n },\n\n build: {\n // Write output file to the `staged/model` directory; note that this path is\n // relative to the bundle `root` directory\n outDir: '.',\n emptyOutDir: false,\n\n lib: {\n entry,\n name: 'worker',\n formats: ['iife'],\n fileName: () => outputFile\n },\n\n rollupOptions: {\n onwarn: (warning, warn) => {\n // XXX: Suppress \"Use of eval is strongly discouraged\" warnings that are\n // triggered by use of the following pattern in threads.js:\n // eval(\"require\")(\"worker_threads\")\n // It would be nice to avoid use of `eval` there, but it's not critical for\n // our use case so we will suppress the warnings for now\n if (warning.code !== 'EVA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D/EO,SAAS,aAAa,SAAuC;AAClE,SAAO,IAAI,aAAa,OAAO;AACjC;AAEA,IAAM,eAAN,MAAqC;AAAA,EACnC,YAA6B,SAA+B;AAA/B;AAAA,EAAgC;AAAA,EAE7D,MAAM,aAAa,SAAyC;AAC1D,UAAM,MAAM,QAAQ;AACpB,QAAI,QAAQ,iBAAiB;AAO7B,UAAM,UAAU,QAAQ,OAAO;AAC/B,UAAM,SAAS;AACf,UAAM,qBAAiB,aAAAC,MAAS,SAAS,UAAU,MAAM;AACzD,UAAM,gBAAgB;AACtB,UAAM,kBAAkB;AAGxB,UAAM,cAAc,KAAK,SAAS,eAAe,KAAC,aAAAA,MAAS,SAAS,eAAe,CAAC;AASpF,eAAW,cAAc,aAAa;AACpC,YAAM,aAAS,sBAAQ,UAAU;AACjC,YAAM,cAAU,uBAAS,UAAU;AACnC,cAAQ,kBAAkB,QAAQ,iBAAiB,QAAQ,OAAO;AAAA,IACpE;AAGA,UAAM,aAAa,iBAAiB,gBAAgB,eAAe,eAAe;AAClF,cAAM,mBAAM,UAAU;AAItB,WAAO;AAAA,EACT;AACF;","names":["import_path","__filename","resolvePath","joinPath"]}
package/dist/index.js CHANGED
@@ -5,52 +5,11 @@ import { build } from "vite";
5
5
  // src/vite-config.ts
6
6
  import { dirname, resolve as resolvePath } from "path";
7
7
  import { fileURLToPath } from "url";
8
-
9
- // src/var-names.ts
10
- function sdeNameForVensimName(name) {
11
- return "_" + name.trim().replace(/"/g, "_").replace(/\s+!$/g, "!").replace(/\s/g, "_").replace(/,/g, "_").replace(/-/g, "_").replace(/\./g, "_").replace(/\$/g, "_").replace(/'/g, "_").replace(/&/g, "_").replace(/%/g, "_").replace(/\//g, "_").replace(/\|/g, "_").toLowerCase();
12
- }
13
- function sdeNameForVensimVarName(varName) {
14
- const m = varName.match(/([^[]+)(?:\[([^\]]+)\])?/);
15
- if (!m) {
16
- throw new Error(`Invalid Vensim name: ${varName}`);
17
- }
18
- let id = sdeNameForVensimName(m[1]);
19
- if (m[2]) {
20
- const subscripts = m[2].split(",").map((x) => sdeNameForVensimName(x));
21
- id += `[${subscripts.join("][")}]`;
22
- }
23
- return id;
24
- }
25
-
26
- // src/vite-config.ts
27
- var __filename2 = fileURLToPath(import.meta.url);
28
- var __dirname2 = dirname(__filename2);
29
- function injectModelSpec(modelSpec) {
30
- const outputVarIds = modelSpec.outputs.map((o) => sdeNameForVensimVarName(o.varName));
31
- const moduleSrc = `
32
- export const numInputs = ${modelSpec.inputs.length};
33
- export const outputVarIds = ${JSON.stringify(outputVarIds)};
34
- `;
35
- const virtualModuleId = "virtual:model-spec";
36
- const resolvedVirtualModuleId = "\0" + virtualModuleId;
37
- return {
38
- name: "vite-plugin-virtual-custom",
39
- resolveId(id) {
40
- if (id === virtualModuleId) {
41
- return resolvedVirtualModuleId;
42
- }
43
- },
44
- load(id) {
45
- if (id === resolvedVirtualModuleId) {
46
- return moduleSrc;
47
- }
48
- }
49
- };
50
- }
51
- function createViteConfig(stagedModelDir, modelJsFile, modelSpec, outputFile) {
8
+ var __filename = fileURLToPath(import.meta.url);
9
+ var __dirname = dirname(__filename);
10
+ function createViteConfig(stagedModelDir, modelJsFile, outputFile) {
52
11
  const root = stagedModelDir;
53
- const entry = resolvePath(__dirname2, "..", "template-worker", "worker.js");
12
+ const entry = resolvePath(__dirname, "..", "template-worker", "worker.js");
54
13
  return {
55
14
  // Don't use an external config file
56
15
  configFile: false,
@@ -64,10 +23,10 @@ function createViteConfig(stagedModelDir, modelJsFile, modelSpec, outputFile) {
64
23
  // Configure path aliases
65
24
  resolve: {
66
25
  alias: [
67
- // In the template, we use `@_wasm_` as an alias for the JS-wrapped Wasm
68
- // file generated by Emscripten
26
+ // In the template, we use `@_generatedModelFile_` as an alias for the model
27
+ // file containing the generated JS or Wasm model
69
28
  {
70
- find: "@_wasm_",
29
+ find: "@_generatedModelFile_",
71
30
  replacement: resolvePath(stagedModelDir, modelJsFile)
72
31
  }
73
32
  ],
@@ -81,10 +40,6 @@ function createViteConfig(stagedModelDir, modelJsFile, modelSpec, outputFile) {
81
40
  // be safe to use this workaround for a while.
82
41
  browserField: false
83
42
  },
84
- plugins: [
85
- // Use a virtual module plugin to inject the model spec values
86
- injectModelSpec(modelSpec)
87
- ],
88
43
  build: {
89
44
  // Write output file to the `staged/model` directory; note that this path is
90
45
  // relative to the bundle `root` directory
@@ -115,13 +70,13 @@ var WorkerPlugin = class {
115
70
  constructor(options) {
116
71
  this.options = options;
117
72
  }
118
- async postGenerate(context, modelSpec) {
73
+ async postGenerate(context) {
119
74
  const log = context.log;
120
75
  log("info", "Building worker");
121
76
  const prepDir = context.config.prepDir;
122
77
  const srcDir = "model";
123
78
  const stagedModelDir = joinPath(prepDir, "staged", srcDir);
124
- const inModelJsFile = "wasm-model.js";
79
+ const inModelJsFile = "generated-model.js";
125
80
  const outWorkerJsFile = "worker.js";
126
81
  const outputPaths = this.options?.outputPaths || [joinPath(prepDir, outWorkerJsFile)];
127
82
  for (const outputPath of outputPaths) {
@@ -129,7 +84,7 @@ var WorkerPlugin = class {
129
84
  const dstFile = basename(outputPath);
130
85
  context.prepareStagedFile(srcDir, outWorkerJsFile, dstDir, dstFile);
131
86
  }
132
- const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, modelSpec, outWorkerJsFile);
87
+ const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, outWorkerJsFile);
133
88
  await build(viteConfig);
134
89
  return true;
135
90
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/plugin.ts","../src/vite-config.ts","../src/var-names.ts"],"sourcesContent":["// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { basename, dirname, join as joinPath } from 'path'\n\nimport { build } from 'vite'\n\nimport type { BuildContext, ModelSpec, Plugin } from '@sdeverywhere/build'\n\nimport type { WorkerPluginOptions } from './options'\nimport { createViteConfig } from './vite-config'\n\nexport function workerPlugin(options?: WorkerPluginOptions): Plugin {\n return new WorkerPlugin(options)\n}\n\nclass WorkerPlugin implements Plugin {\n constructor(private readonly options?: WorkerPluginOptions) {}\n\n async postGenerate(context: BuildContext, modelSpec: ModelSpec): Promise<boolean> {\n const log = context.log\n log('info', 'Building worker')\n\n // Locate the input (model JS/Wasm) file in the staged directory. Note\n // that this relies on the `plugin-wasm` package writing a file called\n // `wasm-model.js` to the `staged/model` directory.\n const prepDir = context.config.prepDir\n const srcDir = 'model'\n const stagedModelDir = joinPath(prepDir, 'staged', srcDir)\n const inModelJsFile = 'wasm-model.js'\n const outWorkerJsFile = 'worker.js'\n\n // If `outputPaths` is undefined, write the `worker.js` to the prep dir\n const outputPaths = this.options?.outputPaths || [joinPath(prepDir, outWorkerJsFile)]\n\n // Add staged file entries; this will cause the generated worker to be copied\n // to the configured output paths during the \"copy staged files\" step\n // TODO: This assumes that other plugins that use the generated worker files\n // will run after the \"copy staged files\" step. There might be use cases\n // where a plugin needs access to these in `postGenerate`, in which case the\n // files will not have been copied already. Need to reconsider the ordering\n // of plugins and the \"copy staged files\" step(s).\n for (const outputPath of outputPaths) {\n const dstDir = dirname(outputPath)\n const dstFile = basename(outputPath)\n context.prepareStagedFile(srcDir, outWorkerJsFile, dstDir, dstFile)\n }\n\n // Build the worker and write generated file to the `staged/model` directory\n const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, modelSpec, outWorkerJsFile)\n await build(viteConfig)\n\n // log('info', 'Done!')\n\n return true\n }\n}\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { dirname, resolve as resolvePath } from 'path'\nimport { fileURLToPath } from 'url'\n\nimport type { InlineConfig, Plugin as VitePlugin } from 'vite'\n\nimport type { ModelSpec } from '@sdeverywhere/build'\n\nimport { sdeNameForVensimVarName } from './var-names'\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = dirname(__filename)\n\n/**\n * This is a virtual module plugin used to inject model-specific configuration\n * values into the generated worker bundle.\n *\n * This follows the \"Virtual Modules Convention\" described here:\n * https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention\n *\n * TODO: This could be simplified by using `vite-plugin-virtual` but that\n * doesn't seem to be working correctly in an ESM setting\n */\nfunction injectModelSpec(modelSpec: ModelSpec): VitePlugin {\n const outputVarIds = modelSpec.outputs.map(o => sdeNameForVensimVarName(o.varName))\n\n const moduleSrc = `\nexport const numInputs = ${modelSpec.inputs.length};\nexport const outputVarIds = ${JSON.stringify(outputVarIds)};\n`\n\n const virtualModuleId = 'virtual:model-spec'\n const resolvedVirtualModuleId = '\\0' + virtualModuleId\n\n return {\n name: 'vite-plugin-virtual-custom',\n resolveId(id: string) {\n if (id === virtualModuleId) {\n return resolvedVirtualModuleId\n }\n },\n load(id: string) {\n if (id === resolvedVirtualModuleId) {\n return moduleSrc\n }\n }\n }\n}\n\n/**\n * Create a Vite `InlineConfig` that can be used to build a complete\n * worker bundle that can run the generated Wasm model in a separate\n * Web Worker or Node worker thread.\n *\n * @param stagedModelDir The `staged` directory under the `sde-prep` directory.\n * @param modelJsFile The name of the JS file containing the embedded Wasm.\n * @param modelSpec The model spec generated earlier in the build process.\n * @param outputFile The name of the generated worker JS file.\n * @return An `InlineConfig` instance that can be passed to Vite's `build` function.\n */\nexport function createViteConfig(\n stagedModelDir: string,\n modelJsFile: string,\n modelSpec: ModelSpec,\n outputFile: string\n): InlineConfig {\n // Use `staged/model` as the root directory for the worker build\n const root = stagedModelDir\n\n // Use `template-worker/worker.js` from this package as the entry file\n const entry = resolvePath(__dirname, '..', 'template-worker', 'worker.js')\n\n return {\n // Don't use an external config file\n configFile: false,\n\n // Use the root directory configured above\n root,\n\n // Don't clear the screen in dev mode so that we can see builder output\n clearScreen: false,\n\n // Disable vite output by default\n // TODO: Re-enable logging if `--verbose` option is used?\n logLevel: 'silent',\n\n // Configure path aliases\n resolve: {\n alias: [\n // In the template, we use `@_wasm_` as an alias for the JS-wrapped Wasm\n // file generated by Emscripten\n {\n find: '@_wasm_',\n replacement: resolvePath(stagedModelDir, modelJsFile)\n }\n ],\n\n // XXX: Prevent Vite from using the `browser` section of `threads/package.json`\n // since we want to force the use of the general module (under dist) that chooses\n // the correct implementation (Web Worker vs worker_threads) at runtime. This\n // gets the job done, but is fragile because it applies to all dependencies even\n // though we really only need this workaround for the threads package. Fortunately\n // the worker template is very simple (only depends on `@sdeverywhere/runtime-async`,\n // which in turn only depends on `@sdeverywhere/runtime` and `threads`, so we should\n // be safe to use this workaround for a while.\n browserField: false\n },\n\n plugins: [\n // Use a virtual module plugin to inject the model spec values\n injectModelSpec(modelSpec)\n ],\n\n build: {\n // Write output file to the `staged/model` directory; note that this path is\n // relative to the bundle `root` directory\n outDir: '.',\n emptyOutDir: false,\n\n lib: {\n entry,\n name: 'worker',\n formats: ['iife'],\n fileName: () => outputFile\n },\n\n rollupOptions: {\n onwarn: (warning, warn) => {\n // XXX: Suppress \"Use of eval is strongly discouraged\" warnings that are\n // triggered by use of the following pattern in threads.js:\n // eval(\"require\")(\"worker_threads\")\n // It would be nice to avoid use of `eval` there, but it's not critical for\n // our use case so we will suppress the warnings for now\n if (warning.code !== 'EVAL') {\n warn(warning)\n }\n }\n }\n }\n }\n}\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\n/**\n * Helper function that converts a Vensim variable or subscript name\n * into a valid C identifier as used by SDE.\n * TODO: Import helper function from `compile` package instead\n */\nfunction sdeNameForVensimName(name: string): string {\n return (\n '_' +\n name\n .trim()\n .replace(/\"/g, '_')\n .replace(/\\s+!$/g, '!')\n .replace(/\\s/g, '_')\n .replace(/,/g, '_')\n .replace(/-/g, '_')\n .replace(/\\./g, '_')\n .replace(/\\$/g, '_')\n .replace(/'/g, '_')\n .replace(/&/g, '_')\n .replace(/%/g, '_')\n .replace(/\\//g, '_')\n .replace(/\\|/g, '_')\n .toLowerCase()\n )\n}\n\n/**\n * Helper function that converts a Vensim variable name (possibly containing\n * subscripts) into a valid C identifier as used by SDE.\n * TODO: Import helper function from `compile` package instead\n */\nexport function sdeNameForVensimVarName(varName: string): string {\n const m = varName.match(/([^[]+)(?:\\[([^\\]]+)\\])?/)\n if (!m) {\n throw new Error(`Invalid Vensim name: ${varName}`)\n }\n let id = sdeNameForVensimName(m[1])\n if (m[2]) {\n const subscripts = m[2].split(',').map(x => sdeNameForVensimName(x))\n id += `[${subscripts.join('][')}]`\n }\n\n return id\n}\n"],"mappings":";AAEA,SAAS,UAAU,WAAAA,UAAS,QAAQ,gBAAgB;AAEpD,SAAS,aAAa;;;ACFtB,SAAS,SAAS,WAAW,mBAAmB;AAChD,SAAS,qBAAqB;;;ACI9B,SAAS,qBAAqB,MAAsB;AAClD,SACE,MACA,KACG,KAAK,EACL,QAAQ,MAAM,GAAG,EACjB,QAAQ,UAAU,GAAG,EACrB,QAAQ,OAAO,GAAG,EAClB,QAAQ,MAAM,GAAG,EACjB,QAAQ,MAAM,GAAG,EACjB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,MAAM,GAAG,EACjB,QAAQ,MAAM,GAAG,EACjB,QAAQ,MAAM,GAAG,EACjB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,YAAY;AAEnB;AAOO,SAAS,wBAAwB,SAAyB;AAC/D,QAAM,IAAI,QAAQ,MAAM,0BAA0B;AAClD,MAAI,CAAC,GAAG;AACN,UAAM,IAAI,MAAM,wBAAwB,OAAO,EAAE;AAAA,EACnD;AACA,MAAI,KAAK,qBAAqB,EAAE,CAAC,CAAC;AAClC,MAAI,EAAE,CAAC,GAAG;AACR,UAAM,aAAa,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,IAAI,OAAK,qBAAqB,CAAC,CAAC;AACnE,UAAM,IAAI,WAAW,KAAK,IAAI,CAAC;AAAA,EACjC;AAEA,SAAO;AACT;;;ADlCA,IAAMC,cAAa,cAAc,YAAY,GAAG;AAChD,IAAMC,aAAY,QAAQD,WAAU;AAYpC,SAAS,gBAAgB,WAAkC;AACzD,QAAM,eAAe,UAAU,QAAQ,IAAI,OAAK,wBAAwB,EAAE,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,SAAuC;AAClE,SAAO,IAAI,aAAa,OAAO;AACjC;AAEA,IAAM,eAAN,MAAqC;AAAA,EACnC,YAA6B,SAA+B;AAA/B;AAAA,EAAgC;AAAA,EAE7D,MAAM,aAAa,SAAuB,WAAwC;AAChF,UAAM,MAAM,QAAQ;AACpB,QAAI,QAAQ,iBAAiB;AAK7B,UAAM,UAAU,QAAQ,OAAO;AAC/B,UAAM,SAAS;AACf,UAAM,iBAAiB,SAAS,SAAS,UAAU,MAAM;AACzD,UAAM,gBAAgB;AACtB,UAAM,kBAAkB;AAGxB,UAAM,cAAc,KAAK,SAAS,eAAe,CAAC,SAAS,SAAS,eAAe,CAAC;AASpF,eAAW,cAAc,aAAa;AACpC,YAAM,SAASC,SAAQ,UAAU;AACjC,YAAM,UAAU,SAAS,UAAU;AACnC,cAAQ,kBAAkB,QAAQ,iBAAiB,QAAQ,OAAO;AAAA,IACpE;AAGA,UAAM,aAAa,iBAAiB,gBAAgB,eAAe,WAAW,eAAe;AAC7F,UAAM,MAAM,UAAU;AAItB,WAAO;AAAA,EACT;AACF;","names":["dirname","__filename","__dirname","dirname"]}
1
+ {"version":3,"sources":["../src/plugin.ts","../src/vite-config.ts"],"sourcesContent":["// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { basename, dirname, join as joinPath } from 'path'\n\nimport { build } from 'vite'\n\nimport type { BuildContext, Plugin } from '@sdeverywhere/build'\n\nimport type { WorkerPluginOptions } from './options'\nimport { createViteConfig } from './vite-config'\n\nexport function workerPlugin(options?: WorkerPluginOptions): Plugin {\n return new WorkerPlugin(options)\n}\n\nclass WorkerPlugin implements Plugin {\n constructor(private readonly options?: WorkerPluginOptions) {}\n\n async postGenerate(context: BuildContext): Promise<boolean> {\n const log = context.log\n log('info', 'Building worker')\n\n // Locate the generated model file in the staged directory. Note that this\n // relies on the `build` package writing the JS model (or the `plugin-wasm`\n // package writing the Wasm model) to a file called `generated-model.js`\n // in the `staged/model` directory.\n // TODO: Allow for customizing the file name/path\n const prepDir = context.config.prepDir\n const srcDir = 'model'\n const stagedModelDir = joinPath(prepDir, 'staged', srcDir)\n const inModelJsFile = 'generated-model.js'\n const outWorkerJsFile = 'worker.js'\n\n // If `outputPaths` is undefined, write the `worker.js` to the prep dir\n const outputPaths = this.options?.outputPaths || [joinPath(prepDir, outWorkerJsFile)]\n\n // Add staged file entries; this will cause the generated worker to be copied\n // to the configured output paths during the \"copy staged files\" step\n // TODO: This assumes that other plugins that use the generated worker files\n // will run after the \"copy staged files\" step. There might be use cases\n // where a plugin needs access to these in `postGenerate`, in which case the\n // files will not have been copied already. Need to reconsider the ordering\n // of plugins and the \"copy staged files\" step(s).\n for (const outputPath of outputPaths) {\n const dstDir = dirname(outputPath)\n const dstFile = basename(outputPath)\n context.prepareStagedFile(srcDir, outWorkerJsFile, dstDir, dstFile)\n }\n\n // Build the worker and write generated file to the `staged/model` directory\n const viteConfig = createViteConfig(stagedModelDir, inModelJsFile, outWorkerJsFile)\n await build(viteConfig)\n\n // log('info', 'Done!')\n\n return true\n }\n}\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport { dirname, resolve as resolvePath } from 'path'\nimport { fileURLToPath } from 'url'\n\nimport type { InlineConfig } from 'vite'\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = dirname(__filename)\n\n/**\n * Create a Vite `InlineConfig` that can be used to build a complete\n * worker bundle that can run the generated Wasm model in a separate\n * Web Worker or Node worker thread.\n *\n * @param stagedModelDir The `staged` directory under the `sde-prep` directory.\n * @param modelJsFile The name of the JS file containing the generated JS or Wasm model.\n * @param outputFile The name of the generated worker JS file.\n * @return An `InlineConfig` instance that can be passed to Vite's `build` function.\n */\nexport function createViteConfig(stagedModelDir: string, modelJsFile: string, outputFile: string): InlineConfig {\n // Use `staged/model` as the root directory for the worker build\n const root = stagedModelDir\n\n // Use `template-worker/worker.js` from this package as the entry file\n const entry = resolvePath(__dirname, '..', 'template-worker', 'worker.js')\n\n return {\n // Don't use an external config file\n configFile: false,\n\n // Use the root directory configured above\n root,\n\n // Don't clear the screen in dev mode so that we can see builder output\n clearScreen: false,\n\n // Disable vite output by default\n // TODO: Re-enable logging if `--verbose` option is used?\n logLevel: 'silent',\n\n // Configure path aliases\n resolve: {\n alias: [\n // In the template, we use `@_generatedModelFile_` as an alias for the model\n // file containing the generated JS or Wasm model\n {\n find: '@_generatedModelFile_',\n replacement: resolvePath(stagedModelDir, modelJsFile)\n }\n ],\n\n // XXX: Prevent Vite from using the `browser` section of `threads/package.json`\n // since we want to force the use of the general module (under dist) that chooses\n // the correct implementation (Web Worker vs worker_threads) at runtime. This\n // gets the job done, but is fragile because it applies to all dependencies even\n // though we really only need this workaround for the threads package. Fortunately\n // the worker template is very simple (only depends on `@sdeverywhere/runtime-async`,\n // which in turn only depends on `@sdeverywhere/runtime` and `threads`, so we should\n // be safe to use this workaround for a while.\n browserField: false\n },\n\n build: {\n // Write output file to the `staged/model` directory; note that this path is\n // relative to the bundle `root` directory\n outDir: '.',\n emptyOutDir: false,\n\n lib: {\n entry,\n name: 'worker',\n formats: ['iife'],\n fileName: () => outputFile\n },\n\n rollupOptions: {\n onwarn: (warning, warn) => {\n // XXX: Suppress \"Use of eval is strongly discouraged\" warnings that are\n // triggered by use of the following pattern in threads.js:\n // eval(\"require\")(\"worker_threads\")\n // It would be nice to avoid use of `eval` there, but it's not critical for\n // our use case so we will suppress the warnings for now\n if (warning.code !== 'EVAL') {\n warn(warning)\n }\n }\n }\n }\n }\n}\n"],"mappings":";AAEA,SAAS,UAAU,WAAAA,UAAS,QAAQ,gBAAgB;AAEpD,SAAS,aAAa;;;ACFtB,SAAS,SAAS,WAAW,mBAAmB;AAChD,SAAS,qBAAqB;AAI9B,IAAM,aAAa,cAAc,YAAY,GAAG;AAChD,IAAM,YAAY,QAAQ,UAAU;AAY7B,SAAS,iBAAiB,gBAAwB,aAAqB,YAAkC;AAE9G,QAAM,OAAO;AAGb,QAAM,QAAQ,YAAY,WAAW,MAAM,mBAAmB,WA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Q,OAAO;AAAA,IACpE;AAGA,UAAM,aAAa,iBAAiB,gBAAgB,eAAe,eAAe;AAClF,UAAM,MAAM,UAAU;AAItB,WAAO;AAAA,EACT;AACF;","names":["dirname","dirname"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdeverywhere/plugin-worker",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "files": [
5
5
  "dist/**",
6
6
  "template-worker/**"
@@ -17,12 +17,12 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
- "@sdeverywhere/runtime": "^0.2.2",
21
- "@sdeverywhere/runtime-async": "^0.2.2",
20
+ "@sdeverywhere/runtime": "^0.2.4",
21
+ "@sdeverywhere/runtime-async": "^0.2.4",
22
22
  "vite": "4.4.9"
23
23
  },
24
24
  "peerDependencies": {
25
- "@sdeverywhere/build": "^0.3.0"
25
+ "@sdeverywhere/build": "^0.3.5"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@sdeverywhere/build": "*",
@@ -48,7 +48,7 @@
48
48
  "test": "echo No tests yet",
49
49
  "test:watch": "echo No tests yet",
50
50
  "test:ci": "echo No tests yet",
51
- "type-check": "tsc --noEmit -p tsconfig-build.json",
51
+ "type-check": "tsc --noEmit -p tsconfig-test.json",
52
52
  "build": "tsup",
53
53
  "docs": "../../scripts/gen-docs.js",
54
54
  "ci:build": "run-s clean lint prettier:check test:ci type-check build docs"
@@ -1,21 +1,10 @@
1
1
  // Copyright (c) 2022 Climate Interactive / New Venture Fund
2
2
 
3
- import { initWasmModelAndBuffers } from '@sdeverywhere/runtime'
4
-
5
3
  // NOTE: This import *must* specify "worker" at the end (instead of the general
6
4
  // "index" import), otherwise Vite will pull in more code than necessary and it will
7
5
  // generate a worker that won't run correctly in Node or the browser.
8
6
  import { exposeModelWorker } from '@sdeverywhere/runtime-async/worker'
9
7
 
10
- import { numInputs, outputVarIds } from 'virtual:model-spec'
11
- import loadWasmModule from '@_wasm_'
12
-
13
- async function initWasmModel() {
14
- // Load the wasm module asynchronously
15
- const wasmModule = await loadWasmModule()
16
-
17
- // Initialize the wasm model and its associated buffers
18
- return initWasmModelAndBuffers(wasmModule, numInputs, outputVarIds)
19
- }
8
+ import loadGeneratedModel from '@_generatedModelFile_'
20
9
 
21
- exposeModelWorker(initWasmModel)
10
+ exposeModelWorker(loadGeneratedModel)