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

Implement custom entry #325

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Implement custom entry #325

wants to merge 11 commits into from

Conversation

mrm007
Copy link
Contributor

@mrm007 mrm007 commented Feb 20, 2024

It became clear in seek-oss/braid-design-system#1448 that Playroom needed...

  • to provide a way to import the reset as part of every code path,
  • to do it once per bundle, and
  • to do avoid the dual ESM/CJS package hazard.

After scanning the module graph, here are the relavant code paths:

entry src/index.js
1. import 'src/Playroom/Playroom.tsx'
  => import 'src/Playroom/Frames/Frames.tsx'
    => import 'src/Playroom/Frames/frameSrc.js'
      => require('__PLAYROOM_ALIAS__FRAME_COMPONENT__')
2. require('src/themes')
  => require('__PLAYROOM_ALIAS__THEMES__')
3. require('src/components')
  => require('__PLAYROOM_ALIAS__COMPONENTS__')
4. require('./snippets')
  => require('__PLAYROOM_ALIAS__SNIPPETS__')
entry src/frame.js
1. import 'src/Playroom/Frame.tsx'
  => import 'src/RenderCode/RenderCode.js'
    => import '__PLAYROOM_ALIAS__USE_SCOPE__'
2. require('src/themes')
  => require('__PLAYROOM_ALIAS__THEMES__')
3. require('src/components')
  => require('__PLAYROOM_ALIAS__COMPONENTS__')
4. require('src/frameComponent')
  => require('__PLAYROOM_ALIAS__FRAME_COMPONENT__')
entry src/preview.js
1. import 'src/Playroom/Preview.tsx'
  => import 'src/RenderCode/RenderCode.js'
    => import '__PLAYROOM_ALIAS__USE_SCOPE__'
2. require('src/themes')
  => require('__PLAYROOM_ALIAS__THEMES__')
3. require('src/components')
  => require('__PLAYROOM_ALIAS__COMPONENTS__')
4. require('src/frameComponent')
  => require('__PLAYROOM_ALIAS__FRAME_COMPONENT__')

My first idea was to import a module at the top of each of the webpack-defined __PLAYROOM_ALIAS__*__ aliases. That's because we might not know which one loads first, if we were to convert the code to ESM/TypeScript (which might happen in #313). So I wasn't convinced if it was a good idea.

This implementation loads the entry at the top of each webpack entry:

entry: {
index: [require.resolve('../src/index.js')],
frame: [require.resolve('../src/frame.js')],
preview: [require.resolve('../src/preview.js')],
},

This ensures the entry is imported only once per bundle, which is what we want. It also avoids the dual ESM/CJS package hazard, because the entry is loaded by webpack, not by our own code which can be import or require calls.

@mrm007 mrm007 requested a review from a team as a code owner February 20, 2024 05:00
Copy link

changeset-bot bot commented Feb 20, 2024

🦋 Changeset detected

Latest commit: a3ca889

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
playroom Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines 46 to 48
index: [customEntry, require.resolve('../src/index.js')],
frame: [customEntry, require.resolve('../src/frame.js')],
preview: [customEntry, require.resolve('../src/preview.js')],
Copy link
Contributor

Choose a reason for hiding this comment

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

My one concern with this is you may not want your global styles (for example) to end up in the playroom editor and the playroom frame/preview.

We need this for Braid's playroom, but not everyone else necessarily will. IMO it should be opt-in to apply the entry to all entrypoints, and default to just the frame and preview. This feature should actively be discouraged as a way to style the playroom editor IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added an escape hatch in 504da1a

cypress/support/utils.js Outdated Show resolved Hide resolved
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

2 participants