Skip to content

Commit

Permalink
feat: adapt core plugins to gatsby hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 6, 2019
1 parent b7382aa commit 4caa47a
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 41 deletions.
3 changes: 0 additions & 3 deletions core/docz-core/src/bundler/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import EventEmitter from 'events'

import { Bundler } from '../lib/Bundler'
import { Config as Args } from '../config/argv'

import { server } from './server'
import { build } from './build'

export const emitter = new EventEmitter()
export const bundler = (args: Args) => {
return new Bundler({ args, build, server: server(args) })
}
3 changes: 0 additions & 3 deletions core/docz-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ export { Plugin, createPlugin } from './lib/Plugin'
export { DataServer } from './lib/DataServer'
export { Entries } from './lib/Entries'
export { Entry } from './lib/Entry'

/** bundler exports */
export { emitter } from './bundler'
52 changes: 51 additions & 1 deletion core/docz-core/src/lib/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
import { get, isFunction } from 'lodash/fp'

import { Config } from '../config/argv'
import { pReduce } from '../utils/p-reduce'
import { Config } from '../config/argv'

export type SetConfig = (config: Config) => Config | Promise<Config>
export type onCreateBabelConfig = (params: any, dev: boolean) => void

export type onCreateWebpack<C = any> = (
config: C,
dev: boolean,
args: Config
) => C

export type ModifyFiles = (files: string[], args: Config) => string[]
export type onCreateDevServer = <A>(app: A) => void
export type OnPreBuild = (args: Config) => void
export type OnPostBuild = (args: Config) => void
export type OnPreRender = () => void
export type OnPostRender = () => void

export interface PluginFactory {
setConfig?: SetConfig
onCreateBabelConfig?: onCreateBabelConfig
onCreateWebpack?: onCreateWebpack
modifyFiles?: ModifyFiles
onCreateDevServer?: onCreateDevServer
onPreBuild?: OnPreBuild
onPostBuild?: OnPostBuild
}

export class Plugin<C = any> implements PluginFactory {
public static runPluginsMethod(
plugins: Plugin[] | undefined
): (method: keyof Plugin, ...args: any[]) => void {
return (method, ...args) => {
if (plugins && plugins.length > 0) {
for (const plugin of plugins) {
const fn = get<Plugin, any>(method, plugin)
isFunction(fn) && fn(...args)
}
}
}
}

public static propsOfPlugins(
plugins: Plugin[]
): (prop: keyof Plugin) => any[] {
return prop =>
plugins && plugins.length > 0
? plugins.map(p => get(prop, p)).filter(Boolean)
: []
}

public static reduceFromPlugins<C>(
plugins: Plugin[] | undefined
): (method: keyof Plugin, initial: C, ...args: any[]) => C {
Expand Down Expand Up @@ -39,11 +79,21 @@ export class Plugin<C = any> implements PluginFactory {
}

public readonly setConfig?: SetConfig
public readonly onCreateWebpack?: onCreateWebpack<C>
public readonly onCreateBabelConfig?: onCreateBabelConfig
public readonly modifyFiles?: ModifyFiles
public readonly onCreateDevServer?: onCreateDevServer
public readonly onPreBuild?: OnPreBuild
public readonly onPostBuild?: OnPostBuild

constructor(p: PluginFactory) {
this.setConfig = p.setConfig
this.onCreateWebpack = p.onCreateWebpack
this.onCreateBabelConfig = p.onCreateBabelConfig
this.modifyFiles = p.modifyFiles
this.onCreateDevServer = p.onCreateDevServer
this.onPreBuild = p.onPreBuild
this.onPostBuild = p.onPostBuild
}
}

Expand Down
3 changes: 0 additions & 3 deletions core/docz-core/templates/gatsby-browser.tpl.js

This file was deleted.

13 changes: 11 additions & 2 deletions core/docz-core/templates/gatsby-config.tpl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const { merge } = require('lodash/fp')

module.exports = merge(<%- config.gatsbyConfig -%>, {
let custom
try {
custom = require('./gatsby-config.custom')
} catch(err) {
custom = {}
}

const config = {
siteMetadata: {
title: "<%- config.title %>",
description: "<%- config.description %>"
Expand Down Expand Up @@ -36,4 +43,6 @@ module.exports = merge(<%- config.gatsbyConfig -%>, {
}
}<%}%>
],
})
}

module.exports = merge(config, custom)
23 changes: 0 additions & 23 deletions core/docz-core/templates/gatsby-node.tpl.js

This file was deleted.

3 changes: 0 additions & 3 deletions core/docz-core/templates/gatsby-ssr.tpl.js

This file was deleted.

4 changes: 4 additions & 0 deletions core/gatsby-theme-docz/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
exports.createPages = require('./lib/createPages')
exports.onCreateBabelConfig = require('./lib/onCreateBabelConfig')
exports.onCreateDevServer = require('./lib/onCreateDevServer')
exports.onCreateWebpackConfig = require('./lib/onCreateWebpackConfig')
exports.onPreBuild = require('./lib/sourceNodes')
exports.onPostBuild = require('./lib/sourceNodes')
exports.sourceNodes = require('./lib/sourceNodes')
11 changes: 8 additions & 3 deletions core/gatsby-theme-docz/lib/onCreateBabelConfig.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const { getBaseConfig } = require('docz-core')
const { Plugin, parseConfig } = require('docz-core')

module.exports = async (params, opts = {}) => {
const config = await parseConfig(opts)
const run = Plugin.runPluginsMethod(config.plugins)

module.exports = ({ actions }, opts = {}) => {
const config = getBaseConfig(opts)
const { paths } = config
const { actions, stage } = params

actions.setBabelPlugin({
name: 'babel-plugin-export-metadata',
options: { root: paths.getRootDir(config) },
})

run('onCreateBabelConfig', params, stage === 'develop')
}
9 changes: 9 additions & 0 deletions core/gatsby-theme-docz/lib/onCreateDevServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { Plugin, parseConfig } = require('docz-core')

module.exports = async (params, opts = {}) => {
const config = await parseConfig(opts)
const run = Plugin.runPluginsMethod(config.plugins)
const { stage } = params

run('onCreateDevServer', params, stage === 'develop')
}
24 changes: 24 additions & 0 deletions core/gatsby-theme-docz/lib/onCreateWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs-extra')
const path = require('path')
const { Plugin, parseConfig } = require('docz-core')

const nodeModules = path.resolve(__dirname, 'node_modules')
const parentNodeModules = path.resolve(__dirname, '../../../node_modules')

module.exports = async (params, opts) => {
const { stage, actions, getConfig } = params
const hasParentNodeModules = fs.pathExistsSync(parentNodeModules)
const args = await parseConfig(opts)
const run = Plugin.runPluginsMethod(args.plugins)
const config = getConfig()

if (hasParentNodeModules && stage === 'develop') {
actions.setWebpackConfig({
resolve: {
modules: [nodeModules, parentNodeModules],
},
})
}

run('onCreateWebpack', config, stage === 'develop', args)
}
6 changes: 6 additions & 0 deletions core/gatsby-theme-docz/lib/onPostBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Plugin, parseConfig } = require('docz-core')

module.exports = async (params, opts = {}) => {
const config = await parseConfig(opts)
Plugin.runPluginsMethod(config.plugins)('onPreBuild')
}
6 changes: 6 additions & 0 deletions core/gatsby-theme-docz/lib/onPreBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Plugin, parseConfig } = require('docz-core')

module.exports = async (params, opts = {}) => {
const config = await parseConfig(opts)
Plugin.runPluginsMethod(config.plugins)('onPreBuild')
}

0 comments on commit 4caa47a

Please sign in to comment.