logtape icon indicating copy to clipboard operation
logtape copied to clipboard

Turbopack has trouble with `.cjs` files inside esm build

Open mkatrenik opened this issue 1 year ago • 6 comments

turbopack has trouble with .cjs files inside esm build

│ Import trace for requested module:
│ ../../node_modules/@logtape/logtape/esm/nodeUtil.cjs
│ ../../node_modules/@logtape/logtape/esm/nodeUtil.js
│ ../../node_modules/@logtape/logtape/esm/formatter.js
│ ../../node_modules/@logtape/logtape/esm/mod.js
│ ./src/lib/logger.ts
│ ./src/app/layout.tsx
│  ⚠ ../../node_modules/@logtape/logtape/esm/fs.cjs
│ Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
│ 
│ ../../node_modules/@logtape/logtape/esm/nodeUtil.cjs
│ Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

mkatrenik avatar Nov 27 '24 19:11 mkatrenik

I guess this issue has similar root cause as https://github.com/dahlia/logtape/issues/19. If we import { **everything** } from '@logtape/logtape';, each export has to be compatible with all environments (Node, Deno, Browser, edge, bundlers, etc.). Dependencies like require("node:fs") are quite hard to mask so that they are only used when needed. Something like this would work without hacks:

import { something, agnostic, to, runtime, environments } from '@logtape/logtape';
import { somethingSpecificToFoo } from '@logtape/logtape/foo';
import { somethingSpecificToBar } from '@logtape/logtape/bar';

The problem with environment-specific hacks like https://github.com/dahlia/logtape/blob/327c94738fa8375796acfe9fd4973576633fa1c3/logtape/fs.ts#L12 is that they are not robust long-term. Even if there are no known issues reported at the moment, the situation may degrade with a change in some bundler at any time.

kachkaev avatar Dec 19 '24 15:12 kachkaev

Could you try v0.9.0-dev.133?

dahlia avatar Feb 24 '25 17:02 dahlia

@dahlia , error is still present

Image

mkatrenik avatar Mar 02 '25 16:03 mkatrenik

@mkatrenik What sinks do you use? Could you share your configuration?

dahlia avatar Mar 04 '25 04:03 dahlia

await configure({
  sinks: {
    console: getConsoleSink({
      formatter: getAnsiColorFormatter({
        categoryStyle: 'bold',
        categoryColor: 'magenta',
        format({ timestamp, level, category, message, record }) {
          const showProperties = env.LOG_SHOW_PROPERTIES === true;
          const properties =
            showProperties && Object.keys(record.properties).length > 0
              ? `\n${util.inspect(record.properties)}`
              : '';
          return `${timestamp} ${level} ${category}: ${message}${properties}`;
        },
      }),
    }),
  },
  loggers: [
    {
      category: ['logtape', 'meta'],
      sinks: [],
    },
    {
      category: 'next-app',
      level: 'info',
      sinks: ['console'],
    },
    {
      category: 'common',
      level: 'info',
      sinks: ['console'],
    },
  ],
});

mkatrenik avatar Mar 04 '25 18:03 mkatrenik

Hmm, unable to reproduce. Could you let me know what version of Next.js you use?

dahlia avatar Mar 05 '25 13:03 dahlia

The latest unstable release @logtape/[email protected] revamped the whole bundling configuration. Could you give it a try?

dahlia avatar Jun 13 '25 00:06 dahlia

@dahlia error is gone!

mkatrenik avatar Jun 14 '25 13:06 mkatrenik