I have got a nextjs project where i'm using tailwind. On build I load in a default config, but then i also want to wait for an API response to get custom styles from an endpoint.
This is what I have got below however I feel like i'm going about this the wrong way, currently getting webpack errors:
Error: Cannot find module './services/getThemeStyles'
// ./tailwind.config.js
// Default styles
const theme = require('./lib/themes/default/tailwind/tailwind.config');
// Website custom styles
const custom = require('./services/getThemeStyles');
module.exports = {
...theme, // Do deep merge here of theme and custom
content: ['./lib/**/*.{js,ts,jsx,tsx}', './pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
};
// ./services/getThemeStyles.ts
// Fake temp endpoint, this would hit an API when working.
const theme = require('/data/theme');
export default async function getThemeStyles(): Promise<any> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(theme);
}, 1);
});
}
// ./data/theme.ts
const Theme = {
extend: {
colors: {
'banner-background': 'rgb(220,220,0)'
}
}
}
export default Theme;
Anyone done this before or can anyone help me out?
Edit
Changed my approach, made a node server so getting data is more realistic, so now my tailwind.config.js looks like this:
const fetch = require('node-fetch');
const merge = require('merge-deep');
module.exports = (async function() {
let theme = require('./lib/themes/default/tailwind/tailwind.config');
const response = await fetch('http://localhost:5000/data');
const data = await response.json();
return {
...merge(theme, data),
content: ['./lib/**/*.{js,ts,jsx,tsx}', './pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
}
})();
However I can't put await infront of fetch due to not being in a async function, so the module is being exported before the request is returned.