Skip to content

Commit

Permalink
fix: better compatible with webpack v4/v5 (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jan 7, 2024
1 parent ab0fbe2 commit 80e3086
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-olives-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-server-renderer": patch
---

fix: better compatible with webpack v4/v5
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/*
4 changes: 2 additions & 2 deletions src/webpack-plugin/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export class ReactSSRServerPlugin implements WebpackPluginInstance {

const entry = entryAssets[0]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!entry || typeof entry !== 'string') {
if (!entry) {
throw new Error(
`Entry "${entryName}" not found. Did you specify the correct entry option?`,
)
}

const bundle = {
entry,
entry: typeof entry === 'string' ? entry : entry.name,
files: {} as Record<string, Buffer | string>,
maps: {} as Record<string, unknown>,
}
Expand Down
9 changes: 6 additions & 3 deletions src/webpack-plugin/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ export const warn = (msg: string) => console.error(red(`${prefix} ${msg}\n`))
export const tip = (msg: string) => console.log(yellow(`${prefix} ${msg}\n`))

export const validate = (compiler: Compiler) => {
if (compiler.options.target !== 'node') {
const { externals, output, target } = compiler.options

if (target !== 'node') {
warn('webpack config `target` should be "node".')
}

if (
output.library?.type !== 'commonjs2' &&
// @ts-expect-error -- compatibility
compiler.options.output.libraryTarget !== 'commonjs2'
output.libraryTarget !== 'commonjs2'
) {
warn('webpack config `output.libraryTarget` should be "commonjs2".')
}

if (!compiler.options.externals) {
if (!externals) {
tip(
'It is recommended to externalize dependencies in the server build for ' +
'better build performance.',
Expand Down

0 comments on commit 80e3086

Please sign in to comment.