Skip to content

Commit d653b1c

Browse files
committed
added sourcebitNext plugin function
1 parent 1dde98b commit d653b1c

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

index.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
1+
const path = require('path');
12
require('dotenv').config();
23
const Sourcebit = require('./lib/sourcebit');
34

45
module.exports.fetch = (config, runtimeParameters, transformCallback) => {
5-
if (!config) {
6-
throw new Error('ERROR: Could not find a valid `sourcebit.js` configuration file.');
7-
}
8-
96
if (typeof runtimeParameters === 'function') {
107
transformCallback = runtimeParameters;
118
runtimeParameters = {};
129
}
1310

11+
const transformDataPromise = fetch(config, runtimeParameters, transformCallback)
12+
13+
if (typeof transformCallback !== 'function') {
14+
return transformDataPromise;
15+
}
16+
};
17+
18+
module.exports.sourcebitNext = ({ config, runtimeParameters, transformCallback } = {}) => {
19+
return function withSourcebit(nextConfig) {
20+
return {
21+
...nextConfig,
22+
// Since Next.js doesn't provide some kind of real "plugin system"
23+
// we're (ab)using the `redirects` option here in order to hook into
24+
// and block the `next build` and initial `next dev` run.
25+
redirects: async () => {
26+
await fetch(config, runtimeParameters, transformCallback);
27+
return nextConfig.redirects ? nextConfig.redirects() : [];
28+
}
29+
}
30+
}
31+
};
32+
33+
async function fetch(config, runtimeParameters, transformCallback) {
34+
if (!config) {
35+
try {
36+
const configPath = path.resolve(process.cwd(), 'sourcebit.js');
37+
config = require(configPath);
38+
} catch (error) {
39+
if (error.code === 'MODULE_NOT_FOUND') {
40+
throw new Error('Could not find sourcebit.js configuration file');
41+
}
42+
throw error;
43+
}
44+
}
1445
const instance = new Sourcebit({ runtimeParameters, transformCallback });
1546
const { plugins = [] } = config;
1647

1748
instance.loadPlugins(plugins);
1849

19-
const transformData = instance.bootstrapAll().then(() => instance.transform());
20-
21-
if (typeof transformCallback !== 'function') {
22-
return transformData;
23-
}
24-
};
50+
return instance.bootstrapAll().then(() => instance.transform());
51+
}
2552

2653
module.exports.Sourcebit = Sourcebit;

0 commit comments

Comments
 (0)