Skip to content

Commit 6a93a52

Browse files
author
Seth Davenport
committed
feat(autoprefixer) Hook up autoprefixer for less, scss, and css.
[Autoprefixer](https://github.com/postcss/autoprefixer) is a tool that uses data from caniuse.com to add vendor prefixes for styles that need them. It saves developers from having to manually prefix things, and is widely used these days. Since it's available as a postcss plugin, it makes sense to inlcude it in the angular-cli build step by default. This implementation uses the default browser targeting settings: last two versions, or whatever's in the user's project's [browserslist](https://github.com/ai/browserslist). Closes #1512.
1 parent ea7c039 commit 6a93a52

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

packages/angular-cli/models/webpack-build-common.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {GlobCopyWebpackPlugin} from '../plugins/glob-copy-webpack-plugin';
44
import {BaseHrefWebpackPlugin} from '@angular-cli/base-href-webpack';
55

66
const HtmlWebpackPlugin = require('html-webpack-plugin');
7+
const autoprefixer = require('autoprefixer');
78

89

910
export function getWebpackCommonConfig(
@@ -131,7 +132,13 @@ export function getWebpackCommonConfig(
131132
new GlobCopyWebpackPlugin({
132133
patterns: appConfig.assets,
133134
globOptions: {cwd: appRoot, dot: true, ignore: '**/.gitkeep'}
134-
})
135+
}),
136+
new webpack.LoaderOptionsPlugin({
137+
test: /\.(css|scss|sass|less|styl)$/,
138+
options: {
139+
postcss: [ autoprefixer() ]
140+
},
141+
}),
135142
],
136143
node: {
137144
fs: 'empty',

tests/e2e/tests/build/styles/css.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ export default function() {
88
@import './imported-styles.css';
99
1010
body { background-color: blue; }
11+
12+
div { flex: 1 }
1113
`,
1214
'src/imported-styles.css': `
1315
p { background-color: red; }
1416
`
1517
})
1618
.then(() => ng('build'))
1719
.then(() => expectFileToMatch('dist/styles.bundle.js', 'body { background-color: blue; }'))
18-
.then(() => expectFileToMatch('dist/styles.bundle.js', 'p { background-color: red; }'));
20+
.then(() => expectFileToMatch('dist/styles.bundle.js', 'p { background-color: red; }'))
21+
.then(() => expectFileToMatch(
22+
'dist/styles.bundle.js',
23+
'div { -webkit-box-flex: 1; -ms-flex: 1; flex: 1 }'));
1924
}

tests/e2e/tests/build/styles/less.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ export default function() {
2222
background: #fff;
2323
}
2424
}
25+
26+
div { flex: 1 }
2527
`
2628
})
2729
.then(() => deleteFile('src/app/app.component.css'))
2830
.then(() => replaceInFile('src/app/app.component.ts',
2931
'./app.component.css', './app.component.less'))
3032
.then(() => ng('build'))
3133
.then(() => expectFileToMatch('dist/main.bundle.js', /.outer.*.inner.*background:\s*#[fF]+/))
34+
.then(() => expectFileToMatch(
35+
'dist/main.bundle.js',
36+
'div { -webkit-box-flex: 1; -ms-flex: 1; flex: 1 }'))
3237
.then(() => moveFile('src/app/app.component.less', 'src/app/app.component.css'));
3338
}

tests/e2e/tests/build/styles/scss.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default function() {
2424
background: #def;
2525
}
2626
}
27+
28+
div { flex: 1 }
2729
`,
2830
'src/app/app.component.partial.scss': stripIndents`
2931
.partial {
@@ -37,5 +39,8 @@ export default function() {
3739
.then(() => ng('build'))
3840
.then(() => expectFileToMatch('dist/main.bundle.js', /\.outer.*\.inner.*background.*#def/))
3941
.then(() => expectFileToMatch('dist/main.bundle.js', /\.partial.*\.inner.*background.*#def/))
42+
.then(() => expectFileToMatch(
43+
'dist/main.bundle.js',
44+
'div { -webkit-box-flex: 1; -ms-flex: 1; flex: 1 }'))
4045
.then(() => moveFile('src/app/app.component.scss', 'src/app/app.component.css'));
4146
}

0 commit comments

Comments
 (0)