|
1 | 1 | import * as webpack from 'webpack';
|
2 | 2 | import * as path from 'path';
|
| 3 | +import { oneLineTrim } from 'common-tags'; |
3 | 4 | import {
|
4 | 5 | SuppressExtractedTextChunksWebpackPlugin
|
5 | 6 | } from '../../plugins/suppress-entry-chunks-webpack-plugin';
|
6 | 7 | import { extraEntryParser, getOutputHashFormat } from './utils';
|
7 | 8 | import { WebpackConfigOptions } from '../webpack-config';
|
8 |
| -import { pluginArgs } from '../../tasks/eject'; |
| 9 | +import { pluginArgs, postcssArgs } from '../../tasks/eject'; |
9 | 10 |
|
10 | 11 | const cssnano = require('cssnano');
|
| 12 | +const postcssUrl = require('postcss-url'); |
11 | 13 | const autoprefixer = require('autoprefixer');
|
12 | 14 | const ExtractTextPlugin = require('extract-text-webpack-plugin');
|
13 | 15 |
|
@@ -39,11 +41,35 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
|
39 | 41 | // https://github.com/webpack-contrib/style-loader#recommended-configuration
|
40 | 42 | const cssSourceMap = buildOptions.extractCss && buildOptions.sourcemap;
|
41 | 43 |
|
42 |
| - // minify/optimize css in production |
43 |
| - // autoprefixer is always run separately so disable here |
44 |
| - const extraPostCssPlugins = buildOptions.target === 'production' |
45 |
| - ? [cssnano({ safe: true, autoprefixer: false })] |
46 |
| - : []; |
| 44 | + // Minify/optimize css in production. |
| 45 | + const cssnanoPlugin = cssnano({ safe: true, autoprefixer: false }); |
| 46 | + |
| 47 | + // Convert absolute resource URLs to account for base-href and deploy-url. |
| 48 | + const baseHref = wco.buildOptions.baseHref; |
| 49 | + const deployUrl = wco.buildOptions.deployUrl; |
| 50 | + const postcssUrlOptions = { |
| 51 | + url: (URL: string) => { |
| 52 | + // Only convert absolute URLs, which CSS-Loader won't process into require(). |
| 53 | + if (!URL.startsWith('/')) { |
| 54 | + return URL; |
| 55 | + } |
| 56 | + // Join together base-href, deploy-url and the original URL. |
| 57 | + // Also dedupe multiple slashes into single ones. |
| 58 | + return `/${baseHref || ''}/${deployUrl || ''}/${URL}`.replace(/\/\/+/g, '/'); |
| 59 | + } |
| 60 | + }; |
| 61 | + const urlPlugin = postcssUrl(postcssUrlOptions); |
| 62 | + // We need to save baseHref and deployUrl for the Ejected webpack config to work (we reuse |
| 63 | + // the function defined above). |
| 64 | + (postcssUrlOptions as any).baseHref = baseHref; |
| 65 | + (postcssUrlOptions as any).deployUrl = deployUrl; |
| 66 | + // Save the original options as arguments for eject. |
| 67 | + urlPlugin[postcssArgs] = postcssUrlOptions; |
| 68 | + |
| 69 | + // PostCSS plugins. |
| 70 | + const postCssPlugins = [autoprefixer(), urlPlugin].concat( |
| 71 | + buildOptions.target === 'production' ? [cssnanoPlugin] : [] |
| 72 | + ); |
47 | 73 |
|
48 | 74 | // determine hashing format
|
49 | 75 | const hashFormat = getOutputHashFormat(buildOptions.outputHashing);
|
@@ -141,7 +167,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
|
141 | 167 | new webpack.LoaderOptionsPlugin({
|
142 | 168 | sourceMap: cssSourceMap,
|
143 | 169 | options: {
|
144 |
| - postcss: [autoprefixer()].concat(extraPostCssPlugins), |
| 170 | + postcss: postCssPlugins, |
145 | 171 | // css-loader, stylus-loader don't support LoaderOptionsPlugin properly
|
146 | 172 | // options are in query instead
|
147 | 173 | sassLoader: { sourceMap: cssSourceMap, includePaths },
|
|
0 commit comments