Skip to content

Commit 204009d

Browse files
authored
[MINOR] CSS-Modules + CSS-Next to be supported on the archetype by default (#5)
* CSS-Modules + CSS-Next to be supported on the archetype by default * extend archetype functionality so that it also has support for stylus, by default archetype supports css-modules + css-next * ternary condition to check for false * support legacy use-case of stylus * linter fix * clean up file exists check with glob
1 parent 1375577 commit 204009d

6 files changed

Lines changed: 52 additions & 33 deletions

File tree

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ gulp, webpack, demo, electrode component archetype, live-reload
4747
create src/styles/product-card.styl
4848
create src/index.js
4949
create demo/demo.jsx
50-
create demo/demo.styl
50+
create demo/demo.css
5151
create demo/examples/product-card.example
5252
create test/client/.eslintrc
5353
create test/client/components/product-card.spec.jsx
@@ -118,10 +118,12 @@ This archetype assumes an architecture as follows:
118118
```
119119
demo/
120120
demo.jsx
121-
demo.styl
121+
demo.css
122122
src
123123
components/
124124
*.jsx
125+
styles/
126+
*.css
125127
index.js
126128
test
127129
client/
@@ -137,15 +139,10 @@ package.json
137139

138140
## CSS Modules + CSS next
139141

140-
By default, this archetype assumes you are using Stylus and `.styl` files containing Global CSS. If you want to use `CSS-Modules + CSS-Next`, you need opt-in and use `.css` files. This archetype does not support using Stylus and `CSS-Modules + CSS-Next` together. You can opt-in to `CSS-Modules + CSS-Next` by adding a config section to your project's package.json and setting the necessary `env` variable to true:
142+
By default, this archetype assumes you are using CSS-Modules + CSS-Next, you need
143+
to opt-in to stylus support by adding a `*.styl` to your *project's* `demo/demo.styl` & `src/styles/*.styl`
141144

142-
```
143-
"config": {
144-
"electrode_archetype_react_component_webpack_css_modules": "true"
145-
}
146-
```
147-
148-
Once enabled, you can import css files in your components and reference class names via the exported object `src/components/<%= componentPath %>.js`:
145+
Import css files in your components and reference class names via the exported object `src/components/<%= componentPath %>.js`:
149146

150147
```javascript
151148
import React from "react";
@@ -175,7 +172,7 @@ Add following styling to `src/styles/<%= componentPath %>.css`
175172
}
176173

177174
.someStyle {
178-
composes: baseComponent;
175+
composes: baseStyle;
179176
font-size: 18px;
180177
}
181178
```

config/webpack/partial/css.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"use strict";
22

3+
const Path = require("path");
4+
const glob = require("glob");
5+
36
const archDevRequire = require("electrode-archetype-react-component-dev/require");
47
const mergeWebpackConfig = archDevRequire("webpack-partial").default;
58
const atImport = archDevRequire("postcss-import");
@@ -8,9 +11,32 @@ const styleLoader = archDevRequire.resolve("style-loader");
811
const cssLoader = archDevRequire.resolve("css-loader");
912
const postcssLoader = archDevRequire.resolve("postcss-loader");
1013

11-
const cssModuleSupport = process.env.npm_package_config_electrode_archetype_react_component_webpack_css_modules === "true" ? // eslint-disable-line max-len
12-
"?modules!" + postcssLoader :
13-
"";
14+
/**
15+
* [cssModuleSupport By default, this archetype assumes you are using CSS-Modules + CSS-Next]
16+
*
17+
* Stylus is also supported for which the following cases can occur.
18+
*
19+
* case 1: *only* demo.css exists => CSS-Modules + CSS-Next
20+
* case 2: *only* demo.styl exists => stylus
21+
* case 3: *both* demo.css & demo.styl exists => CSS-Modules + CSS-Next takes priority
22+
* with a warning message
23+
* case 4: *none* demo.css & demo.styl exists => CSS-Modules + CSS-Next takes priority
24+
*/
25+
26+
const cssNextExists = (glob.sync(Path.join(process.cwd() + "/demo/*.css")).length > 0);
27+
const stylusExists = (glob.sync(Path.join(process.cwd() + "/demo/*.styl")).length > 0);
28+
29+
// By default, this archetype assumes you are using CSS-Modules + CSS-Next
30+
let cssModuleSupport = "?modules!" + postcssLoader;
31+
32+
if (stylusExists && !cssNextExists) {
33+
cssModuleSupport = "";
34+
} else if (stylusExists && cssNextExists) {
35+
/* eslint-disable no-console */
36+
console.log(`**** you have demo.css & demo.styl please delete *.styl
37+
and upgrade to using *.css for CSS-Modules + CSS-Next support ****`);
38+
/* eslint-enable no-console */
39+
}
1440

1541
module.exports = () => (config) => mergeWebpackConfig(config, {
1642
module: {

config/webpack/webpack.config.demo.dev.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ module.exports = {
3636
// Allow root import of `src/FOO` from ROOT/src.
3737
src: path.join(process.cwd(), "src"),
3838
"local-component-demo": path.join(process.cwd() + "/demo/demo.jsx"),
39-
"local-demo-styl": path.join(process.cwd() + "/demo/demo.styl")
39+
// By default, this archetype assumes you are using CSS-Modules + CSS-Next
40+
"local-demo-css": path.join(process.cwd() + "/demo/demo")
4041
}
4142
}),
4243
resolveLoader: base.resolveLoader,

demo-server/client-routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ var createRoutes = require("./create-routes");
1010
var Demo = require("local-component-demo").default;
1111

1212
// Also aliased for same reason as above
13-
require("local-demo-styl");
13+
require("local-demo-css");
1414

1515
module.exports = createRoutes(Demo);

dev/README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ gulp, webpack, demo, electrode component archetype, live-reload
4646
create src/styles/product-card.styl
4747
create src/index.js
4848
create demo/demo.jsx
49-
create demo/demo.styl
49+
create demo/demo.css
5050
create demo/index.jsx
5151
create demo/examples/product-card.example
5252
create test/client/.eslintrc
@@ -118,10 +118,12 @@ This archetype assumes an architecture as follows:
118118
```
119119
demo/
120120
demo.jsx
121-
demo.styl
121+
demo.css
122122
src
123123
components/
124124
*.jsx
125+
styles/
126+
*.css
125127
index.js
126128
test
127129
client/
@@ -137,15 +139,8 @@ package.json
137139

138140
## CSS Modules
139141

140-
By default, this archetype assumes your Stylus files contain Global CSS. If you are using CSS Modules, you need
141-
to opt-in to CSS Modules support by adding a `config` section to your *project's* `package.json` and setting the
142-
necessary env variable to `true`:
143-
144-
```
145-
"config": {
146-
"electrode_archetype_react_component_webpack_css_modules": "true"
147-
}
148-
```
142+
By default, this archetype assumes you are using CSS-Modules + CSS-Next, you need
143+
to opt-in to stylus support by adding a `*.styl` to your *project's* `demo/demo.styl` & `src/styles/*.styl`
149144

150145
## Gotchas
151146

dev/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
},
1717
"scripts": {},
1818
"dependencies": {
19-
"electrode-check-dependencies": "^1.0.2",
20-
"electrode-demo-index": "^1.0.0",
21-
"electrode-docgen": "^1.0.0",
22-
"electrode-gulp-helper": "^1.0.0",
2319
"babel-cli": "^6.6.5",
2420
"babel-core": "^6.7.2",
2521
"babel-eslint": "^5.0.0",
@@ -40,14 +36,18 @@
4036
"component-playground": "^1.0.1",
4137
"config": "^1.19.0",
4238
"css-loader": "^0.23.1",
39+
"electrode-check-dependencies": "^1.0.2",
40+
"electrode-demo-index": "^1.0.0",
41+
"electrode-docgen": "^1.0.0",
42+
"electrode-gulp-helper": "^1.0.0",
4343
"enzyme": "^2.3.0",
4444
"eslint": "^1.7.0",
4545
"eslint-config-defaults": "^9.0.0",
4646
"eslint-plugin-filenames": "^0.1.2",
4747
"eslint-plugin-react": "^3.6.3",
4848
"file-loader": "^0.8.4",
4949
"fs-extra": "^0.26.4",
50-
"glob": "^7.0.0",
50+
"glob": "^7.0.6",
5151
"history": "^1.13.1",
5252
"isparta-loader": "^2.0.0",
5353
"istanbul": "^0.3.18",
@@ -71,8 +71,8 @@
7171
"mocha": "^2.4.5",
7272
"nodemon": "^1.8.1",
7373
"phantomjs": "^1.9.18",
74-
"postcss-import": "^8.1.2",
7574
"postcss-cssnext": "^2.7.0",
75+
"postcss-import": "^8.1.2",
7676
"postcss-loader": "^0.8.2",
7777
"raw-loader": "^0.5.1",
7878
"react": "^15.2.0",

0 commit comments

Comments
 (0)