Skip to content

Commit dec0a43

Browse files
committed
use CSS Modules / add postcss-cssnext/postcss-import
1 parent 93e4b3b commit dec0a43

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

packages/react-scripts/config/webpack.config.dev.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// @remove-on-eject-end
99
'use strict';
1010

11-
const autoprefixer = require('autoprefixer');
1211
const path = require('path');
1312
const webpack = require('webpack');
1413
const HtmlWebpackPlugin = require('html-webpack-plugin');
@@ -30,6 +29,29 @@ const publicUrl = '';
3029
// Get environment variables to inject into our app.
3130
const env = getClientEnvironment(publicUrl);
3231

32+
const postCSSLoaderOptions = {
33+
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
34+
plugins: loader => [
35+
require('postcss-flexbugs-fixes'),
36+
require('postcss-import')({ root: loader.resourcePath }),
37+
require('postcss-cssnext')({
38+
// postcss-cssnext passes `browsers` to multiple features, including autoprefixer
39+
// https://github.com/MoOx/postcss-cssnext/blob/3.1.0/src/index.js#L29-L33
40+
browsers: [
41+
'>1%',
42+
'last 4 versions',
43+
'Firefox ESR',
44+
'not ie < 9', // React doesn't support IE8 anyway
45+
],
46+
feature: {
47+
autoprefixer: {
48+
flexbox: 'no-2009',
49+
},
50+
},
51+
}),
52+
],
53+
};
54+
3355
// This is the development configuration.
3456
// It is focused on developer experience and fast rebuilds.
3557
// The production configuration is different and lives in a separate file.
@@ -182,7 +204,7 @@ module.exports = {
182204
// In production, we use a plugin to extract that CSS to a file, but
183205
// in development "style" loader enables hot editing of CSS.
184206
{
185-
test: /\.css$/,
207+
test: /legacy\.css$/,
186208
use: [
187209
require.resolve('style-loader'),
188210
{
@@ -193,24 +215,26 @@ module.exports = {
193215
},
194216
{
195217
loader: require.resolve('postcss-loader'),
218+
options: postCSSLoaderOptions,
219+
},
220+
],
221+
},
222+
{
223+
test: /\.css$/,
224+
use: [
225+
require.resolve('style-loader'),
226+
{
227+
loader: require.resolve('css-loader'),
196228
options: {
197-
// Necessary for external CSS imports to work
198-
// https://github.com/facebookincubator/create-react-app/issues/2677
199-
ident: 'postcss',
200-
plugins: () => [
201-
require('postcss-flexbugs-fixes'),
202-
autoprefixer({
203-
browsers: [
204-
'>1%',
205-
'last 4 versions',
206-
'Firefox ESR',
207-
'not ie < 9', // React doesn't support IE8 anyway
208-
],
209-
flexbox: 'no-2009',
210-
}),
211-
],
229+
importLoaders: 1,
230+
modules: true,
231+
localIdentName: '[path]__[name]___[local]',
212232
},
213233
},
234+
{
235+
loader: require.resolve('postcss-loader'),
236+
options: postCSSLoaderOptions,
237+
},
214238
],
215239
},
216240
// "file" loader makes sure those assets get served by WebpackDevServer.

packages/react-scripts/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "domain-react-scripts",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Configuration and scripts for Domain React components",
55
"repository": "DomainGroupOSS/create-react-app",
66
"license": "MIT",
@@ -47,7 +47,9 @@
4747
"jest": "20.0.4",
4848
"node-sass": "^4.7.2",
4949
"object-assign": "4.1.1",
50+
"postcss-cssnext": "^3.1.0",
5051
"postcss-flexbugs-fixes": "3.2.0",
52+
"postcss-import": "^11.1.0",
5153
"postcss-loader": "2.0.8",
5254
"promise": "8.0.1",
5355
"raf": "3.4.0",

packages/react-scripts/template/gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ yarn-debug.log*
2121
yarn-error.log*
2222

2323
# Domain legacy Sass
24-
src/legacy.css
24+
/src/legacy.css

0 commit comments

Comments
 (0)