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

#7670: Simplify creation of isolated widgets (IsolatedComponent) #8151

Merged
merged 26 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
CHROME_MANIFEST_KEY=
CHROME_EXTENSION_ID=mpjjildhmpddojocokjkgmlkkkfjnepo

# Chrome extension manifest version. Default is MV=2
# MV=3
# Chrome extension manifest version. Default is MV=3
# MV=2

# Shadow DOM mode for all components. Default is SHADOW_DOM=closed in regular webpack builds, open elsewhere
# SHADOW_DOM=open

# This makes all optional permissions required in the manifest.json to avoid permission popups. Only required for Playwright tests.
# REQUIRE_OPTIONAL_PERMISSIONS_IN_MANIFEST=1
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ jobs:
timeout-minutes: 60
runs-on: ubuntu-latest
env:
SHADOW_DOM: open
SERVICE_URL: https://app.pixiebrix.com
MV: ${{ matrix.MV }}
CHROME_MANIFEST_KEY: ${{ matrix.CHROME_MANIFEST_KEY }}
Expand Down
35 changes: 24 additions & 11 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,25 @@ const config = {
silent: true,
testEnvironment: "./src/testUtils/FixJsdomEnvironment.js",
modulePaths: ["/src"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "yaml", "yml", "json"],
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"mjs",
"yaml",
"yml",
"json",
],
modulePathIgnorePatterns: ["<rootDir>/headers.json", "<rootDir>/dist/"],
testPathIgnorePatterns: ["/end-to-end-tests"],
transform: {
"^.+\\.[jt]sx?$": "@swc/jest",
"^.+\\.mjs$": "@swc/jest",
"^.+\\.ya?ml$": "yaml-jest-transform",
"^.+\\.ya?ml\\?loadAsText$":
"<rootDir>/src/testUtils/rawJestTransformer.mjs",
"^.+\\.txt$": "<rootDir>/src/testUtils/rawJestTransformer.mjs",
"\\.[jt]sx?$": "@swc/jest",
"\\.mjs$": "@swc/jest",
"\\.ya?ml$": "yaml-jest-transform",
"\\.txt$": "<rootDir>/src/testUtils/rawJestTransformer.mjs",
// Note: `?param` URLs aren't supported here: https://github.com/jestjs/jest/pull/6282
// You can only use a mock via `moduleNameMapper` for these.
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 updated these to avoid confusion. See comment/link.

},
transformIgnorePatterns: [`node_modules/(?!${esmPackages.join("|")})`],
setupFiles: [
Expand All @@ -89,11 +98,15 @@ const config = {
"!**/vendor/**",
],
moduleNameMapper: {
"^@contrib/([^?]+)": "<rootDir>/contrib/$1",
"^@schemas/([^?]+)": "<rootDir>/schemas/$1",

"\\.s?css$": "identity-obj-proxy",
"\\.(gif|svg|png)$|\\?loadAsUrl$|\\?loadAsComponent$":
"<rootDir>/src/__mocks__/stringMock.js",
"^@contrib/(.*?)(\\?loadAsText)?$": "<rootDir>/contrib/$1",
"^@schemas/(.*)": "<rootDir>/schemas/$1",
"\\.(gif|svg|png)$": "<rootDir>/src/__mocks__/stringMock.js",

"\\?loadAsUrl$": "<rootDir>/src/__mocks__/stringMock.js",
"\\?loadAsText$": "<rootDir>/src/__mocks__/stringMock.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this conflict with the loadAsText one on line 109 below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that tests and snapshots don't need to be updated, I'm guessing that it's not actually being applied nor needed.

Anyway I think these 4 above should be moved below.

Copy link
Contributor

Choose a reason for hiding this comment

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

I just wondered if it's related to the changes to imports in IntegrationConfigEditorModal.test.tsx

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sort of. I dropped it from that test file because:

  • ?loadAsText did not work in that file, as the original comment suggested, so having it there did nothing
  • this new config made that import etc?loadAsText point to the stringMock file instead of the actual yml file

Since ?loadAsText cannot "load the import as text" in Jest, it cannot be used if your tests depend on the content of the file.

I cleaned up line 109 as well: 1334a91

"\\?loadAsComponent$": "<rootDir>/src/__mocks__/stringMock.js",

// Auto-mocks. See documentation in ./src/__mocks__/readme.md
"^@/(.*)$": ["<rootDir>/src/__mocks__/@/$1", "<rootDir>/src/$1"],
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@uipath/robot": "1.3.1",
"@vespaiach/axios-fetch-adapter": "^0.3.1",
"@xobotyi/scrollbar-width": "^1.9.5",
"abort-utils": "^1.1.0",
"abort-utils": "^1.2.0",
"ace-builds": "^1.32.9",
"autocompleter": "^9.1.2",
"axios": "^0.27.2",
Expand Down
14 changes: 12 additions & 2 deletions scripts/DiscardFilePlugin.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import webpack from "webpack";

// https://github.com/pixiebrix/pixiebrix-extension/pull/7363#discussion_r1458224740
export default class DiscardFilePlugin {
constructor(filesToDiscard) {
this.filesToDiscard = filesToDiscard;
}

apply(compiler) {
compiler.hooks.compilation.tap("DiscardFilePlugin", (compilation) => {
compilation.hooks.processAssets.tapPromise(
Expand All @@ -9,11 +14,16 @@ export default class DiscardFilePlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
},
async (assets) => {
for (const componentPath of this.filesToDiscard) {
delete assets[`${componentPath.split("/").pop()}.js`];
// If `delete assets[]` causes issues in the future, try replacing the content instead:
// assets["DocumentView.js"] = new webpack.sources.RawSource('"Dropped"');
}

// TODO: Use <IsolatedComponent/> and move these to isolatedComponentList
delete assets["DocumentView.js"];
delete assets["EphemeralFormContent.js"];
delete assets["CustomFormComponent.js"];
// If this causes issues in the future, try replacing the content instead:
// assets["DocumentView.js"] = new webpack.sources.RawSource('"Dropped"');
},
);
});
Expand Down
24 changes: 3 additions & 21 deletions scripts/__snapshots__/manifest.test.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 73 additions & 10 deletions src/__snapshots__/Storyshots.test.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions src/bricks/renderers/PropertyTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import "primereact/resources/themes/saga-blue/theme.css";
import "primereact/resources/primereact.min.css";
import "primeicons/primeicons.css";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can just load the stylesheets normally now 🎉


import { TreeTable } from "primereact/treetable";
import type TreeNode from "primereact/treenode";
import { Column } from "primereact/column";
import { Stylesheets } from "@/components/Stylesheets";
import React from "react";

import theme from "primereact/resources/themes/saga-blue/theme.css?loadAsUrl";
import primereact from "primereact/resources/primereact.min.css?loadAsUrl";
import primeicons from "primeicons/primeicons.css?loadAsUrl";

const PropertyTree: React.FunctionComponent<{ value: TreeNode[] }> = ({
value,
}) => (
<Stylesheets href={[theme, primereact, primeicons]}>
<TreeTable value={value}>
<Column field="name" header="Property" expander />
<Column field="value" header="Value" />
</TreeTable>
</Stylesheets>
<TreeTable value={value}>
<Column field="name" header="Property" expander />
<Column field="value" header="Value" />
</TreeTable>
);

export default PropertyTree;
18 changes: 14 additions & 4 deletions src/bricks/renderers/propertyTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { sortBy, isPlainObject } from "lodash";
import { type BrickArgs, type BrickOptions } from "@/types/runtimeTypes";
import { canParseUrl } from "@/utils/urlUtils";
import { propertiesToSchema } from "@/utils/schemaUtils";
import IsolatedComponent from "@/components/IsolatedComponent";
import type TreeNode from "primereact/treenode";

interface Item {
key: string;
Expand Down Expand Up @@ -115,13 +117,21 @@ export class PropertyTableRenderer extends RendererABC {
);

async render({ data }: BrickArgs, { ctxt }: BrickOptions) {
const PropertyTree = await import(
/* webpackChunkName: "widgets" */
"./PropertyTree"
const PropertyTree: React.FC<{ value: TreeNode[] }> = ({ value }) => (
<IsolatedComponent
name="PropertyTree"
lazy={async () =>
import(
/* webpackChunkName: "isolated/PropertyTree" */
"./PropertyTree"
)
}
factory={(PropertyTree) => <PropertyTree value={value} />}
/>
Comment on lines +121 to +130
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the format I ended up with, I think it's compact and readable enough, which is important due to all the restrictions:

  1. name, isolated/${name}, "/${name}" must all match. Unfortunately this cannot use a variable since they're used by webpack. I can add a lint rule though.
  2. the imported package must also appear in the isolatedComponentList.mjs file, which is picked up by webpack, by <IsolatedComponent/> itself for verification, and by ESLint eventually
  3. the lazy/factory attribute names are the best I could come up with. suggestions welcome (one is React.lazy(LAZY) and the other one returns the result of COMPONENT in const COMPONENT = React.lazy(LAZY)

Copy link
Collaborator

@mnholtz mnholtz Apr 8, 2024

Choose a reason for hiding this comment

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

I'd like to document some of these requirements all in one place somewhere (e.g. it's not clear just looking at IsolatedComponent.tsx that you'd need to add that component to isolatedComponentList). Either in the docstring directly or in a README that's referenced in the docstring.

If I'm looking to add a new isolated component, what are the steps that need to be taken and out-of-the box behaviors or assumptions that are made?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes definitely it needs some documentation and a lint rule, but:

  • the JSDoc includes a full example that is identical every time, where only the name and import path need to be changed, usually
  • if it's used incorrectly, either webpack, typescript or the component itself will throw, depending on what part is wrong

I can't think of anything that breaks that isn't already caught but the build.

assumptions that are made

Since "PropertyTree" is now loaded as an isolated component, it needs to import all the stylesheets it needs, including bootstrap if it needs it (it doesn't in this case). No component should need to use <Stylesheets/> directly unless it's loading the user's stylesheets.

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'd like to document some of these requirements all in one place somewhere

I just checked, each prop is already documented and it includes the requirement:

/**
* It must match the `import()`ed component's filename
*/
name: string;
/**
* It must follow the format `isolated/${name}` specified above
* @example () => import(/* webpackChunkName: "isolated/Moon" * /, "@/components/Moon")
*/
lazy: LazyFactory<T>;

Also this:

if (!isolatedComponentList.some((url) => url.endsWith("/" + name))) {
throw new Error(
`Isolated component "${name}" is not listed in isolatedComponentList.mjs. Add it there and restart webpack to create it.`,
);
}

);

return {
Component: PropertyTree.default,
Component: PropertyTree,
props: {
value: shapeData(data ?? ctxt),
},
Expand Down
13 changes: 10 additions & 3 deletions src/components/EmotionShadowRoot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
eslint-disable @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unnecessary-type-assertion --
"Every property exists" (via Proxy), TypeScript doesn't offer such type
Also strictNullChecks config mismatch */
/*
* Copyright (C) 2024 PixieBrix, Inc.
*
Expand All @@ -19,10 +23,13 @@
import EmotionShadowRoot from "react-shadow/emotion";
import { type CSSProperties } from "react";

/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unnecessary-type-assertion --
"Every property exists" (via Proxy), TypeScript doesn't offer such type
Also strictNullChecks config mismatch */
/**
* Wrap components in a shadow DOM. This isolates them from styles inherited from
* the host website. To support react-select and any future potential emotion
* components we used the emotion variant of the react-shadow library.
*/
const ShadowRoot = EmotionShadowRoot.div!;
// TODO: Use EmotionShadowRoot["pixiebrix-widget"] to avoid any CSS conflicts. Requires snapshot/test updates
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 tried this in the POC PR, but it affected too many tests. I can try again later.


export const styleReset: CSSProperties = {
all: "initial",
Expand Down
Loading
Loading