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

add theme file to dependencies to get webpack reloading working #36

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ function replaceTheme(value: string, replace: string) {
return value.replace(/@theme ([a-zA-Z-_0-9]+)/, replace);
}

/** Get the location of the theme file */
function getThemeFilename(cssFile: string) {
return path.join(path.dirname(cssFile), 'theme');
}

/** Try to load component theme from same directory as css file */
function configForComponent(
cssFile: string | undefined,
Expand All @@ -82,7 +87,7 @@ function configForComponent(
if (resolveTheme) {
componentConfig = resolveTheme(cssFile);
} else {
const theme = path.join(path.dirname(cssFile), 'theme');
const theme = getThemeFilename(cssFile);
delete require.cache[require.resolve(theme)];
// eslint-disable-next-line security/detect-non-literal-require, global-require
componentConfig = require(theme);
Expand Down Expand Up @@ -570,7 +575,8 @@ const modernTheme = (

/** Generate a theme */
const themeFile = (options: PostcssThemeOptions = {}) => (
root: postcss.Root
root: postcss.Root,
result: postcss.Result
) => {
const { config, resolveTheme } = options;

Expand Down Expand Up @@ -602,6 +608,18 @@ const themeFile = (options: PostcssThemeOptions = {}) => (

// @ts-ignore
root.source.processed = true;

if (!resolveTheme && root.source.input.file) {
const themeFilename = getThemeFilename(root.source.input.file)

if (fs.existsSync(themeFilename)) {
result.messages.push({
plugin: 'postcss-themed',
type: "dependency",
file: themeFilename
});
}
}
};

export default postcss.plugin('postcss-themed', themeFile);