Skip to content

Commit

Permalink
initial source
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzawa committed Oct 5, 2016
1 parent 404bee1 commit 9b06a22
Show file tree
Hide file tree
Showing 16 changed files with 1,036 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.log
node_modules
dist
31 changes: 31 additions & 0 deletions bin/next
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node

import { resolve } from 'path'
import parseArgs from 'minimist'
import { spawn } from 'cross-spawn';

const defaultCommand = 'dev'
const commands = new Set([
defaultCommand,
'build',
'start'
])

let cmd = process.argv[2]
let args

if (commands.has(cmd)) {
args = process.argv.slice(3)
} else {
cmd = defaultCommand
args = process.argv.slice(2)
}

const bin = resolve(__dirname, 'next-' + cmd)

const proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] })
proc.on('close', (code) => process.exit(code))
proc.on('error', (err) => {
console.log(err)
process.exit(1)
})
44 changes: 44 additions & 0 deletions bin/next-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node

import { resolve, dirname } from 'path'
import parseArgs from 'minimist'
import fs from 'mz/fs'
import mkdirp from 'mkdirp-then';
import glob from 'glob-promise'
import { transpile, bundle } from '../server/build'

const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
},
boolean: ['h']
})

const dir = resolve(argv._[0] || '.')

Promise.resolve()
.then(async () => {
const paths = await glob('**/*.js', { cwd: dir, ignore: 'node_modules/**' })
await Promise.all(paths.map(async (p) => {
const code = await transpile(resolve(dir, p))
const outpath = resolve(dir, '.next', p)
await writeFile(outpath, code)
}))

const pagePaths = await glob('.next/pages/**/*.js', { cwd: dir })
await Promise.all(pagePaths.map(async (p) => {
const code = await bundle(resolve(dir, p))
const outpath = resolve(dir, '.next', p)
await writeFile(outpath, code)
}))
})
.catch((err) => {
console.error(err)
exit(1)
})

async function writeFile (path, data) {
await mkdirp(dirname(path))
await fs.writeFile(path, data, { encoding: 'utf8', flag: 'w+' })
}

27 changes: 27 additions & 0 deletions bin/next-start
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

import parseArgs from 'minimist'
import Server from '../server'

const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
p: 'port'
},
boolean: ['h'],
default: {
p: 3000
}
})

const dir = argv._[0] || '.'

const srv = new Server(dir)
srv.start(argv.port)
.then(() => {
console.log('> Ready on http://localhost:%d', argv.port);
})
.catch((err) => {
console.error(err)
exit(1)
})
31 changes: 31 additions & 0 deletions client/eval-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from '../lib/app'

const modules = new Map([
['react', React],
['react-dom', ReactDOM],
['next/app', App]
])

/**
* IMPORTANT: This module is compiled *without* `use strict`
* so that when we `eval` a dependency below, we don't enforce
* `use strict` implicitly.
*
* Otherwise, modules like `d3` get `eval`d and forced into
* `use strict` where they don't work (at least in current versions)
*
* To see the compilation details, look at `gulpfile.js` and the
* usage of `babel-plugin-transform-remove-strict-mode`.
*/

export default function evalScript (script) {
const module = { exports: {} }
const require = function (path) { // eslint-disable-line no-unused-vars
return modules.get(path)
}
// don't use Function() here since it changes source locations
eval(script) // eslint-disable-line no-eval
return module.exports
}
18 changes: 18 additions & 0 deletions client/next.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createElement } from 'react'
import { render } from 'react-dom'
import evalScript from './eval-script'
import Router from './router'
import DefaultApp from '../lib/app'

const {
__NEXT_DATA__: { app, component, props }
} = window

const App = app ? evalScript(app).default : DefaultApp
const Component = evalScript(component).default

const router = new Router({ Component, props })
const container = document.getElementById('__next')
const appProps = { Component, props, router: {} }

render(createElement(App, { ...appProps }), container)
Loading

0 comments on commit 9b06a22

Please sign in to comment.