@sdeverywhere/plugin-worker 0.1.3 → 0.1.4
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.map +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../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","// 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, ResolverObject } from 'vite'\nimport { nodeResolve } from '@rollup/plugin-node-resolve'\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 startTime = ${modelSpec.startTime};\nexport const endTime = ${modelSpec.endTime};\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 // 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. Currently\n // Vite's library mode is browser focused, so using a `customResolver` seems to be\n // the easiest way to prevent Vite from picking up the `browser` exports.\n {\n find: 'threads',\n replacement: 'threads',\n customResolver: nodeResolve({ browser: false }) as ResolverObject\n }\n ]\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;;;ACEA,mBAAoD;AAEpD,kBAAsB;;;ACFtB,kBAAgD;AAChD,iBAA8B;AAG9B,iCAA4B;;;ACC5B,8BAA8B,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,QA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IACpE;AAGA,UAAM,aAAa,iBAAiB,gBAAgB,eAAe,WAAW,eAAe;AAC7F,UAAM,uBAAM,UAAU;AAItB,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../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","// 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, ResolverObject } from 'vite'\nimport { nodeResolve } from '@rollup/plugin-node-resolve'\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 startTime = ${modelSpec.startTime};\nexport const endTime = ${modelSpec.endTime};\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 // 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. Currently\n // Vite's library mode is browser focused, so using a `customResolver` seems to be\n // the easiest way to prevent Vite from picking up the `browser` exports.\n {\n find: 'threads',\n replacement: 'threads',\n customResolver: nodeResolve({ browser: false }) as ResolverObject\n }\n ]\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;;;ACEA,IAAAA,eAAoD;AAEpD,kBAAsB;;;ACFtB,kBAAgD;AAChD,iBAA8B;AAG9B,iCAA4B;;;ACC5B,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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AAgB,eAAe,WAAW,eAAe;AAC7F,cAAM,mBAAM,UAAU;AAItB,WAAO;AAAA,EACT;AACF;","names":["import_path","resolvePath","joinPath"]}
|
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, ResolverObject } from 'vite'\nimport { nodeResolve } from '@rollup/plugin-node-resolve'\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 startTime = ${modelSpec.startTime};\nexport const endTime = ${modelSpec.endTime};\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 // 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. Currently\n // Vite's library mode is browser focused, so using a `customResolver` seems to be\n // the easiest way to prevent Vite from picking up the `browser` exports.\n {\n find: 'threads',\n replacement: 'threads',\n customResolver: nodeResolve({ browser: false }) as ResolverObject\n }\n ]\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;AAEA;;;ACFA;AACA;AAGA;;;ACCA,8BAA8B,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,iCAAiC,SAAyB;AAC/D,QAAM,IAAI,QAAQ,MAAM,0BAA0B;AAClD,MAAI,CAAC,GAAG;AACN,UAAM,IAAI,MAA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AgB,SAAS;AAAA,IAC3B;AAAA,IAEA,OAAO;AAAA,MAGL,QAAQ;AAAA,MACR,aAAa;AAAA,MAEb,KAAK;AAAA,QACH;AAAA,QACA,MAAM;AAAA,QACN,SAAS,CAAC,MAAM;AAAA,QAChB,UAAU,MAAM;AAAA,MAClB;AAAA,MAEA,eAAe;AAAA,QACb,QAAQ,CAAC,SAAS,SAAS;AAMzB,cAAI,QAAQ,SAAS,QAAQ;AAC3B,iBAAK,OAAO;AAAA,UACd;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ADtIO,sBAAsB,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;AAlBpF;AAmBI,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,YAAK,YAAL,mBAAc,gBAAe,CAAC,SAAS,SAAS,eAAe,CAAC;AASpF,eAAW,cAAc,aAAa;AACpC,YAAM,SAAS,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":[]}
|
|
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, ResolverObject } from 'vite'\nimport { nodeResolve } from '@rollup/plugin-node-resolve'\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 startTime = ${modelSpec.startTime};\nexport const endTime = ${modelSpec.endTime};\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 // 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. Currently\n // Vite's library mode is browser focused, so using a `customResolver` seems to be\n // the easiest way to prevent Vite from picking up the `browser` exports.\n {\n find: 'threads',\n replacement: 'threads',\n customResolver: nodeResolve({ browser: false }) as ResolverObject\n }\n ]\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;AAG9B,SAAS,mBAAmB;;;ACC5B,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,SAAS;AAAA,EACnD;AACA,MAAI,KAAK,qBAAqB,EAAE,EAAE;AAClC,MAAI,EAAE,IAAI;AACR,UAAM,aAAa,EAAE,GAAG,MAAM,GAAG,EAAE,IAAI,OAAK,qBAAqB,CAAC,CAAC;AACnE,UAAM,IAAI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M;AAAA,MAClB;AAAA,MAEA,eAAe;AAAA,QACb,QAAQ,CAAC,SAAS,SAAS;AAMzB,cAAI,QAAQ,SAAS,QAAQ;AAC3B,iBAAK,OAAO;AAAA,UACd;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ADtIO,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,SAAuB,WAAwC;AAlBpF;AAmBI,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,gBAAc,UAAK,YAAL,mBAAc,gBAAe,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","dirname"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdeverywhere/plugin-worker",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist/**",
|
|
6
6
|
"template-worker/**"
|
|
@@ -18,11 +18,10 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
21
|
-
"@sdeverywhere/build": "^0.2.0",
|
|
22
21
|
"@sdeverywhere/runtime": "^0.1.0",
|
|
23
22
|
"@sdeverywhere/runtime-async": "^0.1.0",
|
|
24
23
|
"rollup": "^2.76.0",
|
|
25
|
-
"vite": "
|
|
24
|
+
"vite": "^3.1.3"
|
|
26
25
|
},
|
|
27
26
|
"dependenciesComments": {
|
|
28
27
|
"rollup": [
|
|
@@ -33,7 +32,11 @@
|
|
|
33
32
|
"it as a dependency here."
|
|
34
33
|
]
|
|
35
34
|
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@sdeverywhere/build": "^0.2.0"
|
|
37
|
+
},
|
|
36
38
|
"devDependencies": {
|
|
39
|
+
"@sdeverywhere/build": "*",
|
|
37
40
|
"@types/node": "^16.11.7"
|
|
38
41
|
},
|
|
39
42
|
"author": "Climate Interactive",
|