Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed May 10, 2022
0 parents commit ead088b
Show file tree
Hide file tree
Showing 19 changed files with 10,764 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 100
trim_trailing_whitespace = true

[COMMIT_EDITMSG]
max_line_length = 72
18 changes: 18 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root: true
extends:
- remcohaszing
- remcohaszing/jest
rules:
import/extensions: off
import/no-extraneous-dependencies: off
import/no-unresolved: off

jsdoc/check-param-names: off
jsdoc/check-tag-names: off
jsdoc/require-jsdoc: off
jsdoc/require-property-type: off
jsdoc/valid-types: off

node/no-unpublished-import: off

unicorn/import-index: off
57 changes: 57 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: ci

on:
pull_request:
push:
branches: [main]
tags: ['*']

jobs:
eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with: { node-version: 18 }
- run: npm ci
- run: npx eslint .

jest:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 16, 18]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.version }}
- run: npm ci
- run: npx jest

pack:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with: { node-version: 18 }
- run: npm ci
- run: npm pack

prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with: { node-version: 18 }
- run: npm ci
- run: npx prettier --check .

tsc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with: { node-version: 18 }
- run: npm ci
- run: npx tsc
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.next/
coverage/
node_modules/
*.d.ts
*.tgz
tsconfig.tsbuildinfo
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lockfile-version = 3
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.next/
coverage/
node_modules/
*.d.ts
*.tgz
4 changes: 4 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proseWrap: always
semi: false
singleQuote: true
trailingComma: all
18 changes: 18 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# MIT License

Copyright © 2021 Remco Haszing

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.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# recma-nextjs-static-props

> Expose top-level identifiers in Next.js app.js
## Installation

```sh
npm install recma-nexjs-static-props
```

## Usage

This plugin is intended for use with [Next.js](https://nextjs.org) and [MDX](https://mdxjs.com).

```js
import nextMDX from '@next/mdx'
import recmaNextjsStaticProps from 'recma-nextjs-static-props'

const withMDX = nextMDX({
options: {
recmaPlugins: [recmaNextjsStaticProps],
},
})

export default withMDX()
```

## API

The default export is a recma plugin which exposes variables from the top-level scope in
[Next.js](https://nextjs.org) through `getStaticProps`.

### Options

- `name`: The name of the export to generate. (Default: `'getStaticProps'`)
- `include`: A list to filter identifiers to include in the generated function. This list may
include strings which must be matched exactly, a regular expression to test against, or a function
that will be called with the value to test, and must return a boolean. By default everything will
be included.
- `exclude`: The same as `include`, but matching values will be excluded instead.

## Example

The source code repository for this plugin is setup as a [Next.js](https://nextjs.org) project.

To try it yourself, simply clone, install, and run this project:

```
git clone https://github.com/remcohaszing/recma-nextjs-static-props.git
cd recma-nextjs-static-props
npm ci
npm run dev
```

## Related Projects

This plugin works well with the following [MDX](https://mdxjs.com) plugins:

- [rehype-mdx-title](https://github.com/remcohaszing/rehype-mdx-title)
- [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter)

## License

[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)
21 changes: 21 additions & 0 deletions components/PropsList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Fragment, ReactElement } from 'react'

/**
* Render a list of available props.
*/
export function PropsList(props: Record<string, unknown>): ReactElement {
return (
<dl>
{Object.entries(props).map(([key, value]) => (
<Fragment key={key}>
<dt>{key}</dt>
<dd>
<pre>
<code>{JSON.stringify(value, undefined, 2)}</code>
</pre>
</dd>
</Fragment>
))}
</dl>
)
}
159 changes: 159 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { analyze } from 'periscopic'

/**
* @typedef {RegExp | string | ((value: string) => boolean)} Test
*/

/**
* @typedef {Object} RecmaNextjsStaticPropsOptions
* @property {string} [name='getStaticProps']
* The name of the export to generate.
* @property {Test[]} [include]
* A list to filter identifiers to include in the generated function.
*
* This list may include strings which must be matched exactly, a regular expression to test
* against, or a function that will be called with the value to test, and must return a boolean. By
* default everything will be included.
* @property {Test[]} [exclude]
* The same as `include`, but matching values will be excluded instead.
*/

/**
* Check if a value matches a test.
*
* @param {Test} test - The test to match.
* @param {string} value - The value to match against the test.
* @returns {boolean} Whether or not the value matches the test.
*/
function passTest(test, value) {
if (typeof test === 'string') {
return value === test
}
if (test instanceof RegExp) {
return test.test(value)
}
return test(value)
}

/**
* A recma plugin which exposes variables from the top-level scope in Next.js through
* `getStaticProps`.
*
* @type {import('unified').Plugin<[RecmaNextjsStaticPropsOptions?], import('estree').Program>}
*/
const recmaNextjsStaticProps =
({ exclude, include, name = 'getStaticProps' } = {}) =>
(ast) => {
/**
* @type {string[]}
*/
const identifiers = []
const { scope } = analyze(ast)

for (const [id, node] of scope.declarations) {
if (id === name) {
return
}

// Always filter JSX functions generated by MDX.
if (id === '_jsx' || id === '_jsxs' || id === '_Fragment') {
continue
}

if (exclude && exclude.some((test) => passTest(test, id))) {
continue
}

if (include && !include.some((test) => passTest(test, id))) {
continue
}

// We’re not insterested in for example function or class declarations.
if (
node.type === 'ImportDefaultSpecifier' ||
node.type === 'ImportSpecifier' ||
node.type === 'VariableDeclaration'
) {
identifiers.push(id)
}
}

if (!identifiers.length) {
return
}

ast.body.push({
type: 'ExportNamedDeclaration',
specifiers: [],
declaration: {
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
id: { type: 'Identifier', name },
init: {
type: 'ArrowFunctionExpression',
expression: true,
generator: false,
async: false,
params: [],
body: {
type: 'ObjectExpression',
properties: [
{
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: { type: 'Identifier', name: 'props' },
value: {
type: 'CallExpression',
optional: false,
callee: {
type: 'MemberExpression',
computed: false,
optional: false,
object: { type: 'Identifier', name: 'JSON' },
property: { type: 'Identifier', name: 'parse' },
},
arguments: [
{
type: 'CallExpression',
optional: false,
callee: {
type: 'MemberExpression',
computed: false,
optional: false,
object: { type: 'Identifier', name: 'JSON' },
property: { type: 'Identifier', name: 'stringify' },
},
arguments: [
{
type: 'ObjectExpression',
properties: identifiers.sort().map((id) => ({
type: 'Property',
method: false,
shorthand: true,
computed: false,
kind: 'init',
key: { type: 'Identifier', name: id },
value: { type: 'Identifier', name: id },
})),
},
],
},
],
},
},
],
},
},
},
],
},
})
}

export default recmaNextjsStaticProps
Loading

0 comments on commit ead088b

Please sign in to comment.