项目作者: bastidest

项目描述 :
Webpack plugin to map dynamic (thirdparty) dependencies to a static context map.
高级语言: JavaScript
项目地址: git://github.com/bastidest/context-map-webpack-plugin.git
创建时间: 2019-12-10T23:04:10Z
项目社区:https://github.com/bastidest/context-map-webpack-plugin

开源协议:MIT License

下载


context-map-webpack-plugin

Webpack plugin to map dynamic (thirdparty) dependencies to a static context map.

Install

  1. npm install -D context-map-webpack-plugin

Why do I need it / What does it do?

The Problem

Some (thirdparty) code requires other files with expressions that
cannot be resolved by webpack at compile-time. Webpack tries to
generate a regular expression that determines which files to include
in the webpack bundle. In more complex expressions, webpack is unable
to determine which files to include, thus an empty context map will be
created.

A dynamic import that cannot be resolved might look like this:

  1. // ...
  2. var FILES = UglifyJS.FILES = [
  3. "../lib/utils.js",
  4. "../lib/ast.js",
  5. "../lib/parse.js",
  6. "../lib/transform.js",
  7. "../lib/scope.js",
  8. "../lib/output.js",
  9. "../lib/compress.js",
  10. "../lib/sourcemap.js",
  11. "../lib/mozilla-ast.js",
  12. "../lib/propmangle.js",
  13. "./exports.js",
  14. ].map(function(file){
  15. return require.resolve(file);
  16. });
  17. // ...

Your webpack build will probably have this Critical dependency warning:

  1. WARNING in ./node_modules/uglify-js/tools/node.js 24:11-32
  2. Critical dependency: the request of a dependency is an expression
  3. @ ./node_modules/pug-filters/lib/run-filter.js
  4. @ ./node_modules/pug-filters/index.js
  5. @ ./node_modules/pug/lib/index.js
  6. @ ./src/server/PugHandler.ts
  7. @ ./src/server/Router.ts
  8. @ ./src/server/index.ts
  9. @ multi ./src/server/index.ts webpack/hot/poll?1000

This will result in a bundle output that looks like this:

  1. // ...
  2. ***/ "./node_modules/uglify-js/tools sync recursive":
  3. /*!*******************************************!*\
  4. !*** ./node_modules/uglify-js/tools sync ***!
  5. \*******************************************/
  6. /*! no static exports found */
  7. /***/ (function(module, exports) {
  8. eval("function webpackEmptyContext(req) {\n\tvar e = new Error(\"Cannot find module '\" + req + \"'\");\n\te.code = 'MODULE_NOT_FOUND';\n\tthrow e;\n}
  9. // ...

This empty context will lead to runtime errors:

  1. node ./dist/server/main.js
  2. undefined:4
  3. throw e;
  4. ^
  5. Error: Cannot find module '../lib/utils.js'
  6. at Function.webpackEmptyContext [as resolve] (eval at ./node_modules/uglify-js/tools sync recursive (<project_dir>/dist/server/main.js:5371:1), <anonymous>:2:10)
  7. // ...

The Solution

Create a static context map in compile-time in order to include all
required dependencies. You are required to investigate all possible
runtime values of the required argument where the error originates
from. These values have to be passed as the staticImports argument
in the plugin constructor.

Alternatives

If you use webpack for the backend, you can exclude the required files
from the webpack bundle and depend on node_mdules by using
webpack-node-externals.

Usage

In your webpack configuration:

  1. const ContextMapPlugin = require('context-map-webpack-plugin');
  2. module.exports = {
  3. // ...
  4. plugins: [
  5. new ContextMapPlugin(contextPath, staticImports)
  6. ],
  7. // ...
  8. };

Parameters

contextPath

  • Type: String
  • Default: undefined

Specify the directory in which to create/overwrite the context map.

staticImports

  • Type: Array[String]
  • Default: undefined

Static imports that will be written to the context map (and bundled with webpack at compile-time).

Example (uglify-js)

  1. // ...
  2. plugins: [
  3. new ContextMapPlugin('node_modules/uglify-js/tools', [
  4. '../lib/utils.js',
  5. '../lib/ast.js',
  6. '../lib/parse.js',
  7. '../lib/transform.js',
  8. '../lib/scope.js',
  9. '../lib/output.js',
  10. '../lib/compress.js',
  11. '../lib/sourcemap.js',
  12. '../lib/mozilla-ast.js',
  13. '../lib/propmangle.js',
  14. './exports.js',
  15. ])
  16. ],
  17. // ...

License

MIT