Skip to content

Commit

Permalink
add special tooling
Browse files Browse the repository at this point in the history
generate typings
cleanup test files
test typings with test suite
  • Loading branch information
sokra committed May 6, 2020
1 parent 951108c commit ea894f5
Show file tree
Hide file tree
Showing 24 changed files with 969 additions and 188 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
"no-use-before-define": "off",
"no-unused-vars": ["error", { args: "none", ignoreRestSiblings: true }],
"no-loop-func": "warn",
"node/no-missing-require": ["error", { allowModules: ["webpack"] }],
"node/no-missing-require": "error",
"jsdoc/check-indentation": "error",
"jsdoc/check-param-names": "error",
"jsdoc/check-property-names": "error",
Expand Down
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ module.exports = {
{
files: "*.json",
options: {
parser: "json",
useTabs: false
}
},
{
files: "*.ts",
options: {
parser: "typescript"
}
}
]
};
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sudo: false
language: node_js
node_js:
- "14"
- "12"
- "10"
- "8"
script: npm run travis

after_success:
Expand Down
2 changes: 1 addition & 1 deletion lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const UseFilePlugin = require("./UseFilePlugin");
/** @typedef {string|string[]|false} AliasOptionNewRequest */
/** @typedef {{alias: AliasOptionNewRequest, name: string, onlyModule?: boolean}} AliasOptionEntry */
/** @typedef {{[k: string]: AliasOptionNewRequest}} AliasOptions */
/** @typedef {{apply: function(Resolver): void} | function(Resolver): void} Plugin */
/** @typedef {{apply: function(Resolver): void} | function(this: Resolver, Resolver): void} Plugin */

/**
* @typedef {Object} UserResolveOptions
Expand Down
63 changes: 46 additions & 17 deletions lib/node.js → lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const fs = require("fs");
const CachedInputFileSystem = require("./CachedInputFileSystem");
const ResolverFactory = require("./ResolverFactory");

/** @typedef {import("./PnpPlugin").PnpApiImpl} PnpApi */
/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./ResolverFactory").Plugin} Plugin */

const nodeFileSystem = new CachedInputFileSystem(fs, 4000);

