Skip to content

[Experiment] Allow @apply-ing utility classes that aren't explicitly defined but would be generated - #516

Merged
adamwathan merged 4 commits into
masterfrom
shadow-table
Jul 11, 2018
Merged

adamwathan merged 4 commits into
masterfrom
shadow-table

Conversation

@adamwathan

Copy link
Copy Markdown
Contributor

Resolves #150.

This PR introduces a solution to the long-standing problem of wanting to use @apply with classes that don't exist in the same CSS tree.

This issue most commonly comes up when someone is working with Vue and wants to use @apply in the <style> block of a single file Vue component. It seems like basically every Vue project is setup to run PostCSS on every <style> block independently instead of on the final concatenated CSS, which means if you have 100 components, Tailwind is run 100 independent times, and each run is isolated and can't see any of the classes in the other runs.

This PR solves this by always generating all of Tailwind's utilities (including those registered by plugins) and using it as a fallback table for @apply if the class can't be found.

I'm not ready to fully commit to this without getting folks to try it out and report feedback, so this PR only enables this functionality if you add an experiments key to your Tailwind config file:

module.exports = {
  // ...
  experiments: {
    shadowLookup: true
  }
}

Because this is an opt-in experiment, it's going to remain undocumented outside of this PR for now, and I reserve the right to yank it or break it without following semver, so please don't rely on this functionality for production sites right now, just test it out locally and give me your feedback.

@curtisblackwell

Copy link
Copy Markdown

I'm trying to get this working with Laravel Mix, but without success. Is any additional setup required outside of adding the shadowLookup flag to the tailwind config?

  // ...
  options: {
    prefix:    '',
    important: false,
    separator: ':',
  },

  experiments: {
    shadowLookup: true,
  },
};

In a Vue component, I have this style block:

<style lang="postcss">
  .card-chart {
    @apply rounded;
    /* ... */
  }
</style>

The generated styles include the unprocessed @apply rounded;.

I'm using Tailwind v0.6.3, and I tried adding the plugins key to the Webpack config. It seems like I should somehow pass the tailwind config to the js method, but it only accepts an input and output. Here's my webpack.mix.js:

// ...

mix
  .webpackConfig({
    plugins: [tailwindcss('./tailwind.js')],
    resolve: {
      modules: [
        path.resolve(__dirname),
        path.resolve('./resources/assets/js'),
        path.resolve('./node_modules'),
      ],
    },
  })
  .js('resources/assets/js/app.js', 'public/js').sourceMaps()
  .postCss('resources/assets/css/main.css', 'public/css', [
    postcssImport(),
    tailwindcss('./tailwind.js'),
  ])
  .sourceMaps()
  .purgeCss();

Before I added the plugins key to the Webpack config, the @apply directive just wasn't processed; after adding, I get an error: TypeError: arguments[i].apply is not a function.

@adamwathan

Copy link
Copy Markdown
Contributor Author

@curtisblackwell The problem in this case is your mix configuration. If you use the postCss function to specify your plugins, they are only applied to that CSS file, not to your Vue style blocks.

To configure those PostCSS plugins for all processed CSS, use the options method instead:

  // ...
  
  mix
    .webpackConfig({
      plugins: [tailwindcss('./tailwind.js')],
      resolve: {
        modules: [
          path.resolve(__dirname),
          path.resolve('./resources/assets/js'),
          path.resolve('./node_modules'),
        ],
      },
    })
    .js('resources/assets/js/app.js', 'public/js').sourceMaps()
-   .postCss('resources/assets/css/main.css', 'public/css', [
-     postcssImport(),
-     tailwindcss('./tailwind.js'),
-   ])
+   .postCss('resources/assets/css/main.css', 'public/css')
+   .options({
+     postCss: [
+       postcssImport(),
+       tailwindcss('./tailwind.js'),
+     ]
+   })
    .sourceMaps()
    .purgeCss();

@curtisblackwell

Copy link
Copy Markdown

That fixed it, thanks!

@ghost

ghost commented Sep 3, 2018

Copy link
Copy Markdown

Stopping by, just to say this feature is great. I work with Vue, and it's exactly what I needed.

Thanks!

@adamwathan adamwathan mentioned this pull request Oct 2, 2018
joshukraine added a commit to joshukraine/ofreport.com-v1 that referenced this pull request Nov 30, 2018
* No longer using external css component.
* Found out that @apply directly can now be used inside <style> tags in Vue components.
* tailwindlabs/tailwindcss#516
@adamwathan
adamwathan deleted the shadow-table branch January 21, 2019 19:22
@agcty

agcty commented Dec 10, 2019 •

Copy link
Copy Markdown

Is this still an experiment or is it production ready? I assume it is production ready as it was merged over 1 year ago but there's no documentation about it so I'm unsure if I should use it or not.

@tlgreg

tlgreg commented Dec 10, 2019

Copy link
Copy Markdown

It's on by default for a while now, don't need to configure it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider not relying on previously defined mixin in file for "@apply"

4 participants