Skip to content

Commit

Permalink
feat(autoprefixer) Hook up autoprefixer for less, scss, and css.
Browse files Browse the repository at this point in the history
[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.
  • Loading branch information
Seth Davenport committed Oct 24, 2016
1 parent ea7c039 commit 39c41a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/angular-cli/models/webpack-build-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {GlobCopyWebpackPlugin} from '../plugins/glob-copy-webpack-plugin';
import {BaseHrefWebpackPlugin} from '@angular-cli/base-href-webpack';

const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');


export function getWebpackCommonConfig(
Expand Down Expand Up @@ -131,7 +132,13 @@ export function getWebpackCommonConfig(
new GlobCopyWebpackPlugin({
patterns: appConfig.assets,
globOptions: {cwd: appRoot, dot: true, ignore: '**/.gitkeep'}
})
}),
new webpack.LoaderOptionsPlugin({
test: /\.(css|scss|sass|less|styl)$/,
options: {
postcss: [ autoprefixer() ]
},
}),
],
node: {
fs: 'empty',
Expand Down
7 changes: 6 additions & 1 deletion tests/e2e/tests/build/styles/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ export default function() {
@import './imported-styles.css';
body { background-color: blue; }
div { flex: 1 }
`,
'src/imported-styles.css': `
p { background-color: red; }
`
})
.then(() => ng('build'))
.then(() => expectFileToMatch('dist/styles.bundle.js', 'body { background-color: blue; }'))
.then(() => expectFileToMatch('dist/styles.bundle.js', 'p { background-color: red; }'));
.then(() => expectFileToMatch('dist/styles.bundle.js', 'p { background-color: red; }'))
.then(() => expectFileToMatch(
'dist/styles.bundle.js',
'div { -webkit-box-flex: 1; -ms-flex: 1; flex: 1 }'));
}
5 changes: 5 additions & 0 deletions tests/e2e/tests/build/styles/less.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ export default function() {
background: #fff;
}
}
div { flex: 1 }
`
})
.then(() => deleteFile('src/app/app.component.css'))
.then(() => replaceInFile('src/app/app.component.ts',
'./app.component.css', './app.component.less'))
.then(() => ng('build'))
.then(() => expectFileToMatch('dist/main.bundle.js', /.outer.*.inner.*background:\s*#[fF]+/))
.then(() => expectFileToMatch(
'dist/main.bundle.js',
'div { -webkit-box-flex: 1; -ms-flex: 1; flex: 1 }'))
.then(() => moveFile('src/app/app.component.less', 'src/app/app.component.css'));
}
5 changes: 5 additions & 0 deletions tests/e2e/tests/build/styles/scss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default function() {
background: #def;
}
}
div { flex: 1 }
`,
'src/app/app.component.partial.scss': stripIndents`
.partial {
Expand All @@ -37,5 +39,8 @@ export default function() {
.then(() => ng('build'))
.then(() => expectFileToMatch('dist/main.bundle.js', /\.outer.*\.inner.*background.*#def/))
.then(() => expectFileToMatch('dist/main.bundle.js', /\.partial.*\.inner.*background.*#def/))
.then(() => expectFileToMatch(
'dist/styles.bundle.js',
'div { -webkit-box-flex: 1; -ms-flex: 1; flex: 1 }'))
.then(() => moveFile('src/app/app.component.scss', 'src/app/app.component.css'));
}

0 comments on commit 39c41a1

Please sign in to comment.