Unverified Commit 92ad5c0b authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

[Fast Refresh] Support client-side code being ran in Node (#12426)

parent 45d8a7a5
...@@ -27,10 +27,13 @@ function webpack4(compiler: Compiler) { ...@@ -27,10 +27,13 @@ function webpack4(compiler: Compiler) {
return source return source
} }
// Legacy CSS implementations will `eval` browser code in a Node.js
// context to extract CSS. For backwards compatibility, we need to check
// we're in a browser context before continuing.
return Template.asString([ return Template.asString([
...lines.slice(0, evalIndex), ...lines.slice(0, evalIndex),
` `
var hasRefresh = !!self.$RefreshInterceptModuleExecution$; var hasRefresh = typeof self !== "undefined" && !!self.$RefreshInterceptModuleExecution$;
var cleanup = hasRefresh var cleanup = hasRefresh
? self.$RefreshInterceptModuleExecution$(moduleId) ? self.$RefreshInterceptModuleExecution$(moduleId)
: function() {}; : function() {};
......
...@@ -14,59 +14,66 @@ declare const module: { ...@@ -14,59 +14,66 @@ declare const module: {
} }
} }
// This function gets unwrapped into global scope, which is why we don't invert
// if-blocks. Also, you cannot use `return`.
export default function() { export default function() {
const currentExports = module.__proto__.exports // Legacy CSS implementations will `eval` browser code in a Node.js context
const prevExports = module.hot.data?.prevExports ?? null // to extract CSS. For backwards compatibility, we need to check we're in a
// browser context before continuing.
if (typeof self !== 'undefined') {
const currentExports = module.__proto__.exports
const prevExports = module.hot.data?.prevExports ?? null
// This cannot happen in MainTemplate because the exports mismatch between // This cannot happen in MainTemplate because the exports mismatch between
// templating and execution. // templating and execution.
self.$RefreshHelpers$.registerExportsForReactRefresh( self.$RefreshHelpers$.registerExportsForReactRefresh(
currentExports, currentExports,
module.id module.id
) )
// A module can be accepted automatically based on its exports, e.g. when it // A module can be accepted automatically based on its exports, e.g. when
// is a Refresh Boundary. // it is a Refresh Boundary.
if (self.$RefreshHelpers$.isReactRefreshBoundary(currentExports)) { if (self.$RefreshHelpers$.isReactRefreshBoundary(currentExports)) {
// Save the previous exports on update so we can compare the boundary // Save the previous exports on update so we can compare the boundary
// signatures. // signatures.
module.hot.dispose(data => { module.hot.dispose(data => {
data.prevExports = currentExports data.prevExports = currentExports
}) })
// Unconditionally accept an update to this module, we'll check if it's // Unconditionally accept an update to this module, we'll check if it's
// still a Refresh Boundary later. // still a Refresh Boundary later.
module.hot.accept() module.hot.accept()
// This field is set when the previous version of this module was a Refresh // This field is set when the previous version of this module was a
// Boundary, letting us know we need to check for invalidation or enqueue // Refresh Boundary, letting us know we need to check for invalidation or
// an update. // enqueue an update.
if (prevExports !== null) { if (prevExports !== null) {
// A boundary can become ineligible if its exports are incompatible with // A boundary can become ineligible if its exports are incompatible
// the previous exports. // with the previous exports.
// //
// For example, if you add/remove/change exports, we'll want to // For example, if you add/remove/change exports, we'll want to
// re-execute the importing modules, and force those components to // re-execute the importing modules, and force those components to
// re-render. Similarly, if you convert a class component to a function, // re-render. Similarly, if you convert a class component to a
// we want to invalidate the boundary. // function, we want to invalidate the boundary.
if ( if (
self.$RefreshHelpers$.shouldInvalidateReactRefreshBoundary( self.$RefreshHelpers$.shouldInvalidateReactRefreshBoundary(
prevExports, prevExports,
currentExports currentExports
) )
) { ) {
module.hot.invalidate()
} else {
self.$RefreshHelpers$.scheduleUpdate()
}
}
} else {
// Since we just executed the code for the module, it's possible that the
// new exports made it ineligible for being a boundary.
// We only care about the case when we were _previously_ a boundary,
// because we already accepted this update (accidental side effect).
const isNoLongerABoundary = prevExports !== null
if (isNoLongerABoundary) {
module.hot.invalidate() module.hot.invalidate()
} else {
self.$RefreshHelpers$.scheduleUpdate()
} }
} }
} else {
// Since we just executed the code for the module, it's possible that the
// new exports made it ineligible for being a boundary.
// We only care about the case when we were _previously_ a boundary,
// because we already accepted this update (accidental side effect).
const isNoLongerABoundary = prevExports !== null
if (isNoLongerABoundary) {
module.hot.invalidate()
}
} }
} }
const withSass = require('@zeit/next-sass') const withSass = require('@zeit/next-sass')
module.exports = withSass({}) module.exports = withSass({ experimental: { reactRefresh: true } })
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import { remove } from 'fs-extra' import { remove } from 'fs-extra'
import { nextBuild } from 'next-test-utils' import { findPort, killApp, launchApp, nextBuild } from 'next-test-utils'
import webdriver from 'next-webdriver'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir' import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { join } from 'path' import { join } from 'path'
...@@ -24,3 +25,32 @@ describe('Legacy Sass Support Should Disable New CSS', () => { ...@@ -24,3 +25,32 @@ describe('Legacy Sass Support Should Disable New CSS', () => {
expect(cssFiles.length).toBe(1) expect(cssFiles.length).toBe(1)
}) })
}) })
describe('Legacy Sass Support should work in development', () => {
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
let appPort
let app
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})
it('should render the page', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
const exampleElText = await browser.elementByCss('.example').text()
expect(exampleElText).toBe('Hello World!')
} finally {
if (browser) {
await browser.close()
}
}
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment