Skip to content

Commit

Permalink
docs: internal plugins doc (web-infra-dev#6483)
Browse files Browse the repository at this point in the history
* docs: internal plugins doc

* chore: full doc coverage check

* fix: zh title

* docs: eval-source-map-dev-tool-plugin

* chore: update api.md

* docs: add ApiMeta

* fix: internal plugins warning and nav order
  • Loading branch information
SyMind authored May 10, 2024
1 parent 4e52199 commit 5eeddf8
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 71 deletions.
17 changes: 17 additions & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5494,12 +5494,29 @@ const node_2: z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{

// @public (undocumented)
interface Node_3 {
// (undocumented)
NodeEnvironmentPlugin: typeof NodeEnvironmentPlugin;
// (undocumented)
NodeTargetPlugin: typeof NodeTargetPlugin;
// (undocumented)
NodeTemplatePlugin: typeof NodeTemplatePlugin;
}

// @public (undocumented)
class NodeEnvironmentPlugin {
constructor(options: NodeEnvironmentPluginOptions);
// (undocumented)
apply(compiler: Compiler): void;
// (undocumented)
options: NodeEnvironmentPluginOptions;
}

// @public (undocumented)
interface NodeEnvironmentPluginOptions {
// (undocumented)
infrastructureLogging: InfrastructureLogging;
}

// @public (undocumented)
interface NodeNextConfig extends BaseModuleConfig {
// (undocumented)
Expand Down
33 changes: 23 additions & 10 deletions packages/rspack/scripts/check-documentation-coverage.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import {
ApiModel,
ApiItemKind,
ApiVariable
} from "@microsoft/api-extractor-model";
import { ApiModel, ApiItemKind } from "@microsoft/api-extractor-model";
import { fileURLToPath } from "url";
import { dirname, resolve, extname, basename } from "path";
import { readdirSync } from "fs";
import { dirname, resolve, extname, basename, join } from "path";
import { readdirSync, readFileSync } from "fs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const PLUGIN_REGEX = /^[A-Z][a-zA-Z]+Plugin$/;
const PLUGIN_API_JSON = resolve(__dirname, "../temp/core.api.json");
const PLUGIN_DOCS_DIR = resolve(__dirname, "../../../website/docs/en/plugins");
const INTERNAL_PLUGINS_DOC = join(PLUGIN_DOCS_DIR, "webpack/internal-plugins.mdx");

function getImplementedPlugins() {
const apiModel = new ApiModel();
Expand Down Expand Up @@ -44,6 +41,16 @@ function toCamelCase(s) {
.join("");
}

function extractMarkdownHeadings(markdown) {
const headingRegex = /^(#{1,6})\s+(.+)$/gm;
const headings = [];
let match;
while ((match = headingRegex.exec(markdown)) !== null) {
headings.push(match[2].trim());
}
return headings;
}

function getDocumentedPlugins() {
const documentedPlugins = new Set();

Expand All @@ -66,6 +73,14 @@ function getDocumentedPlugins() {
}
visitDir(PLUGIN_DOCS_DIR);

const internalPluginsDoc = readFileSync(INTERNAL_PLUGINS_DOC, 'utf-8');
const headings = extractMarkdownHeadings(internalPluginsDoc);
for (const heading of headings) {
if (PLUGIN_REGEX.test(heading)) {
documentedPlugins.add(heading);
}
}

return documentedPlugins;
}

Expand Down Expand Up @@ -96,8 +111,6 @@ if (unimplementedPlugins.length) {
);
}

// TODO: remove the comments below once all plugins have been documented
// if (undocumentedPlugins.length || unimplementedPlugins.length) {
if (unimplementedPlugins.length) {
if (undocumentedPlugins.length || unimplementedPlugins.length) {
process.exit(1);
}
8 changes: 7 additions & 1 deletion packages/rspack/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@ export { NormalModuleReplacementPlugin } from "./lib/NormalModuleReplacementPlug

import NodeTemplatePlugin from "./node/NodeTemplatePlugin";
import { NodeTargetPlugin } from "./builtin-plugin";
import NodeEnvironmentPlugin from "./node/NodeEnvironmentPlugin";
interface Node {
NodeTargetPlugin: typeof NodeTargetPlugin;
NodeTemplatePlugin: typeof NodeTemplatePlugin;
NodeEnvironmentPlugin: typeof NodeEnvironmentPlugin;
}
export const node: Node = { NodeTargetPlugin, NodeTemplatePlugin };
export const node: Node = {
NodeTargetPlugin,
NodeTemplatePlugin,
NodeEnvironmentPlugin
};

import { ElectronTargetPlugin } from "./builtin-plugin";
interface Electron {
Expand Down
6 changes: 3 additions & 3 deletions website/docs/en/plugins/webpack/_meta.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[
"index",
"entry-plugin",
"define-plugin",
"provide-plugin",
"banner-plugin",
"hot-module-replacement-plugin",
"ignore-plugin",
"progress-plugin",
"externals-plugin",
"eval-source-map-dev-tool-plugin",
"source-map-dev-tool-plugin",
"split-chunks-plugin",
"node-target-plugin",
"node-template-plugin",
"enable-chunk-loading-plugin",
"enable-library-plugin",
"enable-wasm-loading-plugin",
"electron-target-plugin",
"module-federation-plugin",
"module-federation-plugin-v1",
"environment-plugin",
"limit-chunk-count-plugin",
"normal-module-replacement-plugin"
"normal-module-replacement-plugin",
"internal-plugins"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import WebpackLicense from '@components/webpack-license';

<WebpackLicense from="https://webpack.js.org/plugins/eval-source-map-dev-tool-plugin/" />

# EvalSourceMapDevToolPlugin

This plugin enables more fine grained control of source map generation. It is also enabled automatically by certain settings of the [`devtool`](/config/devtool) configuration option.

```js
new webpack.EvalSourceMapDevToolPlugin(options);
```

## Options

### append

- **Type:** `string | function`

Appends the given value to the original asset. Usually the `#sourceMappingURL` comment. `[url]` is replaced with a URL to the source map file. `false` disables the appending.

### module

- **Type:** `boolean`

Indicates whether loaders should generate source maps (defaults to `true`).

### columns

- **Type:** `boolean`

Indicates whether column mappings should be used (defaults to `true`).

## Examples

### Basic Use Case

You can use the following code to replace the configuration option `devtool: eval-source-map` with an equivalent custom plugin configuration:

```js
module.exports = {
// ...
devtool: false,
plugins: [new rspack.EvalSourceMapDevToolPlugin({})],
};
```

### Exclude Vendor Maps

The following code would exclude source maps for any modules in the `vendor.js` bundle:

```js
new rspack.EvalSourceMapDevToolPlugin({
exclude: ['vendor.js'],
});
```
175 changes: 175 additions & 0 deletions website/docs/en/plugins/webpack/internal-plugins.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { ApiMeta } from '@components/ApiMeta.tsx';
import WebpackLicense from '@components/webpack-license';

<WebpackLicense from="https://webpack.js.org/plugins/internal-plugins/" />

# Internal plugins

This is a list of plugins used internally by Rspack, aligned with the plugins used internally by webpack.

:::warning
You should only concern yourself with these plugins if you are building your own compiler based on Rspack, or for internal inspection.
:::

Categories of internal plugins:

- [environment](#environment)
- [compiler](#compiler)
- [entry](#entry)
- [output](#output)
- [source](#source)
- [optimize](#optimize)
- [loader](#loader)
- [module federation](#module-federation)

## environment

Plugins affecting the environment of the compiler.

### ElectronTargetPlugin

`electron.ElectronTargetPlugin(context)`

Customizes the handling of external dependencies for different contexts (such as main process, preload scripts, and renderer process) in Electron applications.

[`externalsPresets.electron`](/config/externals#electron), [`externalsPresets.electronMain`](/config/externals#electronmain), [`externalsPresets.electronRenderer`](/config/externals#electronrenderer), and [`externalsPresets.electronPreload`](/config/externals#electronpreload) all rely on this plugin.

### NodeEnvironmentPlugin

`node.NodeEnvironmentPlugin()`

Applies Node.js style filesystem to the compiler.

## compiler

Plugins affecting the compiler.

### ProgressPlugin

`ProgressPlugin(handler)`

Hook into the compiler to extract progress information. The `handler` must have the signature `function(percentage, message)`. Percentage is called with a value between 0 and 1, where 0 indicates the start and 1 the end.

## entry

Plugins, which add entry chunks to the compilation.

### EntryPlugin

`EntryPlugin(context, entry, options)`

Adds an entry chunk on compilation. The chunk is named `options.name` and contains only one module (plus dependencies). The module is resolved from `entry` in `context` (absolute path).

- `context`: The base path for module resolution, under which the entry module resolution will be conducted.
- `entry`: Specifies the module path to be used as an entry point.
- `options`: Additional configurations for the entry module.

### DynamicEntryPlugin

<ApiMeta addedVersion={'0.6.4'} />

`DynamicEntryPlugin(context, entry)`

Similar to `EntryPlugin` but accepts a function as the `entry` argument. This function is called during each `make` event in the build process to support dynamically determining the entry points.

### EntryOptionPlugin

`EntryOptionPlugin()`

## output

### EvalDevToolModulePlugin

`EvalDevToolModulePlugin(options)`

Decorates the module template by wrapping each module in a `eval` annotated with `// @sourceURL`.

### WebWorkerTemplatePlugin

`webworker.WebWorkerTemplatePlugin(options)`

Chunks are loaded by `importScripts`.

`options` are the output options.

## source

Plugins affecting the source code of modules.

### ProvidePlugin

<ApiMeta addedVersion={'0.3.3'} />

`ProvidePlugin(name, request)`

If `name` is used in a module it is filled by a module loaded by `require(<request>)`.

### NodeTargetPlugin

<ApiMeta addedVersion={'0.3.3'} />

`node.NodeTargetPlugin()`

The plugins should be used if you run the bundle in a Node.js environment.

If ensures that native modules are loaded correctly even if bundled.

## optimize

Note that all plugins under `rspack.optimize` namespace should only be used when `mode` set to `'none'`. Otherwise you might get into trouble where plugins are applied twice.

### LimitChunkCountPlugin

<ApiMeta addedVersion={'0.3.10'} />

`optimize.LimitChunkCountPlugin(options)`

Merge chunks limit chunk count is lower than `options.maxChunks`.

The overhead for each chunks is provided by `options.chunkOverhead` or defaults to 10000. Entry chunks sizes are multiplied by `options.entryChunkMultiplicator` (or 10).

Chunks that reduce the total size the most are merged first. If multiple combinations are equal the minimal merged size wins.

## loader

### LoaderOptionsPlugin

`LoaderOptionsPlugin(options)`

### LoaderTargetPlugin

`LoaderTargetPlugin(target)`

## module federation

Internal plugins used with Module Federation, which are the basis of the `ModuleFederationPlugin`.

### ContainerPlugin

<ApiMeta addedVersion={'0.5.0'} />

`container.ContainerPlugin(options)`

### ContainerReferencePlugin

<ApiMeta addedVersion={'0.5.0'} />

`container.ContainerReferencePlugin(options)`

### ConsumeSharedPlugin

<ApiMeta addedVersion={'0.5.0'} />

`sharing.ConsumeSharedPlugin(options)`

### ProvideSharedPlugin

<ApiMeta addedVersion={'0.5.0'} />

`sharing.ProvideSharedPlugin(options)`

### SharePlugin

<ApiMeta addedVersion={'0.5.0'} />

`sharing.SharePlugin(options)`
Loading

0 comments on commit 5eeddf8

Please sign in to comment.