This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: major rewrite, separated loader & runtime from webpack plugin
- min webpack version bumped to v5.0.0-beta.15 - added support for module.hot.invalidate - updated configuration instructions - added support for descriptor entries - added optional plugin for embedding react-error-overlay
- Loading branch information
Showing
20 changed files
with
601 additions
and
536 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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/prettierrc", | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
"singleQuote": true | ||
} |
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
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
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,34 @@ | ||
//@ts-check | ||
const { | ||
setEditorHandler, | ||
startReportingRuntimeErrors, | ||
dismissRuntimeErrors, | ||
} = require('react-error-overlay'); | ||
const launchEditorEndpoint = require('react-dev-utils/launchEditorEndpoint'); | ||
|
||
setEditorHandler((errorLocation) => { | ||
// Keep this sync with errorOverlayMiddleware.js | ||
fetch( | ||
launchEditorEndpoint + | ||
'?fileName=' + | ||
window.encodeURIComponent(errorLocation.fileName) + | ||
'&lineNumber=' + | ||
window.encodeURIComponent(errorLocation.lineNumber || 1) + | ||
'&colNumber=' + | ||
window.encodeURIComponent(errorLocation.colNumber || 1) | ||
); | ||
}); | ||
|
||
startReportingRuntimeErrors({ | ||
onError() { | ||
//@ts-ignore | ||
if (module.hot) { | ||
module.hot.addStatusHandler((status) => { | ||
if (status === 'apply') { | ||
// window.location.reload(); | ||
dismissRuntimeErrors(); | ||
} | ||
}); | ||
} | ||
}, | ||
}); |
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,38 @@ | ||
const querystring = require('querystring'); | ||
const url = require('url'); | ||
const SockJS = require('sockjs-client'); | ||
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages'); | ||
const { reportBuildError, dismissBuildError } = require('react-error-overlay'); | ||
|
||
let sockOptions = {}; | ||
if (typeof __resourceQuery === 'string' && __resourceQuery.length > 1) { | ||
sockOptions = querystring.parse(__resourceQuery.substr(1)); | ||
} | ||
|
||
const connection = new SockJS( | ||
url.format({ | ||
protocol: window.location.protocol, | ||
hostname: sockOptions.sockHost || window.location.hostname, | ||
port: sockOptions.sockPort || window.location.port, | ||
pathname: sockOptions.sockPath || '/sockjs-node', | ||
}) | ||
); | ||
|
||
connection.onmessage = function onmessage(e) { | ||
const { type, data } = JSON.parse(e.data); | ||
let formatted; | ||
switch (type) { | ||
case 'ok': | ||
dismissBuildError(); | ||
break; | ||
case 'errors': | ||
formatted = formatWebpackMessages({ | ||
errors: data, | ||
warnings: [], | ||
}); | ||
reportBuildError(formatted.errors[0]); | ||
break; | ||
default: | ||
// Do nothing. | ||
} | ||
}; |
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,107 @@ | ||
//@ts-check | ||
const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware'); | ||
|
||
const chunkPathBasic = require.resolve('./entry-basic.js'); | ||
const chunkPathDevServer = require.resolve('./entry-devserver.js'); | ||
let deps = []; | ||
|
||
/** @typedef {{ sockHost: string, sockPath: string, sockPort: number }} SockOptions */ | ||
/** @typedef {{ dependOn?: [string, ...string[]], filename?: any, import: [string, ...string[]], library?: any }} EntryDescriptionNormalized */ | ||
|
||
class ErrorOverlayPlugin { | ||
apply(/** @type { import("webpack").Compiler } */ compiler) { | ||
const className = this.constructor.name; | ||
|
||
if (compiler.options.mode !== 'development') { | ||
return; | ||
} | ||
|
||
const devServerEnabled = !!compiler.options.devServer; | ||
const usingSocket = typeof compiler.options.devServer.socket === 'string'; | ||
|
||
/** @type { SockOptions } */ | ||
const sockOptions = {}; | ||
if (devServerEnabled && usingSocket) { | ||
sockOptions.sockHost = compiler.options.devServer.sockHost; | ||
sockOptions.sockPath = compiler.options.devServer.sockPath; | ||
sockOptions.sockPort = compiler.options.devServer.sockPort; | ||
} | ||
|
||
//@ts-ignore | ||
compiler.hooks.entryOption.tap(className, (context, entry) => { | ||
if (typeof entry !== 'function') { | ||
Object.keys(entry).forEach((entryName) => { | ||
if (deps.includes(entryName)) { | ||
// Skip dependencies, only inject real entry points | ||
return; | ||
} | ||
entry[entryName] = adjustEntry( | ||
entry[entryName], | ||
devServerEnabled, | ||
usingSocket, | ||
sockOptions | ||
); | ||
}); | ||
} | ||
}); | ||
|
||
compiler.hooks.afterResolvers.tap(className, ({ options }) => { | ||
if (devServerEnabled) { | ||
const originalBefore = options.devServer.before; | ||
options.devServer.before = (app, server) => { | ||
if (originalBefore) { | ||
originalBefore(app, server); | ||
} | ||
app.use(errorOverlayMiddleware()); | ||
}; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Puts dev server chunk path in front of other entries | ||
* @param {EntryDescriptionNormalized} entry | ||
* @param {boolean} enableDevServer | ||
* @param {boolean} usingSocket | ||
* @param {SockOptions} sockOptions | ||
* @returns {EntryDescriptionNormalized} | ||
*/ | ||
function adjustEntry(entry, enableDevServer, usingSocket, sockOptions) { | ||
if (entry.dependOn) { | ||
deps = [...deps, ...entry.dependOn]; | ||
} | ||
|
||
if (entry.library) { | ||
// skip libraries | ||
return entry; | ||
} | ||
|
||
if (typeof entry.import === 'string') { | ||
entry.import = [entry.import]; | ||
} | ||
|
||
if (enableDevServer && usingSocket) { | ||
const sockHost = sockOptions.sockHost | ||
? `&sockHost=${sockOptions.sockHost}` | ||
: ''; | ||
const sockPath = sockOptions.sockPath | ||
? `&sockPath=${sockOptions.sockPath}` | ||
: ''; | ||
const sockPort = sockOptions.sockPort | ||
? `&sockPort=${sockOptions.sockPort}` | ||
: ''; | ||
const chunkPathDevServerWithParams = `${chunkPathDevServer}?${sockHost}${sockPath}${sockPort}`; | ||
if (!entry.import.includes(chunkPathDevServerWithParams)) { | ||
entry.import.unshift(chunkPathDevServerWithParams); | ||
} | ||
} | ||
|
||
if (!entry.import.includes(chunkPathBasic)) { | ||
entry.import.unshift(chunkPathBasic); | ||
} | ||
|
||
return entry; | ||
} | ||
|
||
module.exports = ErrorOverlayPlugin; |
Oops, something went wrong.