Skip to content

Commit 4059566

Browse files
authored
Merge pull request #14 from jerboa88/2-rewrite-in-typescript
2 rewrite in typescript
2 parents 642b68c + 7f383d7 commit 4059566

18 files changed

+10822
-2102
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib",
3+
"editor.codeActionsOnSave": {
4+
"source.organizeImports.biome": "explicit"
5+
}
6+
}

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,30 @@ These options must be passed to the `createImage()` function.
263263
## Contributing
264264
Contributions, issues, and forks are welcome. [SemVer](http://semver.org/) is used for versioning.
265265

266+
This project is written in [TypeScript] so files in the `src/` directory need to be built when you make changes. Compiled JavaScript files can be found in the `dist/` directory.
267+
268+
### Commands
269+
#### Build
270+
```sh
271+
npm run build
272+
```
273+
274+
#### Build and watch for changes
275+
```sh
276+
npm run watch
277+
```
278+
279+
#### Clean the build directory
280+
```sh
281+
npm run clean
282+
```
283+
266284

267285
## License
268286
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details. This project includes various resources which carry their own copyright notices and license terms. See [LICENSE-THIRD-PARTY.md](LICENSE-THIRD-PARTY.md) for more details.
269287

270288

289+
[Typescript]: https://www.typescriptlang.org/
271290
[Puppeteer]: https://pptr.dev/
272291
[vercel/satori]: https://github.com/vercel/satori
273292
[gatsby-plugin-open-graph-images]: https://github.com/squer-solutions/gatsby-plugin-open-graph-images/

gatsby-node.js

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,2 @@
1-
const { setReporter, info } = require('./src/logger');
2-
const {
3-
setGatsbyCreatePageFunction,
4-
setDefaultOptions,
5-
} = require('./src/config');
6-
const { generateImages } = require('./src/generator');
7-
const { setJoi, getPluginOptionsSchema } = require('./src/validator');
8-
const { prettify } = require('./src/utilities');
9-
10-
// Save the reporter and createPage function for later use
11-
exports.onPluginInit = async ({ reporter, actions: { createPage } }) => {
12-
setReporter(reporter);
13-
setGatsbyCreatePageFunction(createPage);
14-
};
15-
16-
// Save Joi for later and return the schema for plugin options validation by Gatsby
17-
exports.pluginOptionsSchema = ({ Joi }) => {
18-
setJoi(Joi);
19-
20-
return getPluginOptionsSchema();
21-
};
22-
23-
// Get plugin options from gatsby-config.js and set default options
24-
exports.onPreBootstrap = async (_, pluginOptions) => {
25-
const defaultOptions = setDefaultOptions(pluginOptions);
26-
27-
info(`Default options set to:\n${prettify(defaultOptions)}`);
28-
};
29-
30-
// Generate images after pages have been built
31-
exports.onPostBuild = async () => {
32-
await generateImages();
33-
};
1+
// Proxy to the dist/gatsby-node.js file
2+
module.exports = require('./dist/gatsby-node');

package.json

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
"pdf-generation"
2323
],
2424
"license": "MIT",
25-
"main": "./index.js",
25+
"main": "dist/index.js",
26+
"types": "dist/index.d.ts",
27+
"files": [
28+
"/dist",
29+
"gatsby-node.js"
30+
],
2631
"homepage": "https://github.com/jerboa88/gatsby-plugin-component-to-image",
2732
"repository": {
2833
"type": "git",
@@ -31,11 +36,28 @@
3136
"bugs": {
3237
"url": "https://github.com/jerboa88/gatsby-plugin-component-to-image/issues"
3338
},
39+
"scripts": {
40+
"build": "tsc",
41+
"watch": "tsc --watch",
42+
"clean": "tsc --build --clean",
43+
"prepare": "npm run build"
44+
},
45+
"peerDependencies": {
46+
"gatsby": "^5.0.0-next"
47+
},
3448
"dependencies": {
3549
"express": "^4.19.2",
3650
"puppeteer": "^22.9.0"
3751
},
3852
"devDependencies": {
39-
"@biomejs/biome": "1.7.3"
53+
"@biomejs/biome": "1.7.3",
54+
"@tsconfig/node20": "^20.1.4",
55+
"@types/express": "^4.17.21",
56+
"gatsby": "^5.0.0-next",
57+
"gatsby-plugin-utils": "^4.0.0",
58+
"typescript": "^5.4.5"
59+
},
60+
"engines": {
61+
"node": ">=20.0.0"
4062
}
4163
}

