Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support webpack 5 #98

Merged
merged 41 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
fb7781c
Upgrade to webpack v5.0.0
jayaddison Oct 11, 2020
a66eb15
Typing: Plugin -> WebpackPluginInstance
jayaddison Oct 11, 2020
c6d0528
Typing: use WebpackError type definition from webpack
jayaddison Oct 11, 2020
ef3582e
Typing: update exit promise resolution handler
jayaddison Oct 11, 2020
0f33cf2
Typing: Use upstream Chunk, Module types from webpack, and adhere to …
jayaddison Oct 11, 2020
a23607c
Update to use new ChunkGraph, ModuleGraph APIs
jayaddison Oct 11, 2020
4790470
Add Travis build env definition for webpack v5
jayaddison Oct 11, 2020
bb9674f
Update yarn.lock
jayaddison Oct 11, 2020
21d9bc0
Merge branch 'master' into upgrades/webpack-5
jayaddison Oct 19, 2020
90b4b07
Revert "Typing: update exit promise resolution handler"
jayaddison Oct 19, 2020
601a919
Treat chunk.files as a Set
jayaddison Oct 19, 2020
60f82a1
Remove Travis build env definitions for webpack v4
jayaddison Oct 19, 2020
f59957b
Merge branch 'master' into upgrades/webpack-5
jayaddison Oct 29, 2020
986535f
Revert "Revert "Typing: update exit promise resolution handler""
jayaddison Oct 29, 2020
a820379
Bump required dependencies
skjnldsv Nov 14, 2020
5d9914e
Remove unnecessary write-file-webpack-plugin
skjnldsv Nov 14, 2020
80ed9a9
Fix assert polyfill requirements
skjnldsv Nov 14, 2020
10cfbce
Make sure we have a valid error message
skjnldsv Nov 14, 2020
d57d9ef
Remove test:file script
jayaddison Nov 19, 2020
59feadb
Fixup: determine moduleId on a per-module basis
jayaddison Dec 1, 2020
3ff335e
Adjust test runner callback handling
jayaddison Dec 14, 2020
9255dba
Revert "Make sure we have a valid error message"
jayaddison Dec 14, 2020
7932938
Revert ensureWebpackErrors implementation changes
jayaddison Dec 14, 2020
712ebdb
Restore webpackStats argument to WEBPACK_READY_EVENT emissions
jayaddison Dec 14, 2020
29d77f2
fix exit code build problem for watch return type
verasztol Feb 6, 2021
10ad998
Merge pull request #2 from verasztol/upgrades/webpack-5
jayaddison Feb 7, 2021
743b0aa
fix: fix isBuilt for webpack 5
haoqunjiang May 11, 2021
22bc90c
fix: fix .map usage
haoqunjiang May 11, 2021
e03a33a
test: fix worker-loader test
haoqunjiang May 11, 2021
2676026
Merge branch 'master' into webpack-5-compat
haoqunjiang May 11, 2021
e67d658
chore: fix yarn lock
haoqunjiang May 11, 2021
ccd2434
fix: fix type error
haoqunjiang May 12, 2021
1ce91e2
test: fix tests that requires on-disk files
haoqunjiang May 12, 2021
7b72058
fix: fix invalid dependencies module warning
haoqunjiang May 12, 2021
0df4b00
fix: add back webpack 4 implementation
haoqunjiang May 12, 2021
6038a1a
ci: add webpack 4 tests
haoqunjiang May 12, 2021
a630b52
ci: fix travis config
haoqunjiang May 12, 2021
c5dd854
ci: fix eslint errors, etc.
haoqunjiang May 12, 2021
4ebefa1
chore: allow webpack 4 as peer dependency
haoqunjiang May 12, 2021
1c66b12
ci: add mocha 6 test back
haoqunjiang May 12, 2021
b30cc73
chore: lockfile maintenance
haoqunjiang May 12, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ env:
- WEBPACK_VERSION=4 MOCHA_VERSION=6
- WEBPACK_VERSION=4 MOCHA_VERSION=7
- WEBPACK_VERSION=4 MOCHA_VERSION=8
- WEBPACK_VERSION=5 MOCHA_VERSION=7
- WEBPACK_VERSION=5 MOCHA_VERSION=8

before_script:
- "yarn add -D webpack@$WEBPACK_VERSION"
- "yarn add -D mocha@$MOCHA_VERSION"