const nodeContext = {
Expand All @@ -19,13 +24,7 @@ const asyncResolver = ResolverFactory.createResolver({
extensions: [".js", ".json", ".node"],
fileSystem: nodeFileSystem
});
module.exports = function resolve(
context,
path,
request,
resolveContext,
callback
) {
function resolve(context, path, request, resolveContext, callback) {
if (typeof context === "string") {
callback = resolveContext;
resolveContext = request;
Expand All @@ -37,23 +36,23 @@ module.exports = function resolve(
callback = resolveContext;
}
asyncResolver.resolve(context, path, request, resolveContext, callback);
};
}

const syncResolver = ResolverFactory.createResolver({
extensions: [".js", ".json", ".node"],
useSyncFileSystemCalls: true,
fileSystem: nodeFileSystem
});
module.exports.sync = function resolveSync(context, path, request) {
function resolveSync(context, path, request) {
if (typeof context === "string") {
request = path;
path = context;
context = nodeContext;
}
return syncResolver.resolveSync(context, path, request);
};
}

module.exports.create = function create(options) {
function create(options) {
options = {
fileSystem: nodeFileSystem,
...options
Expand All @@ -72,9 +71,9 @@ module.exports.create = function create(options) {
}
resolver.resolve(context, path, request, resolveContext, callback);
};
};
}

module.exports.create.sync = function createSync(options) {
function createSync(options) {
options = {
useSyncFileSystemCalls: true,
fileSystem: nodeFileSystem,
Expand All @@ -89,9 +88,39 @@ module.exports.create.sync = function createSync(options) {
}
return resolver.resolveSync(context, path, request);
};
};
}

// Export Resolver, FileSystems and Plugins
module.exports.ResolverFactory = ResolverFactory;
/**
* @template A
* @template B
* @param {A} obj input a
* @param {B} exports input b
* @returns {A & B} merged
*/
const mergeExports = (obj, exports) => {
const descriptors = Object.getOwnPropertyDescriptors(exports);
Object.defineProperties(obj, descriptors);
return /** @type {A & B} */ (Object.freeze(obj));
};

module.exports.CachedInputFileSystem = CachedInputFileSystem;
module.exports = mergeExports(resolve, {
get sync() {
return resolveSync;
},
create: mergeExports(create, {
get sync() {
return createSync;
}
}),
ResolverFactory,
CachedInputFileSystem,
get CloneBasenamePlugin() {
return require("./CloneBasenamePlugin");
},
get LogInfoPlugin() {
return require("./LogInfoPlugin");
},
get forEachBail() {
return require("./forEachBail");
}
});
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "Offers a async require.resolve function. It's highly configurable.",
"files": [
"lib",
"types.d.ts",
"LICENSE"
],
"dependencies": {
Expand All @@ -27,21 +28,23 @@
"nyc": "^14.1.1",
"prettier": "^1.15.2",
"should": "^13.2.3",
"tooling": "webpack/tooling#v1.6.0",
"typescript": "^3.8.3"
},
"engines": {
"node": ">=8.9.0"
"node": ">=10.13.0"
},
"main": "lib/node.js",
"main": "lib/index.js",
"types": "types.d.ts",
"homepage": "http://github.com/webpack/enhanced-resolve",
"scripts": {
"lint": "yarn run code-lint && yarn run type-lint && yarn run special-lint",
"fix": "yarn run code-lint-fix && yarn run special-lint-fix",
"code-lint": "eslint --cache lib test",
"code-lint-fix": "eslint --cache lib test --fix",
"type-lint": "tsc",
"special-lint": "node tooling/format-file-header.js",
"special-lint-fix": "node tooling/format-file-header.js --write",
"special-lint": "node node_modules/tooling/lockfile-lint && node node_modules/tooling/inherit-types && node node_modules/tooling/format-file-header && node node_modules/tooling/generate-types",
"special-lint-fix": "node node_modules/tooling/inherit-types --write && node node_modules/tooling/format-file-header --write && node node_modules/tooling/generate-types --write",
"pretty": "prettier --loglevel warn --write \"{lib,test}/**/*.{js,json}\"",
"pretest": "yarn lint",
"test": "mocha --full-trace --check-leaks",
Expand Down
3 changes: 2 additions & 1 deletion test/CachedInputFileSystem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var CachedInputFileSystem = require("../lib/CachedInputFileSystem");
var should = require("should");

var { CachedInputFileSystem } = require("../");

describe("CachedInputFileSystem", function() {
this.timeout(3000);
var fs;
Expand Down
3 changes: 2 additions & 1 deletion test/alias.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("should");
var ResolverFactory = require("../lib/ResolverFactory");

var { Volume } = require("memfs");
var { ResolverFactory } = require("../");

describe("alias", function() {
var resolver;
Expand Down
5 changes: 3 additions & 2 deletions test/browserField.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require("should");

var path = require("path");
var fs = require("fs");

var ResolverFactory = require("../lib/ResolverFactory");
var { ResolverFactory } = require("../");

var browserModule = path.join(__dirname, "fixtures", "browser-module");

Expand Down
1 change: 1 addition & 0 deletions test/dependencies.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("should");

var { Volume } = require("memfs");
var resolve = require("../");

Expand Down
6 changes: 3 additions & 3 deletions test/extensions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require("should");

const path = require("path");
const fs = require("fs");
require("should");
const ResolverFactory = require("../lib/ResolverFactory");
const CachedInputFileSystem = require("../lib/CachedInputFileSystem");
const { ResolverFactory, CachedInputFileSystem } = require("../");

const nodeFileSystem = new CachedInputFileSystem(fs, 4000);

Expand Down
4 changes: 3 additions & 1 deletion test/forEachBail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const forEachBail = require("../lib/forEachBail");
require("should");

const { forEachBail } = require("../");

describe("forEachBail", () => {
it("should iterate correctly", done => {
Expand Down
5 changes: 3 additions & 2 deletions test/missing.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var resolve = require("../");
var path = require("path");
var should = require("should");

var path = require("path");
var resolve = require("../");

describe("missing", function() {
/**
* @type {Array<[string, string, Array<string>]>}
Expand Down
5 changes: 3 additions & 2 deletions test/plugins.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use strict";

const ResolverFactory = require("../").ResolverFactory;
const CloneBasenamePlugin = require("../lib/CloneBasenamePlugin");
require("should");

const path = require("path");
const { ResolverFactory, CloneBasenamePlugin } = require("../");

describe("plugins", function() {
it("should resolve with the CloneBasenamePlugin", function(done) {
Expand Down
6 changes: 3 additions & 3 deletions test/pnp.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require("should");

const path = require("path");
const fs = require("fs");
require("should");
const ResolverFactory = require("../lib/ResolverFactory");
const CachedInputFileSystem = require("../lib/CachedInputFileSystem");
const { ResolverFactory, CachedInputFileSystem } = require("../");

/** @typedef {import("../lib/PnpPlugin").PnpApiImpl} PnpApi */

Expand Down
4 changes: 3 additions & 1 deletion test/pr-53.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var CachedInputFileSystem = require("../lib/CachedInputFileSystem");
require("should");

var { CachedInputFileSystem } = require("../");

describe("pr-53", function() {
it("should allow to readJsonSync in CachedInputFileSystem", function() {
Expand Down
5 changes: 1 addition & 4 deletions test/resolve.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var should = require("should");

var path = require("path");
var resolve = require("../");

Expand Down
13 changes: 7 additions & 6 deletions test/simple.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
var resolve = require("../");
var should = require("should");

var path = require("path");
var resolve = require("../");

describe("simple", function() {
var pathsToIt = [
[__dirname, "../lib/node", "direct"],
[__dirname, "../", "as directory"],
[__dirname, "../lib/index", "direct"],
[__dirname, "..", "as directory"],
[path.join(__dirname, "..", ".."), "./enhanced-resolve", "as module"],
[
path.join(__dirname, "..", ".."),
"./enhanced-resolve/lib/node",
"./enhanced-resolve/lib/index",
"in module"
]
];
Expand All @@ -22,15 +23,15 @@ describe("simple", function() {
);
should.exist(filename);
filename.should.have.type("string");
filename.should.be.eql(path.join(__dirname, "..", "lib", "node.js"));
filename.should.be.eql(path.join(__dirname, "..", "lib", "index.js"));
done();
});
});
it("should resolve itself sync " + pathToIt[2], function() {
var filename = resolve.sync(pathToIt[0], pathToIt[1]);
should.exist(filename);
filename.should.have.type("string");
filename.should.be.eql(path.join(__dirname, "..", "lib", "node.js"));
filename.should.be.eql(path.join(__dirname, "..", "lib", "index.js"));
});
});
});
Loading

0 comments on commit ea894f5

Please sign in to comment.