diff --git a/server/build/webpack.js b/server/build/webpack.js index 0f90a8b75bb76..c8f67f27222ff 100644 --- a/server/build/webpack.js +++ b/server/build/webpack.js @@ -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'] : [] @@ -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') @@ -57,10 +56,7 @@ 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]' } @@ -68,15 +64,12 @@ export default async function createCompiler (dir, { hotReload = false } = {}) { .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: [ diff --git a/server/index.js b/server/index.js index ecbf95e96b44f..a5558bdd14315 100644 --- a/server/index.js +++ b/server/index.js @@ -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 }) { @@ -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() } @@ -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 || [])) @@ -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) })