-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add webpack-dev-server to peerDependencies, improve peerDependen…
…cy error handling, fix index-template.html path (#15149)
- Loading branch information
1 parent
bd71274
commit 2863e33
Showing
3 changed files
with
86 additions
and
1 deletion.
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 +1,5 @@ | ||
const { validatePeerDependencies } = require('./dist/errors') | ||
|
||
validatePeerDependencies() | ||
|
||
module.exports = require('./dist') |
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,80 @@ | ||
export interface MissingDependency { | ||
prettyName: string | ||
packageName: string | ||
} | ||
|
||
const peerDeps: MissingDependency[] = [ | ||
{ | ||
prettyName: 'Webpack Dev Server', | ||
packageName: 'webpack-dev-server', | ||
}, | ||
{ | ||
prettyName: 'Html Webpack Plugin', | ||
packageName: 'html-webpack-plugin', | ||
}, | ||
{ | ||
prettyName: 'Webpack', | ||
packageName: 'webpack', | ||
}, | ||
] | ||
|
||
const makePrettyLibs = (libs) => { | ||
return libs.reduce((acc, curr, idx) => { | ||
acc.prettyNames.push(curr.prettyName) | ||
acc.packageNames.push(curr.packageName) | ||
|
||
if (idx >= libs.length) return acc | ||
|
||
return { | ||
prettyNames: acc.prettyNames.join(', '), | ||
packageNames: acc.packageNames.join(' '), | ||
} | ||
}, { prettyNames: [], packageNames: [] }) | ||
} | ||
|
||
export class MissingPeerDependency extends Error { | ||
private libs: { prettyNames: string[], packageNames: string[] } | ||
|
||
constructor (message, | ||
libs: MissingDependency[]) { | ||
const prettyLibs = makePrettyLibs(libs) | ||
|
||
super(`${message} ${prettyLibs.prettyNames}`) | ||
this.name = 'PeerDependencyMissing' | ||
this.libs = prettyLibs | ||
|
||
Object.setPrototypeOf(this, MissingPeerDependency.prototype) | ||
} | ||
|
||
get prettyMessage () { | ||
return `❌ Missing ${this.libs.prettyNames}. Please install them with npm or yarn. | ||
npm i ${this.libs.packageNames} -D | ||
yarn add ${this.libs.packageNames} --dev | ||
Updating webpack config is unnecessary | ||
` | ||
} | ||
} | ||
|
||
export function validatePeerDependencies () { | ||
const missingPeerDeps = peerDeps.filter((peerDep) => { | ||
try { | ||
require(peerDep.packageName) | ||
} catch (err) { | ||
return true | ||
} | ||
|
||
return false | ||
}) | ||
|
||
if (missingPeerDeps.length) { | ||
const error = new MissingPeerDependency(`@cypress/webpack-dev-server is missing peer dependencies`, missingPeerDeps) | ||
|
||
console.error(error.prettyMessage) // eslint-disable-line | ||
|
||
throw error | ||
} | ||
|
||
return true | ||
} |