1
0
Fork 0
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:
nkzawa 2016-10-19 21:58:08 +09:00
parent 2e2db37ccb
commit ec774a39da
5 changed files with 19 additions and 19 deletions

View file

@ -1,6 +1,6 @@
#!/usr/bin/env node
import { resolve } from 'path'
import { join } from 'path'
import { spawn } from 'cross-spawn'
const defaultCommand = 'dev'
@ -21,7 +21,7 @@ if (commands.has(cmd)) {
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] })
proc.on('close', (code) => process.exit(code))

View file

@ -14,17 +14,17 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
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 defaultErrorPath = resolve(nextPagesDir, '_error.js')
const defaultErrorPath = join(nextPagesDir, '_error.js')
if (!entry[errorEntry]) entry[errorEntry] = defaultErrorPath
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
const nodeModulesDir = resolve(__dirname, '..', '..', '..', 'node_modules')
const nodeModulesDir = join(__dirname, '..', '..', '..', 'node_modules')
const plugins = [
hotReload
@ -54,7 +54,7 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
.concat(hotReload ? [{
test: /\.js$/,
loader: 'hot-self-accept-loader',
include: resolve(dir, 'pages')
include: join(dir, 'pages')
}] : [])
.concat([{
test: /\.js$/,
@ -91,7 +91,7 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
context: dir,
entry,
output: {
path: resolve(dir, '.next'),
path: join(dir, '.next'),
filename: '[name]',
libraryTarget: 'commonjs2',
publicPath: hotReload ? 'http://localhost:3030/' : null
@ -109,13 +109,13 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
resolve: {
root: [
nodeModulesDir,
resolve(dir, 'node_modules')
join(dir, 'node_modules')
]
},
resolveLoader: {
root: [
nodeModulesDir,
resolve(__dirname, 'loaders')
join(__dirname, 'loaders')
]
},
plugins,

View file

@ -41,12 +41,12 @@ export default class Server {
defineRoutes () {
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)
})
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)
})

View file

@ -1,4 +1,4 @@
import { resolve } from 'path'
import { join } from 'path'
import { parse } from 'url'
import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
@ -16,11 +16,11 @@ export async function render (url, ctx = {}, {
staticMarkup = false
} = {}) {
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 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 app = createElement(App, {
@ -54,7 +54,7 @@ export async function render (url, ctx = {}, {
export async function renderJSON (url, { dir = process.cwd() } = {}) {
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 }
}
@ -72,5 +72,5 @@ export function errorToJSON (err) {
}
function getPath (url) {
return parse(url || '/').pathname.slice(1).replace(/\.json$/, '')
return parse(url || '/').pathname.replace(/\.json$/, '')
}

View file

@ -1,9 +1,9 @@
import test from 'ava'
import { resolve } from 'path'
import { join } from 'path'
import build from '../server/build'
import { render as _render } from '../server/render'
const dir = resolve(__dirname, 'fixtures', 'basic')
const dir = join(__dirname, 'fixtures', 'basic')
test.before(() => build(dir))