Skip to content

Commit

Permalink
Move to a more general plugin system; TODO: resolve server start vs w…
Browse files Browse the repository at this point in the history
…ebpack transpiler delay when loading plugins
  • Loading branch information
jmfirth committed Oct 26, 2016
1 parent c42b562 commit d63bf53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
15 changes: 4 additions & 11 deletions server/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import DynamicEntryPlugin from './plugins/dynamic-entry-plugin'
export default async function createCompiler (dir, { hotReload = false } = {}) {
dir = resolve(dir)

const entries = await glob('+(pages|api)/**/*.js', { cwd: dir })
const entries = await glob('!(node_modules|static)/**/*.js', { cwd: dir })

const entry = {}
const defaultEntries = hotReload ? ['webpack/hot/dev-server'] : []
Expand All @@ -19,7 +19,6 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
}

const nextPagesDir = join(__dirname, '..', '..', 'pages')
const nextApiDir = join(__dirname, '..', '..', 'api')

const errorEntry = join('bundles', 'pages', '_error.js')
const defaultErrorPath = join(nextPagesDir, '_error.js')
Expand Down Expand Up @@ -57,26 +56,20 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
const loaders = [{
test: /\.js$/,
loader: 'emit-file-loader',
include: [dir, nextPagesDir, nextApiDir],
exclude (str) {
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0 && str.indexOf(nextApiDir) !== 0
},
exclude: /node_modules/,
query: {
name: 'dist/[path][name].[ext]'
}
}]
.concat(hotReload ? [{
test: /\.js$/,
loader: 'hot-self-accept-loader',
include: [join(dir, 'pages'), join(dir, 'api')]
exclude: /node_modules/
}] : [])
.concat([{
test: /\.js$/,
loader: 'babel',
include: [dir, nextPagesDir, nextApiDir],
exclude (str) {
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0 && str.indexOf(nextApiDir) !== 0
},
exclude: /node_modules/,
query: {
presets: ['es2015', 'react'],
plugins: [
Expand Down
24 changes: 20 additions & 4 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Router from './router'
import { render, renderAPI, renderJSON, errorToJSON } from './render'
import HotReloader from './hot-reloader'
import { resolveFromList } from './resolve'
import requireModule from './require'
import glob from 'glob-promise'

export default class Server {
constructor ({ dir = '.', dev = false, hotReload = false }) {
Expand All @@ -23,6 +25,19 @@ export default class Server {
})
})

// @TODO: workaround delay between server starting and webpack transpiling plugins
// and emitting them in the final dir
setTimeout(() => { this.getRoutes(); console.log('loaded routes') }, 10000)
}

async getRoutes () {
const plugins = await glob('plugins/**/*.js')
for (const p of plugins) {
const plugin = await requireModule(join(this.dir, '.next', 'dist', p).replace(/\\/g, '/'))
if (plugin.default) {
plugin.default(this.addRoute.bind(this))
}
}
this.defineRoutes()
}

Expand All @@ -39,6 +54,11 @@ export default class Server {
})
}

addRoute (path, fn, method = 'GET') {
const { dir, dev } = this
this.router.add(method.toUpperCase(), path, async (req, res, params) => fn(req.url, { req, res, params }, { dir, dev }))
}

defineRoutes () {
this.router.get('/_next/:path+', async (req, res, params) => {
const p = join(__dirname, '..', 'client', ...(params.path || []))
Expand All @@ -50,10 +70,6 @@ export default class Server {
await this.serveStatic(req, res, p)
})

this.router.get('/api/:path+', async (req, res, params) => {
await this.renderAPI(req, res, params)
})

this.router.get('/:path+.json', async (req, res) => {
await this.renderJSON(req, res)
})
Expand Down

0 comments on commit d63bf53

Please sign in to comment.