tailwindcss icon indicating copy to clipboard operation
tailwindcss copied to clipboard

resolve-url-loader cannot operate: CSS error when using @apply

Open thestepafter opened this issue 9 years ago • 25 comments

I put the following in my .scss file:

.btn {
  @apply .font-bold .py-2 .px-4 .rounded;
}
.btn-blue {
  @apply .bg-blue .text-white;
}
.btn-blue:hover {
  @apply .bg-blue-dark;
}

and after running webpack I can use the classes but I get the following error at the end of the build process:

WARNING in ./node_modules/css-loader?{"url":true,"sourceMap":false,"importLoaders":1}!./node_modules/postcss-loader/lib?{"sourceMap":true,"ident":"postcss","plugins":[{"version":"6.0.13","plugins":[null,null,null,null,null,null,null,null,null],"postcssPlugin":"tailwind","postcssVersion":"6.0.13"},null,null]}!./node_modules/resolve-url-loader?{"sourceMap":true,"root":"/home/vagrant/gitlab/resources/statamic-site-template/public/site/themes/foundation/node_modules"}!./node_modules/sass-loader/lib/loader.js?{"precision":8,"outputStyle":"expanded","includePaths":["./node_modules/foundation-sites/scss/","./node_modules/font-awesome/scss/","./node_modules/motion-ui/","./node_modules/video.js/src/css/"],"sourceMap":true}!./sass/styles.scss (Emitted value instead of an instance of Error) resolve-url-loader cannot operate: CSS error /home/vagrant/gitlab/resources/statamic-site-template/public/site/themes/foundation/sass/styles.scss:6698:3: missing '}' at error (/home/vagrant/gitlab/resources/statamic-site-template/public/site/themes/foundation/node_modules/css/lib/parse/index.js:62:15) @ ./sass/styles.scss 4:14-245 @ multi ./node_modules/jquery/dist/jquery.min.js ./node_modules/what-input/dist/what-input.min.js ./node_modules/foundation-sites/dist/js/foundation.min.js ./node_modules/motion-ui/motion-ui.js ./node_modules/video.js/dist/video.min.js ./sass/styles.scss

When I remove @apply the build process completes without error.

thestepafter avatar Nov 02 '17 03:11 thestepafter

The same error occurs if I use @responsive.

thestepafter avatar Nov 02 '17 03:11 thestepafter

If I put the @apply and @responsive code in a .css file and import into the SCSS file then I don't get the error anymore.

thestepafter avatar Nov 02 '17 03:11 thestepafter

I spoke too soon, putting the code in an external .css works for @responsive but @apply throws an error saying the class doesn't exist.

thestepafter avatar Nov 02 '17 04:11 thestepafter

Hey @thestepafter! This is unfortunately a known issue with resolve-url-loader due to one of its dependencies falling over with CSS it doesn't understand, even if it's parseable:

https://github.com/bholloway/resolve-url-loader/issues/28

It looks like there are plans to remove the dependency that is causing that issue, but in the mean time, there's two options for working around it:

  1. Disable CSS url() rewrites in Mix which will disable that plugin:

    mix.sass('resources/assets/sass/app.scss', 'public/css')
       .options({
          processCssUrls: false,
          postCss: [ tailwindcss('path/to/config.js') ],
       });
    

    This will disable that feature of course as well which is a bit of a bummer, but personally I've never even used it.

  2. ~~Use the .standaloneSass option (also aliased as .fastSass) instead of the normal .sass compilation. This disables all fancy Webpack Sass features and just processes your Sass files in the most boring way possible:~~

    // mix.fastSass('resources/assets/sass/app.scss', 'public/css')
    //   .options({
    //    postCss: [ tailwindcss('path/to/config.js') ],
    // });
    

    ~~This will also bypass that plugin, as well as a bunch of other stuff that you might not need. Your stuff will compile way faster too 👍~~

EDIT: Scratch option two, it doesn't run your CSS through PostCSS at all when compiling Sass that way. Stick with option 1!

Let me know if that solves your issue!

adamwathan avatar Nov 02 '17 13:11 adamwathan

Awesome, thank you so much for prompt reply Adam! I added processCssUrls: false, to my options and that resolved the issue.

thestepafter avatar Nov 02 '17 14:11 thestepafter

While option 1 worked great, option 2 simply copied over the import statement from the sass file into the css, vs actually doing anything with it.

scottzirkel avatar Nov 02 '17 16:11 scottzirkel

@scottzirkel What does your import look like? This may not be your problem but worth mentioning in case it is; Sass won't process CSS imports, only Sass imports.

For example, Sass will not actually inline this import because it's a CSS file and @import is totally valid regular CSS:

@import "path/to/some/styles.css";

You can get around that by leaving off the .css extension though:

@import "path/to/some/styles";

If that's not your problem though then that's an annoying issue for sure 🤔

adamwathan avatar Nov 02 '17 17:11 adamwathan

Oh, i mean the @tailwind preflight and @tailwind utilities were just copied over. No imports or compiling were done at all. And those were the only thing I had in my app.scss file.

scottzirkel avatar Nov 02 '17 17:11 scottzirkel

@scottzirkel ah you're totally right! standaloneSass doesn't do any PostCSS stuff at all, so that option is a no go. Updated my original comment!

adamwathan avatar Nov 02 '17 18:11 adamwathan

You know, if you write tests before you code it really helps. :wink:

scottzirkel avatar Nov 02 '17 18:11 scottzirkel

I've heard this is a good idea! 😄

adamwathan avatar Nov 02 '17 18:11 adamwathan

How might one resolve this outside of Laravel? I'm in Rails and hitting the same warning.

macmartine avatar Nov 30 '17 17:11 macmartine

@macmartine What's your build setup look like? Are you using the new Rails Webpacker thing?

adamwathan avatar Nov 30 '17 18:11 adamwathan

@adamwathan Yes, exactly, the webpacker gem. I followed the steps described by @jasoncharnes here: https://github.com/tailwindcss/discuss/issues/4

macmartine avatar Nov 30 '17 18:11 macmartine

Unfortunately looking through the commit history there it looks like this is hard-coded into Webpacker and can't be disabled the same way:

https://github.com/rails/webpacker/blob/master/package/rules/css.js#L24

I'm not sure if Webpacker lets you add custom loaders or tweak these options after the fact in anyway 🤔

adamwathan avatar Nov 30 '17 18:11 adamwathan

Similar issue using Symfony Encore (webpack). https://github.com/symfony/webpack-encore.

I'll see if I can debug the issue.

leevigraham avatar Jan 10 '18 01:01 leevigraham

For Webpack Encore this can be resolved by setting resolveUrlLoader to false in the enableSassLoader call

.enableSassLoader(function (sassOptions) {}, {
  resolveUrlLoader: false
})

leevigraham avatar Jan 10 '18 01:01 leevigraham

@leevigraham thank you, that's what i was looking for ! Except for me it's resolve_url_loader and not resolveUrlLoader that works. Thanx again :)

arnaud-lecat avatar Jan 24 '18 22:01 arnaud-lecat

@macmartine did you ever get this to work inside of Rails without throwing this warning? It was still compiling for me, so I didn't think it'd be that big of an issue, but it appears Heroku is rejecting the build because of it.

kevinjbayer avatar Jun 17 '18 02:06 kevinjbayer

If any other Rails people find this, I was able to resolve it, see here: https://github.com/rails/webpacker/issues/1570

kevinjbayer avatar Jun 17 '18 03:06 kevinjbayer