# Need to skip ts check for getAffectedModuleIds in webpack 4
- |
if [ $WEBPACK_VERSION == 4 ]; then
echo -e "// @ts-nocheck\n/*eslint-disable*/\n$(cat src/webpack/util/getAffectedModuleIds.ts)" > src/webpack/util/getAffectedModuleIds.ts;
fi
- |
if [ $WEBPACK_VERSION == 5 ]; then
echo -e "// @ts-nocheck\n$(cat src/webpack/util/webpack4GetBuildStats.ts)" > src/webpack/util/webpack4GetBuildStats.ts;
fi

script:
- yarn run cover

Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"license": "MIT",
"peerDependencies": {
"mocha": ">=6 <9",
"webpack": "^4.0.0"
"webpack": "^4.0.0 || ^5.0.0"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
Expand All @@ -66,11 +66,11 @@
"@types/node": "^12.12.17",
"@types/sinon": "^9.0.0",
"@types/sinon-chai": "^3.2.4",
"@types/webpack": "^4.41.0",
"@types/yargs": "^15.0.4",
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"anymatch": "3.1.1",
"assert": "^2.0.0",
"babel-eslint": "^9.0.0",
"babel-loader": "^8.0.0",
"babel-plugin-istanbul": "4.1.6",
Expand All @@ -79,11 +79,11 @@
"chai": "^4.1.0",
"coffee-script": "^1.11.1",
"cross-env": "6.0.3",
"css-loader": "3.2.0",
"css-loader": "^5.0.1",
"del": "5.1.0",
"del-cli": "3.0.0",
"eslint": "^6.7.2",
"eslint-config-airbnb-base": "12.1.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.9.0",
"fs-extra": "5.0.0",
Expand All @@ -99,16 +99,15 @@
"np": "5.1.0",
"nyc": "14.1.1",
"prettier": "^1.19.1",
"sass-loader": "6.0.7",
"sass-loader": "^10.1.0",
"sinon": "^9.0.2",
"sinon-chai": "^3.5.0",
"strip-ansi": "^5.2.0",
"tiny-worker": "2.3.0",
"ts-mocha": "^7.0.0",
"typescript": "^3.8.3",
"webpack": "4.41.0",
"worker-loader": "2.0.0",
"write-file-webpack-plugin": "^4.2.0"
"webpack": "5.0.0",
"worker-loader": "^3.0.5"
},
"dependencies": {
"@babel/runtime-corejs2": "^7.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ async function cli() {
}
return mochaWebpack.run()
})
.then((failures: number) => {
exit(cliOptions.mocha.cli.exit, failures)
.then((failures: number | void) => {
exit(cliOptions.mocha.cli.exit, failures || 0)
})
.catch((e: Error) => {
if (e) console.error(e.stack) // eslint-disable-line
Expand Down
14 changes: 11 additions & 3 deletions src/runner/TestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import createWatchCompiler, {
import registerInMemoryCompiler from '../webpack/compiler/registerInMemoryCompiler'
import registerReadyCallback from '../webpack/compiler/registerReadyCallback'
import getBuildStats, { BuildStats } from '../webpack/util/getBuildStats'
import webpack4GetBuildStats from '../webpack/util/webpack4GetBuildStats'
import webpack from 'webpack'

import createWebpackConfig from './runnerUtils/createWebpackConfig'
import {
Expand Down Expand Up @@ -73,7 +75,13 @@ export default class TestRunner extends EventEmitter {
prepareMocha(webpackConfig: WebpackConfig, stats: Stats): Mocha {
const mocha: Mocha = initMocha(this.options.mocha, this.cwd)
const outputPath = webpackConfig.output.path
const buildStats: BuildStats = getBuildStats(stats, outputPath)

let buildStats: BuildStats
if (webpack.version[0] === '4') {
buildStats = webpack4GetBuildStats(stats, outputPath)
} else {
buildStats = getBuildStats(stats, outputPath)
}

// @ts-ignore
global.__webpackManifest__ = buildStats.affectedModules // eslint-disable-line
Expand Down Expand Up @@ -106,7 +114,7 @@ export default class TestRunner extends EventEmitter {
failures = await new Promise((resolve, reject) => {
registerReadyCallback(
compiler,
(err: (Error | string) | null, webpackStats: Stats | null) => {
(err: Error, webpackStats: Stats) => {
this.emit(WEBPACK_READY_EVENT, err, webpackStats)
if (err || !webpackStats) {
reject()
Expand Down Expand Up @@ -216,7 +224,7 @@ export default class TestRunner extends EventEmitter {
// register webpack ready callback
registerReadyCallback(
compiler,
(err: (Error | string) | null, webpackStats: Stats | null) => {
(err: Error, webpackStats: Stats) => {
this.emit(WEBPACK_READY_EVENT, err, webpackStats)
if (err) {
// wait for fixed tests
Expand Down
4 changes: 2 additions & 2 deletions src/runner/runnerUtils/createWebpackConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
has as _has,
merge as _merge
} from 'lodash'
import { Configuration, Plugin, RuleSetRule } from 'webpack'
import { Configuration, WebpackPluginInstance, RuleSetRule } from 'webpack'
import { glob } from '../../../util/glob'
import { EntryConfig } from '../../../webpack/loader/entryLoader'
import { buildProgressPlugin } from '../../../webpack/plugin/buildProgressPlugin'
Expand Down Expand Up @@ -48,7 +48,7 @@ const getPublicPath = (
const buildPluginsArray = (
webpackConfig: Configuration,
interactive: boolean
): Plugin[] => {
): WebpackPluginInstance[] => {
const plugins = webpackConfig.plugins || []

if (interactive) {
Expand Down
4 changes: 2 additions & 2 deletions src/runner/runnerUtils/createWebpackConfig/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Configuration, Plugin, RuleSetRule } from 'webpack'
import { Configuration, WebpackPluginInstance, RuleSetRule } from 'webpack'
import { EntryConfig } from '../../../webpack/loader/entryLoader'

export interface CreateWebpackConfigOptions {
Expand All @@ -19,7 +19,7 @@ export interface BuildLoaderRulesOptions extends CreateWebpackConfigOptions {
export interface BuildWebpackConfigOptions extends BuildLoaderRulesOptions {
loaderRules: RuleSetRule[]
outputPath: string
plugins: Plugin[]
plugins: WebpackPluginInstance[]
publicPath: string
}

Expand Down
6 changes: 5 additions & 1 deletion src/webpack/compiler/registerInMemoryCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs'
import path from 'path'
import sourceMapSupport from 'source-map-support'
import MemoryFileSystem from 'memory-fs'
Expand All @@ -13,7 +14,10 @@ export default function registerInMemoryCompiler(
): VoidFunction {
// register memory fs to webpack
const memoryFs = new MemoryFileSystem()
compiler.outputFileSystem = memoryFs // eslint-disable-line no-param-reassign
// the flag is used for integration tests that need to read compiled files
if (!process.env.MOCHAPACK_WRITE_TO_DISK) {
compiler.outputFileSystem = memoryFs // eslint-disable-line no-param-reassign
}

// build asset map to allow fast checks for file existence
const assetMap = new Map()
Expand Down
8 changes: 4 additions & 4 deletions src/webpack/compiler/registerReadyCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { MOCHAPACK_NAME } from '../../util/constants'

export default function registerReadyCallback(
compiler: Compiler,
cb: (err: (Error | string) | null, stats: Stats | null) => void
) {
compiler.hooks.failed.tap(MOCHAPACK_NAME, cb)
cb: (err: Error, stats: Stats) => void
) {
compiler.hooks.failed.tap(MOCHAPACK_NAME, (error: Error) => cb(error, null))
compiler.hooks.done.tap(MOCHAPACK_NAME, (stats: Stats) => {
if (stats.hasErrors()) {
const jsonStats = stats.toJson()
const [err] = jsonStats.errors
cb(err, stats)
cb(err as Error, stats)
} else {
cb(null, stats)
}
Expand Down
9 changes: 5 additions & 4 deletions src/webpack/loader/entryLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ export const entryLoader = function entryLoader() {
// Remove all dependencies of the loader result
this.clearDependencies()

const dependencies: Array<string> = config
.getFiles()
.map(file => loaderUtils.stringifyRequest(this, file))

const entries = config.getFiles()

// add all entries as dependencies
dependencies.forEach(this.addDependency.bind(this))
// note this.addDependency requires an absolute path
entries.forEach(e => this.addDependency(e))

// build source code
const dependencies = entries.map(file => loaderUtils.stringifyRequest(this, file))
const sourceCode: string = createEntry(dependencies)

this.callback(null, sourceCode, null)
Expand Down
1 change: 0 additions & 1 deletion src/webpack/util/createEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default function createEntry(filePaths: Array<string>): string {
'var ids = [',
filePaths.map(path => `require.resolve(${path})`).join(','),
'];',
'',
'ids.filter(inManifest).forEach(run)'
].join('\n')
}
2 changes: 1 addition & 1 deletion src/webpack/util/createStatsFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { EOL } from 'os'
import chalk from 'chalk'
import { Stats } from 'webpack'
import RequestShortener from 'webpack/lib/RequestShortener'
import WebpackError from 'webpack/lib/WebpackError'
import { formatErrorMessage, stripLoaderFromPath } from './formatUtil'
import { WebpackError } from '../types'

const createGetFile = (requestShortener: RequestShortener) => (
e: WebpackError
Expand Down
Loading