-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ead088b
Showing
19 changed files
with
10,764 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.next/ | ||
coverage/ | ||
node_modules/ | ||
*.d.ts | ||
*.tgz | ||
tsconfig.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lockfile-version = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.next/ | ||
coverage/ | ||
node_modules/ | ||
*.d.ts | ||
*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
proseWrap: always | ||
semi: false | ||
singleQuote: true | ||
trailingComma: all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.