Skip to content

Commit 634ed39

Browse files
authored
Fix CSS source maps (#225)
* Fix css source maps * update test * update test after cssnano minifies colors * fix unit test
1 parent 96e9fd9 commit 634ed39

File tree

5 files changed

+71
-52
lines changed

5 files changed

+71
-52
lines changed

package-lock.json

Lines changed: 47 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@types/clean-webpack-plugin": "0.1.2",
6464
"@types/connect-history-api-fallback": "1.3.1",
6565
"@types/copy-webpack-plugin": "4.4.1",
66+
"@types/cssnano": "4.0.0",
6667
"@types/execa": "0.8.0",
6768
"@types/express": "4.11.0",
6869
"@types/globby": "6.1.0",
@@ -109,6 +110,7 @@
109110
"connect-history-api-fallback": "1.5.0",
110111
"connect-inject": "0.4.0",
111112
"css-loader": "1.0.1",
113+
"cssnano": "4.1.7",
112114
"eventsource-polyfill": "0.9.6",
113115
"express": "4.16.2",
114116
"express-static-gzip": "1.1.3",

src/base.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import * as path from 'path';
1111
import * as tsnode from 'ts-node';
1212
import * as ts from 'typescript';
1313
import * as webpack from 'webpack';
14+
import * as cssnano from 'cssnano';
1415

1516
const postcssPresetEnv = require('postcss-preset-env');
1617
const postcssImport = require('postcss-import');
1718
const IgnorePlugin = require('webpack/lib/IgnorePlugin');
1819
const slash = require('slash');
1920
const WrapperPlugin = require('wrapper-webpack-plugin');
21+
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
2022

2123
const basePath = process.cwd();
2224
const srcPath = path.join(basePath, 'src');
@@ -371,6 +373,17 @@ export default function webpackConfigFactory(args: any): webpack.Configuration {
371373
new webpack.DefinePlugin({
372374
__MAIN_ENTRY: JSON.stringify(mainEntryPath)
373375
}),
376+
new OptimizeCssAssetsPlugin({
377+
cssProcessor: cssnano,
378+
cssProcessorOptions: {
379+
map: {
380+
inline: false
381+
}
382+
},
383+
cssProcessorPluginOptions: {
384+
preset: ['default', { calc: false }]
385+
}
386+
}),
374387
!singleBundle &&
375388
new BootstrapPlugin({
376389
entryPath: mainEntryPath,

src/dist.config.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { WebAppManifest } from './interfaces';
1717

1818
const BrotliPlugin = require('brotli-webpack-plugin');
1919
const CompressionPlugin = require('compression-webpack-plugin');
20-
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
2120
const TerserPlugin = require('terser-webpack-plugin');
2221
const WebpackPwaManifest = require('webpack-pwa-manifest');
2322

@@ -43,15 +42,7 @@ function webpackConfig(args: any): webpack.Configuration {
4342

4443
config.optimization = {
4544
...config.optimization,
46-
minimizer: [
47-
new TerserPlugin({ sourceMap: true, cache: true }),
48-
new OptimizeCssAssetsPlugin({
49-
cssProcessor: require('cssnano'),
50-
cssProcessorPluginOptions: {
51-
preset: ['default', { calc: false }]
52-
}
53-
})
54-
]
45+
minimizer: [new TerserPlugin({ sourceMap: true, cache: true })]
5546
};
5647

5748
config.plugins = [

tests/integration/build.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ Currently Rendered by BTR: false`
4646
it('correctly inlines and resolves external variables for legacy builds', () => {
4747
cy.request('/test-app/output/dev-app/main.css').then((response) => {
4848
const css = response.body;
49-
expect(css).to.contain('color: var(--foreground-color);');
50-
expect(css).to.contain('color: blue;');
51-
expect(css).to.contain('color: var(--primary);');
52-
expect(css).to.contain('color: red;');
49+
expect(css).to.contain('color:var(--foreground-color);');
50+
expect(css).to.contain('color:#00f;');
51+
expect(css).to.contain('color:var(--primary);');
52+
expect(css).to.contain('color:red;');
5353
});
5454
});
5555
it('correctly inlines and resolves external variables for evergreen builds', () => {
5656
cy.request('/test-app/output/dev-app-evergreen/main.css').then((response) => {
5757
const css = response.body;
58-
expect(css).to.contain('color: var(--foreground-color);');
59-
expect(css).to.contain('color: blue;');
60-
expect(css).to.contain('color: var(--primary);');
61-
expect(css).to.contain('color: red;');
58+
expect(css).to.contain('color:var(--foreground-color);');
59+
expect(css).to.contain('color:#00f;');
60+
expect(css).to.contain('color:var(--primary);');
61+
expect(css).to.contain('color:red;');
6262
});
6363
});
6464
});

0 commit comments

Comments
 (0)