Code Monkey home page Code Monkey logo

next.js-style-loader's Introduction

next-style-loader

NPM version Downloads Build Status Dependency status Dev Dependency status

This module is deprecated and is no longer maintained. We have moved to our own boilerplate.

Makes loading of CSS files possible in next.js projects through babel & webpack.

This module is similar to what webpack style-loader does but it's compatible with next.js.

Installation

$ npm install --save-dev next-style-loader

Setup

First you will need to create a next.config.js file:

module.exports = {
    webpack: (config, { dev }) => {
        config.module.rules.push(
            {
                test: /\.css$/,
                loader: 'emit-file-loader',
                options: {
                    name: 'dist/[path][name].[ext]',
                },
            },
            {
                test: /\.css$/,
                // Simplest example (non-minified)..
                loader: `babel-loader!next-style-loader`,
                // Example with `css-loader` to minify CSS
                // NOTE: The `url` option from the css loader must be disabled; images, fonts, etc should go into /static
                loader: `babel-loader!next-style-loader!css-loader?sourceMap&minimize=${!dev}&url=false`,
                // Same as above but with CSS modules
                loader: `babel-loader!next-style-loader!css-loader?sourceMap&minimize=${!dev}&url=false&modules`,
                // Example with `css-loader` and `postcss-loader' (you may also activate CSS modules just like above)
                // Enable `postcss-imports` plugin must be enabled in the `postcss.config.js` file to process @import declarations
                loader: `babel-loader!next-style-loader!css-loader?sourceMap&minimize=${!dev}&url=false!postcss-loader`,
                // Example with `css-loader` and `sass-loader'
                loader: `babel-loader!next-style-loader!css-loader?sourceMap&minimize=${!dev}&url=false!sass-loader`,
            }
        );

        return config;
    },
};

Finally, create a pages/_document.js file:

class MyDocument extends Document {
    render() {
        const { nextStyle } = this.props;

        return (
            <html>
                <Head>
                    { nextStyle.tag }
                </Head>
                <body>
                    <Main />
                    <NextScript />
                </body>
            </html>
        );
    }
}

MyDocument.getInitialProps = function (ctx) {
    const props = Document.getInitialProps(ctx);

    props.nextStyle = flush();

    return props;
};

export default MyDocument;

Usage

After setting the project, you may import CSS files like so:

import styles from './MyComponent.css';

// If you are using `css-loader` with CSS modules,
// `styles` would be an object containing the generated classnames

const MyComponent extends Component {
    render() {
        return <div>Hello</div>;
    }
}

export default applyStyles(styles)(MyComponent);

Enjoy!

License

MIT License

next.js-style-loader's People

Contributors

satazor avatar iaincollins avatar kosai106 avatar marcooliveira avatar

Watchers

James Cloos avatar Amine S. avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.