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 custom alias for scss import in 'with-global-stylesheet' example #1325

Closed
aulneau opened this issue Mar 1, 2017 · 5 comments
Closed

Comments

@aulneau
Copy link

aulneau commented Mar 1, 2017

Hello!

First off, I love what ya'll are making :)

I am trying to add an alias so that I can import some common scss files into places that need it without having to reference the files relatively.

~scss/filename vs ../../../../common/scss/filename

I have used this in other webpack projects:

    resolve: {
        alias: {
            'scss': path.resolve(__dirname, "common/scss")
        }
    }

But I am not sure how to implement it in my next.config.js
And this is my next.config.js

module.exports = {
    webpack: (config, {dev}) => {
        config.module.rules.push(
            {
                test: /\.(css|scss)/,
                loader: 'emit-file-loader',
                options: {
                    name: 'dist/[path][name].[ext]'
                }
            }
            ,
            {
                test: /\.css$/,
                loader: 'babel-loader!raw-loader'
            }
            ,
            {
                test: /\.scss$/,
                loader: 'babel-loader!raw-loader!sass-loader',
            }
        )
        return config
    }
}
@davibe
Copy link
Contributor

davibe commented Mar 1, 2017

There is some discussion going on about this in here davibe/next.js-example-with-global-stylesheet#3
This is the repository where i developed and i continue developing the `with-global-stylesheet example.

@timneutkens
Copy link
Member

@davibe lets keep the discussion there then. When it's done I'd love a PR back 👍

@orlin
Copy link
Contributor

orlin commented Mar 1, 2017

@aulneau I plan to work on a PR / upgrade of with-global-stylesheet addressing this issue, probably tomorrow. In the meantime, if you'd like to @import with Sass then change this code from includePaths: ['node_modules', 'node_modules/@material/*'] to ['common/scss', 'node_modules'], which seems to be the path in your case, relative to the project root dir. If you'd like to access the .scss from .js pages or components without relative paths - take a look at babel-plugin-module-resolver which would enable you to import / require Sass stylesheets from JavaScript. I plan to add that to the example as well.

@aulneau
Copy link
Author

aulneau commented Mar 1, 2017

@orlin @timneutkens @davibe great, thank you all.

For anyone who happens upon this issue, I have it working as such:

next.config.js

const path = require('path')
const glob = require('glob')
module.exports = {
    webpack: (config, {dev}) => {
        config.module.rules.push(
            {
                test: /\.(css|scss)/,
                loader: 'emit-file-loader',
                options: {
                    name: 'dist/[path][name].[ext]'
                }
            }
            ,
            {
                test: /\.css$/,
                loader: 'babel-loader!raw-loader'
            }
            ,
            {
                test: /\.scss$/,
                loader: 'babel-loader!raw-loader!sass-loader'
            },
            {
                test: /\.scss$/,
                loader: 'sass-loader',
                options: {
                    includePaths: ['common/scss']
                        .map((d) => path.join(__dirname, d))
                        .map((g) => glob.sync(g))
                        .reduce((a, c) => a.concat(c), [])
                }
            }
        )
        return config
    }
}

and I can now import in my scss files as such: @import "partials/abstract" which points to common/scss/partials/abstract.scss

@orlin
Copy link
Contributor

orlin commented Mar 1, 2017

@aulneau you’re welcome. Btw, the following part of your next.config.js

            {
                test: /\.scss$/,
                loader: 'babel-loader!raw-loader!sass-loader'
            },
            {
                test: /\.scss$/,
                loader: 'sass-loader',
                options: {
                    includePaths: ['common/scss']
                        .map((d) => path.join(__dirname, d))
                        .map((g) => glob.sync(g))
                        .reduce((a, c) => a.concat(c), [])
                }
            }

… can be reduced to:

      { test: /\.scss$/,
        use: [
          'babel-loader', 'raw-loader',
          { loader: 'sass-loader',
            options: {
              includePaths: ['common/scss']
                .map((d) => path.join(__dirname, d))
            }
          }
        ]
      },

You aren’t using glob* (in this case), nor should be matching the scss twice - I was surprised that the latter even worked (guessing it would break importing scss from js).

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants