@polyglot-sql/sdk 0.1.11 → 0.1.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/README.md CHANGED
@@ -818,6 +818,113 @@ console.log(isInitialized()); // true
818
818
 
819
819
  > **Note:** The ESM build (`import`) auto-initializes via top-level `await`, so `init()` is not required there. The CJS build requires it because `require()` is synchronous.
820
820
 
821
+ ## Bundler Configuration
822
+
823
+ 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.
824
+
825
+ ### Vite
826
+
827
+ Vite works out of the box with `vite-plugin-wasm`. This is the setup used by the [Polyglot Playground](https://github.com/tobilg/polyglot):
828
+
829
+ ```typescript
830
+ // vite.config.ts
831
+ import { defineConfig } from 'vite';
832
+ import wasm from 'vite-plugin-wasm';
833
+
834
+ export default defineConfig({
835
+ plugins: [wasm()],
836
+ build: {
837
+ target: 'esnext', // required for top-level await
838
+ },
839
+ optimizeDeps: {
840
+ exclude: ['@polyglot-sql/sdk'], // prevent esbuild from pre-bundling WASM
841
+ },
842
+ });
843
+ ```
844
+
845
+ ### Next.js
846
+
847
+ Next.js requires enabling WebAssembly experiments in the webpack config:
848
+
849
+ ```javascript
850
+ // next.config.js
851
+ module.exports = {
852
+ webpack: (config) => {
853
+ config.experiments = {
854
+ ...config.experiments,
855
+ asyncWebAssembly: true,
856
+ topLevelAwait: true,
857
+ layers: true,
858
+ };
859
+ return config;
860
+ },
861
+ };
862
+ ```
863
+
864
+ For **production SSR builds**, Next.js may emit `.wasm` files at a different path than where it tries to load them. A common workaround is to add a plugin that fixes the output path:
865
+
866
+ ```javascript
867
+ // next.config.js
868
+ class WasmChunksFixPlugin {
869
+ apply(compiler) {
870
+ compiler.hooks.thisCompilation.tap('WasmChunksFixPlugin', (compilation) => {
871
+ compilation.hooks.processAssets.tap(
872
+ { name: 'WasmChunksFixPlugin' },
873
+ (assets) =>
874
+ Object.entries(assets).forEach(([pathname, source]) => {
875
+ if (!pathname.match(/\.wasm$/)) return;
876
+ compilation.deleteAsset(pathname);
877
+ const name = pathname.split('/')[1];
878
+ const info = compilation.assetsInfo.get(pathname);
879
+ compilation.emitAsset(name, source, info);
880
+ })
881
+ );
882
+ });
883
+ }
884
+ }
885
+
886
+ module.exports = {
887
+ webpack: (config, { isServer, dev }) => {
888
+ config.experiments = {
889
+ ...config.experiments,
890
+ asyncWebAssembly: true,
891
+ topLevelAwait: true,
892
+ layers: true,
893
+ };
894
+ if (!dev && isServer) {
895
+ config.output.webassemblyModuleFilename = 'chunks/[id].wasm';
896
+ config.plugins.push(new WasmChunksFixPlugin());
897
+ }
898
+ return config;
899
+ },
900
+ };
901
+ ```
902
+
903
+ ### webpack 5 (standalone)
904
+
905
+ Enable the required experiments in your webpack config:
906
+
907
+ ```javascript
908
+ // webpack.config.js
909
+ module.exports = {
910
+ experiments: {
911
+ asyncWebAssembly: true,
912
+ topLevelAwait: true,
913
+ },
914
+ // ...
915
+ };
916
+ ```
917
+
918
+ `topLevelAwait` is enabled by default since webpack 5.83.0. `asyncWebAssembly` must be set explicitly.
919
+
920
+ ### Troubleshooting
921
+
922
+ If you see `undefined` exports (e.g., `Dialect` is `undefined`) or errors about `require("fs")`:
923
+
924
+ 1. Make sure `asyncWebAssembly` and `topLevelAwait` experiments are enabled in your bundler config
925
+ 2. Ensure the bundler is resolving the ESM build (the `"import"` or `"browser"` export condition), not the CJS build which uses Node.js APIs
926
+ 3. For SSR frameworks (Next.js, Nuxt), check that the `.wasm` file is correctly included in the server build output
927
+
821
928
  ## License
822
929
 
823
930
  [MIT](../../LICENSE)