vue-test-loader icon indicating copy to clipboard operation
vue-test-loader copied to clipboard

Prefer Webpack chunks instead of fs.writeFileSync()?

Open Cobertos opened this issue 4 years ago • 1 comments

I'm admittedly not a Webpack expert (especially for chunks, which I rarely use), but instead of using fs.writeFileSync and have webpack synchronously write out files, wouldn't be better to just use Webpack's emitFile method for this? You can see it in action in the file-loader which emits the file to disk and returns the path.

By emitting a file, I'm pretty sure it gets refed to webpack and extra processing can happen. At that point, you can use SplitChunksPlugin, specifically the .name property to bundle the tests, or not (it becomes up to the user). It'd probably require similar options to file-loader tbh, which would get around the hardcoded __tests__ path.

Perhaps it would just be better to use file-loader?

Cobertos avatar Jan 25 '21 07:01 Cobertos

After experimenting it looks like I can in fact just use file-loader to load the tests like this module does, but with better integration into Webpack.

I also had to use the latest documentation for vue-loader.vuejs.org as the example for adding a loader to vue-loader in the README wasn't working for me

This is for extracting ava tests from components in my Nuxt project using the build.extend property.

extend(config) {
  config.externals = config.externals || {};
  config.externals.ava = 'ava';

  config.module.rules.push({
    resourceQuery: /blockType=test/,
    loader: 'file-loader',
    options: {
      name: '[name].spec.js',
      outputPath: 'componentTests'
    }
  });
}

Cobertos avatar Jan 25 '21 09:01 Cobertos