New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CommonJS register/unregister methods #460
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
I'm currently implementing this via https://github.com/pvtnbr/tsx/pull/3 The usage will look like this: import { require } from 'tsx/cjs/api';
// Instead of `createRequire(import.meta.url)`, you can pass `import.meta.url` or `__filename` as the second argument
const loaded = require('./file', import.meta.url); RE: postcss/postcss-load-config#249 (comment) Seems it's possible to detect what files were loaded via the CommonJS graph already. What do you think of this approach? // To detect watch files, we can parse the CommonJS module graph
const resolvedPath = require.resolve('./file', import.meta.url);
// User-land code
const collectDependencies = (
module,
) => [
module.filename,
...module.children.flatMap(collectDependencies)
];
console.log(collectDependencies(require.cache[resolvedPath])); |
Is require.cache part of tsx' API too or is it the native one? Most of the vite projects had went ESM only last year 👀 If it's native one, then I wonder if that works with |
If you'd like to try it out: npm i 'privatenumber/tsx#npm/cjs-require'
I plan to explore this separately via #239 but I'm not confident this will be safe yet. Also, I'd like to eventually offer an |
Yeah for postcss, watching file is not needed. Simply require would work. |
Can you share what's Ah I think it should be |
Yep, just fixed! Sorry |
Just tested. Seems to work fine. Also, would it be possible to emit .d.ts files for those 👀 |
Thanks, I appreciate it! And yes, good catch! |
Precheck
Feature request
register
/unregister
methods for the CommonJS APIregister
Motivations
postcss/postcss-load-config#249 (comment)
tailwindlabs/tailwindcss#12506
Alternatives
No response
Additional context
No response
Contributions
The text was updated successfully, but these errors were encountered: