kentcdodds / kentcdodds/babel-plugin-codegen
Add support for full module resolution in require & import
- Dominant language
- TypeScript
- Stars
- 347
- Forks
- 27
- PR merge metrics
- No merged PRs in 30d
Description
I've created a reusable codegen which works great (thanks for the library!). However, once I published & consume the codegen as an [npm module (`@ceteio/next-layout-loader`)](https://github.com/ceteio/next-layout-loader), it no longer works:
```javascript
const Layout = codegen.require("@ceteio/next-layout-loader", __filename);
```
```
ModuleBuildError: Module build failed (from ./node_modules/next/dist/build/babel/loader/index.js):
Error: Cannot find module '/sandbox/pages/@ceteio/next-layout-loader'
Require stack:
- /sandbox/node_modules/babel-plugin-codegen/dist/helpers.js
```
[See an example on codesandbox](https://codesandbox.io/s/github/ceteio/next-layout-loader/tree/e2f5640/example)
It looks like the code for loading `codegen.require` / `import` statements is hard-coded to assume a relative path is always passed:
https://github.com/kentcdodds/babel-plugin-codegen/blob/664b96c57d1f2ede88acef6da1606df0bcd834bc/src/helpers.ts#L101-L111
**Suggested fix**
I'm not 100% sure how the lookup is meant to work here, but perhaps this could be replaced with [the `resolve` module](https://github.com/browserify/resolve) to take into account both relative imports and module-style ones?
Contributor guide
Research direction
Start in src/helpers.ts at lines 101-111 and reproduce the failure with the linked @ceteio/next-layout-loader example. Trace how codegen.require and import resolve their paths, then verify that both relative paths and published module-style paths work without the /pages/ prefix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100