75748caf7f
This replaces the server-sent events HMR connection with a WebSocket connection to prevent hitting browser connection limits, allow sending events back from the browser, and overall better performance. This approach sets up the the `upgrade` event listener on the server immediately when created via `next dev` and on the first request using `req.socket.server` when created via a custom server. In a follow-up PR we can push the files changed via the WebSocket as well. x-ref: https://github.com/vercel/next.js/issues/10061 x-ref: https://github.com/vercel/next.js/issues/8064 x-ref: https://github.com/vercel/next.js/issues/4495 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes
112 lines
3.5 KiB
JavaScript
112 lines
3.5 KiB
JavaScript
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
const findUp = require('find-up')
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
const ncc = require('@vercel/ncc')
|
|
const { existsSync, readFileSync } = require('fs')
|
|
const { basename, dirname, extname, join, resolve } = require('path')
|
|
const { Module } = require('module')
|
|
|
|
// See taskfile.js bundleContext definition for explanation
|
|
const m = new Module(resolve(__dirname, 'bundles', '_'))
|
|
m.filename = m.id
|
|
m.paths = Module._nodeModulePaths(m.id)
|
|
const bundleRequire = m.require
|
|
bundleRequire.resolve = (request, options) =>
|
|
Module._resolveFilename(request, m, false, options)
|
|
|
|
module.exports = function (task) {
|
|
// eslint-disable-next-line require-yield
|
|
task.plugin('ncc', {}, function* (file, options) {
|
|
if (options.externals && options.packageName) {
|
|
options.externals = { ...options.externals }
|
|
delete options.externals[options.packageName]
|
|
}
|
|
let precompiled = options.precompiled !== false
|
|
delete options.precompiled
|
|
|
|
return ncc(join(__dirname, file.dir, file.base), {
|
|
filename: file.base,
|
|
minify: options.minify === false ? false : true,
|
|
...options,
|
|
}).then(({ code, assets }) => {
|
|
Object.keys(assets).forEach((key) => {
|
|
let data = assets[key].source
|
|
|
|
this._.files.push({
|
|
data,
|
|
base: basename(key),
|
|
dir: join(file.dir, dirname(key)),
|
|
})
|
|
})
|
|
|
|
if (options && options.packageName) {
|
|
writePackageManifest.call(
|
|
this,
|
|
options.packageName,
|
|
file.base,
|
|
options.bundleName,
|
|
precompiled
|
|
)
|
|
}
|
|
|
|
file.data = Buffer.from(code, 'utf8')
|
|
})
|
|
})
|
|
}
|
|
|
|
// This function writes a minimal `package.json` file for a compiled package.
|
|
// It defines `name`, `main`, `author`, and `license`. It also defines `types`.
|
|
// n.b. types intended for development usage only.
|
|
function writePackageManifest(packageName, main, bundleName, precompiled) {
|
|
// some newer packages fail to include package.json in the exports
|
|
// so we can't reliably use require.resolve here
|
|
let packagePath
|
|
|
|
try {
|
|
packagePath = bundleRequire.resolve(packageName + '/package.json')
|
|
} catch (_) {
|
|
packagePath = findUp.sync('package.json', {
|
|
cwd: dirname(bundleRequire.resolve(packageName)),
|
|
})
|
|
}
|
|
let { name, author, license } = require(packagePath)
|
|
|
|
const compiledPackagePath = join(
|
|
__dirname,
|
|
`${!precompiled ? 'dist/' : ''}compiled/${bundleName || packageName}`
|
|
)
|
|
|
|
const potentialLicensePath = join(dirname(packagePath), './LICENSE')
|
|
if (existsSync(potentialLicensePath)) {
|
|
this._.files.push({
|
|
dir: compiledPackagePath,
|
|
base: 'LICENSE',
|
|
data: readFileSync(potentialLicensePath, 'utf8'),
|
|
})
|
|
} else {
|
|
// license might be lower case and not able to be found on case-sensitive
|
|
// file systems (ubuntu)
|
|
const otherPotentialLicensePath = join(dirname(packagePath), './license')
|
|
if (existsSync(otherPotentialLicensePath)) {
|
|
this._.files.push({
|
|
dir: compiledPackagePath,
|
|
base: 'LICENSE',
|
|
data: readFileSync(otherPotentialLicensePath, 'utf8'),
|
|
})
|
|
}
|
|
}
|
|
|
|
this._.files.push({
|
|
dir: compiledPackagePath,
|
|
base: 'package.json',
|
|
data:
|
|
JSON.stringify(
|
|
Object.assign(
|
|
{},
|
|
{ name, main: `${basename(main, '.' + extname(main))}` },
|
|
author ? { author } : undefined,
|
|
license ? { license } : undefined
|
|
)
|
|
) + '\n',
|
|
})
|
|
}
|