Skip to content
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

Fixes to make web frameworks work for plugin #6000

Merged
merged 8 commits into from
Jun 20, 2023
Merged

Conversation

hsubox76
Copy link
Contributor

  1. WebFrameworks in src/frameworks/index.ts was supposed to be imported from the new frameworks.ts file which uses imports (stable, bundleable) instead of runtime requires (causes problems in webpack bundle for VSCode plugin). James told me this was probably a bad rebase and it should have been checked in.

  2. A couple of places in the CLI code use require, but after being run through webpack, it replaces it with webpack's own require, which works slightly differently (mainly, it expects modules only, not files). There are 2 workarounds for this.

    1. Probably the most robust one is to use createRequire(import.meta.url) but unfortunately you can't use import.meta.url unless the target is changed from commonjs to node16 or nodenext, which I don't think the CLI codebase is ready for.
    2. The other workaround is to use a string replacer loader in webpack to replace specific appearances of require at compile time with __non_webpack_require__. TS hates this so we need a @ts-ignore, and we don't want to replace all instances of require (the webpack one works fine and probably better on actual modules), just specific ones.

@hsubox76 hsubox76 marked this pull request as ready for review June 16, 2023 19:42
Copy link
Contributor

@joehan joehan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love string-replace-loader, as it feels a bit hacky + fragile, but I trust your judgement on whether it is the best choice here.

strict: true
},
{
search: "require(path",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels quite fragile - consider at least adding some explicit comments inframeworks/utils.ts warning about this string replace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.

@hsubox76
Copy link
Contributor Author

I don't love string-replace-loader, as it feels a bit hacky + fragile, but I trust your judgement on whether it is the best choice here.

I think the most robust choice is probably to use createRequire(import.meta.url) but it won't work with TypeScript until/if the CLI can use node16 or nodenext targets: https://stackoverflow.com/questions/73494747/ts1343-the-import-meta-meta-property-is-only-allowed-when-the-module-opti

@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2023

Codecov Report

Patch coverage: 33.33% and project coverage change: +0.08 🎉

Comparison is base (4612ef2) 54.94% compared to head (46d9d23) 55.03%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6000      +/-   ##
==========================================
+ Coverage   54.94%   55.03%   +0.08%     
==========================================
  Files         342      339       -3     
  Lines       23264    23220      -44     
  Branches     4754     4746       -8     
==========================================
- Hits        12783    12779       -4     
+ Misses       9351     9311      -40     
  Partials     1130     1130              
Impacted Files Coverage Δ
src/frameworks/constants.ts 82.60% <ø> (-1.27%) ⬇️
src/frameworks/utils.ts 35.33% <0.00%> (-0.27%) ⬇️
src/dynamicImport.js 33.33% <50.00%> (+8.33%) ⬆️
src/frameworks/index.ts 11.87% <100.00%> (+0.31%) ⬆️

... and 5 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@hsubox76
Copy link
Contributor Author

I don't love string-replace-loader, as it feels a bit hacky + fragile, but I trust your judgement on whether it is the best choice here.

I think the most robust choice is probably to use createRequire(import.meta.url) but it won't work with TypeScript until/if the CLI can use node16 or nodenext targets: https://stackoverflow.com/questions/73494747/ts1343-the-import-meta-meta-property-is-only-allowed-when-the-module-opti

Actually I switched to using a conditional, it's not great either but it's less fragile than the string replace. It checks to see if __webpack_require__ is defined which if true, means it's in webpack, and it means non_webpack_require also exists, and it will sub it in for require. Strangely you can't check for typeof __non_webpack_require__ directly. Solution's described in this comment: webpack/webpack#4175 (comment)

Let me know what you think.

@hsubox76 hsubox76 merged commit 17eb321 into master Jun 20, 2023
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants