Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add css generating script and package.json hook (2/7) #162

Merged
merged 1 commit into from
Jul 2, 2018

Conversation

philipfweiss
Copy link
Contributor

@philipfweiss philipfweiss commented Jun 29, 2018

Adds a script which will generate a CSS stylesheet from an example component. Based off a similar implementation in https://github.com/airbnb/react-dates.

Sample result of script (inside css/styles.css). Another PR with improvements to/refactoring of the CSS/theming coming soon.

screen shot 2018-06-29 at 10 18 04 am

Copy link
Contributor

@noratarano noratarano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nits! But this is looking great.

function App() {
return (
<LabeledSlider />
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just simplify this return <LabeledSlider />;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, ReactDOM.render(<LabeledSlider /> - no need for App at all.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think simplifying to Jordan's comment seems reasonable.


const path = './scripts/renderAllComponents.jsx';

const CSS = compileCSS(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid this dereference:

const CSS = compileCSS('./scripts/renderAllComponents.jsx');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also define the format function before this.

const format = new CleanCSS({...});
const CSS = compileCSS('./scripts/renderAllComponents.jsx');
const { styles: formattedCSS } = format.minify(CSS);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not passionate about this order.

const args = process.argv.slice(2);
const optimizeForProduction = args.includes('-o') || args.includes('--optimize');

registerMaxSpecificity(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important to run this before registerCSSInterfaceWithDefaultTheme?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the order matters, as long as it's before the compile script is run. Looks like it just sets a global flag.

https://github.com/airbnb/react-with-styles-interface-css/blob/738405f658e90b254983df3c088c172d2c1786cf/packages/interface/src/utils/registerMaxSpecificity.js

const compileCSS = require('react-with-styles-interface-css-compiler');

const registerMaxSpecificity = require('react-with-styles-interface-css/dist/utils/registerMaxSpecificity').default;
const registerCSSInterfaceWithDefaultTheme = require('../src/utils/registerCSSInterfaceWithDefaultTheme').default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would bunch up all of these imports without whitespace between them.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm - the .default is unfortunate. can this pull from lib instead of src - so it must run after the build process - and then it can require from the babel output, which would have add-module-exports applied?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is add-module-exports part of the airbnb babel config? We tried to pull from lib and it still needs the .default (also notably, registerMaxSpecificity needs the .default even though it is a built file.

@noratarano
Copy link
Contributor

@philipfweiss Can you post a sample result of this script?

ljharb
ljharb previously requested changes Jun 29, 2018
package.json Outdated
@@ -5,7 +5,10 @@
"main": "lib/Slider",
"jsnext:main": "src/Slider.jsx",
"scripts": {
"build": "npm run clean && babel src -d lib",
"prebuild": "npm run clean",
"build": "npm run clean && babel src -d lib && npm run build:css -- --optimize ",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the clean script can be removed here, since it's in prebuild.

also, let's add a "postbuild" that does npm run build:css, instead of doing it inside the build step?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the css building part of the build step (just like, semantically).

Maybe let's move the babel src -d lib to a npm run build:js and then change npm run build to be npm run build:js && npm run build:css -- --optimize

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sure that's fine too

const compileCSS = require('react-with-styles-interface-css-compiler');

const registerMaxSpecificity = require('react-with-styles-interface-css/dist/utils/registerMaxSpecificity').default;
const registerCSSInterfaceWithDefaultTheme = require('../src/utils/registerCSSInterfaceWithDefaultTheme').default;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm - the .default is unfortunate. can this pull from lib instead of src - so it must run after the build process - and then it can require from the babel output, which would have add-module-exports applied?

function App() {
return (
<LabeledSlider />
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, ReactDOM.render(<LabeledSlider /> - no need for App at all.

{
"rules": {
"import/no-extraneous-dependencies": [2, {
"devDependencies": true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these utils maybe live inside scripts/, since they're not for public consumption?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well actually they might be? as part of the eventual initialize script.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if that's the case then they definitely shouldn't be allowed to have dev deps :-)

package.json Outdated
@@ -5,7 +5,10 @@
"main": "lib/Slider",
"jsnext:main": "src/Slider.jsx",
"scripts": {
"build": "npm run clean && babel src -d lib",
"prebuild": "npm run clean",
"build": "npm run clean && babel src -d lib && npm run build:css -- --optimize ",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the css building part of the build step (just like, semantically).

Maybe let's move the babel src -d lib to a npm run build:js and then change npm run build to be npm run build:js && npm run build:css -- --optimize

package.json Outdated
@@ -54,6 +58,8 @@
"react-addons-pure-render-mixin": "^15.6.2",
"react-dom": "^15.6.2",
"react-with-styles-interface-aphrodite": "^5.0.0",
"react-with-styles-interface-css": "^4.0.2",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is going to be used by the consumer as part of the initialize script, it will need to be part of the regular dependencies

const compileCSS = require('react-with-styles-interface-css-compiler');

const registerMaxSpecificity = require('react-with-styles-interface-css/dist/utils/registerMaxSpecificity').default;
const registerCSSInterfaceWithDefaultTheme = require('../src/utils/registerCSSInterfaceWithDefaultTheme').default;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is add-module-exports part of the airbnb babel config? We tried to pull from lib and it still needs the .default (also notably, registerMaxSpecificity needs the .default even though it is a built file.

function App() {
return (
<LabeledSlider />
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think simplifying to Jordan's comment seems reasonable.

{
"rules": {
"import/no-extraneous-dependencies": [2, {
"devDependencies": true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well actually they might be? as part of the eventual initialize script.

import ThemedStyleSheet from 'react-with-styles/lib/ThemedStyleSheet';
import DefaultTheme from '../themes/DefaultTheme';

export default function registerInterfaceWithDefaultTheme(reactWithStylesInterface) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be good to change the test setup and storybook config to use this instead maybe

@philipfweiss philipfweiss force-pushed the pw--rheostat-add-css-building-scripts branch 2 times, most recently from e10ddba to 3fd8ae1 Compare June 29, 2018 17:55
@philipfweiss philipfweiss force-pushed the pw--rheostat-add-css-building-scripts branch from 3fd8ae1 to 5ecf6e1 Compare June 29, 2018 18:30
@philipfweiss philipfweiss changed the title Add css generating script and package.json hook Add css generating script and package.json hook (2/7) Jun 29, 2018
@philipfweiss philipfweiss changed the base branch from master to pw--move-build-job June 29, 2018 23:30
@philipfweiss philipfweiss changed the base branch from pw--move-build-job to master June 29, 2018 23:31
@philipfweiss philipfweiss force-pushed the pw--rheostat-add-css-building-scripts branch 5 times, most recently from ca50ace to 7f4a8eb Compare July 1, 2018 21:56
Copy link
Collaborator

@majapw majapw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me! I have a few small comments and then I think it's ready to merge.

package.json Outdated
"build": "npm run clean && babel src -d lib",
"postbuild": "npm run build:css",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still prefer to have:

"build:js": "babel src -d lib",

as a separate script and then have "build" be "npm run build:js && npm run build:css". The CSS step is not a postbuild, it's part of the build itself.

package.json Outdated
@@ -5,7 +5,11 @@
"main": "lib/Slider",
"jsnext:main": "src/Slider.jsx",
"scripts": {
"prebuild": "npm run clean",
"build": "npm run clean && babel src -d lib",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this npm run clean since it's in the prebuild now.

package.json Outdated
@@ -63,9 +68,11 @@
},
"dependencies": {
"airbnb-prop-types": "^2.10.0",
"clean-css": "^4.1.11",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean-css should be a dev dep

const registerCSSInterfaceWithDefaultTheme = require('./utils/registerCSSInterfaceWithDefaultTheme').default;

const args = process.argv.slice(2);
const optimizeForProduction = args.includes('-o') || args.includes('--optimize');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not critical, but it might be cleaner to update this to use yargs or something.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we’re going to do that, maybe this should be a command line util in the css interface repo?

fs.mkdirSync(dir);
}

const outputFilePath = optimizeForProduction ? `${dir}/_styles.css` : `${dir}/styles.css`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the underscore is probably not necessary (it was there for some backwards compatibility w/ react-dates). It might be nice to name this file rheostat.css so that it is easy to understand in the context of a bigger app.

import '../css/slider-horizontal.css';
import '../css/slider-vertical.css';
/* Register react with styles interface */
ThemedStyleSheet.registerTheme(DefaultTheme);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use utils/registerInterfaceWithDefaultTheme.js here with the aphroditeInterface. :)

@philipfweiss philipfweiss force-pushed the pw--rheostat-add-css-building-scripts branch from 7f4a8eb to e5cb7f8 Compare July 2, 2018 18:49
Copy link
Collaborator

@majapw majapw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except for the one comment

"build": "npm run build:js && npm run build:css",
"build:js": "babel src -d lib",
"prebuild:css": "rimraf lib/css && mkdir -p lib/css",
"build:css": "node scripts/buildCSS.js",
"clean": "rimraf lib",
"prepublish": "in-publish && safe-publish-latest && npm run build || not-in-publish",
"lint": "eslint --ext .js,.jsx src test stories",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add a script for running the css storybook? e.g.

"storybook:css": "npm run build:css && start-storybook -p 9001 -c .storybook-css",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have this in a later commit! #166

@philipfweiss philipfweiss merged commit b42a856 into master Jul 2, 2018
@ljharb ljharb deleted the pw--rheostat-add-css-building-scripts branch July 2, 2018 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants