Skip to content

Commit 08f7f10

Browse files
committed
Remove the use of CDN and bundle everything inside the app.
We still pre-build the prefetcher because it needs different webpack config which only targets browsers supports Service Workers.
1 parent 59d514e commit 08f7f10

9 files changed

+15
-149
lines changed

client/next-dev.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import 'react-hot-loader/patch'
22
import * as next from './next'
3-
import { requireModule } from '../lib/eval-script'
43

54
window.next = next
6-
module.exports = requireModule

client/next.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import domready from 'domready'
55
import { rehydrate } from '../lib/css'
66
import Router from '../lib/router'
77
import App from '../lib/app'
8-
import evalScript, { requireModule } from '../lib/eval-script'
8+
import evalScript from '../lib/eval-script'
99

1010
const {
1111
__NEXT_DATA__: {
@@ -41,5 +41,3 @@ domready(() => {
4141
if (ids) rehydrate(ids)
4242
render(createElement(App, appProps), container)
4343
})
44-
45-
module.exports = requireModule

gulpfile.js

+1-61
Original file line numberDiff line numberDiff line change
@@ -71,69 +71,9 @@ gulp.task('copy-bench-fixtures', () => {
7171
})
7272

7373
gulp.task('build', [
74-
'build-dev-client',
75-
'build-client',
7674
'build-prefetcher'
7775
])
7876

79-
gulp.task('build-dev-client', ['compile-lib', 'compile-client'], () => {
80-
return gulp
81-
.src('dist/client/next-dev.js')
82-
.pipe(webpack({
83-
quiet: true,
84-
output: { filename: 'next-dev.bundle.js', libraryTarget: 'var', library: 'require' },
85-
module: {
86-
loaders: [
87-
{
88-
test: /eval-script\.js$/,
89-
exclude: /node_modules/,
90-
loader: 'babel',
91-
query: {
92-
plugins: [
93-
'babel-plugin-transform-remove-strict-mode'
94-
]
95-
}
96-
}
97-
]
98-
}
99-
}))
100-
.pipe(gulp.dest('dist/client'))
101-
.pipe(notify('Built dev client'))
102-
})
103-
104-
gulp.task('build-client', ['compile-lib', 'compile-client'], () => {
105-
return gulp
106-
.src('dist/client/next.js')
107-
.pipe(webpack({
108-
quiet: true,
109-
output: { filename: 'next.bundle.js', libraryTarget: 'var', library: 'require' },
110-
plugins: [
111-
new webpack.webpack.DefinePlugin({
112-
'process.env': {
113-
NODE_ENV: JSON.stringify('production')
114-
}
115-
}),
116-
new webpack.webpack.optimize.UglifyJsPlugin()
117-
],
118-
module: {
119-
loaders: [
120-
{
121-
test: /eval-script\.js$/,
122-
exclude: /node_modules/,
123-
loader: 'babel',
124-
query: {
125-
plugins: [
126-
'babel-plugin-transform-remove-strict-mode'
127-
]
128-
}
129-
}
130-
]
131-
}
132-
}))
133-
.pipe(gulp.dest('dist/client'))
134-
.pipe(notify('Built release client'))
135-
})
136-
13777
gulp.task('build-prefetcher', ['compile-lib', 'compile-client'], () => {
13878
return gulp
13979
.src('client/next-prefetcher.js')
@@ -172,7 +112,7 @@ gulp.task('build-prefetcher', ['compile-lib', 'compile-client'], () => {
172112
.pipe(notify('Built release prefetcher'))
173113
})
174114

175-
gulp.task('test', () => {
115+
gulp.task('test', ['compile'], () => {
176116
return gulp.src('./test')
177117
.pipe(jest.default({
178118
coverage: true,

lib/eval-script.js

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,3 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom'
3-
import App from '../lib/app'
4-
import Link from '../lib/link'
5-
import * as Prefetch from '../lib/prefetch'
6-
import * as Css from '../lib/css'
7-
import Head from '../lib/head'
8-
9-
const modules = new Map([
10-
['react', React],
11-
['react-dom', ReactDOM],
12-
['next/app', App],
13-
['next/link', Link],
14-
['next/prefetch', Prefetch],
15-
['next/css', Css],
16-
['next/head', Head]
17-
])
18-
19-
function require (moduleName) {
20-
const name = moduleName
21-
const m = modules.get(name)
22-
23-
if (m) return m
24-
25-
throw new Error(`Module "${moduleName}" is not exists in the bundle`)
26-
}
27-
28-
export const requireModule = require
29-
301
/**
312
* IMPORTANT: This module is compiled *without* `use strict`
323
* so that when we `eval` a dependency below, we don't enforce
@@ -41,6 +12,7 @@ export const requireModule = require
4112

4213
export default function evalScript (script) {
4314
const module = { exports: {} }
15+
4416
eval(script) // eslint-disable-line no-eval
4517
return module.exports
4618
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"react": "15.4.1",
6464
"react-dom": "15.4.1",
6565
"react-hot-loader": "3.0.0-beta.6",
66-
"read-pkg-up": "2.0.0",
6766
"send": "0.14.1",
6867
"source-map-support": "0.4.6",
6968
"strip-ansi": "3.0.1",

server/build/webpack.js

+5-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export default async function createCompiler (dir, { dev = false } = {}) {
1818
ignore: 'pages/_document.js'
1919
})
2020

21-
const entry = {}
21+
const entry = {
22+
'main.js': dev ? require.resolve('../../client/next-dev') : require.resolve('../../client/next')
23+
}
24+
2225
const defaultEntries = dev
2326
? [join(__dirname, '..', '..', 'client/webpack-hot-middleware-client')] : []
2427
for (const p of pages) {
@@ -145,6 +148,7 @@ export default async function createCompiler (dir, { dev = false } = {}) {
145148
alias: {
146149
'babel-runtime': babelRuntimePath,
147150
react: require.resolve('react'),
151+
'react-dom': require.resolve('react-dom'),
148152
'next/link': require.resolve('../../lib/link'),
149153
'next/prefetch': require.resolve('../../lib/prefetch'),
150154
'next/css': require.resolve('../../lib/css'),
@@ -179,22 +183,6 @@ export default async function createCompiler (dir, { dev = false } = {}) {
179183
return `webpack:///${resourcePath}?${id}`
180184
}
181185
},
182-
externals: [
183-
'react',
184-
'react-dom',
185-
{
186-
[require.resolve('react')]: 'react',
187-
[require.resolve('../../lib/link')]: 'next/link',
188-
[require.resolve('../../lib/prefetch')]: 'next/prefetch',
189-
[require.resolve('../../lib/css')]: 'next/css',
190-
[require.resolve('../../lib/head')]: 'next/head',
191-
// React addons ask for React like this.
192-
// That causes webpack to push react into the app's bundle.
193-
// This fix simply prevents that and ask to use React from the next-bundle
194-
'./React': 'react',
195-
'./ReactDOM': 'react-dom'
196-
}
197-
],
198186
resolve: {
199187
root: [
200188
nodeModulesDir,

server/document.js

+1-35
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import React, { Component, PropTypes } from 'react'
22
import htmlescape from 'htmlescape'
33
import { renderStatic } from 'glamor/server'
4-
import readPkgUp from 'read-pkg-up'
5-
6-
const { pkg } = readPkgUp.sync({
7-
cwd: __dirname,
8-
normalize: false
9-
})
104

115
export default class Document extends Component {
126
static getInitialProps ({ renderPage }) {
@@ -82,37 +76,9 @@ export class NextScript extends Component {
8276
}
8377

8478
render () {
85-
const { staticMarkup, dev, cdn } = this.context._documentProps
8679
return <div>
87-
{staticMarkup ? null : createClientScript({ dev, cdn })}
8880
<script type='text/javascript' src='/_next/commons.js' />
81+
<script type='text/javascript' src='/_next/main.js' />
8982
</div>
9083
}
9184
}
92-
93-
function createClientScript ({ dev, cdn }) {
94-
if (dev) {
95-
return <script type='text/javascript' src='/_next/next-dev.bundle.js' />
96-
}
97-
98-
if (!cdn) {
99-
return <script type='text/javascript' src='/_next/next.bundle.js' />
100-
}
101-
102-
return <script dangerouslySetInnerHTML={{ __html: `
103-
(function () {
104-
load('https://cdn.zeit.co/next.js/${pkg.version}/next.min.js', function (err) {
105-
if (err) load('/_next/next.bundle.js')
106-
})
107-
function load (src, fn) {
108-
fn = fn || function () {}
109-
var script = document.createElement('script')
110-
script.src = src
111-
script.onload = function () { fn(null) }
112-
script.onerror = fn
113-
script.crossorigin = 'anonymous'
114-
document.body.appendChild(script)
115-
}
116-
})()
117-
`}} />
118-
}

server/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ export default class Server {
5555
await this.serveStatic(req, res, p)
5656
})
5757

58+
this.router.get('/_next/main.js', async (req, res, params) => {
59+
const p = join(this.dir, '.next/main.js')
60+
await this.serveStatic(req, res, p)
61+
})
62+
5863
this.router.get('/_next/commons.js', async (req, res, params) => {
5964
const p = join(this.dir, '.next/commons.js')
6065
await this.serveStatic(req, res, p)

test/integration.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict'
44

55
import { join } from 'path'
6-
import next from '../server/next'
6+
import next from '../dist/server/next'
77

88
const dir = join(__dirname, 'fixtures', 'basic')
99
const app = next({

0 commit comments

Comments
 (0)