Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(readme): add types #903

Merged
merged 5 commits into from Jan 25, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
139 changes: 101 additions & 38 deletions README.md
Expand Up @@ -31,10 +31,16 @@ Compared to the extract-text-webpack-plugin:

To begin, you'll need to install `mini-css-extract-plugin`:

```bash
```console
npm install --save-dev mini-css-extract-plugin
```

or

```console
yarn add -D mini-css-extract-plugin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change it on npm, because we use npm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to provide yarn, we should also provide pnpm example, i.e. for all package managers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I've seen npm and yarn in some repos. That is why I started adding yarn in all repos. Should we consider adding pnpm as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add yarn we should add pnpm too

```

It's recommended to combine `mini-css-extract-plugin` with the [`css-loader`](https://github.com/webpack-contrib/css-loader)

Then add the loader and the plugin to your `webpack` config. For example:
Expand Down Expand Up @@ -79,20 +85,25 @@ module.exports = {

### Plugin Options

| Name | Type | Default | Description |
| :---------------------------------------------------------------: | :------------------: | :-----------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------- |
| **[`filename`](#filename)** | `{String\|Function}` | `[name].css` | This option determines the name of each output CSS file |
| **[`chunkFilename`](#chunkFilename)** | `{String\|Function}` | `based on filename` | This option determines the name of non-entry chunk files |
| **[`ignoreOrder`](#ignoreOrder)** | `{Boolean}` | `false` | Remove Order Warnings |
| **[`insert`](#insert)** | `{String\|Function}` | `document.head.appendChild(linkTag);` | Inserts the `link` tag at the given position for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) CSS chunks |
| **[`attributes`](#attributes)** | `{Object}` | `{}` | Adds custom attributes to the `link` tag for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) CSS chunks |
| **[`linkType`](#linkType)** | `{String\|Boolean}` | `text/css` | Allows loading asynchronous chunks with a custom link type |
| **[`runtime`](#runtime)** | `{Boolean}` | `true` | Allows to enable/disable the runtime generation |
| **[`experimentalUseImportModule`](#experimentalUseImportModule)** | `{Boolean}` | `undefined` | Use an experimental webpack API to execute modules instead of child compilers |
- **[`filename`](#filename)**
- **[`chunkFilename`](#chunkFilename)**
- **[`ignoreOrder`](#ignoreOrder)**
- **[`insert`](#insert)**
- **[`attributes`](#attributes)**
- **[`linkType`](#linkType)**
- **[`runtime`](#runtime)**
- **[`experimentalUseImportModule`](#experimentalUseImportModule)**

#### `filename`

Type: `String|Function`
Type:

```ts
type filename =
| string
| ((pathData: PathData, assetInfo?: AssetInfo) => string);
```

Default: `[name].css`

This option determines the name of each output CSS file.
Expand All @@ -101,26 +112,43 @@ Works like [`output.filename`](https://webpack.js.org/configuration/output/#outp

#### `chunkFilename`

Type: `String|Function`
Type:

```ts
type chunkFilename =
| string
| ((pathData: PathData, assetInfo?: AssetInfo) => string);
```

Default: `based on filename`

> i Specifying `chunkFilename` as a `function` is only available in webpack@5
> Specifying `chunkFilename` as a `function` is only available in webpack@5

This option determines the name of non-entry chunk files.

Works like [`output.chunkFilename`](https://webpack.js.org/configuration/output/#outputchunkfilename)

#### `ignoreOrder`

Type: `Boolean`
Type:

```ts
type ignoreOrder = boolean;
```

Default: `false`

Remove Order Warnings.
See [examples](#remove-order-warnings) below for details.

#### `insert`

Type: `String|Function`
Type:

```ts
type insert = string | ((linkTag: any) => void);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using any, if you found it in our code, it means bug, we should improve this type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linkTag here is an HTMLElement right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, but mostly yes, just check it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to HTMLLinkElement

```

Default: `document.head.appendChild(linkTag);`

> ⚠️ Only for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks.
Expand All @@ -133,7 +161,7 @@ In such cases `insert` can be configured to be a function or a custom selector.

If you target an [iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement) make sure that the parent document has sufficient access rights to reach into the frame document and append elements to it.

##### `String`
##### `string`

Allows to setup custom [query selector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector).
A new `<link>` element will be inserted after the found item.
Expand All @@ -148,7 +176,7 @@ new MiniCssExtractPlugin({

A new `<link>` element will be inserted after the element with id `some-element`.

##### `Function`
##### `function`

Allows to override default behavior and insert styles at any position.

Expand All @@ -173,7 +201,12 @@ A new `<link>` element will be inserted before the element with id `some-element

#### `attributes`

