@polyglot-sql/sdk 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -807,7 +807,7 @@ const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic,
807
807
 
808
808
  ## CDN Usage
809
809
 
810
- For browser use without a bundler:
810
+ For browser use without a bundler, load the CDN ESM build. It resolves `../polyglot_sql.wasm` relative to `dist/cdn/polyglot.esm.js`, so self-hosted deployments must serve `dist/polyglot_sql.wasm` alongside the CDN bundle.
811
811
 
812
812
  ```html
813
813
  <script type="module">
@@ -860,11 +860,11 @@ console.log(isInitialized()); // true
860
860
 
861
861
  ## Bundler Configuration
862
862
 
863
- The ESM build uses **top-level `await`** (ES2022) and **`new URL("...", import.meta.url)`** for WASM loading. Both are web standards supported by all modern bundlers, but some require explicit configuration.
863
+ The SDK publishes separate browser, Node, CommonJS, CDN, and manual-loader entry points. The default browser ESM build uses **top-level `await`** and a sibling `polyglot_sql.wasm` asset. Node ESM and CommonJS resolve the same asset from disk. For bundlers that do not copy WASM files from `new URL(..., import.meta.url)` references, use the manual entry point and import `@polyglot-sql/sdk/polyglot_sql.wasm` explicitly.
864
864
 
865
865
  ### Vite
866
866
 
867
- Vite works out of the box with `vite-plugin-wasm`. This is the setup used by the [Polyglot Playground](https://github.com/tobilg/polyglot):
867
+ Vite works with the default SDK import and `vite-plugin-wasm`. This is the setup used by the [Polyglot Playground](https://github.com/tobilg/polyglot):
868
868
 
869
869
  ```typescript
870
870
  // vite.config.ts
@@ -882,9 +882,47 @@ export default defineConfig({
882
882
  });
883
883
  ```
884
884
 
885
+ Application code can use the regular entry point:
886
+
887
+ ```typescript
888
+ import { transpile, Dialect } from '@polyglot-sql/sdk';
889
+ ```
890
+
891
+ ### esbuild
892
+
893
+ esbuild does not copy WASM files referenced only through `new URL(..., import.meta.url)`. Use the manual entry point and import the WASM asset directly:
894
+
895
+ ```typescript
896
+ import wasmUrl from '@polyglot-sql/sdk/polyglot_sql.wasm';
897
+ import { init, transpile, Dialect } from '@polyglot-sql/sdk/manual';
898
+
899
+ await init({ wasmUrl });
900
+
901
+ const result = transpile('SELECT IFNULL(a, b)', Dialect.MySQL, Dialect.PostgreSQL);
902
+ console.log(result.sql?.[0]);
903
+ ```
904
+
905
+ Configure esbuild to emit the WASM file:
906
+
907
+ ```javascript
908
+ import * as esbuild from 'esbuild';
909
+
910
+ await esbuild.build({
911
+ entryPoints: ['src/main.ts'],
912
+ bundle: true,
913
+ format: 'esm',
914
+ platform: 'browser',
915
+ target: 'es2022',
916
+ outdir: 'dist',
917
+ loader: {
918
+ '.wasm': 'file',
919
+ },
920
+ });
921
+ ```
922
+
885
923
  ### Next.js
886
924
 
887
- Next.js requires enabling WebAssembly experiments in the webpack config:
925
+ Next.js uses webpack under the hood. Use the default SDK import and enable the WebAssembly experiments in the webpack config:
888
926
 
889
927
  ```javascript
890
928
  // next.config.js
@@ -957,13 +995,49 @@ module.exports = {
957
995
 
958
996
  `topLevelAwait` is enabled by default since webpack 5.83.0. `asyncWebAssembly` must be set explicitly.
959
997
 
998
+ ### Node.js
999
+
1000
+ ESM can use the default entry point. Node resolves the `"node"` export to a build that loads `polyglot_sql.wasm` from disk:
1001
+
1002
+ ```javascript
1003
+ import { transpile, Dialect } from '@polyglot-sql/sdk';
1004
+
1005
+ const result = transpile('SELECT IFNULL(a, b)', Dialect.MySQL, Dialect.PostgreSQL);
1006
+ console.log(result.sql?.[0]);
1007
+ ```
1008
+
1009
+ CommonJS requires explicit initialization because `require()` is synchronous:
1010
+
1011
+ ```javascript
1012
+ const { init, transpile, Dialect } = require('@polyglot-sql/sdk');
1013
+
1014
+ await init();
1015
+
1016
+ const result = transpile('SELECT IFNULL(a, b)', Dialect.MySQL, Dialect.PostgreSQL);
1017
+ console.log(result.sql?.[0]);
1018
+ ```
1019
+
1020
+ ### CDN
1021
+
1022
+ The CDN ESM build expects `polyglot_sql.wasm` to be served next to the package `dist` files:
1023
+
1024
+ ```html
1025
+ <script type="module">
1026
+ import polyglot from 'https://unpkg.com/@polyglot-sql/sdk/dist/cdn/polyglot.esm.js';
1027
+
1028
+ const result = polyglot.transpile('SELECT 1', polyglot.Dialect.Generic, polyglot.Dialect.PostgreSQL);
1029
+ console.log(result.sql[0]);
1030
+ </script>
1031
+ ```
1032
+
960
1033
  ### Troubleshooting
961
1034
 
962
- If you see `undefined` exports (e.g., `Dialect` is `undefined`) or errors about `require("fs")`:
1035
+ If you see missing WASM, `undefined` exports, or Node built-in resolution errors:
963
1036
 
964
- 1. Make sure `asyncWebAssembly` and `topLevelAwait` experiments are enabled in your bundler config
965
- 2. Ensure the bundler is resolving the ESM build (the `"import"` or `"browser"` export condition), not the CJS build which uses Node.js APIs
966
- 3. For SSR frameworks (Next.js, Nuxt), check that the `.wasm` file is correctly included in the server build output
1037
+ 1. For browser builds, ensure the bundler resolves the `"browser"` or `"import"` export, not the CommonJS build.
1038
+ 2. For esbuild, use `@polyglot-sql/sdk/manual` with the `@polyglot-sql/sdk/polyglot_sql.wasm` asset export.
1039
+ 3. For webpack-based SSR frameworks, verify the emitted `.wasm` file is present in both client and server build outputs.
1040
+ 4. Keep the build target at ES2022 or newer so top-level `await` is preserved.
967
1041
 
968
1042
  ## License
969
1043