martpie / next-transpile-modules

Next.js plugin to transpile code from node_modules. Please see: https://github.com/martpie/next-transpile-modules/issues/291
MIT License
1.13k stars 85 forks source link

Does this module work with dynamic imports? #214

Closed jaredrada closed 1 year ago

jaredrada commented 3 years ago

Are you trying to transpile a local package or an npm package? I am trying to transpile an npm package lodash-es which is a subdependency of an private/internal package.

Describe the bug I have a next/dynamic import of a private/internal dependency and there is a subdependency on lodash-es. I can see that while I have debug: true the path to the offending file (lodash-es/debounce) is listed by this module as undergoing transpiling, but the error I'm fixing still persists, so I am wondering if my usage of next/dynamic eg dynamic(() => import('my-internal-component') is preventing the transpiling?

/[my-project]/nextjs/node_modules/lodash-es/debounce.js:1
import isObject from './isObject.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module

To Reproduce

  1. Make a dynamic import, which has a subdependency on lodash-es: const myComponent = dynamic(() => import('my-internal-component'))
  2. Notice transpiling does not occur for module specified for transpiling, lodash-es

Expected behavior Even if the dependency/subdependency is imported via @next/dynamic it should still transpile the underlying module to CJS.

Setup

next.config.js

const path = require('path')
const extraStuff = path.join(__dirname, 'extraStuff')
const withTM = require('next-transpile-modules')(['lodash-es'], {debug: true});

module.exports = withTM({
  poweredByHeader: false,
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.module.rules[0].include.push(extraStuff)
    return config
  }
});

Additional context N/A

martpie commented 3 years ago

This is a very good question, I would expect dynamic imports to have their separate bundles and to be built at build time. I will have to double check that.

louisgv commented 2 years ago

I tested out dynamic import of a local scoped package in my project and it sadly does not work.