Anber / wyw-in-js

MIT License
191 stars 8 forks source link

Interpolating imported object fails on rebuild #36

Open bogdbo opened 5 months ago

bogdbo commented 5 months ago

Environment

Description

Given a file colors.ts that contains an object with color names, importing this object into another file and interpolating colors.blue in a css template will work only on the initial build (esbuild). Making changes to colors.ts and adding a new color (e.g. colors.red) will trigger esbuild to rebuild but when trying to use colors.red in the same css (which rebuilds again) I get an error that the colors.red is not set and I see the following message:

The expression evaluated to 'undefined', which is probably a mistake. If you want it to be inserted into CSS, explicitly cast or transform the value to a string, e.g. - 'String(colors.red)'

Note (1): if I restart esbuild, the error goes away

Note (2): esbuild is set up to watch for changes

const ctx = await esbuild.context(config);
await ctx.watch();

Note (3): there are no errors if colors and css are in the same file

Note (4): I have noticed that disabling globalCache with the following config fixes the issue

module.exports = {
  evaluate: true,
  displayName: true,
  features: {
    globalCache: ['!**/colors.ts'],
  }
};

Reproducible Demo

colors.ts index.ts
initial ```js const colors = { blue: 'blue' }; ``` ```js import { colors } from './colors'; const example = css` color: ${colors.blue}; `; root.render(
hello
); ```
Added color red and update `css` to use colors.red instead of colors.blue
update ```js const colors = { blue: 'blue', red: 'red' // new }; ``` ```js import { colors } from './colors'; const example = css` color: ${colors.red}; /* updated */ `; root.render(
hello
); ```
poteboy commented 2 months ago

Any updates on this? Or, do you have any insights into what might be causing this issue? If so, I'd be happy to contribute.