diff --git a/src/content/api/module-methods.md b/src/content/api/module-methods.mdx similarity index 98% rename from src/content/api/module-methods.md rename to src/content/api/module-methods.mdx index 9729c7cf5d27..c83c4e9e4368 100644 --- a/src/content/api/module-methods.md +++ b/src/content/api/module-methods.mdx @@ -172,7 +172,7 @@ T> Note that `webpackInclude` and `webpackExclude` options do not interfere with The goal of CommonJS is to specify an ecosystem for JavaScript outside the browser. The following CommonJS methods are supported by webpack: -### `require` +### require ```typescript require(dependency: String); @@ -185,9 +185,12 @@ var $ = require('jquery'); var myModule = require('my-module'); ``` +It's possible to enable magic comments for `require` as well, see [`module.parser.javascript.commonjsMagicComments`](/configuration/module/#moduleparserjavascriptcommonjsmagiccomments) for more. + W> Using it asynchronously may not have the expected effect. + ### `require.resolve` ```typescript diff --git a/src/content/configuration/module.mdx b/src/content/configuration/module.mdx index 1947b8bbf928..25b85311814d 100644 --- a/src/content/configuration/module.mdx +++ b/src/content/configuration/module.mdx @@ -63,7 +63,7 @@ module.exports = { }; ``` -## `module.parser` +## module.parser @@ -107,6 +107,51 @@ module.exports = { }; ``` + +### module.parser.javascript + +Configure options for JavaScript parser. + +```js +module.exports = { + module: { + parser: { + javascript: { + // ... + commonjsMagicComments: true, + }, + }, + }, +}; +``` + +It's allowed to configure those options in [`Rule.parser`](/configuration/module/#ruleparser) as well to target specific modules. + +#### module.parser.javascript.commonjsMagicComments + +Enable [magic comments](/api/module-methods/#magic-comments) support for CommonJS. + +- Type: `boolean` +- Available: 5.17.0+ +- Example: + ```js + module.exports = { + module: { + parser: { + javascript: { + commonjsMagicComments: true, + }, + } + }, + }; + ``` + +Note that only `webpackIgnore` comment is supported at the moment: + +```js +const x = require(/* webpackIgnore: true */ 'x'); +``` + ## `module.noParse` `RegExp` `[RegExp]` `function(resource)` `string` `[string]` @@ -421,6 +466,7 @@ module.exports = { browserify: false, // disable special handling of Browserify bundles requireJs: false, // disable requirejs.* node: false, // disable __dirname, __filename, module, require.extensions, require.main, etc. + commonjsMagicComments: false, // disable magic comments support for CommonJS node: {...}, // reconfigure [node](/configuration/node) layer on module level worker: ["default from web-worker", "..."] // Customize the WebWorker handling for javascript files, "..." refers to the defaults. }