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

Added option to disable async loading of CSS #348

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ new MiniCssExtractPlugin({
}),
```

### Disable Asynchronous Downloading of CSS

For projects in which you do not want CSS asynchronously fetched on the client side, and only want the CSS that is delivered on the server side (via link tags) to be fetched and parsed, enabling this option will give you that functionality.

```javascript
new MiniCssExtractPlugin({
disableAsync: true,
}),
```

### Media Query Plugin

If you'd like to extract the media queries from the extracted CSS (so mobile users don't need to load desktop or tablet specific CSS anymore) you should use one of the following plugins:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"test:watch": "cross-env NODE_ENV=test jest --watch",
"test:coverage": "cross-env NODE_ENV=test jest --collectCoverageFrom=\"src/**/*.js\" --coverage",
"test:manual": "webpack-dev-server test/manual/src/index.js --open --config test/manual/webpack.config.js",
"test:manualDisableAsync": "webpack-dev-server test/manualDisableAsyncOption/src/index.js --open --config test/manualDisableAsyncOption/webpack.config.js",
"pretest": "npm run lint",
"test": "cross-env NODE_ENV=test npm run test:coverage",
"defaults": "webpack-defaults"
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class MiniCssExtractPlugin {
mainTemplate.hooks.requireEnsure.tap(
pluginName,
(source, chunk, hash) => {
if (this.options.disableAsync) return null;
const chunkMap = this.getCssChunkObject(chunk);

if (Object.keys(chunkMap).length > 0) {
Expand Down
15 changes: 15 additions & 0 deletions test/manualDisableAsyncOption/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>mini-css-extract-plugin testcase</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/dist/a.css" />
</head>
<body>
<script type="text/javascript" src="/dist/a.js"></script>
<script type="text/javascript" src="/dist/b.js"></script>
<script type="text/javascript" src="/dist/main.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions test/manualDisableAsyncOption/src/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.container {
background: blue;
color: red;
width: 100%;
height: 20px;
text-align: center;
}
7 changes: 7 additions & 0 deletions test/manualDisableAsyncOption/src/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.container {
background: purple;
color: green;
width: 100%;
height: 20px;
text-align: center;
}
14 changes: 14 additions & 0 deletions test/manualDisableAsyncOption/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-env browser */

import './a.css';
import './b.css';

const render = () => {
const div = document.createElement('div');
div.setAttribute('class', 'container');
const text = 'My background color should be blue, and my text should be red!';
div.innerHTML = text;
document.body.appendChild(div);
};

render();
45 changes: 45 additions & 0 deletions test/manualDisableAsyncOption/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Self = require('../../');

module.exports = {
mode: 'development',
output: {
chunkFilename: '[name].js',
publicPath: '/dist/',
crossOriginLoading: 'anonymous',
},
optimization: {
splitChunks: {
cacheGroups: {
default: false,
a: {
name: 'a',
test: /a.css/,
chunks: 'all',
enforce: true,
},
b: {
name: 'b',
test: /b.css/,
chunks: 'all',
enforce: true,
},
},
},
},
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, 'css-loader'],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
devServer: {
contentBase: __dirname,
},
};