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.

4
  • Are you using Single File Component in Vue, you might need vue-loader as well. The error is about .day-disabled not @apply Commented Nov 14, 2019 at 2:58
  • I have that. If I remove that @apply everything works. Commented Nov 14, 2019 at 3:00
  • Not very familiar with postcss but do you need require('postcss-apply') in your mix option? Commented Nov 14, 2019 at 3:12
  • I am not sure. But assume I do not need this because it is working for CSS that is outside Vue component. Commented Nov 14, 2019 at 14:22

2 Answers 2

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'
        ]
      }
    ],
  },
})
Sign up to request clarification or add additional context in comments.

1 Comment

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 more
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]

1 Comment

Confirmed working for Laravel Mix 7 + Vue 3 + Tailwind 2.0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.