Skip to content

Commit

Permalink
[ts] (#5) Allow importing external css files
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenwenc committed Feb 3, 2018
1 parent a827c16 commit 897e658
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 52 deletions.
3 changes: 3 additions & 0 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
appIndexJs: resolveApp('src/index.tsx'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
appStyles: resolveApp('src/styles'),
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveApp('src/setupTests.ts'),
appNodeModules: resolveApp('node_modules'),
Expand All @@ -77,6 +78,7 @@ module.exports = {
appIndexJs: resolveApp('src/index.tsx'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
appStyles: resolveApp('src/styles'),
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveApp('src/setupTests.ts'),
appNodeModules: resolveApp('node_modules'),
Expand Down Expand Up @@ -109,6 +111,7 @@ if (
appIndexJs: resolveOwn('template/src/index.tsx'),
appPackageJson: resolveOwn('package.json'),
appSrc: resolveOwn('template/src'),
appStyles: resolveOwn('template/src/styles'),
yarnLockFile: resolveOwn('template/yarn.lock'),
testsSetup: resolveOwn('template/src/setupTests.ts'),
appNodeModules: resolveOwn('node_modules'),
Expand Down
76 changes: 49 additions & 27 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
const getClientEnvironment = require('./env');
const paths = require('./paths');

// Options for PostCSS as we reference these options twice
// Adds vendor prefixing based on your specified browser support in
// package.json
const postCSSLoaderOptions = {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-import')({
path: [path.resolve(paths.appStyles)],
}),
require('postcss-cssnext')({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
features: {
autoprefixer: {
flexbox: 'no-2009',
},
},
}),
],
};

// Webpack uses `publicPath` to determine where the app is being served from.
// In development, we always serve from the root. This makes config easier.
const publicPath = '/';
Expand Down Expand Up @@ -171,6 +199,25 @@ module.exports = {
// in development "style" loader enables hot editing of CSS.
{
test: /\.css$/,
include: paths.appNodeModules,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: postCSSLoaderOptions,
},
],
},
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
{
test: /\.css$/,
include: paths.appSrc,
use: [
{
// Necessary for css-modules-loader
Expand All @@ -179,40 +226,15 @@ module.exports = {
{
loader: require.resolve('css-loader'),
options: {
sourceMap: true,
minimize: false,
camelCase: true,
modules: true,
camelCase: true,
importLoaders: 1,
localIdentName: '[name]__[local]--[hash:base64:5]',
},
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-import')({
path: [path.resolve(paths.appSrc, './styles')],
}),
require('postcss-cssnext')({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
features: {
autoprefixer: {
flexbox: 'no-2009',
},
},
}),
],
},
options: postCSSLoaderOptions,
},
],
},
Expand Down
87 changes: 63 additions & 24 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ const extractTextPluginOptions = shouldUseRelativeAssetPaths
{ publicPath: Array(cssFilename.split('/').length).join('../') }
: {};

// Options for PostCSS as we reference these options twice
// Adds vendor prefixing based on your specified browser support in
// package.json
const postCSSLoaderOptions = {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-import')({
path: [path.resolve(paths.appStyles)],
}),
require('postcss-cssnext')({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
features: {
autoprefixer: {
flexbox: 'no-2009',
},
},
}),
],
};

// This is the production configuration.
// It compiles slowly and is focused on producing a fast and minimal bundle.
// The development configuration is different and lives in a separate file.
Expand Down Expand Up @@ -179,6 +207,40 @@ module.exports = {
// in the main CSS file.
{
test: /\.css$/,
include: paths.appNodeModules,
loader: ExtractTextPlugin.extract(
Object.assign(
{
fallback: {
loader: require.resolve('style-loader'),
options: {
hmr: false,
},
},
use: [
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
minimize: true,
sourceMap: shouldUseSourceMap,
},
},
{
loader: require.resolve('postcss-loader'),
options: postCSSLoaderOptions,
},
],
},
extractTextPluginOptions
)
),
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
},
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
{
test: /\.css$/,
include: paths.appSrc,
loader: ExtractTextPlugin.extract(
Object.assign(
{
Expand All @@ -203,30 +265,7 @@ module.exports = {
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-import')({
path: [path.resolve(paths.appSrc, './styles')],
}),
require('postcss-cssnext')({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
features: {
autoprefixer: {
flexbox: 'no-2009',
},
},
}),
],
},
options: postCSSLoaderOptions,
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.4.0",
"version": "1.4.1",
"name": "zc-react-scripts",
"description": "Configuration and scripts for Create React App.",
"license": "MIT",
Expand Down

0 comments on commit 897e658

Please sign in to comment.