kit icon indicating copy to clipboard operation
kit copied to clipboard

Importing External Style Library (Semantic-ui-react)

Open chainlink opened this issue 9 years ago • 9 comments

First off thank you for an excellent boilerplate kit. I only wish all projects came with this level of polish and documentation!

I'm having a devil of a time getting this is play nicely with the Semantic UI Library and I think this issue externals to using external styles in general

Steps to Reproduce:

  1. npm install semantic-ui-react --save
  2. npm install semantic-ui-css --save
  3. Add import 'semantic-ui-css/semantic.min.css'; to src/app.js
  4. Remove server-dev from start in package.json (for debugging only)

Fails with error:

(undefined) ./~/css-loader?{"minimize":false,"localIdentName":"[local]-[hash:base64]","importLoaders":1,"sourceMap":true,"modules":true}!./~/postcss-loader/lib?{"sourceMap":true}!./~/semantic-ui-css/semantic.css
Module not found: Error: Can't resolve 'themes/default/assets/images/flags.png' in '/Users/kyle/Documents/reactql-app/node_modules/semantic-ui-css'
 @ ./~/css-loader?{"minimize":false,"localIdentName":"[local]-[hash:base64]","importLoaders":1,"sourceMap":true,"modules":true}!./~/postcss-loader/lib?{"sourceMap":true}!./~/semantic-ui-css/semantic.css 6:204774-204823
 @ ./~/semantic-ui-css/semantic.css
 @ ./src/app.js
 @ ./kit/entry/browser.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server react-hot-loader/patch webpack-dev-server/client?http://localhost:8080 webpack/hot/only-dev-server ./kit/entry/browser.js

Note that themes/default/assets/images/flags.png does indeed exist in /Users/kyle/Documents/reactql-app/node_modules/semantic-ui-css

I've looked into css-loader an postcss-loader to no avail. Any tips on loading this library? I'd be happy to write up a section for your documentation.

chainlink avatar Jul 06 '17 13:07 chainlink

Thanks for the nice words, @chainlink. I've actually got Semantic UI running locally-- I'll drop some code here in a little while that shows you how to solve this.

leebenson avatar Jul 06 '17 13:07 leebenson

Ok, so to fix this, you just need to place your import statements inside a *.global.css file, like so:

styles.global.css

@import '~semantic-ui-css/semantic.css';

And then import that file as normal:

app.js

import './styles.global.scss';

That will take care of the CSS. By using *.global.css, you're telling the css-loader in Webpack to avoid hashing class names. It will still go through the PostCSS optimisation pipeline, so the minified version should be comparable with the 'pre-minified' version that Semantic UI offers out-the-box.

Then, you're free to import Semantic UI React components as usual:

someComponent.js

import { Button } from 'semantic-ui-react';

export default () => (
  <Button>click me!</Button>
);

If you run into any other issues, feel free to re-open the issue.

leebenson avatar Jul 06 '17 15:07 leebenson

Cool thanks!

chainlink avatar Jul 06 '17 16:07 chainlink

@chainlink if it helps (and for people coming here in the future /cc @leebenson ) - we're working with semantic-ui-react and moved away from using the semantic-ui-css import to using semantic-ui-less which allows you to define and build themes

eudaimos avatar Aug 10 '17 18:08 eudaimos

@leebenson , @eudaimos

I followed the similar steps for the semantic-ui-less but I am getting an error.

Added file styles.global.less with the import statement @import '~semantic-ui-less/semantic.less';

And then import that file in app.js import './styles.global.less';

I got the following error

Failed to compile.

(undefined) ./node_modules/css-loader??ref--8-1!./node_modules/postcss-loader/lib??ref--8-2!./node_modules/less-loader/dist/cjs.js!./node_modules/semantic-ui-less/semantic.less
Module build failed: 