src/config.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/config.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { Actions } from 'gatsby';
2+
import type { DefaultOptions, JobOptions } from './types';
3+
import { validateDefaultOptions } from './validator';
4+
5+
const jobQueue: JobOptions[] = [];
6+
7+
let gatsbyCreatePageFunction: Actions['createPage'];
8+
let defaultOptions: DefaultOptions = {
9+
verbose: false,
10+
component: undefined,
11+
context: {},
12+
size: {
13+
width: 1200,
14+
height: 630,
15+
},
16+
type: 'png',
17+
quality: undefined,
18+
optimizeForSpeed: false,
19+
};
20+
21+
export function getDefaultOptions() {
22+
return defaultOptions;
23+
}
24+
25+
export function setDefaultOptions(
26+
newDefaultOptions: Partial<DefaultOptions>,
27+
): DefaultOptions {
28+
defaultOptions = validateDefaultOptions(newDefaultOptions, defaultOptions);
29+
30+
return defaultOptions;
31+
}
32+
33+
export function addJob(job: JobOptions) {
34+
jobQueue.push(job);
35+
}
36+
37+
export function getAllJobs() {
38+
return jobQueue;
39+
}
40+
41+
// Store the Gatsby createPage function to use later
42+
export function setGatsbyCreatePageFunction(
43+
newGatsbyCreatePageFunction: Actions['createPage'],
44+
) {
45+
gatsbyCreatePageFunction = newGatsbyCreatePageFunction;
46+
}
47+
48+
// Call the Gatsby createPage function
49+
export function createPage(...args: Parameters<Actions['createPage']>) {
50+
return gatsbyCreatePageFunction(...args);
51+
}

src/gatsby-node.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { GatsbyNode } from 'gatsby';
2+
import { setDefaultOptions, setGatsbyCreatePageFunction } from './config';
3+
import { generateImages } from './generator';
4+
import { info, setReporter } from './logger';
5+
import type { DefaultOptions } from './types';
6+
import { prettify } from './utilities';
7+
import { getPluginOptionsSchema, setJoi } from './validator';
8+
9+
// Save the reporter and createPage function for later use
10+
export const onPluginInit: GatsbyNode['onPluginInit'] = async ({
11+
reporter,
12+
actions: { createPage },
13+
}) => {
14+
setReporter(reporter);
15+
setGatsbyCreatePageFunction(createPage);
16+
};
17+
18+
// Save Joi for later and return the schema for plugin options validation by Gatsby
19+
export const pluginOptionsSchema: GatsbyNode['pluginOptionsSchema'] = ({
20+
Joi,
21+
}) => {
22+
setJoi(Joi);
23+
24+
return getPluginOptionsSchema();
25+
};
26+
27+
// Get plugin options from gatsby-config.js and set default options
28+
export const onPreBootstrap: GatsbyNode['onPreBootstrap'] = async (
29+
_,
30+
pluginOptions,
31+
) => {
32+
// PluginOptions should be a superset of DefaultOptions
33+
const defaultOptions = setDefaultOptions(
34+
pluginOptions as unknown as Partial<DefaultOptions>,
35+
);
36+
37+
info(`Default options set to:\n${prettify(defaultOptions)}`);
38+
};
39+
40+
// Generate images after pages have been built
41+
export const onPostBuild: GatsbyNode['onPostBuild'] = async () => {
42+
await generateImages();
43+
};

0 commit comments

Comments
 (0)