Type: `Object`
Type:

```ts
type attributes = object;
harish-sethuraman marked this conversation as resolved.
Show resolved Hide resolved
```

Default: `{}`

> ⚠️ Only for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks.
Expand Down Expand Up @@ -209,12 +242,17 @@ Note: It's only applied to dynamically loaded css chunks, if you want to modify

#### `linkType`

Type: `String|Boolean`
Type:

```ts
type linkType = string | boolean;
```

Default: `text/css`

This option allows loading asynchronous chunks with a custom link type, such as `<link type="text/css" ...>`.

##### `String`
##### `string`

Possible values: `text/css`

Expand All @@ -240,7 +278,7 @@ module.exports = {
};
```

##### `Boolean`
##### `boolean`

`false` disables the link `type` attribute

Expand Down Expand Up @@ -268,12 +306,17 @@ module.exports = {

#### `runtime`

Type: `Boolean`
Type:

```ts
type runtime = boolean;
```

Default: `true`

Allows to enable/disable the runtime generation.
CSS will be still extracted and can be used for a custom loading methods.
For example, you can use [assets-webpack-plugin](https://github.com/ztoben/assets-webpack-plugin) to retreive them then use your own runtime code to download assets when needed.
For example, you can use [assets-webpack-plugin](https://github.com/ztoben/assets-webpack-plugin) to retrieve them then use your own runtime code to download assets when needed.

`true` to skip.

Expand Down Expand Up @@ -301,7 +344,12 @@ module.exports = {

#### `experimentalUseImportModule`

Type: `Boolean`
Type:

```ts
type experimentalUseImportModule = boolean;
```

Default: `undefined`

Enabled by default if not explicitly enabled (i.e. `true` and `false` allow you to explicitly control this option) and new API is available (at least webpack `5.52.0` is required).
Expand All @@ -322,7 +370,7 @@ module.exports = {
new MiniCssExtractPlugin({
// You don't need this for `>= 5.52.0` due to the fact that this is enabled by default
// Required only for `>= 5.33.2 & <= 5.52.0`
// Not avaliable/unsafe for `<= 5.33.2`
// Not available/unsafe for `<= 5.33.2`
experimentalUseImportModule: true,
}),
],
Expand All @@ -339,21 +387,26 @@ module.exports = {

### Loader Options

| Name | Type | Default | Description |
| :-----------------------------: | :------------------: | :--------------------------------: | :-------------------------------------------------------------------------------- |
| **[`publicPath`](#publicPath)** | `{String\|Function}` | `webpackOptions.output.publicPath` | Specifies a custom public path for the external resources like images, files, etc |
| **[`emit`](#emit)** | `{Boolean}` | `true` | If false, the plugin will extract the CSS but **will not** emit the file |
| **[`esModule`](#esModule)** | `{Boolean}` | `true` | Use ES modules syntax |
- **[`publicPath`](#publicPath)**
- **[`emit`](#emit)**
- **[`esModule`](#esModule)**

#### `publicPath`

Type: `String|Function`
Type:

```ts
type publicPath =
| string
| ((resourcePath: string, rootContext: string) => string);
```

Default: the `publicPath` in `webpackOptions.output`

Specifies a custom public path for the external resources like images, files, etc inside `CSS`.
Works like [`output.publicPath`](https://webpack.js.org/configuration/output/#outputpublicpath)

##### `String`
##### `string`

**webpack.config.js**

Expand Down Expand Up @@ -388,7 +441,7 @@ module.exports = {
};
```

##### `Function`
##### `function`

**webpack.config.js**

Expand Down Expand Up @@ -427,15 +480,25 @@ module.exports = {

#### `emit`

Type: `Boolean`
Type:

```ts
type emit = boolean;
```

Default: `true`

If true, emits a file (writes a file to the filesystem). If false, the plugin will extract the CSS but **will not** emit the file.
It is often useful to disable this option for server-side packages.

#### `esModule`

Type: `Boolean`
Type:

```ts
type esModule = boolean;
```

Default: `true`

By default, `mini-css-extract-plugin` generates JS modules that use the ES modules syntax.
Expand Down Expand Up @@ -471,13 +534,13 @@ module.exports = {

## Examples

### Recommend
### Recommended

For `production` builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
This can be achieved by using the `mini-css-extract-plugin`, because it creates separate css files.
For `development` mode (including `webpack-dev-server`) you can use [style-loader](https://github.com/webpack-contrib/style-loader), because it injects CSS into the DOM using multiple <style></style> and works faster.

> i Do not use together `style-loader` and `mini-css-extract-plugin`.
> Do not use `style-loader` and `mini-css-extract-plugin` together.

**webpack.config.js**

Expand Down