question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support new babel feature allowing plugins to indicate external dependencies

See original GitHub issue

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:

// @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!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
goatandsheepcommented, Feb 8, 2022

heads up: make sure you use 7.17.2 as there was a bug in 7.17.0

0reactions
TSMMarkcommented, Mar 28, 2022

I tried implementing this but it seems like external dependencies must be added before the plugin is returned and registered to babel (e.g. right here: https://github.com/kentcdodds/babel-plugin-codegen/blob/5e8d0acfc255b801d9f3a57dbc46b05b8589d32d/src/index.ts#L14), and the cache.using entries can’t be added / updated dynamically after the plugin has been returned.

So that leads me to the assumption that this feature proposal will not be possible with the current version of the caching feature of babel… However, I’m hoping I’m wrong and that someone more familiar with babel core will prove me wrong eventually, or the babel caching will be updated to support dynamic cache deps.


FTR The specific error is closely related to this open issue https://github.com/babel/babel/issues/14252

It seems like the babel cache/deps are intented only to be used for a static set of files, all of which are known at plugin definition time, and can not be dynamic, and can not be delayed until compile time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow plugins to indicate dependencies on random files
Feature Request In contexts where Babel is running alongside a filesystem ... Allow plugins/presets to indicate external dependencies #14065.
Read more >
Options
Note : The option also allows Plugin instances from Babel itself, but using these directly is not recommended. If you need to create...
Read more >
Configure Babel
How Babel merges config items · For assumptions , parserOpts and generatorOpts , objects are merged, rather than replaced. · For plugins and...
Read more >
babel/register
@babel/register · Install · Usage · Specifying options · Environment variables · Compiling plugins and presets on the fly · Experimental Babel 8...
Read more >
Config Files
Babel can be configured using any file extension natively supported by Node.js, as mentioned in Configuration File Types section: babel.config.json and .babelrc ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found