-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
53 lines (45 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const path = require('path');
require('dotenv').config();
const Sourcebit = require('./lib/sourcebit');
module.exports.fetch = (config, runtimeParameters, transformCallback) => {
if (typeof runtimeParameters === 'function') {
transformCallback = runtimeParameters;
runtimeParameters = {};
}
const transformDataPromise = fetch(config, runtimeParameters, transformCallback)
if (typeof transformCallback !== 'function') {
return transformDataPromise;
}
};
module.exports.sourcebitNext = ({ config, runtimeParameters, transformCallback } = {}) => {
return function withSourcebit(nextConfig) {
return {
...nextConfig,
// Since Next.js doesn't provide some kind of real "plugin system"
// we're (ab)using the `redirects` option here in order to hook into
// and block the `next build` and initial `next dev` run.
redirects: async () => {
await fetch(config, runtimeParameters, transformCallback);
return nextConfig.redirects ? nextConfig.redirects() : [];
}
}
}
};
async function fetch(config, runtimeParameters, transformCallback) {
if (!config) {
try {
const configPath = path.resolve(process.cwd(), 'sourcebit.js');
config = require(configPath);
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
throw new Error('Could not find sourcebit.js configuration file');
}
throw error;
}
}
const instance = new Sourcebit({ runtimeParameters, transformCallback });
const { plugins = [] } = config;
instance.loadPlugins(plugins);
return instance.bootstrapAll().then(() => instance.transform());
}
module.exports.Sourcebit = Sourcebit;