Skip to content

Commit 40ee692

Browse files
gwyneplainejesstelfordselenehinkleymartenbjork
authored
[polaris.shopify.com] Add playroom prototype (#7032)
### WHY are these changes introduced? This PR is the core PR for the [composite component tooling project](https://vault.shopify.io/gsd/projects/30559). ### WHAT is this pull request doing? At a high level the changes include: * Adding a playroom integration to polaris.shopify.com * On dev this spins up a separate dev server that the core next app redirects to on hitting the route `/playroom` * In prod this can all be run statically, `build` builds assets to the public/playroom folder of the core polaris.shopify.com next app. * Added a `/sandbox` route for branding and guidance * Made changes to playroom src via `patch-package` to allow us to apply code transforms before playroom runs its own transforms via babel. <!-- ℹ️ Delete the following for small / trivial changes --> ### How to 🎩 🖥 [Local development instructions](https://github.com/Shopify/polaris/blob/main/README.md#local-development) 🗒 [General tophatting guidelines](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md) 📄 [Changelog guidelines](https://github.com/Shopify/polaris/blob/main/.github/CONTRIBUTING.md#changelog) * Run `yarn workspace polaris.shopify.com dev` on the root of the repo * Visit the sandbox route at `localhost:3000/sandbox` * Play with the editor ### 🎩 checklist - [ ] Tested on [mobile](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md#cross-browser-testing) - [ ] Tested on [multiple browsers](https://help.shopify.com/en/manual/shopify-admin/supported-browsers) - [ ] Tested for [accessibility](https://github.com/Shopify/polaris/blob/main/documentation/Accessibility%20testing.md) - [ ] Updated the component's `README.md` with documentation changes - [ ] [Tophatted documentation](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting%20documentation.md) changes in the style guide Co-authored-by: Jess Telford <[email protected]> Co-authored-by: Selene Hinkley <[email protected]> Co-authored-by: Marten Bjork <[email protected]> Co-authored-by: Jess Telford <[email protected]>
1 parent 5bc8857 commit 40ee692

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2943
-80
lines changed

.changeset/fast-boats-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'polaris.shopify.com': patch
3+
---
4+
5+
Improved the design of the Sandbox feature.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'polaris.shopify.com': minor
3+
---
4+
5+
Added Playroom integration to Polaris docs site.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ node_modules
1515
/polaris.shopify.com/.cache/
1616
/polaris.shopify.com/public/sitemap.xml
1717
/polaris.shopify.com/public/og-images
18+
/polaris.shopify.com/public/playroom

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist
33
node_modules
44
/polaris-react/build
55
/polaris-react/build-internal
6+
/polaris.shopify.com/public/sandbox

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"version-packages": "yarn preversion-packages && changeset version",
4242
"release": "turbo run build --filter='!polaris.shopify.com' && changeset publish",
4343
"preversion": "echo \"Error: use @changsets/cli to version packages\" && exit 1",
44-
"new-migration": "yarn workspace @shopify/polaris-migrator generate"
44+
"new-migration": "yarn workspace @shopify/polaris-migrator generate",
45+
"postinstall": "patch-package"
4546
},
4647
"devDependencies": {
4748
"@babel/core": "^7.15.0",
@@ -73,6 +74,8 @@
7374
"jest-preset-stylelint": "^5.0.3",
7475
"jest-watch-typeahead": "^1.0.0",
7576
"npm-run-all": "^4.1.5",
77+
"patch-package": "^6.4.7",
78+
"postinstall-postinstall": "^2.1.0",
7679
"prettier": "^2.5.0",
7780
"rollup": "^2.70.2",
7881
"rollup-plugin-node-externals": "^4.0.0",

patches/playroom+0.28.0.patch

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
diff --git a/node_modules/playroom/README.md b/node_modules/playroom/README.md
2+
index 6c82bbe..f05b80b 100644
3+
--- a/node_modules/playroom/README.md
4+
+++ b/node_modules/playroom/README.md
5+
@@ -160,6 +160,12 @@ export { themeB } from './themeB';
6+
// etc...
7+
```
8+
9+
+## Additional Code Transformations
10+
+
11+
+A hook into the internal processing of code is available via the `processCode` option, which is a path to a file that exports a function that receives the code as entered into the editor, and returns the new code to be rendered.
12+
+
13+
+One example is [wrapping code in an IIFE for state support](https://github.com/seek-oss/playroom/issues/66).
14+
+
15+
## TypeScript Support
16+
17+
If a `tsconfig.json` file is present in your project, static prop types are parsed using [react-docgen-typescript](https://github.com/styleguidist/react-docgen-typescript) to provide better autocompletion in the Playroom editor.
18+
diff --git a/node_modules/playroom/lib/defaultModules/processCode.js b/node_modules/playroom/lib/defaultModules/processCode.js
19+
new file mode 100644
20+
index 0000000..36a436c
21+
--- /dev/null
22+
+++ b/node_modules/playroom/lib/defaultModules/processCode.js
23+
@@ -0,0 +1 @@
24+
+export default code => code;
25+
diff --git a/node_modules/playroom/lib/makeWebpackConfig.js b/node_modules/playroom/lib/makeWebpackConfig.js
26+
index 56defa7..1e7cf3b 100644
27+
--- a/node_modules/playroom/lib/makeWebpackConfig.js
28+
+++ b/node_modules/playroom/lib/makeWebpackConfig.js
29+
@@ -54,6 +54,9 @@ module.exports = async (playroomConfig, options) => {
30+
__PLAYROOM_ALIAS__USE_SCOPE__: playroomConfig.scope
31+
? relativeResolve(playroomConfig.scope)
32+
: require.resolve('./defaultModules/useScope'),
33+
+ __PLAYROOM_ALIAS__PROCESS_CODE__: playroomConfig.processCode
34+
+ ? relativeResolve(playroomConfig.processCode)
35+
+ : require.resolve('./defaultModules/processCode'),
36+
},
37+
},
38+
module: {
39+
diff --git a/node_modules/playroom/src/utils/compileJsx.ts b/node_modules/playroom/src/utils/compileJsx.ts
40+
index dadea77..82d080c 100644
41+
--- a/node_modules/playroom/src/utils/compileJsx.ts
42+
+++ b/node_modules/playroom/src/utils/compileJsx.ts
43+
@@ -1,9 +1,18 @@
44+
import { transform } from '@babel/standalone';
45+
+/* eslint-disable-next-line import/no-unresolved */
46+
+import processCode from '__PLAYROOM_ALIAS__PROCESS_CODE__';
47+
48+
-export const compileJsx = (code: string) =>
49+
- transform(`<React.Fragment>${code.trim() || ''}</React.Fragment>`, {
50+
+export const compileJsx = (code: string) => {
51+
+ const processedCode = processCode(code);
52+
+
53+
+ if (typeof processedCode !== 'string') {
54+
+ throw new Error('processCode function must return a string of code.');
55+
+ }
56+
+
57+
+ return transform(`<React.Fragment>${processedCode.trim()}</React.Fragment>`, {
58+
presets: ['react'],
59+
}).code;
60+
+}
61+
62+
export const validateCode = (code: string) => {
63+
try {
64+
diff --git a/node_modules/playroom/src/utils/formatting.ts b/node_modules/playroom/src/utils/formatting.ts
65+
index a1819bf..70ac15c 100644
66+
--- a/node_modules/playroom/src/utils/formatting.ts
67+
+++ b/node_modules/playroom/src/utils/formatting.ts
68+
@@ -133,10 +133,10 @@ export const formatAndInsert = ({
69+
snippet,
70+
});
71+
72+
- return formatCode({
73+
+ return {
74+
code: newCode,
75+
cursor: updatedCursor,
76+
- });
77+
+ };
78+
};
79+
80+
export const formatForInsertion = ({

polaris.shopify.com/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
root: true,
33
extends: ['next/core-web-vitals'],
4+
ignorePatterns: ['public/playroom'],
45
rules: {},
56
};

polaris.shopify.com/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Not a TS file because our playroom.config.js needs to access it also, and can't understand ts imports.
2+
module.exports = {
3+
playroom: {
4+
baseUrl: '/playroom/',
5+
},
6+
};

polaris.shopify.com/next.config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,41 @@ const nextConfig = {
88
experimental: {
99
scrollRestoration: true,
1010
},
11+
async rewrites() {
12+
return [
13+
// We want to rewrite the sandbox route in production
14+
// to point at the public directory that our playroom assets are built to
15+
// We leverage a rewrite here instead of a redirect in order to preserve
16+
// a "pretty" url for the main playroom editor.
17+
...(process.env.NODE_ENV !== 'production'
18+
? [
19+
{
20+
source: '/playroom/:path*',
21+
destination: 'http://localhost:9000/:path*',
22+
},
23+
]
24+
: []),
25+
];
26+
},
1127

1228
async redirects() {
1329
return [
30+
// We run a redirect to port 9000 for non prod environments
31+
// as playroom files aren't built to the public directory in dev mode.
32+
// We redirect to /preview/index.html here because Playroom's webpack is configured
33+
// to generate an html file for the preview page that reaches for assets in the root directory via a relative path.
34+
// In this case we don't care about a pretty url, and want to make absolutely certain that the browser is pointing to preview/index.html
35+
// such that it resolves the relative asset requests correctly.
36+
{
37+
source: '/playroom',
38+
destination: '/playroom/index.html',
39+
permanent: true,
40+
},
41+
{
42+
source: '/playroom/preview',
43+
destination: '/playroom/preview/index.html',
44+
permanent: true,
45+
},
1446
{
1547
source: '/components/get-started',
1648
destination: '/components',

polaris.shopify.com/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"version": "0.21.1",
44
"private": true,
55
"scripts": {
6-
"build": "yarn gen-assets && next build",
6+
"build": "yarn gen-assets && playroom build && next build",
77
"start": "next start",
88
"dev": "run-p dev:*",
99
"dev:server": "open http://localhost:3000 && next dev",
10+
"dev:playroom": "playroom start",
1011
"dev:watch-md": "node scripts/watch-md.mjs",
1112
"lint": "run-p lint:*",
1213
"lint:js": "TIMING=1 eslint --cache .",
@@ -18,7 +19,7 @@
1819
"gen-assets": "node scripts/gen-assets.mjs"
1920
},
2021
"dependencies": {
21-
"@floating-ui/react-dom-interactions": "^0.6.1",
22+
"@floating-ui/react-dom-interactions": "^0.10.1",
2223
"@headlessui/react": "^1.6.5",
2324
"@shopify/polaris": "^10.7.0",
2425
"@shopify/polaris-icons": "^6.4.0",
@@ -51,13 +52,16 @@
5152
"eslint-config-next": "12.1.0",
5253
"execa": "^6.1.0",
5354
"frontmatter": "^0.0.3",
55+
"babel-plugin-preval": "^5.1.0",
5456
"generact": "^0.4.0",
5557
"get-site-urls": "3.0.0-alpha.1",
5658
"globby": "^11.1.0",
5759
"js-yaml": "^4.1.0",
5860
"lodash.set": "^4.3.2",
61+
"playroom": "^0.28.0",
5962
"marked": "^4.0.16",
6063
"puppeteer": "^16.0.0",
64+
"style-loader": "^3.3.1",
6165
"rehype-raw": "^6.1.1",
6266
"sass": "^1.49.9",
6367
"typescript": "^4.7.4"

0 commit comments

Comments
 (0)