Skip to content

Commit bceecd5

Browse files
committed
[optimizer] upgrade to postcss+autoprefixer
1 parent a1e3357 commit bceecd5

4 files changed

Lines changed: 110 additions & 33 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"angular-route": "1.4.7",
8989
"angular-sanitize": "1.5.7",
9090
"ansicolors": "0.3.2",
91-
"autoprefixer": "6.3.7",
91+
"autoprefixer": "6.5.4",
9292
"autoprefixer-loader": "2.0.0",
9393
"babel": "5.8.38",
9494
"babel-core": "5.8.38",
@@ -133,7 +133,7 @@
133133
"json-stringify-safe": "5.0.1",
134134
"jstimezonedetect": "1.0.5",
135135
"leaflet": "0.7.5",
136-
"less": "2.7.0",
136+
"less": "2.7.1",
137137
"less-loader": "2.2.3",
138138
"lodash": "3.10.1",
139139
"marked": "0.3.6",
@@ -146,6 +146,7 @@
146146
"node-sass": "3.8.0",
147147
"node-uuid": "1.4.7",
148148
"pegjs": "0.9.0",
149+
"postcss-loader": "1.2.1",
149150
"querystring-browser": "1.0.4",
150151
"raw-loader": "0.5.1",
151152
"request": "2.61.0",

src/optimize/base_optimizer.js

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
import { resolve } from 'path';
2+
import { writeFile } from 'fs';
3+
import { inherits } from 'util';
4+
import { format as formatUrl } from 'url';
5+
16
import webpack from 'webpack';
27
import Boom from 'boom';
38
import DirectoryNameAsMain from 'webpack-directory-name-as-main';
49
import ExtractTextPlugin from 'extract-text-webpack-plugin';
510
import CommonsChunkPlugin from 'webpack/lib/optimize/CommonsChunkPlugin';
611
import DefinePlugin from 'webpack/lib/DefinePlugin';
712
import UglifyJsPlugin from 'webpack/lib/optimize/UglifyJsPlugin';
13+
import { defaults, transform } from 'lodash';
814

915
import fromRoot from '../utils/from_root';
1016
import babelOptions from './babel_options';
11-
import { inherits } from 'util';
12-
import { defaults, transform } from 'lodash';
13-
import { resolve } from 'path';
14-
import { writeFile } from 'fs';
15-
const babelExclude = [/[\/\\](webpackShims|node_modules|bower_components)[\/\\]/];
1617
import pkg from '../../package.json';
18+
import { setLoaderQueryParam, makeLoaderString } from './loaders';
19+
20+
const babelExclude = [/[\/\\](webpackShims|node_modules|bower_components)[\/\\]/];
1721

