-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,036 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.log | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+' }) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.