;(function ($, window, document, undefined) {
                                          ^
Missing closing ')'
      in /Users/sujith/projects/personal/react/recruit-webapp/node_modules/semantic-ui-less/definitions/modules/transition.js (line 11, column 44)
 @ ./node_modules/semantic-ui-less/semantic.less 4:14-143 18:2-22:4 19:20-149
 @ ./src/app.js
 @ ./kit/entry/browser.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server react-hot-loader/patch webpack-dev-server/client?http://localhost:8080/ webpack/hot/only-dev-server ./kit/entry/browser.js

How can i get the semantic ui theming work with reactql kit and semantic-ui-less

sujithvellat avatar Feb 27 '18 18:02 sujithvellat

@sujithvellat Did you ever solve that issue? I'm trying to work through the same thing right now

Brantron avatar Mar 15 '18 20:03 Brantron

this worked for me when importing semantic ui less (not entirely my solution, credits down bellow):

  1. Add file styles.global.less with the import statement @import '~semantic-ui-less/semantic.less';

  2. Import that file in app.js import './styles.global.less';

  3. Create a theme folder in your src and copy node_modules/semantic-ui-less/theme.config.example to it as theme.config

mkdir src/semantic_theme
cp node_modules/semantic-ui-less/theme.config.example src/semantic_theme/theme.config
  1. Create a semantic site template inside your theme folder, you can look at node_modules/semantic-ui-less/_site for reference
mkdir src/semantic_theme/site
mkdir src/semantic_theme/site/
mkdir src/semantic_theme/site/collections  
mkdir src/semantic_theme/site/elements  
mkdir src/semantic_theme/site/globals  
mkdir src/semantic_theme/site/modules  
mkdir src/semantic_theme/site/views
  1. Add a path to your theme.config in your config/paths.js semanticTheme: path.join(root, 'src', 'semantic_theme', 'theme.config'),

  2. Create an alias that resolves semantics '../../theme.config' to your newly created config in your kit/webpack/base.js

.....
  resolve: {
       .....
    modules: [
        ....
    ],

    alias: {
      '../../theme.config$': PATHS.semanticTheme,
    }
  },
    .....

  1. Update your theme.config with the correct siteFolder, theme.less and font paths

/*******************************
            Folders
*******************************/

   .....

/* Path to site override folder */
@siteFolder  : '../../src/semantic_theme/site';


/*******************************
         Import Theme
*******************************/

@import "~semantic-ui-less/theme.less";
@fontPath : "../../../themes/@{theme}/assets/fonts";

/* End Config */

That's it! Now you can use your semantic theme folder to create a custom theme as detailed in the semantic ui theming section.

if you're getting "variable @googleProtocol is undefined" error then just downgrade your less to version 2.7.3

yarn remove less
yarn add [email protected]

credit and thanks: @leebenson @eudaimos @sujithvellat @dongcai @3nvi @neekey references:

https://medium.com/webmonkeys/webpack-2-semantic-ui-theming-a216ddf60daf
https://github.com/neekey
https://github.com/Semantic-Org/Semantic-UI-LESS/issues/30
https://webpack.js.org/configuration/resolve/
https://semantic-ui.com/usage/theming.html
https://github.com/Semantic-Org/Semantic-UI-LESS/issues/30
https://github.com/reactql/kit/issues/45

Vino4 avatar Jun 07 '18 04:06 Vino4

At step 3: Add import 'semantic-ui-css/semantic.min.css'; to src/app.js

Add it to src/index.js instead.

Nathan961207 avatar Sep 01 '18 18:09 Nathan961207

Ok, so to fix this, you just need to place your import statements inside a *.global.css file, like so:

styles.global.css

@import '~semantic-ui-css/semantic.css';

And then import that file as normal:

app.js

import './styles.global.scss';

That will take care of the CSS. By using *.global.css, you're telling the css-loader in Webpack to avoid hashing class names. It will still go through the PostCSS optimisation pipeline, so the minified version should be comparable with the 'pre-minified' version that Semantic UI offers out-the-box.

Then, you're free to import Semantic UI React components as usual:

someComponent.js

import { Button } from 'semantic-ui-react';

export default () => (
  <Button>click me!</Button>
);

If you run into any other issues, feel free to re-open the issue.

I am facing a similar issue. I am trying to add semantic-ui-react in my project but getting an error. Can you please briefly specify how do we add *.global.css to our webpack.config file ? and where do we put styles.global.css ?

Puja988 avatar Mar 08 '19 06:03 Puja988