|
| 1 | +import { resolve } from 'path'; |
| 2 | +import { writeFile } from 'fs'; |
| 3 | +import { inherits } from 'util'; |
| 4 | +import { format as formatUrl } from 'url'; |
| 5 | + |
1 | 6 | import webpack from 'webpack'; |
2 | 7 | import Boom from 'boom'; |
3 | 8 | import DirectoryNameAsMain from 'webpack-directory-name-as-main'; |
4 | 9 | import ExtractTextPlugin from 'extract-text-webpack-plugin'; |
5 | 10 | import CommonsChunkPlugin from 'webpack/lib/optimize/CommonsChunkPlugin'; |
6 | 11 | import DefinePlugin from 'webpack/lib/DefinePlugin'; |
7 | 12 | import UglifyJsPlugin from 'webpack/lib/optimize/UglifyJsPlugin'; |
| 13 | +import { defaults, transform } from 'lodash'; |
8 | 14 |
|
9 | 15 | import fromRoot from '../utils/from_root'; |
10 | 16 | 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)[\/\\]/]; |
16 | 17 | import pkg from '../../package.json'; |
| 18 | +import { setLoaderQueryParam, makeLoaderString } from './loaders'; |
| 19 | + |
| 20 | +const babelExclude = [/[\/\\](webpackShims|node_modules|bower_components)[\/\\]/]; |
17 | 21 |
|
18 | 22 | class BaseOptimizer { |
19 | 23 | constructor(opts) { |
@@ -64,8 +68,38 @@ class BaseOptimizer { |
64 | 68 | } |
65 | 69 |
|
66 | 70 | 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 | + }; |
69 | 103 |
|
70 | 104 | return { |
71 | 105 | context: fromRoot('.'), |
@@ -101,40 +135,24 @@ class BaseOptimizer { |
101 | 135 |
|
102 | 136 | module: { |
103 | 137 | 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() }, |
119 | 141 | { test: /\.jade$/, loader: 'jade' }, |
120 | 142 | { test: /\.json$/, loader: 'json' }, |
121 | 143 | { 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') }, |
125 | 147 | { |
126 | 148 | test: /\.js$/, |
127 | 149 | exclude: babelExclude.concat(this.env.noParse), |
128 | | - loader: 'babel', |
129 | | - query: babelOptions.webpack |
| 150 | + loader: makeBabelLoader(), |
130 | 151 | }, |
131 | 152 | { |
132 | 153 | test: /\.jsx$/, |
133 | 154 | exclude: babelExclude.concat(this.env.noParse), |
134 | | - loader: 'babel', |
135 | | - query: defaults({ |
136 | | - nonStandard: true, |
137 | | - }, babelOptions.webpack) |
| 155 | + loader: makeBabelLoader({ nonStandard: true }), |
138 | 156 | } |
139 | 157 | ].concat(this.env.loaders), |
140 | 158 | postLoaders: this.env.postLoaders || [], |
|
0 commit comments