1822
class BaseOptimizer {
1923
constructor(opts) {
@@ -64,8 +68,38 @@ class BaseOptimizer {
6468
}
6569

6670
getConfig() {
67-
const mapQ = this.sourceMaps ? '?sourceMap' : '';
68-
const mapQPre = mapQ ? mapQ + '&' : '?';
71+
const loaderWithSourceMaps = (loader) =>
72+
setLoaderQueryParam(loader, 'sourceMap', !!this.sourceMaps);
73+
74+
const makeStyleLoader = preprocessor => {
75+
let loaders = [
76+
loaderWithSourceMaps('css')
77+
];
78+
79+
if (preprocessor) {
80+
loaders = [
81+
...loaders,
82+
{
83+
name: 'postcss',
84+
query: {
85+
config: require.resolve('./postcss.config')
86+
}
87+
},
88+
loaderWithSourceMaps(preprocessor)
89+
];
90+
}
91+
92+
return ExtractTextPlugin.extract(makeLoaderString(loaders));
93+
};
94+
95+
const makeBabelLoader = query => {
96+
return makeLoaderString([
97+
{
98+
name: 'babel',
99+
query: defaults({}, query || {}, babelOptions.webpack)
100+
}
101+
]);
102+
};
69103

70104
return {
71105
context: fromRoot('.'),
@@ -101,40 +135,24 @@ class BaseOptimizer {
101135

102136
module: {
103137
loaders: [
104-
{
105-
test: /\.less$/,
106-
loader: ExtractTextPlugin.extract(
107-
'style',
108-
`css${mapQ}!autoprefixer${mapQPre}{ "browsers": ["last 2 versions","> 5%"] }!less${mapQPre}dumpLineNumbers=comments`
109-
)
110-
},
111-
{
112-
test: /\.scss$/,
113-
loader: ExtractTextPlugin.extract(
114-
'style',
115-
`css${mapQ}!autoprefixer${mapQPre}{ "browsers": ["last 2 versions","> 5%"] }!sass${mapQPre}`
116-
)
117-
},
118-
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style', `css${mapQ}`) },
138+
{ test: /\.less$/, loader: makeStyleLoader('less') },
139+
{ test: /\.scss$/, loader: makeStyleLoader('sass') },
140+
{ test: /\.css$/, loader: makeStyleLoader() },
119141
{ test: /\.jade$/, loader: 'jade' },
120142
{ test: /\.json$/, loader: 'json' },
121143
{ test: /\.(html|tmpl)$/, loader: 'raw' },
122-
{ test: /\.png$/, loader: 'url?limit=10000&name=[path][name].[ext]' },
123-
{ test: /\.(woff|woff2|ttf|eot|svg|ico)(\?|$)/, loader: 'file?name=[path][name].[ext]' },
124-
{ test: /[\/\\]src[\/\\](core_plugins|ui)[\/\\].+\.js$/, loader: `rjs-repack${mapQ}` },
144+
{ test: /\.png$/, loader: 'url' },
145+
{ test: /\.(woff|woff2|ttf|eot|svg|ico)(\?|$)/, loader: 'file' },
146+
{ test: /[\/\\]src[\/\\](core_plugins|ui)[\/\\].+\.js$/, loader: loaderWithSourceMaps('rjs-repack') },
125147
{
126148
test: /\.js$/,
127149
exclude: babelExclude.concat(this.env.noParse),
128-
loader: 'babel',
129-
query: babelOptions.webpack
150+
loader: makeBabelLoader(),
130151
},
131152
{
132153
test: /\.jsx$/,
133154
exclude: babelExclude.concat(this.env.noParse),
134-
loader: 'babel',
135-
query: defaults({
136-
nonStandard: true,
137-
}, babelOptions.webpack)
155+
loader: makeBabelLoader({ nonStandard: true }),
138156
}
139157
].concat(this.env.loaders),
140158
postLoaders: this.env.postLoaders || [],

src/optimize/loaders.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { parse as parseUrl, format as formatUrl } from 'url';
2+
3+
class Loader {
4+
constructor({ name, query } = {}) {
5+
if (!name) {
6+
throw new Error('Loaders must define a name');
7+
}
8+
9+
this.name = name;
10+
this.query = query || {};
11+
}
12+
13+
static fromUrl(url) {
14+
const parsed = parseUrl(url, true);
15+
return new Loader({
16+
name: parsed.pathname,
17+
query: parsed.query
18+
});
19+
}
20+
21+
toString() {
22+
return formatUrl({
23+
pathname: this.name,
24+
query: this.query
25+
});
26+
}
27+
28+
setQueryParam(name, value) {
29+
this.query[name] = value;
30+
return this;
31+
}
32+
}
33+
34+
function parseLoader(spec) {
35+
if (typeof spec === 'string') {
36+
return Loader.fromUrl(spec);
37+
}
38+
39+
return new Loader(spec);
40+
}
41+
42+
export const makeLoaderString = (loaders) => {
43+
return loaders.map(parseLoader).map(l => l.toString()).join('!');
44+
};
45+
46+
export const setLoaderQueryParam = (loader, name, value) => {
47+
return parseLoader(loader).setQueryParam(name, value).toString();
48+
};

src/optimize/postcss.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
plugins: [
3+
require('autoprefixer')({
4+
browsers: [
5+
'last 2 versions',
6+
'> 5%'
7+
]
8+
})
9+
]
10+
};

0 commit comments

Comments
 (0)