discuss icon indicating copy to clipboard operation
discuss copied to clipboard

Importing a JS into the Tailwind Config File

Open ghost opened this issue 7 years ago • 5 comments

I would like to organize and modify the tailwind config file by creating module files that I then import. For example -- I'd like to create different colors.js files which I can import when I want/need (basic-colors.js; material-colors.js, etc).

To test out if this would work, I created a test.js file and tried importing it into the tailwind-config file like this: import test from '/tailwind/tl1/test'

But when I try to compile, I get a Syntax error: SyntaxError: Unexpected token import

I am wondering if the issue is with my webpack mix file - which looks like this:

let mix = require('laravel-mix');
let tailwindcss = require('tailwindcss');


mix.postCss('dev/styles.css', 'prod/css', [
  tailwindcss('./tailwind.config.js'),
])

The tailwind.config.js is running through postCss. I assume that is the problem for using import.

Either way, any suggestion on how to make this work?

Thanks.

ghost avatar Feb 12 '18 14:02 ghost

You can't use import statements in your tailwind.js file because Node doesn't support ES6 module syntax. If you use regular require statements it should work though!

adamwathan avatar Feb 12 '18 14:02 adamwathan

Thanks.

Now I have a new error: Module build failed: Error: Cannot find module '/tailwind/tl1/test'

but I know that the file is there.

This is what I wrote in the tailwind-config file:

let defaultConfig = require('tailwindcss/defaultConfig')()
let test = require( '/tailwind/tl1/test')

Any ideas?

ghost avatar Feb 12 '18 14:02 ghost

Need to use a relative path; if I'm not mistaken the way you have it there is looking at the root of your hard drive.

Try this, assuming tailwind/tl1/test is a folder relative to the this JS file:

let defaultConfig = require('tailwindcss/defaultConfig')()
let test = require( './tailwind/tl1/test')

If that's not how you have things structured you just need to traverse around directories as needed to get to the right place (../../tailwind/tl1/test sort of thing).

adamwathan avatar Feb 12 '18 15:02 adamwathan

That fixed it - thanks.

ghost avatar Feb 12 '18 15:02 ghost

@adamwathan is there any way to add support for static imports in tailwind.config.js? Maybe it can be run via projects babel webpack setup? I use Nuxt and have to share several files I use both on client and server and I can't import files with module.exports which is required by require and also I can't require files that were exported with export default.

AndrewBogdanovTSS avatar Mar 06 '20 11:03 AndrewBogdanovTSS