@sdeverywhere/plugin-worker 0.2.10 → 0.2.12

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
@@ -17,14 +17,14 @@ var __copyProps = (to, from, except, desc) => {
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
18
 
19
19
  // src/index.ts
20
- var src_exports = {};
21
- __export(src_exports, {
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
22
  workerPlugin: () => workerPlugin
23
23
  });
24
- module.exports = __toCommonJS(src_exports);
24
+ module.exports = __toCommonJS(index_exports);
25
25
 
26
- // ../../node_modules/.pnpm/tsup@8.2.4_typescript@5.2.2/node_modules/tsup/assets/cjs_shims.js
27
- var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
26
+ // ../../node_modules/.pnpm/tsup@8.5.1_postcss@8.5.6_typescript@5.2.2/node_modules/tsup/assets/cjs_shims.js
27
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
28
28
  var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
29
29
 
30
30
  // src/plugin.ts
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../../../node_modules/.pnpm/tsup@8.2.4_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. Note that the default value of this\n // property is `['browser', 'module', 'jsnext:main', 'jsnext']`, so we override it\n // to omit the 'browser' item.\n mainFields: ['module', 'jsnext:main', 'jsnext']\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":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,OAClD,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEpC,IAAM,gBAAgC,iCAAiB;;;ACT9D,IAAAA,eAAoD;AAEpD,kBAAsB;;;ACFtB,kBAAgD;AAChD,iBAA8B;AAI9B,IAAMC,kBAAa,0BAAc,aAAe;AAChD,IAAM,gBAAY,qBAAQA,WAAU;AAY7B,SAAS,iBAAiB,gBAAwB,aAAqB,YAAkC;AAE9G,QAAM,OAAO;AAGb,QAAM,YAAQ,YAAAC,SAAY,WAAW,MAAM,mBAAmB,WAAW;AAEzE,SAAO;AAAA;AAAA,IAEL,YAAY;AAAA;AAAA,IAGZ;AAAA;AAAA,IAGA,aAAa;AAAA;AAAA;AAAA,IAIb,UAAU;AAAA;AAAA,IAGV,SAAS;AAAA,MACP,OAAO;AAAA;AAAA;AAAA,QAGL;AAAA,UACE,MAAM;AAAA,UACN,iBAAa,YAAAA,SAAY,gBAAgB,WAAW;AAAA,QACtD;AAAA,MACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAYA,YAAY,CAAC,UAAU,eAAe,QAAQ;AAAA,IAChD;AAAA,IAEA,OAAO;AAAA;AAAA;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;;;ADjFO,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"]}
1
+ {"version":3,"sources":["../src/index.ts","../../../node_modules/.pnpm/tsup@8.5.1_postcss@8.5.6_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.tagName.toUpperCase() === 'SCRIPT') \n ? 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. Note that the default value of this\n // property is `['browser', 'module', 'jsnext:main', 'jsnext']`, so we override it\n // to omit the 'browser' item.\n mainFields: ['module', 'jsnext:main', 'jsnext']\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":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,QAAQ,YAAY,MAAM,WAC1E,SAAS,cAAc,MACvB,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEtC,IAAM,gBAAgC,iCAAiB;;;ACV9D,IAAAA,eAAoD;AAEpD,kBAAsB;;;ACFtB,kBAAgD;AAChD,iBAA8B;AAI9B,IAAMC,kBAAa,0BAAc,aAAe;AAChD,IAAM,gBAAY,qBAAQA,WAAU;AAY7B,SAAS,iBAAiB,gBAAwB,aAAqB,YAAkC;AAE9G,QAAM,OAAO;AAGb,QAAM,YAAQ,YAAAC,SAAY,WAAW,MAAM,mBAAmB,WAAW;AAEzE,SAAO;AAAA;AA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C,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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdeverywhere/plugin-worker",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "files": [
5
5
  "dist/**",
6
6
  "template-worker/**"
@@ -19,14 +19,14 @@
19
19
  "dependencies": {
20
20
  "@sdeverywhere/runtime": "^0.2.7",
21
21
  "@sdeverywhere/runtime-async": "^0.2.7",
22
- "vite": "5.4.2"
22
+ "vite": "^7.1.12"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "@sdeverywhere/build": "^0.3.7"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@sdeverywhere/build": "*",
29
- "@types/node": "^20.5.7"
29
+ "@types/node": "^20.14.8"
30
30
  },
31
31
  "author": "Climate Interactive",
32
32
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "scripts": {
43
43
  "clean": "rm -rf dist",
44
- "lint": "eslint src --ext .ts --max-warnings 0",
44
+ "lint": "eslint src --max-warnings 0",
45
45
  "prettier:check": "prettier --check .",
46
46
  "prettier:fix": "prettier --write .",
47
47
  "precommit": "../../scripts/precommit",