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

Simplify importing index.scss in an npm package #535

Closed
sindresorhus opened this issue Feb 6, 2018 · 13 comments · Fixed by #649
Closed

Simplify importing index.scss in an npm package #535

sindresorhus opened this issue Feb 6, 2018 · 13 comments · Fixed by #649

Comments

@sindresorhus
Copy link

Let's say an npm package named foo contains:

.
├── index.scss
├── package.json

Currently, I can use the following to import it:

@import "~foo/index";

But it would be nicer if we could just import it as:

@import "~foo";

Like with JS npm packages.

@alexander-akait
Copy link
Member

@sindresorhus thanks for issue, maybe we can use sass field from package.json, maybe we already support this 😄 Can you check this (https://github.com/webpack-contrib/sass-loader/blob/master/lib/loader.js#L40), resolve from webpack api. If it is not support we can fix in webpack

@sindresorhus
Copy link
Author

sindresorhus commented Feb 11, 2018

I'm not sure what sass field you're referring to, but that's not really what I'm requesting here. I'm requesting the above to just work without any additional config in either package.json or webpack.

@michael-ciniawsky
Copy link
Member

config.resolve.mainFields = [ 'style' ]. e.g normalize.css supports the pkg.style property, which is the most widely used approached I'm aware of atm. style could eventually be supported by webpack as a default mainField, but not sure.... or sass-loader programmatically passes options to the resolver, the question is which...

@alexander-akait
Copy link
Member

@michael-ciniawsky need add tests for this 👍

@TheLarkInn
Copy link

Only problem is not everyone will expect this behavior no? It would be a webpack/webpack@WebpackOptionsDefaulter change.

@xzyfer
Copy link

xzyfer commented Jun 5, 2018

Note node-sass@5 will automatically support @import "foo"; resolving to @import "foo/index.scss"; if foo is a folder.

@pitpit
Copy link

pitpit commented Oct 12, 2018

Thanks to mainFields in webpack.config.js, you can resolve file with "sass" or "style" fields from package.json of the npm package.

    module: {
      rules: [
        {
          test: /\.scss|\.css$/,
          use: [
              "style-loader",
              'css-loader',
              "sass-loader"
          ],
          resolve: {
            mainFields: ['sass', 'style']
          }
        }
      ]
    },

Then you can import stylesheet with @import "~foo";

@alexander-akait
Copy link
Member

@pitpit Maybe will be good create issue about this in webpack, loader can't register own resolve options, this feature will fix also #556

@pitpit
Copy link

pitpit commented Oct 12, 2018

@evilebottnawi actually a loader can register its own resolve option. It does work !
(tested with webpack@4.20.0)

@alexander-akait
Copy link
Member

@pitpit how? Without hacks?

@pitpit
Copy link

pitpit commented Oct 12, 2018

no hacks. Look at this repo https://github.com/pitpit/saas-field

@alexander-akait
Copy link
Member

@pitpit yep, here issue about what it should be by default for sass-loader to avoid extra configuration

@alexander-akait
Copy link
Member

Need support sass field in package.json too

jackkoppa added a commit to politico/vue-accessible-selects that referenced this issue Sep 24, 2020
jackkoppa added a commit to politico/vue-accessible-selects that referenced this issue Sep 24, 2020
Given that there's no *automatically configured* way to specify a target SCSS file as the index, using package.json, we just make the import path as obvious & forward-compatible as possible
See what's required in Webpack to be able to do  without the : https://github.com/pitpit/saas-field/blob/master/webpack.config.js\#L19-L22 & webpack-contrib/sass-loader#535
jackkoppa added a commit to politico/vue-accessible-selects that referenced this issue Sep 24, 2020
jackkoppa added a commit to politico/vue-accessible-selects that referenced this issue Sep 24, 2020
Given that there's no *automatically configured* way to specify a target SCSS file as the index, using package.json, we just make the import path as obvious & forward-compatible as possible
See what's required in Webpack to be able to do  without the : https://github.com/pitpit/saas-field/blob/master/webpack.config.js\#L19-L22 & webpack-contrib/sass-loader#535
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants