Skip to content

Commit ab87ea9

Browse files
committed
feat: add onCreateServer & onCreateApolloServerConfig
Also refactor plugin hooks
1 parent 169817f commit ab87ea9

19 files changed

+171
-108
lines changed

packages/smooth/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
1111
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1212

13-
1413
- [How to use](#how-to-use)
1514
- [Setup](#setup)
1615
- [Structure](#structure)
@@ -896,6 +895,8 @@ The `modules` option on `"preset-env"` should be kept to `false` otherwise webpa
896895
897896
### smooth-node.js
898897
898+
- onCreateServer
899+
- onCreateApolloServerConfig
899900
- onRenderBody
900901
- onCreateBabelConfig
901902
- getContents

packages/smooth/src/build/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22
import webpack from 'webpack'
3-
import { applyAsyncHook } from '../plugin'
3+
import { onBuild } from '../plugin/nodeHooks'
44
import {
55
createSchemaDefinition,
66
makeExecutableSchema,
@@ -40,7 +40,7 @@ export async function buildSchemaDefinition({ config }) {
4040
export async function buildSchema({ config }) {
4141
const cache = createCache({ config })
4242
const { schemaDefinition, schema } = await buildSchemaDefinition({ config })
43-
await applyAsyncHook(config, 'onBuild', { schemaDefinition })
43+
await onBuild(config)({ schemaDefinition })
4444
const fragmentTypes = await getFragmentTypes({ schema })
4545
await cache.writeCacheFile(
4646
'fragmentTypes.json',

packages/smooth/src/config/webpack.js

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'fs'
33
import webpack from 'webpack'
44
import nodeExternals from 'webpack-node-externals'
55
import LoadablePlugin from '@loadable/webpack-plugin'
6-
import { applyHook } from '../plugin/node'
6+
import { onCreateBabelConfig } from '../plugin/nodeHooks'
77
import SmoothCacheHotReloader from '../webpack/plugins/smooth-cache-hot-reloader'
88

99
function fileExistsSync(filepath) {
@@ -46,36 +46,10 @@ function getDirectory(config, dirPath, defaultPath) {
4646
: path.join(config.cachePath, defaultPath)
4747
}
4848

49-
function getBabelOptions({ config, isServer }) {
50-
let mixOpts = { plugins: [], presets: [] }
51-
52-
const actions = {
53-
setBabelOptions(opts) {
54-
mixOpts = { ...mixOpts, ...opts }
55-
},
56-
setBabelPlugin({ name, options = {} }) {
57-
mixOpts = {
58-
...mixOpts,
59-
plugins: [...mixOpts.plugins, [name, options]],
60-
}
61-
},
62-
setBabelPreset({ name, options = {} }) {
63-
mixOpts = {
64-
...mixOpts,
65-
presets: [...mixOpts.presets, [name, options]],
66-
}
67-
},
68-
}
69-
70-
applyHook(config, 'onCreateBabelConfig', { isServer, actions })
71-
72-
return mixOpts
73-
}
74-
7549
function getTargetConfig(target, { config, dev }) {
7650
const mainEntry = path.join(__dirname, '../client', `main-${target}.js`)
7751
const isServer = target === 'node'
78-
const babelOptions = getBabelOptions({ config, isServer })
52+
const babelOptions = onCreateBabelConfig(config)({ isServer })
7953
const defaultLoaders = {
8054
babel: {
8155
loader: 'smooth-babel-loader',

packages/smooth/src/content/Query.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import gql from 'graphql-tag'
44
import { Redirect, useRouter } from '../router'
55
import { usePageContext } from '../page/PageContext'
66
import { HTTPError } from '../router/HTTPError'
7-
import { applyHook } from '../plugin/browser'
7+
import { onSelectContentFields } from '../plugin/browserHooks'
88
import { Query as BaseQuery } from '../query/Query'
99
import {
1010
getFragmentDefinition,
@@ -23,11 +23,7 @@ function getQuery(page) {
2323
const type = getDefinitionType(fragmentDefinition)
2424
const queryField = getQueryField(type)
2525
const fragmentName = getDefinitionName(fragmentDefinition)
26-
const fields = applyHook(
27-
'onSelectContentFields',
28-
{ fields: [`...${fragmentName}`] },
29-
'fields',
30-
)
26+
const fields = onSelectContentFields({ fragmentName })
3127

3228
const query = gql`
3329
query Content(

packages/smooth/src/page/Page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useRef } from 'react'
22
import loadable from '@loadable/component'
33
import App from '__smooth_app'
44
import Content from '__smooth_content'
5-
import { applyHook } from '../plugin/browser'
5+
import { wrapContentElement } from '../plugin/browserHooks'
66
import { PageContextProvider } from './PageContext'
77
import { usePause } from '../router/HiddenRouter'
88

@@ -87,7 +87,7 @@ function enrichPageRef(
8787
const ContentComponent = props => {
8888
const Component = pageRef.current.module.default
8989
const element = <Component {...props} />
90-
return applyHook('wrapContentElement', { element, props }, 'element')
90+
return wrapContentElement({ element, props })
9191
}
9292
const PageComponent = () => <Content Component={ContentComponent} />
9393
pageRef.current.ContentComponent = ContentComponent
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './onRouteUpdate'
2+
export * from './onSelectContentFields'
3+
export * from './wrapContentElement'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { applyHook } from '../browser'
2+
3+
export const onRouteUpdate = options => applyHook('onRouteUpdate', options)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { applyHook } from '../browser'
2+
3+
export const onSelectContentFields = ({ fragmentName }) =>
4+
applyHook(
5+
'onSelectContentFields',
6+
{ fields: [`...${fragmentName}`] },
7+
'fields',
8+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { applyHook } from '../browser'
2+
3+
export const wrapContentElement = ({ element, props }) =>
4+
applyHook('wrapContentElement', { element, props }, 'element')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './onBuild'
2+
export * from './onCreateApolloServerConfig'
3+
export * from './onCreateServer'
4+
export * from './onCreateBabelConfig'
5+
export * from './onRenderBody'
6+
export * from './wrapRootElement'

0 commit comments

Comments
 (0)