question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load external CSS

See original GitHub issue

I would like to use external CSS with react-static to migrate from create-react-app completely. Actually it is very straightforward to load fonts via the typefaces packages and then adding them via simple module imports. The problem is that those imports require appropiate css-loaders which are not included inside of react-static.

import 'typeface-montserrat'; // typeface-montserrat/index.css

After a few hours of trying I feel closer to the solution and replaced nearly all webpack loaders with a custom toolchain to support those .css imports, but it still does not work properly. I think it would be a good idea to support css imports out of the box, because the setup is not easy and requires extended knowledge about the project itself & the underlying toolchain.

It seems that you have to add the css-loaders before the file-loader (like in create-react-app), so I had to swap all loaders with a custom configuration in a hacky way. The provided webpackConfigurator gives us no option to insert the loaders in a specific order.

I already tried just using the style-loader and css-loader.

Here is my actual static.config.js:

import NODE_MODULES from 'react-static/dist/paths';

export default {
	[...],
	webpack: (webpackConfigurator, { dev }) => {
		webpackConfigurator._config.module.rules = [];
		webpackConfigurator.merge({
			module: {
				rules: [
					{
						test: /\.(js|jsx)$/,
						exclude: NODE_MODULES,
						use: [
							{
								loader: 'babel-loader',
							},
						],
					},
					{
						test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
						loader: 'url-loader',
						options: {
							limit: 10000,
						},
					},
					{
						test: /\.css$/,
						use: [
							'style-loader',
							{ loader: 'css-loader', options: { importLoaders: 1 } },
							{
								loader: 'postcss-loader',
								options: {
									config: {
										path: './postcss.config.js',
									},
								},
							},
						],
					},
					{
						exclude: [/\.js$/, /\.html$/, /\.json$/],
						loader: require.resolve('file-loader'),
						options: {
							name: '[name].[hash:8].[ext]',
						},
					},
				],
			},
		});
	},
};

I also had to configure postcss properly and install some dependencies. Still it does not work yet:

./node_modules/typeface-montserrat/index.css
Module build failed: Error: Expected a backslash preceding the semicolon.

I will go for sleep now, maybe I will get it working tomorrow. Nevertheless, it would be great if react-static would support this out-of-the-box.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tannerlinsleycommented, Oct 20, 2017

This should be fixed in the latest version. Please upgrade both global and local packages to version 2.0.0

Make sure you import and use the withCssLoader from react-static’s plugins in your static.config.js file:

import axios from 'axios'
//
import withCss from 'react-static/lib/plugins/withCssLoader'
import withFiles from 'react-static/lib/plugins/withFileLoader'

export default {
  getSiteProps: () => ({
    title: 'React Static',
  }),
  getRoutes: async () => {
    const { data: posts } = await axios.get('https://jsonplaceholder.typicode.com/posts')
    return [
      {
        path: '/',
        component: 'src/containers/Home',
      },
      {
        path: '/about',
        component: 'src/containers/About',
      },
      {
        path: '/blog',
        component: 'src/containers/Blog',
        getProps: () => ({
          posts,
        }),
        children: posts.map(post => ({
          path: `/post/${post.id}`,
          component: 'src/containers/Post',
          getProps: () => ({
            post,
          }),
        })),
      },
      {
        is404: true,
        component: 'src/containers/404',
      },
    ]
  },
  webpack: [withCss, withFiles],
}

Then you can simply import css in your js

import './app.css'
0reactions
tannerlinsleycommented, Sep 11, 2018

Plugins are for v6 only (which is still in beta). You can see the v6 branch here: https://github.com/nozzle/react-static/tree/v6/

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add CSS - W3Schools
With an external style sheet, you can change the look of an entire website by changing just one file! Each HTML page must...
Read more >
External CSS Stylesheets – How to Link CSS to HTML and ...
It is considered a best practice to have your CSS stylesheets in an external file. So how can you link that CSS to...
Read more >
How to Link CSS to HTML Files in Web Development - Hostinger
While there are multiple approaches linking CSS to an HTML file, the most efficient way is to link an external style sheet to...
Read more >
How to Include CSS in HTML Pages - Tutorial Republic
Inline styles — Using the style attribute in the HTML start tag. ; Embedded styles — Using the <style> element in the head...
Read more >
Two ways to load only the CSS you need - LogRocket Blog
A style sheet can be included in a web page through various ways; you can embed a <link> tag that links to an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found