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
12 changes: 12 additions & 0 deletions .changeset/strong-suits-end.md
@@ -0,0 +1,12 @@
---
'playroom': minor
---

Add custom entry file support

You can provide a custom entry file via the `entry` option, which is a path to a file that runs some code before everything else. For example, if you wanted to apply a CSS reset or other global styles, polyfills etc.:

```js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```
Comment on lines +9 to +12
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';
```

10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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',

themes: './src/themes',
snippets: './playroom/snippets.js',
frameComponent: './playroom/FrameComponent.js',
Expand Down Expand Up @@ -158,6 +159,15 @@ export default function useScope() {
};
```

## Custom Entry

You can provide a custom entry file via the `entry` option, which is a path to a file that runs some code before everything else. For example, if you wanted to apply a CSS reset or other global styles, polyfills etc.:

```js
import '../path/to/your/theming-system/reset';
import '../path/to/your/theming-system/global-styles.css';
```
Comment on lines +166 to +169
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';
```


## Theme Support

If your component library has multiple themes, you can customise Playroom to render every theme simultaneously via the `themes` configuration option.
Expand Down
36 changes: 36 additions & 0 deletions cypress/e2e/entry.cy.js
@@ -0,0 +1,36 @@
import { assertPreviewContains, typeCode, visit } from '../support/utils';

describe('entry', () => {
const assertGlobalCounter = () =>
cy.window().its('counter').should('equal', 1);

describe('loads the entry only once', () => {
it('for main app', () => {
cy.visit('http://localhost:9002');
// introduce some delay to make sure everything loads
typeCode('-');

assertGlobalCounter();
});

it('for frames', () => {
visit('http://localhost:9002');
// introduce some delay to make sure everything loads
typeCode('-');

assertGlobalCounter();
});

it('for preview', () => {
cy.visit(
'http://localhost:9002/preview#code=N4Igxg9gJgpiBcIC0IC%2BQ'
).then(() => {
cy.get('[data-testid="splashscreen"]').should('not.be.visible');
});
// wait for rendering to finish to make sure everything loads
assertPreviewContains('-');

assertGlobalCounter();
});
});
});
5 changes: 5 additions & 0 deletions cypress/projects/typescript/entry.mjs
@@ -0,0 +1,5 @@
import { counter, increment } from './state.mjs';

export default counter;

increment();
1 change: 1 addition & 0 deletions cypress/projects/typescript/playroom.config.js
@@ -1,4 +1,5 @@
module.exports = {
entry: './entry.mjs',
components: './components.ts',
snippets: './snippets.ts',
outputPath: './dist',
Expand Down
9 changes: 9 additions & 0 deletions cypress/projects/typescript/state.mjs
@@ -0,0 +1,9 @@
/* eslint-disable no-console */

export let counter = 0;

export function increment() {
window.counter = ++counter;

console.log('incremented', window.counter);
}
1 change: 1 addition & 0 deletions lib/defaultModules/entry.js
@@ -0,0 +1 @@
// this doesn't do anything by default
9 changes: 6 additions & 3 deletions lib/makeWebpackConfig.js
Expand Up @@ -36,13 +36,16 @@ module.exports = async (playroomConfig, options) => {
}

const staticTypes = await getStaticTypes(playroomConfig);
const customEntry = playroomConfig.entry
? relativeResolve(playroomConfig.entry)
: require.resolve('./defaultModules/entry');

const ourConfig = {
mode: options.production ? 'production' : 'development',
entry: {
index: [require.resolve('../src/index.js')],
frame: [require.resolve('../src/frame.js')],
preview: [require.resolve('../src/preview.js')],
index: [customEntry, require.resolve('../src/index.js')],
frame: [customEntry, require.resolve('../src/frame.js')],
preview: [customEntry, require.resolve('../src/preview.js')],
askoufis marked this conversation as resolved.
Show resolved Hide resolved
},
output: {
filename: '[name].[contenthash].js',
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Expand Up @@ -2,6 +2,7 @@ interface PlayroomConfig {
components: string;
outputPath: string;
title?: string;
entry?: string;
themes?: string;
widths?: number[];
snippets?: Snippet[];
Expand Down