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

fix(cli): add styled-components aliasing. #7298

Closed
wants to merge 1 commit into from
Closed

Conversation

ricokahler
Copy link
Contributor

Description

The following PR adds additional aliasing for styled-components for both the internal build in the monorepo and any user builds.

What to review

Do the changes make sense? Are the updated tests adequate?

Testing

  • Unit tests were updated
  • I built the test-studio with the new styled-component aliases and without. See below.
  • I also packed this new version of sanity and tried it out in the admin studio to check the non-monorepo alises of styled-components.
monorepo-aliasing.mp4

Notes for release

  • Adds aliases for styled-components to prevent missing context errors.

@ricokahler ricokahler requested a review from a team as a code owner August 2, 2024 03:51
@ricokahler ricokahler requested review from binoy14 and removed request for a team August 2, 2024 03:51
Copy link

vercel bot commented Aug 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ❌ Failed (Inspect) Aug 2, 2024 3:51am
performance-studio ❌ Failed (Inspect) Aug 2, 2024 3:51am
test-compiled-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 2, 2024 3:51am
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 2, 2024 3:51am
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 2, 2024 3:51am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Aug 2, 2024 3:51am

Copy link
Contributor

github-actions bot commented Aug 2, 2024

No changes to documentation

Copy link
Contributor

github-actions bot commented Aug 2, 2024

Component Testing Report Updated Aug 2, 2024 4:07 AM (UTC)

File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 42s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 9s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 31s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 36s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 18s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 10s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 2m 33s 1 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 43s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 46s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 13s 18 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 9s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 25s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 18s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 1m 13s 21 0 0
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 1m 48s 30 0 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 19s 3 0 0

Copy link
Member

@stipsan stipsan left a comment

Choose a reason for hiding this comment

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

This is an awesome initiative and will solve a lot of problems 💖 We're planning on introducing a utility in next-sanity to accomplish this for embedded studio users too :)

We should be using the browser build of styled-components though, as we're not SSR-ing the Studio today so it doesn't make sense for the heavier and slower build of styled-components to be used 🙌

},
{
find: /^styled-components$/,
replacement: expect.stringContaining('styled-components.esm.js'),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
replacement: expect.stringContaining('styled-components.esm.js'),
replacement: expect.stringContaining('styled-components.browser.esm.js'),

expect(aliases).toMatchObject({
...resolvedDevAliases,
'styled-components/package.json': expect.stringContaining('styled-components/package.json'),
'styled-components': expect.stringContaining('styled-components.esm.js'),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'styled-components': expect.stringContaining('styled-components.esm.js'),
'styled-components': expect.stringContaining('styled-components.browser.esm.js'),

// entry point
aliases.push({
find: /^styled-components$/,
replacement: path.resolve(styledComponentsRoot, styledComponentsPkg.module),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
replacement: path.resolve(styledComponentsRoot, styledComponentsPkg.module),
replacement: path.resolve(styledComponentsRoot, styledComponentsPkg.browser[styledComponentsPkg.module]),

@rexxars
Copy link
Member

rexxars commented Aug 2, 2024

We should be using the browser build of styled-components though

I have had some doubt about using the resolve.exports module in general, or specifically pointing to a module path. In my mind it would be ideal to just defer this to vite and use the defaults so it matches whatever the package maintainers "intended".

Fairly certain that if you point at the module folder as the alias, it still does its normal entrypoint resolution, eg psuedo-code:

// will point to a specific file which might be the node.js one,
// eg not the one we want to use
const scFilePath = resolveFrom(studioPath, 'styled-components')

// find the root of the module, eg the closest folder with a `package.json`
// (could check for `.name` if we wanted to be super-safe)
const scPath = recursivelyFindClosestPackageJson(scFilePath)

aliases['styled-components'] = scPath

Just a thought. What do you think?

@ricokahler
Copy link
Contributor Author

We should be using the browser build of styled-components though

I have had some doubt about using the resolve.exports module in general, or specifically pointing to a module path. In my mind it would be ideal to just defer this to vite and use the defaults so it matches whatever the package maintainers "intended".

Fairly certain that if you point at the module folder as the alias, it still does its normal entrypoint resolution, eg psuedo-code:

Oh that's definitely the way to do it if that works. I assumed it didn't. I'll test and report back!

@ricokahler
Copy link
Contributor Author

Fairly certain that if you point at the module folder as the alias, it still does its normal entrypoint resolution

Alright I've did some research into this issue, and I've found that making the alias the module folder does not always do what we want. Here's a claude paste of my findings:

  1. Entrypoint Resolution for Aliased Modules: When setting the module folder as the alias, Vite's behavior can vary depending on the module's structure:

    a) For modules without exports in their package.json, aliasing to the module folder can appear to follow normal resolution behavior. This is due to the Node.js module resolution algorithm and how package.json works.

    b) However, for modules that utilize the Node.js exports field in their package.json, Vite does not always follow normal entrypoint resolution.

  2. Example of Correct Resolution: The styled-components package is an example where aliasing to the module folder works correctly. When you alias styled-components to its installed folder (e.g., ./node_modules/styled-components), it picks up the package.json at that location and uses it to find the entry point. This gives the appearance of normal resolution.

  3. Vite's Alias Resolution Mechanism: Vite uses @rollup/plugin-alias for alias resolution. This plugin performs a simple swap when it encounters an aliased path, without considering the exports field or subpath exports.

  4. Impact on Modules with exports: This becomes problematic for modules like sanity that have exports and subpath exports (e.g., sanity/router, sanity/structure). If these exports don't resolve to files at the top level of the module folder, the aliasing breaks.

  5. Example of the Issue: Consider a package.json like this:

    {
      "name": "sanity",
      "version": "3.50.0",
      "exports": {
        ".": {
          "import": "./dist/mock-index.mjs",
          "require": "./wrong.cjs",
          "default": "./wrong.cjs"
        },
        "./router": {
          "import": "./dist/mock-router.mjs",
          "require": "./wrong.cjs",
          "default": "./wrong.cjs"
        },
        "./structure": {
          "import": "./dist/mock-structure.mjs",
          "require": "./wrong.cjs",
          "default": "./wrong.cjs"
        }
      }
    }

    In this case, aliasing sanity to its module folder would not correctly resolve sanity/router or sanity/structure.

  6. Rollup Alias Plugin Behavior: The @rollup/plugin-alias does a straightforward replacement without considering the intricacies of the exports field. This means it won't handle complex module structures as we might expect.

Given these findings, we need to be more careful with how we set up aliases, especially for modules with complex export structures. The behavior can vary significantly between modules with and without exports in their package.json.


On another note, I actually want to throw away this implementation and use vite's resolve.dedupe.

I'll close this PR and open another that does that.

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.

3 participants