svelte-preprocess: PostCSS configuration was not passed or is invalid

Describe the bug I have an issue when I’d like to use the tailwind @apply syntax within a Svelte template:

<style>
.my-button {
  @apply bg-gray-50
}
</style>

It works without any issue in a new project npm init svelte@next svelte-next but not in our existing project. Whats interesting, if I change something in a file it sometimes reloads and it just works as expected.

Logs

[svelte-preprocess] PostCSS configuration was not passed or is invalid. If you expect to load it from a file make sure to install "postcss-load-config" and try again. Error: Cannot find module '/Users/wiese/Dev/project/project-web/package.json' imported from '/Users/wiese/Dev/project/project-web/node_modules/.pnpm/lilconfig@2.0.4/node_modules/lilconfig/dist/index.js'

and/or:


/Users/wiese/Dev/project/project-web/src/components/auth/LoginOverlay.svelte:7:131 Semicolon or block is expected
  5 |  
  6 |  .input {
  7 |    @apply appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-gray-500 focus:border-gray-500 focus:z-10 sm:text-sm;
                                                                                                                                          ^
  8 |  }
  9 |  

Expected behavior I’d like to understand why it happens and how I could find the issue because it works in a new project.

Information about your project:

  • Latest Chome
  • OSX 12.2
  • Node, v16.13.2, preprocess: 4.10.2, sveltekit 1.0.0-next.247
svelte.config.js
import adapter from "@sveltejs/adapter-vercel";
import preprocess from "svelte-preprocess";
import { resolve } from "path";

/** @type {import('@sveltejs/kit').Config} */
const config = {
  preprocess: [preprocess({ postcss: true })],
  kit: {
    adapter: adapter(),
    target: "#svelte",
    vite: {
      ssr: {
        noExternal: ["svelte-hero-icons"],
      },
      resolve: {
        alias: {
          $components: resolve("src/components"),
          $icons: resolve("src/components/icons"),
        },
      },
    },
  },
};

export default config;
postcss.config.cjs
const tailwindcss = require("tailwindcss");
const autoprefixer = require("autoprefixer");

const config = {
  plugins: [
    tailwindcss(),
    autoprefixer(),
  ],
};

module.exports = config;
package.json
{
  "dependencies": {
    "@googlemaps/js-api-loader": "^1.13.2",
    "@supabase/supabase-js": "^1.29.4",
    "@uploadcare/upload-client": "^2.2.0",
    "algoliasearch": "^4.12.0",
    "autonumeric": "^4.6.0",
    "clearbit": "^1.3.5",
    "cohere-js": "^1.0.19",
    "color": "^4.2.0",
    "datebook": "^7.0.8",
    "instantsearch.js": "^4.37.3",
    "jsonwebtoken": "^8.5.1",
    "libphonenumber-js": "^1.9.46",
    "lodash-es": "^4.17.21",
    "mapbox-gl": "^2.6.1",
    "marked": "^4.0.12",
    "node-fetch": "^3.2.0",
    "pluralize": "^8.0.0",
    "svelte-hero-icons": "^4.0.3",
    "tldjs": "^2.3.1",
    "ua-parser-js": "^1.0.2",
    "uploadcare-widget": "^3.16.0",
    "zxcvbn": "^4.4.2"
  },
  "devDependencies": {
    "@sveltejs/adapter-vercel": "^1.0.0-next.39",
    "@sveltejs/kit": "^1.0.0-next.247",
    "@tailwindcss/aspect-ratio": "^0.4.0",
    "@tailwindcss/forms": "^0.4.0",
    "@tailwindcss/line-clamp": "^0.3.1",
    "@tailwindcss/typography": "^0.5.0",
    "@tsconfig/svelte": "^3.0.0",
    "@types/lodash-es": "^4.17.5",
    "@types/pluralize": "^0.0.29",
    "autoprefixer": "^10.4.2",
    "cssnano": "^5.0.16",
    "postcss": "^8.4.5",
    "postcss-load-config": "^3.1.1",
    "prettier": "^2.5.1",
    "prettier-plugin-svelte": "^2.6.0",
    "svelte": "^3.46.3",
    "svelte-check": "^2.3.0",
    "svelte-preprocess": "^4.10.2",
    "tailwindcss": "^3.0.17",
    "tslib": "^2.3.1",
    "typescript": "^4.5.5",
    "vite": "^2.7.13"
  },
  "type": "module"
}

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 6
  • Comments: 16 (1 by maintainers)

Most upvoted comments

Moving the postcss config to the svelte.config.js

import adapter from "@sveltejs/adapter-vercel";
import preprocess from "svelte-preprocess";
import { resolve } from "path";
import tailwindcss from "tailwindcss";
import autoprefixer from "autoprefixer";

/** @type {import('@sveltejs/kit').Config} */
const config = {
  preprocess: [
    preprocess({
      postcss: {
        plugins: [tailwindcss(), autoprefixer()],
      },
    }),
  ],
  kit: {
    adapter: adapter(),
    target: "#svelte",
    vite: {
      css: {
        postcss: {
          plugins: [tailwindcss(), autoprefixer()], // <-- here
        },
      },
      ssr: {
        noExternal: ["svelte-hero-icons"], // <-- and also here
      },
      resolve: {
        alias: {
          $components: resolve("src/components"),
          $icons: resolve("src/components/icons"),
        },
      },
    },
  },
};

export default config;

and deleting postcss.config.cjs fixed it 🤷

Interesting clues, it is starting to look like the issue is that sometimes the hot reload code path does not look for the configuration file, tries to use a cached version but fails, or looks for the wrong file/setup. (resetting the hole dev env fixes it for a while, duplicating configuration into two places solves it, …)

I can confirm that I need both configuration blocks mentioned above set to have a smooth workflow again.

No, it is not really a fix but at least I can continue working with that šŸ˜… I tested both variants and only if I add the plugins to both, it works.

I’m not sure what the postcss plugin array requires. I also checked what tailwind and autoprefixer actually returns and it looks like, both are functions (tw for sure)

I would like to understand more what is going on behind.

Same here 😃

On the shoulders of @wiesson solution I had to modify svelte.config.js for svelte-kit 1.0

import preprocess from 'svelte-preprocess';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	preprocess: [
		preprocess({
			postcss: {
				plugins: [tailwindcss(), autoprefixer()]
			}
		})
	]
};

export default config;

and vite.config.js

import { sveltekit } from '@sveltejs/kit/vite';
import { resolve } from 'path';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';


/** @type {import('vite').UserConfig} */
const config = {
	plugins: [sveltekit()],
	test: {
		include: ['src/**/*.{test,spec}.{js,ts}']
	},
	css: {
		postcss: {
			plugins: [tailwindcss(), autoprefixer()] // <-- here
		}
	},
	ssr: {
		noExternal: ['svelte-hero-icons'] // <-- and also here
	},
	resolve: {
		alias: {
			$components: resolve('src/components'),
			$icons: resolve('src/components/icons')
		}
	}
};

export default config;

Closing my editor, terminal and browser tab seems to fix it until the next time it happens. I personally prefer that to a config change.