Socket
Socket
Sign inDemoInstall

enhanced-resolve

Package Overview
Dependencies
Maintainers
1
Versions
130
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enhanced-resolve - npm Package Compare versions

Comparing version 0.4.12 to 0.5.0

lib/CachedInputFileSystem.js

11

package.json
{
"name": "enhanced-resolve",
"version": "0.4.12",
"version": "0.5.0",
"author": "Tobias Koppers @sokra",
"description": "Offers a async require.resolve function. It's highly configurable.",
"dependencies": {
"tapable": "0.1.x"
},

@@ -15,4 +16,4 @@ "licenses": [

"devDependencies": {
"mocha": "*",
"should": "*"
"mocha": "1.3.x",
"should": "1.1.x"
},

@@ -22,8 +23,8 @@ "engines": {

},
"main": "lib/cachedFsResolve.js",
"main": "lib/node.js",
"homepage": "http://github.com/webpack/enhanced-resolve",
"scripts": {
"test": "node node_modules/mocha/bin/_mocha --reporter spec"
"test": "mocha --reporter spec"
},
"license": "MIT"
}

@@ -5,165 +5,12 @@ # enhanced-resolve

[documentation](https://github.com/webpack/docs/wiki)
## Features
* sync and async versions
* loaders and query strings
* normal resolve
* context resolve (resolve a directory)
* loaders resolve
* code completion
* plugin system
* provide a custom filesystem
* sync and async node.js filesystems included
## Request Format
relative: `./file`, `.././../folder/./file`
absolute: `/home/file`, `C:\folder\file`
module: `module`, `module/with/sub/file`
query: `resourceFile?query` (with resourceFile one of above, and query be any string)
loaders: `loader!resource`, `loader1!loader2!resource` (with loader and resource each one of above)
Example: `raw!./customLoader?evil,strict!C:\fail\loader?fail=big!../file.js?charset=utf-8`
## Methods
``` javascript
var resolve = require("enhanced-resolve");
// Resolve a normal request
resolve(context: String, identifier: String, options?: Object, callback: (err: Error, result: String))
resolve.sync(context: String, identifier: String, options?: Object) => String
// Resolve a context request, which means the result should be a folder
resolve.context(context: String, identifier: String, options?: Object, callback: (err: Error, result: String))
resolve.context.sync(context: String, identifier: String, options?: Object) => String
// Only resolve loaders, a array of resolved loaders is the result
resolve.loaders(context: String, identifier: String, options?: Object, callback: (err: Error, result: String[]))
resolve.loaders.sync(context: String, identifier: String, options?: Object) => String[]
// Autocomplete a incomplete require expression.
// identifier must contain exactly one "*", which indicates the insert position
resolve.complete(context: String, identifier: String, options?: Object, callback: (err: Error, result: Completion[]))
resolve.complete.sync(context: String, identifier: String, options?: Object) => Completion[]
// parse a request
resolve.parse(identifier: String) => {loaders: Part[], resource: Part}
// parse only a part
resolve.parse.part(identifierPart: String) => Part
// stringify a parsed request
resolve.stringify(parsed: {loaders: Part[], resource: Part}) => String
// stringify only a part
resolve.stringify.part(part: Part) => String
// checks if a request part is a module
resolve.parse.isModule(identifierPart: String) => Boolean
// the type used for parse and stringify
type Part { path: String, query: String, module: Boolean }
type Completion { // examples for "loader!module/dir/fi*?query"
insert: String, // i. e. "le.js"
seqment: String, // i. e. "file.js"
part: String, // i. e. "module/dir/file.js?query"
result: String // i. e. "loader!module/dir/file.js?query"
}
```
## Options
``` javascript
{
paths: ["/my/absolute/dirname"],
// default: []
// search paths for modules
modulesDirectories: ["xyz_modules", "node_modules"],
// default: (defaults are NOT included if you define your own)
// ["node_modules"];
// directories to be searched for modules
alias: {
"old-module": "new-module",
"another-module": "new-module/more/stuff"
},
// replace a module
extensions: ["", ".www.js", ".js"],
// defaults: (defaults are NOT included if you define your own)
// ["", ".js"]
// postfixes for files to try
packageMains: ["abc", "main"]
// defaults: ["main"]
// lookup fields in package.json
loaderExtensions: [".loader.js", ".www-loader.js", "", ".js"],
// defaults: (defaults are NOT included if you define your own)
// [".node-loader.js", ".loader.js", "", ".js"]
// postfixes for loaders to try
loaderPostfixes: ["-loader", "-xyz", ""],
// defaults: (defaults are NOT included if you define your own)
// ["-node-loader", "-loader", ""]
// postfixes for loader modules to try
loaderPackageMains: ["myloader", "main"]
// defaults: ["loader", "main"]
// lookup fields for loaders in package.json
loaders: [{
// test, include and exclude can be undefined, RegExp, string or array of these
test: /\.generator\.js/,
include: "\\.js",
exclude: [
/\.no\.generator\.js/,
"\\.nono\\.generator\\.js"
}
loader: "val"
}],
// default: []
// automatically use loaders if resolved filename match RegExp
// and no loader is specified.
postprocess: {
normal: [function(filename, callback) {
// webpack will not find files including ".exclude."
if(/\.exclude\.[^\\\/]*$/.test(filename))
return callback(new Error("File is excluded"));
callback(null, filename);
}],
// defaults: []
// postprocess resolved filenames by all specified async functions
// a postprocessor must call the callback
// You can pass a filename instead of a function
// The filename is required and the exports are expected to be a function.
context: [],
// same as postprocess.normal but for contextes
}
disableLoaders: false,
// disallow loaders
disableResourceQuery: false,
// disallow query at resource
disableResourcePureQuery: false,
// disallow only query without resource
disableLoaderQuery: false,
// disallow queries at loaders
}
```
## Tests

@@ -180,4 +27,4 @@

Copyright (c) 2012 Tobias Koppers
Copyright (c) 2012-2013 Tobias Koppers
MIT (http://www.opensource.org/licenses/mit-license.php)

@@ -25,5 +25,5 @@ function AsyncFileSystem(fs) {

AsyncFileSystem.prototype.readFile = function(path, encoding, callback) {
AsyncFileSystem.prototype.readFile = function(path, callback) {
asAsync(function() {
return this.fs.readFileSync(path, encoding);
return this.fs.readFileSync(path);
}.bind(this), callback);

@@ -30,0 +30,0 @@ }

@@ -7,10 +7,9 @@ var ConstFileSystem = require("./ConstFileSystem");

var TestContents = require("./TestContents");
var customResolveFactory = require("./customResolveFactory");
var Resolver = require("../../lib/Resolver");
var ModulesInDirectoriesPlugin = require("../../lib/ModulesInDirectoriesPlugin");
var ModuleAsDirectoryPlugin = require("../../lib/ModuleAsDirectoryPlugin");
var ModuleDefaultFilePlugin = require("../../lib/ModuleDefaultFilePlugin");
var ModuleDescriptionFilePlugin = require("../../lib/ModuleDescriptionFilePlugin");
var FileAppendPlugin = require("../../lib/FileAppendPlugin");
var defaultOptions = {
modulesDirectories: ["web_modules", "node_modules"],
extensions: ["", ".webpack.js", ".web.js", ".js"],
packageMains: ["webpack", "browserify", "web", "main"],
}
var cases = [

@@ -71,5 +70,13 @@ {

if(!testCase) return;
var loggedFs = new LoggedFileSystem(/*new DebugFileSystem*/(new ConstFileSystem(TestContents[testCase.content])));
var loggedFs = new LoggedFileSystem(new DebugFileSystem(new ConstFileSystem(TestContents[testCase.content])));
var fs = new AsyncFileSystem(new CachedFileSystem(loggedFs));
var resolve = customResolveFactory(fs);
var resolver = new Resolver(fs);
resolver.apply(
new ModulesInDirectoriesPlugin("node", ["web_modules", "node_modules"]),
new ModuleAsDirectoryPlugin("node"),
new ModuleDescriptionFilePlugin("package.json", ["webpack", "browserify", "web", "main"]),
new ModuleDefaultFilePlugin(["index"]),
new FileAppendPlugin(["", ".web.js", ".js"])
);
var stopTick = false;

@@ -83,6 +90,6 @@ var ticks = 0;

});
resolve(testCase.context, testCase.resolve, testCase.options || defaultOptions, function(err, result) {
resolver.resolve(testCase.context, testCase.resolve, function(err, result) {
console.log(testCase.title + ": " + testCase.resolve + " in " + testCase.context);
if(err) console.log("err -> " + err);
else console.log("-> " +result);
else console.log("-> " + result);
console.log("stat: " + loggedFs.count.stat +

@@ -89,0 +96,0 @@ ", readFile: " + loggedFs.count.readFile +

@@ -9,17 +9,12 @@ /*

var options = {
alias: {
"recursive-module": "recursive-module/file"
},
loaders: [
{test: ".load1$", loader: "m2/b"},
{test: ".load2$", loader: "m1/a!m2/b"}
]
}
var fixtures = path.join(__dirname, "fixtures");
function testResolve(name, context, moduleName, result) {
describe(name, function() {
it("should resolve sync correctly", function() {
var filename = resolve.sync(context, moduleName);
should.exist(filename);
filename.should.equal(result);
});
it("should resolve async correctly", function(done) {
resolve(context, moduleName, options, function(err, filename) {
resolve(context, moduleName, function(err, filename) {
if(err) done(err);

@@ -31,7 +26,19 @@ should.exist(filename);

});
});
}
function testResolveLoader(name, context, moduleName, result) {
describe(name, function() {
it("should resolve sync correctly", function() {
var filename = resolve.sync(context, moduleName, options);
var filename = resolve.loader.sync(context, moduleName);
should.exist(filename);
filename.should.equal(result);
});
it("should resolve async correctly", function(done) {
resolve.loader(context, moduleName, function(err, filename) {
if(err) done(err);
should.exist(filename);
filename.should.equal(result);
done();
});
});
});

@@ -42,3 +49,3 @@ }

it("should resolve async correctly", function(done) {
resolve.context(context, moduleName, {}, function(err, filename) {
resolve.context(context, moduleName, function(err, filename) {
if(err) done(err);

@@ -51,3 +58,3 @@ should.exist(filename)

it("should resolve sync correctly", function() {
var filename = resolve.context.sync(context, moduleName, {});
var filename = resolve.context.sync(context, moduleName);
should.exist(filename)

@@ -86,45 +93,2 @@ filename.should.equal(result);

testResolve("absolute path with loader",
fixtures,
path.join(fixtures, "node_modules", "m1", "a.js") + "!!" + path.join(fixtures, "main1.js"),
path.join(fixtures, "node_modules", "m1", "a.js") + "!" + path.join(fixtures, "main1.js"));
testResolve("loader",
fixtures, "m1/a!./main1.js", path.join(fixtures, "node_modules", "m1", "a.js") + "!" + path.join(fixtures, "main1.js"));
testResolve("loader with prefix",
fixtures, "m2/b!./main1.js", path.join(fixtures, "node_modules", "m2-loader", "b.js") + "!" + path.join(fixtures, "main1.js"));
testResolve("multiple loaders",
fixtures, "m1/a!m1/b!m2/b!./main1.js", path.join(fixtures, "node_modules", "m1", "a.js") + "!" +
path.join(fixtures, "node_modules", "m1", "b.js") + "!" +
path.join(fixtures, "node_modules", "m2-loader", "b.js") + "!" +
path.join(fixtures, "main1.js"));
testResolve("multiple loaders with queries",
fixtures, "m1/a?q1!m1/b?q2!m2/b?q3!./main1.js?q4", path.join(fixtures, "node_modules", "m1", "a.js") + "?q1!" +
path.join(fixtures, "node_modules", "m1", "b.js") + "?q2!" +
path.join(fixtures, "node_modules", "m2-loader", "b.js") + "?q3!" +
path.join(fixtures, "main1.js") + "?q4");
testResolve("loader without resource",
fixtures, "m1/a?q1!", path.join(fixtures, "node_modules", "m1", "a.js") + "?q1!");
testResolve("loader without resource but query",
fixtures, "m1/a?q1!?q2", path.join(fixtures, "node_modules", "m1", "a.js") + "?q1!?q2");
testResolve("automatic one loader",
fixtures, "./file.load1",
path.join(fixtures, "node_modules", "m2-loader", "b.js") + "!" +
path.join(fixtures, "file.load1"));
testResolve("automatic two loader",
fixtures, "./file.load2",
path.join(fixtures, "node_modules", "m1", "a.js") + "!" +
path.join(fixtures, "node_modules", "m2-loader", "b.js") + "!" +
path.join(fixtures, "file.load2"));
testResolve("overwrite automatic loader",
fixtures, "m1/a!./file.load1",
path.join(fixtures, "node_modules", "m1", "a.js") + "!" +
path.join(fixtures, "file.load1"));
testResolve("disable automatic loader",
fixtures, "!./file.load1",
path.join(fixtures, "file.load1"));
testResolveContext("context for fixtures",

@@ -134,17 +98,8 @@ fixtures, "./", fixtures);

fixtures, "./lib", path.join(fixtures, "lib"));
testResolveContext("context with loader",
fixtures, "m1/a!./", path.join(fixtures, "node_modules", "m1", "a.js") + "!" + fixtures);
testResolveContext("context with loaders in parent directory",
fixtures, "m1/a!m2/b.js!../", path.join(fixtures, "node_modules", "m1", "a.js") + "!" +
path.join(fixtures, "node_modules", "m2-loader", "b.js") + "!" +
path.join(fixtures, ".."));
testResolveContext("context for fixtures with ..",
fixtures, "./lib/../../fixtures/./lib/..", fixtures);
testResolveContext("context for fixtures with query",
fixtures, "./?query", fixtures + "?query");
testResolveContext("context with loader and query",
fixtures, "m1/a?q1!./?q2", path.join(fixtures, "node_modules", "m1", "a.js") + "?q1!" + fixtures + "?q2");
testResolve("infinite loop by alias",
fixtures, "recursive-module", path.join(fixtures, "node_modules", "recursive-module", "file.js"));
testResolve("differ between directory and file, resolve file",

@@ -154,2 +109,7 @@ fixtures, "./dirOrFile", path.join(fixtures, "dirOrFile.js"));

fixtures, "./dirOrFile/", path.join(fixtures, "dirOrFile", "index.js"));
testResolveLoader("loader with template without extension",
fixtures, "m2/b", path.join(fixtures, "node_modules", "m2-loader", "b.js"));
testResolveLoader("loader with template as file",
fixtures, "l", path.join(fixtures, "node_loaders", "l-loader.js"));
});

@@ -7,6 +7,6 @@ var resolve = require("../");

var pathsToIt = [
[__dirname, "../lib/cachedFsResolve", "direct"],
[__dirname, "../lib/node", "direct"],
[__dirname, "../", "as directory"],
[path.join(__dirname, "..", ".."), "./enhanced-resolve", "as module"],
[path.join(__dirname, "..", ".."), "./enhanced-resolve/lib/cachedFsResolve", "in module"]
[path.join(__dirname, "..", ".."), "./enhanced-resolve/lib/node", "in module"]
];

@@ -19,3 +19,3 @@ pathsToIt.forEach(function(pathToIt) {

filename.should.be.a("string");
filename.should.be.eql(path.join(__dirname, "..", "lib", "cachedFsResolve.js"));
filename.should.be.eql(path.join(__dirname, "..", "lib", "node.js"));
done();

@@ -28,3 +28,3 @@ });

filename.should.be.a("string");
filename.should.be.eql(path.join(__dirname, "..", "lib", "cachedFsResolve.js"));
filename.should.be.eql(path.join(__dirname, "..", "lib", "node.js"));
});

@@ -31,0 +31,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc