Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference theme.extend.x in Javascript #1201

Closed
bradley-varol opened this issue Nov 4, 2019 · 6 comments
Closed

Reference theme.extend.x in Javascript #1201

bradley-varol opened this issue Nov 4, 2019 · 6 comments

Comments

@bradley-varol
Copy link

bradley-varol commented Nov 4, 2019

I've scoured the internet for how to do the following, with no luck:

tailwind.config.js:

const defaultTheme = require('tailwindcss/defaultTheme');

module.exports = {
    theme: {
        extend: {
            colors: {
                primary: defaultTheme.colors.purple,
            },
        },
    },
    variants: {},
    plugins: [],
};

vue compoennt:

<script>
import resolveConfig from 'tailwindcss/resolveConfig';
const { theme } = resolveConfig('@/tailwind.config.js');

console.log(theme.extend.colors.primary[500]); // TypeError: "theme.extend is undefined"
</script>

Is it possible to access the extended theme from Javascript?
Awesome library, Adam. Thanks.

@tlgreg
Copy link

tlgreg commented Nov 4, 2019

The resolved config doesn't have extend anymore, your values are merged in at that point, in the above example you have theme.colors.primary

@bradley-varol
Copy link
Author

Thanks for a speedy response, @tlgreg , but theme.colors.primary is undefined.

@adamwathan
Copy link
Member

adamwathan commented Nov 4, 2019

resolveConfig expects a config object to resolve, not a path:

import resolveConfig from 'tailwindcss/resolveConfig';
import tailwindConfig from '../tailwind.config.js';
const { theme } = resolveConfig(tailwindConfig);

Docs here: https://tailwindcss.com/docs/configuration/#referencing-in-javascript

@bradley-varol
Copy link
Author

I've changed my imports to be like the docs and I'm getting the following error during compilation:

./components/HelloWorld.vue?vue&type=script&lang=js& (./node_modules/babel-loader/lib??ref--2-0!./node_modules/vue-loader/lib??vue-loader-options!./components/HelloWorld.vue?vue&type=script&lang=js&) 46:35-49
"export 'default' (imported as 'tailwindConfig') was not found in '@/tailwind.config.js' vendors.app.js:2885:15
TypeError: ""exports" is read-only"

@adamwathan
Copy link
Member

Sounds like a webpack issue with mixing ES6 and CommonJS imports. Try using regular require instead of import to get the tailwindConfig.

@bradley-varol
Copy link
Author

Adding sourceType: 'unambiguous' to my Babel config fixed this by allowing me to import a module.exports.

Thanks for your time, Adam.

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

No branches or pull requests

3 participants