kentcdodds / kentcdodds/babel-plugin-codegen

Support new babel feature allowing plugins to indicate external dependencies

Open
#49 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
347
Forks
27
PR merge metrics
No merged PRs in 30d

Description

Hey Kent, thanks for this awesome plugin, been using it for years and it's really handy! Also saw you got in a car accident recently and I hope you're not too beat up and recovering well.

I've been following this feature which just got merged to babel: https://github.com/babel/babel/pull/14065

I think that might be useful for this plugin to implement. It could allow us to invalidate the codegen output if the code has a dependency on some files on disk.

It seems pretty simple to utilize the new feature, if I'm right you just call `api.addExternalDependency(external)` where `external` is a filepath. https://github.com/babel/babel/pull/14065/files#diff-02893c5f57e6e79d081162d32cbfe84ccfac5ed70b8933be563e1dd7256f179cR15

---

Here's an example of a codegen module whose result depends on files on the filesystem, and could benefit from external dependency awareness:

```js
// @codegen
const join = require('lodash/join')
const replace = require('lodash/replace')
const glob = require('glob')
const path = require('path')

const examplesFiles = glob.sync(path.resolve(__dirname, '**/examples.*'))

export default `export default {
${join(examplesFiles.map((exampleFile) => {
const chopSrcFromPath = replace(exampleFile, /(.*):?src\//, '')
return `'${chopSrcFromPath}': require('./${chopSrcFromPath}')`
}), ',\n')}
}
/* 02-03-2022-09:22:00 */`
```

Right now we resort to updating the timestamp comment to force an invalidation + codegen rebuild. (Maybe there's a better way that I don't know about?)

I'm not sure the internals of this library but do you think it makes sense to implement? I might be able to take a stab at implementing it. Let me know if you have any pointers. Thanks!

Contributor guide

Open the contributing guide

Research direction

Start by tracing the Babel plugin entry points and how codegen output is invalidated, then read Babel PR 14065 to understand the external-dependency API. Confirm the intended behavior with the issue's example: codegen modules that read filesystem files should trigger regeneration when those files change, with tests covering dependency registration and invalidation.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, typescript
Domain
build-system, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.