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

Using buildAllPlatform more than once in a single node process doesn't reload source files. #404

Closed
Okazari opened this issue Mar 18, 2020 · 3 comments

Comments

@Okazari
Copy link
Contributor

Okazari commented Mar 18, 2020

Hello there.

I'm currently building a design-token package for my lerna-monorepo that is able to build from a "noBrand" design to a multi brand design. To do so i have a personnal design-tokens package. It define the base file for my design-tokens. Each of my brand package use my design-tokens package as a CLI to build their own design-tokens. They can provide some new file for their brand to surchage some of the token values.

Everything works fine. Until i want to add a --watch option to my cli.

To do so i'm using chokidar like this :

const path = require('path')
const chokidar = require('chokidar')
const StyleDictionaryPackage = require('style-dictionary')

const baseFilesPath = path.join(__dirname, './src/base/*.json')

const build = ({ buildPath, brandFiles }) => {
  console.log({ buildPath, brandFiles, baseFilesPath })
  const StyleDictionary = StyleDictionaryPackage.extend({
    source: [baseFilesPath, brandFiles],
    platforms: {
      'web/json': {
        transformGroup: 'web',
        buildPath,
        files: [
          {
            destination: 'tokens.json',
            format: 'json/nested',
            filter: (props) => props.attributes.type !== 'global',
          },
        ],
      },
    },
  })

  StyleDictionary.cleanAllPlatforms()
  StyleDictionary.buildAllPlatforms()
}

const watch = (watchPath, args) => {
  chokidar
    .watch(watchPath, { ignoreInitial: true })
    .on('add', () => build(args))
    .on('change', () => build(args))
    .on('unlink', () => build(args))
}

module.exports = (args) => {
  if (args.watch) {
    watch(baseFilesPath, args)
    watch(args.brandFiles, args)
  } else {
    build(args)
  }
}

Problem is, executing build() twice doesn't reflect any changes to any of my watched files.
I investigated a bit and it's due to the way file are loaded.

See this line :

file_content = require(resolvedPath);

I think it's due to some node optimisation. When running twice on a similiar require() it apear that it use cache.

I'm able to solve this by replacing require() with JSON.parse(fs.readFileSync(, { encoding: "utf8" }))

But as you support JS files, i can't decently make a PR for this workaround.

Do you have any idea ? Thanks !

EDIT:
i found something else.
Adding

  delete require.cache[resolvedPath]

just before the require force to load the new version of the file.

I will make a PR accordingly 😄

@Okazari Okazari changed the title Cannot use buildAllPlatform more than once in a single node process. Using buildAllPlatform more than once in a single node process doesn't reload source files. Mar 18, 2020
@muhammadsamir11
Copy link

I add delete require.cache[resolvedPath] and still the same problem

@chazzmoney
Copy link
Collaborator

Fixed in #405

Thank you!

@dbanksdesign
Copy link
Member

@muhammadsamir11 can you provide some more details, where are you adding that code and what problem are you having?

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

4 participants