Skip to content

Commit

Permalink
Merge pull request #381 from webpack/refactor-types
Browse files Browse the repository at this point in the history
refactor: types
  • Loading branch information
TheLarkInn authored May 24, 2023
2 parents 24a4279 + 76b2c9e commit 617b37a
Show file tree
Hide file tree
Showing 62 changed files with 1,453 additions and 964 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 14.x
node-version: 16.x
cache: yarn
- run: yarn --frozen-lockfile
- run: yarn lint
Expand All @@ -23,7 +23,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 14.x, 16.x, 17.x]
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -32,8 +32,14 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: yarn
- name: Install dependencies
run: |
yarn upgrade typescript@^4 --ignore-engines
yarn --frozen-lockfile
if: matrix.node-version == '10.x'
- name: Install dependencies
run: yarn --frozen-lockfile
if: matrix.node-version != '10.x'
- name: Run tests with coverage
run: npm run test:coverage -- --ci
- if: ${{ matrix.os != 'windows-latest' }}
Expand Down
1 change: 0 additions & 1 deletion declarations.d.ts

This file was deleted.

17 changes: 12 additions & 5 deletions lib/AliasFieldPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DescriptionFileUtils = require("./DescriptionFileUtils");
const getInnerRequest = require("./getInnerRequest");

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").JsonPrimitive} JsonPrimitive */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */

Expand Down Expand Up @@ -49,13 +50,18 @@ module.exports = class AliasFieldPlugin {
);
return callback();
}
/** @type {JsonPrimitive | undefined} */
const data = Object.prototype.hasOwnProperty.call(
fieldData,
innerRequest
)
? fieldData[innerRequest]
? /** @type {{[Key in string]: JsonPrimitive}} */ (fieldData)[
innerRequest
]
: innerRequest.startsWith("./")
? fieldData[innerRequest.slice(2)]
? /** @type {{[Key in string]: JsonPrimitive}} */ (fieldData)[
innerRequest.slice(2)
]
: undefined;
if (data === innerRequest) return callback();
if (data === undefined) return callback();
Expand All @@ -71,10 +77,11 @@ module.exports = class AliasFieldPlugin {
}
return callback(null, ignoreObj);
}
/** @type {ResolveRequest} */
const obj = {
...request,
path: request.descriptionFileRoot,
request: data,
path: /** @type {string} */ (request.descriptionFileRoot),
request: /** @type {string} */ (data),
fullySpecified: false
};
resolver.doResolve(
Expand All @@ -85,7 +92,7 @@ module.exports = class AliasFieldPlugin {
" with mapping '" +
innerRequest +
"' to '" +
data +
/** @type {string} */ (data) +
"'",
resolveContext,
(err, result) => {
Expand Down
25 changes: 24 additions & 1 deletion lib/AliasPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const { PathType, getType } = require("./util/path");
/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
/** @typedef {{alias: string|Array<string>|false, name: string, onlyModule?: boolean}} AliasOption */
/** @typedef {string | Array<string> | false} Alias */
/** @typedef {{alias: Alias, name: string, onlyModule?: boolean}} AliasOption */

module.exports = class AliasPlugin {
/**
Expand All @@ -31,13 +32,22 @@ module.exports = class AliasPlugin {
*/
apply(resolver) {
const target = resolver.ensureHook(this.target);
/**
* @param {string} maybeAbsolutePath path
* @returns {null|string} absolute path with slash ending
*/
const getAbsolutePathWithSlashEnding = maybeAbsolutePath => {
const type = getType(maybeAbsolutePath);
if (type === PathType.AbsolutePosix || type === PathType.AbsoluteWin) {
return resolver.join(maybeAbsolutePath, "_").slice(0, -1);
}
return null;
};
/**
* @param {string} path path
* @param {string} maybeSubPath sub path
* @returns {boolean} true, if path is sub path
*/
const isSubPath = (path, maybeSubPath) => {
const absolutePath = getAbsolutePathWithSlashEnding(maybeSubPath);
if (!absolutePath) return false;
Expand All @@ -51,6 +61,7 @@ module.exports = class AliasPlugin {
forEachBail(
this.options,
(item, callback) => {
/** @type {boolean} */
let shouldStop = false;
if (
innerRequest === item.name ||
Expand All @@ -59,7 +70,13 @@ module.exports = class AliasPlugin {
? innerRequest.startsWith(`${item.name}/`)
: isSubPath(innerRequest, item.name)))
) {
/** @type {string} */
const remainingRequest = innerRequest.slice(item.name.length);
/**
* @param {Alias} alias alias
* @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
* @returns {void}
*/
const resolveWithAlias = (alias, callback) => {
if (alias === false) {
/** @type {ResolveRequest} */
Expand All @@ -79,6 +96,7 @@ module.exports = class AliasPlugin {
) {
shouldStop = true;
const newRequestStr = alias + remainingRequest;
/** @type {ResolveRequest} */
const obj = {
...request,
request: newRequestStr,
Expand All @@ -104,6 +122,11 @@ module.exports = class AliasPlugin {
}
return callback();
};
/**
* @param {null|Error} [err] error
* @param {null|ResolveRequest} [result] result
* @returns {void}
*/
const stoppingCallback = (err, result) => {
if (err) return callback(err);

Expand Down
2 changes: 2 additions & 0 deletions lib/AppendPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */

module.exports = class AppendPlugin {
Expand All @@ -29,6 +30,7 @@ module.exports = class AppendPlugin {
resolver
.getHook(this.source)
.tapAsync("AppendPlugin", (request, resolveContext, callback) => {
/** @type {ResolveRequest} */
const obj = {
...request,
path: request.path + this.appending,
Expand Down
Loading

0 comments on commit 617b37a

Please sign in to comment.