Skip to content

Commit 9b06a22

Browse files
committed
initial source
1 parent 404bee1 commit 9b06a22

File tree

16 files changed

+1036
-0
lines changed

16 files changed

+1036
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.log
2+
node_modules
3+
dist

bin/next

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
3+
import { resolve } from 'path'
4+
import parseArgs from 'minimist'
5+
import { spawn } from 'cross-spawn';
6+
7+
const defaultCommand = 'dev'
8+
const commands = new Set([
9+
defaultCommand,
10+
'build',
11+
'start'
12+
])
13+
14+
let cmd = process.argv[2]
15+
let args
16+
17+
if (commands.has(cmd)) {
18+
args = process.argv.slice(3)
19+
} else {
20+
cmd = defaultCommand
21+
args = process.argv.slice(2)
22+
}
23+
24+
const bin = resolve(__dirname, 'next-' + cmd)
25+
26+
const proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] })
27+
proc.on('close', (code) => process.exit(code))
28+
proc.on('error', (err) => {
29+
console.log(err)
30+
process.exit(1)
31+
})

bin/next-build

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
3+
import { resolve, dirname } from 'path'
4+
import parseArgs from 'minimist'
5+
import fs from 'mz/fs'
6+
import mkdirp from 'mkdirp-then';
7+
import glob from 'glob-promise'
8+
import { transpile, bundle } from '../server/build'
9+
10+
const argv = parseArgs(process.argv.slice(2), {
11+
alias: {
12+
h: 'help',
13+
},
14+
boolean: ['h']
15+
})
16+
17+
const dir = resolve(argv._[0] || '.')
18+
19+
Promise.resolve()
20+
.then(async () => {
21+
const paths = await glob('**/*.js', { cwd: dir, ignore: 'node_modules/**' })
22+
await Promise.all(paths.map(async (p) => {
23+
const code = await transpile(resolve(dir, p))
24+
const outpath = resolve(dir, '.next', p)
25+
await writeFile(outpath, code)
26+
}))
27+
28+
const pagePaths = await glob('.next/pages/**/*.js', { cwd: dir })
29+
await Promise.all(pagePaths.map(async (p) => {
30+
const code = await bundle(resolve(dir, p))
31+
const outpath = resolve(dir, '.next', p)
32+
await writeFile(outpath, code)
33+
}))
34+
})
35+
.catch((err) => {
36+
console.error(err)
37+
exit(1)
38+
})
39+
40+
async function writeFile (path, data) {
41+
await mkdirp(dirname(path))
42+
await fs.writeFile(path, data, { encoding: 'utf8', flag: 'w+' })
43+
}
44+

bin/next-start

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
3+
import parseArgs from 'minimist'
4+
import Server from '../server'
5+
6+
const argv = parseArgs(process.argv.slice(2), {
7+
alias: {
8+
h: 'help',
9+
p: 'port'
10+
},
11+
boolean: ['h'],
12+
default: {
13+
p: 3000
14+
}
15+
})
16+
17+
const dir = argv._[0] || '.'
18+
19+
const srv = new Server(dir)
20+
srv.start(argv.port)
21+
.then(() => {
22+
console.log('> Ready on http://localhost:%d', argv.port);
23+
})
24+
.catch((err) => {
25+
console.error(err)
26+
exit(1)
27+
})

client/eval-script.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import App from '../lib/app'
4+
5+
const modules = new Map([
6+
['react', React],
7+
['react-dom', ReactDOM],
8+
['next/app', App]
9+
])
10+
11+
/**
12+
* IMPORTANT: This module is compiled *without* `use strict`
13+
* so that when we `eval` a dependency below, we don't enforce
14+
* `use strict` implicitly.
15+
*
16+
* Otherwise, modules like `d3` get `eval`d and forced into
17+
* `use strict` where they don't work (at least in current versions)
18+
*
19+
* To see the compilation details, look at `gulpfile.js` and the
20+
* usage of `babel-plugin-transform-remove-strict-mode`.
21+
*/
22+
23+
export default function evalScript (script) {
24+
const module = { exports: {} }
25+
const require = function (path) { // eslint-disable-line no-unused-vars
26+
return modules.get(path)
27+
}
28+
// don't use Function() here since it changes source locations
29+
eval(script) // eslint-disable-line no-eval
30+
return module.exports
31+
}

client/next.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createElement } from 'react'
2+
import { render } from 'react-dom'
3+
import evalScript from './eval-script'
4+
import Router from './router'
5+
import DefaultApp from '../lib/app'
6+
7+
const {
8+
__NEXT_DATA__: { app, component, props }
9+
} = window
10+
11+
const App = app ? evalScript(app).default : DefaultApp
12+
const Component = evalScript(component).default
13+
14+
const router = new Router({ Component, props })
15+
const container = document.getElementById('__next')
16+
const appProps = { Component, props, router: {} }
17+
18+
render(createElement(App, { ...appProps }), container)

0 commit comments

Comments
 (0)