Skip to content

Commit 07e6ee1

Browse files
committed
Add general mix.css() command.
This is an alias for mix.postCss().
1 parent 8338c1f commit 07e6ee1

File tree

8 files changed

+93
-21
lines changed

8 files changed

+93
-21
lines changed

docs/css.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# General CSS Compilation
2+
3+
- [Basic Usage](#basic-usage)
4+
- [Advanced Usage](#advanced-usage)
5+
6+
### Basic Usage
7+
8+
Mix provides the `mix.css()` command for basic CSS compilation. Here's an example that imports the Normalize CSS library and adds a single rule.
9+
10+
```css
11+
/* src/app.css */
12+
13+
@import '~normalize.css/normalize.css';
14+
15+
body {
16+
color: red;
17+
}
18+
```
19+
20+
We can now add CSS compilation to our `webpack.mix.js` file, like so:
21+
22+
```js
23+
// webpack.mix.js
24+
let mix = require('laravel-mix');
25+
26+
mix.css('src/app.css', 'dist');
27+
```
28+
29+
Run webpack with `npx mix` and you'll find a `/dist/app.css` file that contains the full Normalize library, as well as your `body` rule.
30+
31+
Easy!
32+
33+
### Advanced Usage
34+
35+
As you'll find in the next section, `mix.css()` is an alias for `mix.postCss()`. This means you have full access to the entire PostCSS plugin ecosystem as part of your compilation.
36+

docs/postcss.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# PostCss Preprocessing
1+
# PostCSS Preprocessing
22

33
- [Basic Usage](#basic-usage)
44
- [Autoprefixer](#autoprefixer)
55

66
### Basic Usage
77

88
Here's a quick example to get you started. Imagine that you have the following CSS file that needs to be compiled and piped through a series of PostCSS plugins. In the example below,
9-
we'll need to pull in the `postcss-custom-properties` PostCss plugin.
9+
we'll need to pull in the `postcss-custom-properties` PostCSS plugin.
1010

1111
```css
1212
:root {
@@ -18,7 +18,7 @@ we'll need to pull in the `postcss-custom-properties` PostCss plugin.
1818
}
1919
```
2020

21-
No problem. Let's add PostCss compilation to our `webpack.mix.js` file.
21+
No problem. Let's add PostCSS compilation to our `webpack.mix.js` file.
2222

2323
```js
2424
// webpack.mix.js
@@ -28,6 +28,8 @@ let mix = require('laravel-mix');
2828
mix.postCss('src/app.css', 'dist', [require('postcss-custom-properties')]);
2929
```
3030

31+
> Tip: `mix.postCss()` and `mix.css()` are aliases. Either option works for all examples in this section.
32+
3133
Notice how, as the third argument to `mix.postCss()`, we can provide an optional array of PostCSS plugins that should be included as part of the build.
3234

3335
Compile this down as usual \(`npx mix`\), and you'll find a `/dist/app.css` file that contains:

docs/readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121

2222
### CSS
2323

24+
- [General CSS Compilation](css.md)
25+
- [PostCSS Compilation](postcss.md)
2426
- [Sass Compilation](sass.md)
2527
- [Less Compilation](less.md)
2628
- [Stylus Compilation](stylus.md)
27-
- [PostCss Compilation](postcss.md)
2829
- [Relative URL Rewriting](url-rewriting.md)
2930

3031
### Tasks

src/components/ComponentRegistrar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let components = [
1111
'Sass',
1212
'Stylus',
1313
'PostCss',
14-
'Css',
14+
'CssWebpackConfig',
1515
'BrowserSync',
1616
'Combine',
1717
'Copy',

src/components/Css.js renamed to src/components/CssWebpackConfig.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let mapValues = require('lodash').mapValues;
33
let AutomaticComponent = require('./AutomaticComponent');
44
let MiniCssExtractPlugin = require('mini-css-extract-plugin');
55

6-
class Css extends AutomaticComponent {
6+
class CssWebpackConfig extends AutomaticComponent {
77
/**
88
* webpack rules to be appended to the master config.
99
*/
@@ -12,13 +12,13 @@ class Css extends AutomaticComponent {
1212
{
1313
test: /\.css$/,
1414
use: [
15-
...Css.afterLoaders(),
15+
...CssWebpackConfig.afterLoaders(),
1616
{ loader: 'css-loader', options: { importLoaders: 1 } },
1717
{
1818
loader: 'postcss-loader',
1919
options: this.postCssOptions()
2020
},
21-
...Css.beforeLoaders({
21+
...CssWebpackConfig.beforeLoaders({
2222
type: 'css',
2323
injectGlobalStyles: true
2424
})
@@ -29,7 +29,7 @@ class Css extends AutomaticComponent {
2929
test: /\.scss$/,
3030
exclude: this.excludePathsFor('sass'),
3131
use: [
32-
...Css.afterLoaders(),
32+
...CssWebpackConfig.afterLoaders(),
3333
{ loader: 'css-loader' },
3434
{
3535
loader: 'postcss-loader',
@@ -44,7 +44,7 @@ class Css extends AutomaticComponent {
4444
}
4545
}
4646
},
47-
...Css.beforeLoaders({
47+
...CssWebpackConfig.beforeLoaders({
4848
type: 'scss',
4949
injectGlobalStyles: true
5050
})
@@ -55,7 +55,7 @@ class Css extends AutomaticComponent {
5555
test: /\.sass$/,
5656
exclude: this.excludePathsFor('sass'),
5757
use: [
58-
...Css.afterLoaders(),
58+
...CssWebpackConfig.afterLoaders(),
5959
{ loader: 'css-loader' },
6060
{
6161
loader: 'postcss-loader',
@@ -71,7 +71,7 @@ class Css extends AutomaticComponent {
7171
}
7272
}
7373
},
74-
...Css.beforeLoaders({
74+
...CssWebpackConfig.beforeLoaders({
7575
type: 'sass',
7676
injectGlobalStyles: true
7777
})
@@ -82,14 +82,14 @@ class Css extends AutomaticComponent {
8282
test: /\.less$/,
8383
exclude: this.excludePathsFor('less'),
8484
use: [
85-
...Css.afterLoaders(),
85+
...CssWebpackConfig.afterLoaders(),
8686
{ loader: 'css-loader' },
8787
{
8888
loader: 'postcss-loader',
8989
options: this.postCssOptions()
9090
},
9191
{ loader: 'less-loader' },
92-
...Css.beforeLoaders({
92+
...CssWebpackConfig.beforeLoaders({
9393
type: 'less',
9494
injectGlobalStyles: true
9595
})
@@ -100,14 +100,14 @@ class Css extends AutomaticComponent {
100100
test: /\.styl(us)?$/,
101101
exclude: this.excludePathsFor('stylus'),
102102
use: [
103-
...Css.afterLoaders(),
103+
...CssWebpackConfig.afterLoaders(),
104104
{ loader: 'css-loader' },
105105
{
106106
loader: 'postcss-loader',
107107
options: this.postCssOptions()
108108
},
109109
{ loader: 'stylus-loader' },
110-
...Css.beforeLoaders({
110+
...CssWebpackConfig.beforeLoaders({
111111
type: 'stylus',
112112
injectGlobalStyles: true
113113
})
@@ -220,7 +220,9 @@ class Css extends AutomaticComponent {
220220

221221
if (Mix.globalStyles && injectGlobalStyles) {
222222
let resources =
223-
Css.normalizeGlobalStyles(Mix.globalStyles)[type] || [];
223+
CssWebpackConfig.normalizeGlobalStyles(Mix.globalStyles)[
224+
type
225+
] || [];
224226

225227
if (resources.length) {
226228
loaders.push({
@@ -251,4 +253,4 @@ class Css extends AutomaticComponent {
251253
}
252254
}
253255

254-
module.exports = Css;
256+
module.exports = CssWebpackConfig;

src/components/PostCss.js

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ let File = require('../File');
33
let Preprocessor = require('./Preprocessor');
44

55
class PostCss extends Preprocessor {
6+
/**
7+
* The Mix API name for the component.
8+
*/
9+
name() {
10+
return ['postCss', 'css'];
11+
}
12+
613
/**
714
* Register the component.
815
*

src/components/Preprocessor.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ let Assert = require('../Assert');
22
let path = require('path');
33
let File = require('../File');
44
let { Chunks } = require('../Chunks');
5-
let Css = require('./Css');
5+
let CssWebpackConfig = require('./CssWebpackConfig');
66
let PostCssPluginsFactory = require('../PostCssPluginsFactory');
77

88
class Preprocessor {
@@ -32,7 +32,7 @@ class Preprocessor {
3232

3333
this.details.forEach(preprocessor => {
3434
let loaders = [
35-
...Css.afterLoaders({ method: 'extract' }),
35+
...CssWebpackConfig.afterLoaders({ method: 'extract' }),
3636
{
3737
loader: 'css-loader',
3838
options: {
@@ -66,7 +66,7 @@ class Preprocessor {
6666
}
6767

6868
loaders.push(
69-
...Css.beforeLoaders({
69+
...CssWebpackConfig.beforeLoaders({
7070
type: preprocessor.type,
7171
injectGlobalStyles: false
7272
})

test/features/postcss.js

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ test.after.always(() => {
2020
postCssConfigFile.delete();
2121
});
2222

23+
test('basic mix.postCss() compilation', async t => {
24+
let css = 'body { color: red; }';
25+
26+
let file = new Stub('css/basic-compilation.css', css);
27+
28+
mix.postCss(file.relativePath(), 'css');
29+
30+
await webpack.compile();
31+
32+
t.true(file.hasCompiledContent(css));
33+
});
34+
35+
test('mix.css() is an alias for mix.postCss()', async t => {
36+
let css = 'body { color: red; }';
37+
38+
let file = new Stub('css/basic-compilation.css', css);
39+
40+
mix.css(file.relativePath(), 'css');
41+
42+
await webpack.compile();
43+
44+
t.true(file.hasCompiledContent(css));
45+
});
46+
2347
test('it applies autoprefixer to compiled CSS', async t => {
2448
let css = `
2549
@keyframes testing {

0 commit comments

Comments
 (0)