unjs / jiti

Runtime TypeScript and ESM support for Node.js
MIT License
1.91k stars 62 forks source link

Warning using v2 in next.js #329

Closed bryanjtc closed 1 month ago

bryanjtc commented 1 month ago

Environment

node 20.16.0 pnpm 9.12.0

Reproduction

https://github.com/t3-oss/create-t3-turbo This the original monorepo. The error originates when running the next.js app which uses jiti v2

Describe the bug

image

Additional context

No response

Logs

No response

pi0 commented 1 month ago

It does not seem to be a bug with jiti but webpack not being compatible with ESM syntax used in jiti v2 (it should keep it untouched, while webpack tries to eagerly bundle a runtime usage)

I would recommend to report it upstream, i guess jiti needs to be externalized.

/cc @juliusmarminge @ahkhanjani LMK if could help to solve it on upstream or anything

ahkhanjani commented 1 month ago

Hi @pi0. I'm not a member but I did contribute the jiti v2 upgrade. I would appreciate your help.

Basically all we're doing is this one line inside next.config.js:

import { createJiti } from "jiti";

// Import env files to validate at build time. Use jiti so we can load .ts files in here.
await createJiti(fileURLToPath(import.meta.url)).import("./src/env");

The ./src/env file is just a utility that checks the validity of environment variables at build time once and nothing more.

How can we externalize jiti here?

pi0 commented 1 month ago

I am not familiar with how it works but the warning tells that webpack is trying to bundle jiti (i guess whole next.config), which it can’t.

You need to find a way to asd jiti to webpack externals.

ahkhanjani commented 1 month ago

I am not familiar with how it works but the warning tells that webpack is trying to bundle jiti (i guess whole next.config), which it can’t.

You need to find a way to asd jiti to webpack externals.

@pi0 Simply adding jiti to webpack externals isn't working.

// next.config.js
const config = {
  webpack(config) {
    config.externals.push("jiti");
    console.log({ ext: config.externals });
    return config;
  },
}

Log results:

{
  ext: [
    '_http_agent',         '_http_client',        '_http_common',
    '_http_incoming',      '_http_outgoing',      '_http_server',
    '_stream_duplex',      '_stream_passthrough', '_stream_readable',
    '_stream_transform',   '_stream_wrap',        '_stream_writable',
    '_tls_common',         '_tls_wrap',           'assert',
    'assert/strict',       'async_hooks',         'buffer',
    'child_process',       'cluster',             'console',
    'constants',           'crypto',              'dgram',
    'diagnostics_channel', 'dns',                 'dns/promises',
    'domain',              'events',              'fs',
    'fs/promises',         'http',                'http2',
    'https',               'inspector',           'inspector/promises',
    'module',              'net',                 'os',
    'path',                'path/posix',          'path/win32',
    'perf_hooks',          'process',             'punycode',
    'querystring',         'readline',            'readline/promises',
    'repl',                'stream',              'stream/consumers',
    'stream/promises',     'stream/web',          'string_decoder',
    'sys',                 'timers',              'timers/promises',
    'tls',                 'trace_events',        'tty',
    'url',                 'util',                'util/types',
    'v8',                  'vm',                  'wasi',
    'worker_threads',      'zlib',                [Function (anonymous)],
    'jiti'
  ]
}
{
  ext: [
    'next',
    { '@builder.io/partytown': '{}', 'next/dist/compiled/etag': '{}' },
    {
      buffer: 'commonjs node:buffer',
      'node:buffer': 'commonjs node:buffer',
      events: 'commonjs node:events',
      'node:events': 'commonjs node:events',
      assert: 'commonjs node:assert',
      'node:assert': 'commonjs node:assert',
      util: 'commonjs node:util',
      'node:util': 'commonjs node:util',
      async_hooks: 'commonjs node:async_hooks',
      'node:async_hooks': 'commonjs node:async_hooks'
    },
    [AsyncFunction: handleWebpackExternalForEdgeRuntime],
    'jiti'
  ]
}
{ ext: [ 'next', 'jiti' ] }
<w> [webpack.cache.PackFileCacheStrategy/webpack.FileSystemInfo] Parsing of ...\node_modules\jiti\lib\jiti.mjs for build dependencies failed at 'import(id)'.
<w> Build dependencies behind this expression are ignored and might cause incorrect cache invalidation.
 ⚠ Compiled with warnings
juliusmarminge commented 1 month ago

My guess is it's too late at that point. Maybe just rollback to v1, next 15 will have .ts support so we won't need jiti then anyways (sorry Pooya ☺️)