@apply not working inside Vue component in Laravel Mix
Asked Answered
C

2

5

I am using Tailwind CSS in Laravel with VueJS component like this.

<template>

</template>

<script>

</script>

<style lang="postcss" scoped>

    .day-disabled {

        @apply text-gray-400;
    }

</style>

However it is complaining that

Module parse failed: Unexpected token (685:0)
File was processed with these loaders:
 * ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|
|
> .day-disabled {
|
|     @apply text-gray-400;

Is there anyway to use @apply directive in VueJS component using Laravel Mix. This is my webpack.mix.js

const mix = require('laravel-mix');
mix.options({ extractVueStyles: true})
   .js('resources/js/app.js', 'public/js')
   .postCss('resources/css/app.css', 'public/css', [
       require('postcss-import'),
       require('tailwindcss'),
       require('postcss-nested'),
       require('postcss-custom-properties'),
       require('autoprefixer')
   ]);

Is there anyway to resolve this issue.

Columbarium answered 14/11, 2019 at 2:7 Comment(4)
Are you using Single File Component in Vue, you might need vue-loader as well. The error is about .day-disabled not @applyTourer
I have that. If I remove that @apply everything works.Columbarium
Not very familiar with postcss but do you need require('postcss-apply') in your mix option?Tourer
I am not sure. But assume I do not need this because it is working for CSS that is outside Vue component.Columbarium
G
5

It seems this may be a bug in Laravel Mix, see this issue.

Try adding the following to your Mix config (source):

.webpackConfig({
  module: {
    rules: [
      {
        test: /\.(postcss)$/,
        use: [
          'vue-style-loader',
          { loader: 'css-loader', options: { importLoaders: 1 } },
          'postcss-loader'
        ]
      }
    ],
  },
})
Gamin answered 21/11, 2019 at 18:51 Comment(1)
For this to work correctly, you also need to create a postcss configuration file called postcss.config.js in the root folder with the code provided by the tailwind team. read moreBedim
A
3

For the above answer to work make sure to create a postcss.config.js file if you don't have it already and paste this snippet inside

const purgecss = require('@fullhuman/postcss-purgecss')({
    content: [
        './resources/**/*.vue',
        './resources/**/*.js'
    ],
    whitelistPatterns: [ /-(leave|enter|appear)(|-(to|from|active))$/, /data-v-.*/, /v-deep/ ],
    whitelistPatternsChildren: [ /pretty$/, /xmx-.*$/, /^(.*?)\.tooltip/ ],
    defaultExtractor: content => content.match(/[\w-/.:]+(?<!:)/g) || []
})

module.exports = {
    plugins: [
        require('tailwindcss'),
        require('autoprefixer'),
        ...process.env.NODE_ENV === 'production' ? [purgecss] : []
    ]
}

Here is the source [https://github.com/JeffreyWay/laravel-mix/issues/2312]

Aerobatics answered 5/12, 2020 at 14:43 Comment(1)
Confirmed working for Laravel Mix 7 + Vue 3 + Tailwind 2.0Cytologist

© 2022 - 2024 — McMap. All rights reserved.