mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
refactor paths
This commit is contained in:
parent
2e2db37ccb
commit
ec774a39da
4
bin/next
4
bin/next
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import { resolve } from 'path'
|
import { join } from 'path'
|
||||||
import { spawn } from 'cross-spawn'
|
import { spawn } from 'cross-spawn'
|
||||||
|
|
||||||
const defaultCommand = 'dev'
|
const defaultCommand = 'dev'
|
||||||
|
@ -21,7 +21,7 @@ if (commands.has(cmd)) {
|
||||||
args = process.argv.slice(2)
|
args = process.argv.slice(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
const bin = resolve(__dirname, 'next-' + cmd)
|
const bin = join(__dirname, 'next-' + cmd)
|
||||||
|
|
||||||
const proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] })
|
const proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] })
|
||||||
proc.on('close', (code) => process.exit(code))
|
proc.on('close', (code) => process.exit(code))
|
||||||
|
|
|
@ -14,17 +14,17 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
|
||||||
entry[join('bundles', p)] = defaultEntries.concat(['./' + p])
|
entry[join('bundles', p)] = defaultEntries.concat(['./' + p])
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextPagesDir = resolve(__dirname, '..', '..', 'pages')
|
const nextPagesDir = join(__dirname, '..', '..', 'pages')
|
||||||
|
|
||||||
const errorEntry = join('bundles', 'pages', '_error.js')
|
const errorEntry = join('bundles', 'pages', '_error.js')
|
||||||
const defaultErrorPath = resolve(nextPagesDir, '_error.js')
|
const defaultErrorPath = join(nextPagesDir, '_error.js')
|
||||||
if (!entry[errorEntry]) entry[errorEntry] = defaultErrorPath
|
if (!entry[errorEntry]) entry[errorEntry] = defaultErrorPath
|
||||||
|
|
||||||
const errorDebugEntry = join('bundles', 'pages', '_error-debug.js')
|
const errorDebugEntry = join('bundles', 'pages', '_error-debug.js')
|
||||||
const errorDebugPath = resolve(nextPagesDir, '_error-debug.js')
|
const errorDebugPath = join(nextPagesDir, '_error-debug.js')
|
||||||
entry[errorDebugEntry] = errorDebugPath
|
entry[errorDebugEntry] = errorDebugPath
|
||||||
|
|
||||||
const nodeModulesDir = resolve(__dirname, '..', '..', '..', 'node_modules')
|
const nodeModulesDir = join(__dirname, '..', '..', '..', 'node_modules')
|
||||||
|
|
||||||
const plugins = [
|
const plugins = [
|
||||||
hotReload
|
hotReload
|
||||||
|
@ -54,7 +54,7 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
|
||||||
.concat(hotReload ? [{
|
.concat(hotReload ? [{
|
||||||
test: /\.js$/,
|
test: /\.js$/,
|
||||||
loader: 'hot-self-accept-loader',
|
loader: 'hot-self-accept-loader',
|
||||||
include: resolve(dir, 'pages')
|
include: join(dir, 'pages')
|
||||||
}] : [])
|
}] : [])
|
||||||
.concat([{
|
.concat([{
|
||||||
test: /\.js$/,
|
test: /\.js$/,
|
||||||
|
@ -91,7 +91,7 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
|
||||||
context: dir,
|
context: dir,
|
||||||
entry,
|
entry,
|
||||||
output: {
|
output: {
|
||||||
path: resolve(dir, '.next'),
|
path: join(dir, '.next'),
|
||||||
filename: '[name]',
|
filename: '[name]',
|
||||||
libraryTarget: 'commonjs2',
|
libraryTarget: 'commonjs2',
|
||||||
publicPath: hotReload ? 'http://localhost:3030/' : null
|
publicPath: hotReload ? 'http://localhost:3030/' : null
|
||||||
|
@ -109,13 +109,13 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
|
||||||
resolve: {
|
resolve: {
|
||||||
root: [
|
root: [
|
||||||
nodeModulesDir,
|
nodeModulesDir,
|
||||||
resolve(dir, 'node_modules')
|
join(dir, 'node_modules')
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
resolveLoader: {
|
resolveLoader: {
|
||||||
root: [
|
root: [
|
||||||
nodeModulesDir,
|
nodeModulesDir,
|
||||||
resolve(__dirname, 'loaders')
|
join(__dirname, 'loaders')
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
plugins,
|
plugins,
|
||||||
|
|
|
@ -41,12 +41,12 @@ export default class Server {
|
||||||
|
|
||||||
defineRoutes () {
|
defineRoutes () {
|
||||||
this.router.get('/_next/:path+', async (req, res, params) => {
|
this.router.get('/_next/:path+', async (req, res, params) => {
|
||||||
const p = resolve(__dirname, '../client', (params.path || []).join('/'))
|
const p = join(__dirname, '..', 'client', ...(params.path || []))
|
||||||
await this.serveStatic(req, res, p)
|
await this.serveStatic(req, res, p)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.router.get('/static/:path+', async (req, res, params) => {
|
this.router.get('/static/:path+', async (req, res, params) => {
|
||||||
const p = resolve(this.dir, 'static', (params.path || []).join('/'))
|
const p = join(this.dir, 'static', ...(params.path || []))
|
||||||
await this.serveStatic(req, res, p)
|
await this.serveStatic(req, res, p)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { resolve } from 'path'
|
import { join } from 'path'
|
||||||
import { parse } from 'url'
|
import { parse } from 'url'
|
||||||
import { createElement } from 'react'
|
import { createElement } from 'react'
|
||||||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
||||||
|
@ -16,11 +16,11 @@ export async function render (url, ctx = {}, {
|
||||||
staticMarkup = false
|
staticMarkup = false
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const path = getPath(url)
|
const path = getPath(url)
|
||||||
const mod = await requireModule(resolve(dir, '.next', 'dist', 'pages', path))
|
const mod = await requireModule(join(dir, '.next', 'dist', 'pages', path))
|
||||||
const Component = mod.default || mod
|
const Component = mod.default || mod
|
||||||
|
|
||||||
const props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
const props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
||||||
const component = await read(resolve(dir, '.next', 'bundles', 'pages', path))
|
const component = await read(join(dir, '.next', 'bundles', 'pages', path))
|
||||||
|
|
||||||
const { html, css } = StyleSheetServer.renderStatic(() => {
|
const { html, css } = StyleSheetServer.renderStatic(() => {
|
||||||
const app = createElement(App, {
|
const app = createElement(App, {
|
||||||
|
@ -54,7 +54,7 @@ export async function render (url, ctx = {}, {
|
||||||
|
|
||||||
export async function renderJSON (url, { dir = process.cwd() } = {}) {
|
export async function renderJSON (url, { dir = process.cwd() } = {}) {
|
||||||
const path = getPath(url)
|
const path = getPath(url)
|
||||||
const component = await read(resolve(dir, '.next', 'bundles', 'pages', path))
|
const component = await read(join(dir, '.next', 'bundles', 'pages', path))
|
||||||
return { component }
|
return { component }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,5 +72,5 @@ export function errorToJSON (err) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPath (url) {
|
function getPath (url) {
|
||||||
return parse(url || '/').pathname.slice(1).replace(/\.json$/, '')
|
return parse(url || '/').pathname.replace(/\.json$/, '')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import test from 'ava'
|
import test from 'ava'
|
||||||
import { resolve } from 'path'
|
import { join } from 'path'
|
||||||
import build from '../server/build'
|
import build from '../server/build'
|
||||||
import { render as _render } from '../server/render'
|
import { render as _render } from '../server/render'
|
||||||
|
|
||||||
const dir = resolve(__dirname, 'fixtures', 'basic')
|
const dir = join(__dirname, 'fixtures', 'basic')
|
||||||
|
|
||||||
test.before(() => build(dir))
|
test.before(() => build(dir))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue