Skip to content
This repository was archived by the owner on Mar 22, 2019. It is now read-only.

context.cn

e-cloud edited this page Mar 28, 2016 · 1 revision

dynamic requires

动态 require

A context is created if your request contains expressions, so the exact module is not known on compile time.

如果你的请求包含表达式,一个上下文对象便会创建,所以在编译期是不知道实际的模块的。

Example:

require("./template/" + name + ".jade");

webpack parses the require statement and extracts some information:

webpack会解析require语句并提取部分信息:

  • Directory: ./template

  • Regular expression: /^.*\.jade$/

  • 目录:./template

  • 正则表达式:/^.*\.jade$/

context module

上下文模块

A context module is generated. It contains references to all modules in that directory that can be required with a request matching the regular expression. The context module contains a map which translates requests to module ids.

被生成的上下文模块包含对应目录内所有模块的引用,模块可以通过正则匹配引用到。上下文模块有一份映射表用于模块请求与模块id的映射

Example:

{
	"./table.jade": 22,
	"./table-row.jade": 23,
	"./directory/folder.jade": 24
}

The context module also contains a bit runtime logic to access the map.

上下文模块同时含有一点运行时逻辑来访问映射表。

dynamic require rewriting

改写动态的require

The original require statement gets rewritten by the compiler to access the context module: (assuming the context module gets the module id 21)

原始的require语句会被编译器改写以访问上下文模块:(假设上下文模块有个模块id是21

Example:

// original statement
require("./template/" + name + ".jade");

// rewritten statement
require(21)("./" + name + ".jade");

parser evaluation

解析器的代码自执行

Not every expression results in a context. The parser has a small evaluation engine to evaluate simple expressions. Here are some examples:

不是每个表达式都会产生一个上下文对象。解析器有个小型执行引擎用来执行简单的表达式。下面是样例:

require(expr ? "a" : "b"); // => require(expr ? 25 : 26)
require("a" + "b"); // => require(27)
require("not a".substr(4).replace("a", "b")); // => require(26)
// ...

require.context

You can create your own context with the require.context function. It allow to pass a directory, regular expression and a flag if subdirectories should be used too.

你可以使用require.context函数创建自己的上下文对象。它运行传入一个目录,正则表达式,和一个标志(如果子目录也需用到)

require.context(directory, useSubdirectories = false, regExp = /^\.\//)

Examples:

require.context("./test", false, /Test$/)
// a context with all files from the test directory that can be
// required with a request endings with "Test"

require.context("..", true, /^grunt-[^\/]+\/tasks/[^\/]+$/)
// all grunt task that are in a modules directory of the parent folder

context module API

上下文模块的API

A context module exports a require function that takes one argument: the request.

一个上下文模块会导出一个require函数,该函数只接收一个参数:请求的模块标识

The exported function has a property resolve which is a function and returns the module id of the parsed request.

导出的函数有一个属性resolve,是一个返回解析后的模块id的函数。

The exported function has another property keys which is a function that returns all possible requests that the context module can handle.

导出的函数有另外一个属性keys,是一个返回上下文模块可以处理的所有可能request的函数。[不太通顺]

And the exported function has another property id which is the module id of the context module. This may be useful for module.hot.accept.

导出的函数还有另外一个属性id,是上下文模块的id。可能对module.hot.accept有用。

Examples:

var req = require.context("./templates", true, /^\.\/.*\.jade$/);

var tableTemplate = req("./table.jade");
// tableTemplate === require("./templates/table.jade");

var tableTemplateId = req.resolve("./table.jade");
// tableTemplateId === require.resolve("./templates/table.jade");

req.keys();
// is ["./table.jade", "./table-row.jade", "./directory/folder.jade"]

req.id;
// is i. e. 42

or

function requireAll(requireContext) {
  return requireContext.keys().map(requireContext);
}
// requires and returns all modules that match

var modules = requireAll(require.context("./spec", true, /^\.\/.*\.js$/));
// is an array containing all the matching modules

Note: keys depends on Object.keys. You may need to polyfill it for older browsers.

注意:keys取决于Object.keys。可能需要在古代浏览器上面进行polyfill

ContextReplacementPlugin

This plugin can overwrite the details for a context (i. e. the RegExp). See list of plugins.

这个插件可以重写一个上下文的详细信息(如 RegExp).参见list of plugins.

Critical dependencies

非法依赖

If the module source contains a require that cannot be statically analyzed, the context is the current directory.

如果一个模块源码中某个require语句无法静态分析,那么上下文就是当前目录。

In this case a Critical dependencies warning is emitted. You need to use the ContextReplacementPlugin in most cases.

这种情况下,会产生一个Critical dependencies警告。大部分情况下你需要使用ContextReplacementPlugin

Examples: someFn(require) require.bind(null)

例如:someFn(require) require.bind(null)

Example

See an example here.

Clone this wiki locally