Skip to content

Commit

Permalink
feat(plugin): create plugin to integrate modules (#462)
Browse files Browse the repository at this point in the history
Allows clients to create "modules". See my test modules here:
https://github.com/asanehisa/widgets-test-site/tree/main/src/modules
Hosted here:
https://www.yext.com/s/4059983/yextsites/134396/branch/96516/deploys/recent?b=4059983

example use case: to allow clients to put react Search SDK on their non
react pages (similar to how we used to iframe the Search Theme in the
past)

All a user would need to do to have this work is:
1. create a separate folder under the "modules" subfolder in the
pages-starter for each module with the name of the folder corresponding
to the name of the module (ex. "`search`") which will be what is used
when packaging into a umd module (ex. `search.umd.js`)

In each folder they can have:
- The main module file (ex `search.tsx`) (this will contain all of the
logic and configuration that pages allows you to provide, some variable
of type Module is required. If ModuleConfig is configured, it'll use the
provided name.)
- `tailwind.config.ts file`
- `postcss.config.cjs file` that specifies the path of the tailwind
config file they want the module to use
`module.exports = {
  plugins: {
    "tailwindcss": {config: './tailwind-config.ts' },
    "autoprefixer": {},
  },
};`
Also ensure the css code (if used) is scoped properly to the widget. It
can affect code in the non-react site if not.

This also adds response headers to the `config.yaml` such that different
sites can import the widget. This can be manually changed to only allow
certain sites to use the widget.

Here is a test site to see how modules appear. 
https://vibrantly-plain-lark.pgsdemo.com/test.html

To inject a module into non-react site the following is required: 
```
<div id="moduleName"> </div>
<script type="module" src="moduleUrl"></script>
```


Here's my most recent instructions / setup guide: 

https://snapdragon-hat-825.notion.site/Widgets-in-PagesJS-fc01b4e61005482e92ec4ebee462e603?pvs=4

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Alexis Sanehisa <asanehisa@yext.com>
Co-authored-by: Alexis Sanehisa <106991746+asanehisa@users.noreply.github.com>
  • Loading branch information
4 people committed Feb 29, 2024
1 parent 434121d commit a7ad9ea
Show file tree
Hide file tree
Showing 27 changed files with 799 additions and 51 deletions.
63 changes: 62 additions & 1 deletion THIRD-PARTY-NOTICES
Expand Up @@ -7001,6 +7001,37 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-----------

The following npm package may be included in this product:

- postcss-selector-parser@6.0.13

This package contains the following license and notice below:

Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

-----------

The following npm package may be included in this product:

- entities@2.2.0
Expand Down Expand Up @@ -7231,6 +7262,7 @@ IN THE SOFTWARE.

The following npm packages may be included in this product:

- cssesc@3.0.0
- emoji-regex@10.2.1
- punycode@1.3.2

Expand Down Expand Up @@ -10568,7 +10600,7 @@ THE SOFTWARE.

The following npm package may be included in this product:

- postcss@8.4.32
- postcss@8.4.35

This package contains the following license and notice below:

Expand All @@ -10595,6 +10627,35 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-----------

The following npm package may be included in this product:

- postcss-nested@6.0.1

This package contains the following license and notice below:

The MIT License (MIT)

Copyright 2014 Andrey Sitnik <andrey@sitnik.ru>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-----------

The following npm package may be included in this product:

- nanoid@3.3.7
Expand Down
17 changes: 9 additions & 8 deletions package.json
Expand Up @@ -38,29 +38,30 @@
"@types/fs-extra": "^11.0.4",
"@types/lodash": "^4.14.202",
"@types/minimist": "^1.2.5",
"@types/node": "^20.10.6",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.6",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-react": "^7.33.2",
"execa": "^8.0.1",
"fs-extra": "^11.2.0",
"yaml": "^2.3.4",
"generate-changelog": "^1.8.0",
"generate-license-file": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"minimist": "^1.2.8",
"picocolors": "^1.0.0",
"prettier": "^3.1.0",
"prompts": "^2.4.2",
"semver": "^7.5.4",
"tsx": "^4.6.2",
"typescript": "^5.3.3",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-react": "^7.33.2",
"tsx": "^4.6.2"
"yaml": "^2.3.4"
},
"packageManager": "pnpm@8.3.1",
"pnpm": {
Expand Down
58 changes: 58 additions & 0 deletions packages/pages/THIRD-PARTY-NOTICES
Expand Up @@ -5170,5 +5170,63 @@ SOFTWARE.

-----------

The following npm package may be included in this product:

- postcss@8.4.35

This package contains the following license and notice below:

The MIT License (MIT)

Copyright 2013 Andrey Sitnik <andrey@sitnik.ru>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-----------

The following npm package may be included in this product:

- postcss-nested@6.0.1

This package contains the following license and notice below:

The MIT License (MIT)

Copyright 2014 Andrey Sitnik <andrey@sitnik.ru>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-----------

This file was generated with the generate-license-file npm package!
https://www.npmjs.com/package/generate-license-file
3 changes: 3 additions & 0 deletions packages/pages/docs/api/pages.md
Expand Up @@ -10,6 +10,8 @@
| ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [FunctionModule](./pages.functionmodule.md) | Defines the functions and fields that are available to the serverless function. |
| [HeadConfig](./pages.headconfig.md) | The configuration that allows users to entirely arbitarily set the inner contents of the head element that will be prepended to the generated HTML document. |
| [ModuleConfig](./pages.moduleconfig.md) | The configuration options for a Module. |
| [ModuleProps](./pages.moduleprops.md) | The shape of the data passed directly to the module's default export. |
| [PagesHttpRequest](./pages.pageshttprequest.md) | The argument passed to a http/api type function. |
| [PagesHttpResponse](./pages.pageshttpresponse.md) | The return value for a http/api serverless function. |
| [PagesOnUrlChangeRequest](./pages.pagesonurlchangerequest.md) | The argument passed to an onUrlChange type plugin. |
Expand Down Expand Up @@ -41,6 +43,7 @@
| [GetRedirects](./pages.getredirects.md) | The type definiton for the template's getRedirects function. |
| [HttpFunction](./pages.httpfunction.md) | A function that runs when a specific path is visited on the site. |
| [Manifest](./pages.manifest.md) | A manifest of bundled files present during a production build. |
| [Module](./pages.module.md) | The type definition for the module's default function. |
| [OnUrlChangeFunction](./pages.onurlchangefunction.md) | A function that runs when the path of a production page changes. |
| [PagesOnUrlChangeResponse](./pages.pagesonurlchangeresponse.md) | onUrlUpdate plugins return void. |
| [Render](./pages.render.md) | The type definition for the template's render function. |
Expand Down
13 changes: 13 additions & 0 deletions packages/pages/docs/api/pages.module.md
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/pages](./pages.md) &gt; [Module](./pages.module.md)

