virtualstate / navigation

Native JavaScript navigation [web api] implementation
MIT License
77 stars 5 forks source link

Does not work with esbuild #16

Closed lockieluke closed 11 months ago

lockieluke commented 1 year ago

Prerequisites

Version (i.e. v2.x.x)

1.0.1-alpha.192

Node.js version (i.e. 18.x, or N/A)

20.1.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10, or N/A)

13.4

Description

It doesn't compile in a esbuild project

Steps to Reproduce

This is the error it gives:

✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@virtualstate/navigation/esnext/util/uuid-or-random.js:26:20:
      26 │ const uuidModule = (await getImportUUIDOrNodeRandomUUID());
         ╵                     ~~~~~

✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@virtualstate/navigation/esnext/util/structured-json.js:2:25:
      2 │ const structuredClone = (await getStructuredCloneModule()
        ╵                          ~~~~~

✘ [ERROR] Could not resolve "node:crypto"

    node_modules/@virtualstate/navigation/esnext/util/uuid-or-random.js:13:36:
      13 │         const crypto = await import("node:crypto");
         ╵                                     ~~~~~~~~~~~~~

  The package "node:crypto" wasn't found on the file system but is built into
  node. Are you trying to bundle for node? You can use "platform: 'node'" to
  do that, which will remove this error.

/Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:1636
  let error = new Error(`${text}${summary}`);
              ^

Error: Build failed with 3 errors:
node_modules/@virtualstate/navigation/esnext/util/structured-json.js:2:25: ERROR: Top-level await is currently not supported with the "cjs" output format
node_modules/@virtualstate/navigation/esnext/util/uuid-or-random.js:13:36: ERROR: Could not resolve "node:crypto"
node_modules/@virtualstate/navigation/esnext/util/uuid-or-random.js:26:20: ERROR: Top-level await is currently not supported with the "cjs" output format
    at failureErrorWithLog (/Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:1636:15)
    at /Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:1048:25
    at /Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:993:52
    at buildResponseToResult (/Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:1046:7)
    at /Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:1075:16
    at responseCallbacks.<computed> (/Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:697:9)
    at handleIncomingPacket (/Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:752:9)
    at Socket.readFromStdout (/Users/sherlockluk/Documents/Proj/TypeScript/freedom-proxy/node_modules/esbuild/lib/main.js:673:7)
    at Socket.emit (node:events:511:28)
    at addChunk (node:internal/streams/readable:332:12) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 25,
        file: 'node_modules/@virtualstate/navigation/esnext/util/structured-json.js',
        length: 5,
        line: 2,
        lineText: 'const structuredClone = (await getStructuredCloneModule()',
        namespace: '',
        suggestion: ''
      },
      notes: [],
      pluginName: '',
      text: 'Top-level await is currently not supported with the "cjs" output format'
    },
    {
      detail: undefined,
      id: '',
      location: {
        column: 36,
        file: 'node_modules/@virtualstate/navigation/esnext/util/uuid-or-random.js',
        length: 13,
        line: 13,
        lineText: '        const crypto = await import("node:crypto");',
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: `The package "node:crypto" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.`
        }
      ],
      pluginName: '',
      text: 'Could not resolve "node:crypto"'
    },
    {
      detail: undefined,
      id: '',
      location: {
        column: 20,
        file: 'node_modules/@virtualstate/navigation/esnext/util/uuid-or-random.js',
        length: 5,
        line: 26,
        lineText: 'const uuidModule = (await getImportUUIDOrNodeRandomUUID());',
        namespace: '',
        suggestion: ''
      },
      notes: [],
      pluginName: '',
      text: 'Top-level await is currently not supported with the "cjs" output format'
    }
  ],
  warnings: []
}

esbuild config:

import esbuild from 'esbuild';
import * as path from "path";
import * as url from "url";

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

await esbuild.build({
    entryPoints: ['runtime/index.ts'],
    bundle: true,
    treeShaking: true,
    minify: true,
    platform: 'browser',
    outfile: path.join(__dirname, 'dist', 'runtime.js'),
    format: 'cjs',
    define: {
        'process': "{}",
        'process.env': '{}'
    }
});

console.log('✅  FreedomRuntime Rebuilt');

Expected Behavior

No response

fabiancook commented 1 year ago

Specifically, it doesn't work with cjs by default where the compiler isn't handling top level await.

This project is ESM first, and was designed initially to work with ESM which includes standard support for top level await, I'm looking to avoid making changes to support cjs in that core code.

I've added an additional rollup for cjs now, and included this in the exports definition for the package.json file. I am hoping that esbuild picks up on this where the output is going to be cjs anyway...

fabiancook commented 11 months ago

The default export includes a rollup/commonjs version of the module available.

https://github.com/virtualstate/navigation/blob/46ffcd8e8fa772f2ba7f48bf92a21eda9c6e2d45/package.json#L48-L51

Closing this issue with the assumption that it covers this use case.

Please re-open the issue if you continue to run into issues here.