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

lib/makeWebpackConfig.js Outdated Show resolved Hide resolved
cypress/support/utils.js Outdated Show resolved Hide resolved
Copy link
Contributor

@askoufis askoufis left a comment

Choose a reason for hiding this comment

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

Feel like we should show the API in the changeset, and be a bit more clear about its usage.

@@ -62,6 +62,7 @@ module.exports = {

// Optional:
title: 'My Awesome Library',
entry: './src/entry',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
entry: './src/entry',
entry: './src/entry.js',

Comment on lines +166 to +169
```js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```
Copy link
Contributor

@askoufis askoufis May 23, 2024

Choose a reason for hiding this comment

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

Suggested change
```js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```
```js
// playroom.config.js
entry: './src/entry.js',
```
```js
// ./src/entry.js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```

Comment on lines +9 to +12
```js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```
```js
// playroom.config.js
entry: './src/entry.js',
```
```js
// ./src/entry.js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```

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.

2 participants