## Module type

The type definition for the module's default function.

**Signature:**

```typescript
export type Module = () => React.JSX.Element;
```
19 changes: 19 additions & 0 deletions packages/pages/docs/api/pages.moduleconfig.md
@@ -0,0 +1,19 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/pages](./pages.md) &gt; [ModuleConfig](./pages.moduleconfig.md)

## ModuleConfig interface

The configuration options for a Module.

**Signature:**

```typescript
export interface ModuleConfig
```

## Properties

| Property | Modifiers | Type | Description |
| ------------------------------------- | --------- | ------ | -------------------------------------------------------------------------------------------- |
| [name?](./pages.moduleconfig.name.md) | | string | _(Optional)_ Name of the module. If not defined uses the module filename (without extension) |
13 changes: 13 additions & 0 deletions packages/pages/docs/api/pages.moduleconfig.name.md
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/pages](./pages.md) &gt; [ModuleConfig](./pages.moduleconfig.md) &gt; [name](./pages.moduleconfig.name.md)

## ModuleConfig.name property

Name of the module. If not defined uses the module filename (without extension)

**Signature:**

```typescript
name?: string;
```
15 changes: 15 additions & 0 deletions packages/pages/docs/api/pages.moduleprops.__meta.md
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/pages](./pages.md) &gt; [ModuleProps](./pages.moduleprops.md) &gt; [\_\_meta](./pages.moduleprops.__meta.md)

## ModuleProps.\_\_meta property

Additional metadata added by the toolchain

**Signature:**

```typescript
__meta: {
mode: "development" | "production";
}
```
19 changes: 19 additions & 0 deletions packages/pages/docs/api/pages.moduleprops.md
@@ -0,0 +1,19 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/pages](./pages.md) &gt; [ModuleProps](./pages.moduleprops.md)

## ModuleProps interface

The shape of the data passed directly to the module's default export.

**Signature:**

```typescript
export interface ModuleProps
```

## Properties

| Property | Modifiers | Type | Description |
| ----------------------------------------- | --------- | ---------------------------------------- | ------------------------------------------ |
| [\_\_meta](./pages.moduleprops.__meta.md) | | { mode: "development" \| "production"; } | Additional metadata added by the toolchain |
15 changes: 15 additions & 0 deletions packages/pages/etc/pages.api.md
Expand Up @@ -91,6 +91,21 @@ export type Manifest = {
bundlerManifest?: any;
};

// @public
export type Module = () => React.JSX.Element;

// @public
export interface ModuleConfig {
name?: string;
}

// @public
export interface ModuleProps {
__meta: {
mode: "development" | "production";
};
}

// @public
export type OnUrlChangeFunction = (
arg: PagesOnUrlChangeRequest
Expand Down
2 changes: 2 additions & 0 deletions packages/pages/package.json
Expand Up @@ -75,6 +75,8 @@
"open": "^9.1.0",
"ora": "^7.0.1",
"picocolors": "^1.0.0",
"postcss": "8.4.35",
"postcss-nested": "6.0.1",
"pretty-ms": "^8.0.0",
"prompts": "^2.4.2",
"rollup": "^4.7.0",
Expand Down
29 changes: 29 additions & 0 deletions packages/pages/src/common/src/module/types.ts
@@ -0,0 +1,29 @@
/**
* The type definition for the module's default function.
*
* @public
*/
export type Module = () => React.JSX.Element;

/**
* The configuration options for a Module.
*
* @public
*/
export interface ModuleConfig {
/** Name of the module. If not defined uses the module filename (without extension) */
name?: string;
}

/**
* The shape of the data passed directly to the module's default export.
*
* @public
*/
export interface ModuleProps {
/** Additional metadata added by the toolchain */
__meta: {
/** Specifies if the data is returned in development or production mode */
mode: "development" | "production";
};
}

0 comments on commit a7ad9ea

Please sign in to comment.