HorusGoul / vite-plugin-stylex

Vite Plugin for StyleX
MIT License
110 stars 11 forks source link

ability to control the outputFileName #34

Open 0xHexE opened 5 months ago

0xHexE commented 5 months ago

I think it is hardcoded the output file name if you have library which is using this library then it will generate random file each time.

https://github.com/HorusGoul/vite-plugin-stylex/blob/703a3750dfa96178d3bcc5f5ed2e423eb7f51991/packages/vite-plugin-stylex/src/index.mts#L264

And one more thing I think there is version update require I got error inject not function function something so I downgraded version of the stylex.

evangrayk commented 5 months ago

+1 to this. It would be nice to simply respect if you set build.rollupOptions.output.assetFileNames. E.g. assetFileNames: 'myAssets/[name].[ext]' should not include the hash and should put it in myAssets/ instead of hardcoding to assets/.

If it helps anyone, this is the custom plugin I used to work around this so I got a consistent stylex.css filename I can use elsewhere:

function moveStylexFilenamePlugin(): Plugin {
  return {
    name: 'move-stylex-filename',
    writeBundle(options, bundle) {
      for (const name in bundle) {
        const chunk = bundle[name];
        // Check if this is the stylex output cssfile
        if (chunk.type === 'asset' && /assets[/\\]stylex\.[a-f0-9]+\.css/.test(chunk.fileName)) {
          // Rename the file, move it from "assets" to "res" where the rest of our assets are for my use case.
          // Ideally this would use the format from `rollupOptions.output.assetFileNames`.
          const newName = 'res/stylex.css';
          if (options.dir == null) {
            this.error('Could not replace StyleX output, dir must be set');
          }
          const dir = options.dir as string;
          const oldPath = path.resolve(dir, chunk.fileName);
          const newPath = path.resolve(dir, newName);
          this.info(`Replacing StyleX output file ${chunk.fileName} with ${newName}`);
          fs.renameSync(oldPath, newPath);
          // Update the bundle object
          chunk.fileName = newName;
          bundle[newName] = chunk;
          delete bundle[name];
        }
      }
    },
  };
}
HorusGoul commented 5 months ago

Hey! I don't have a lot of time these days, so if you want to implement this and submit PR go ahead :smile: