kwonoj / swc-plugin-coverage-instrument

istanbuljs compatible SWC coverage instrumentation plugin
MIT License
90 stars 12 forks source link

Read sourcemap from webpack/swc-loader #245

Closed satouriko closed 2 months ago

satouriko commented 2 months ago

I'm trying to instrument code with swc, using this plugin and swc-loader.

import Config from 'webpack-5-chain';

const config = new Config();
const rule = config.module.rule('ts').test(/\.ts$/);
rule
  .use('swc-loader')
  .loader('swc-loader')
  .options({
    sourceMaps: true,
    parseMap: true,
    jsc: {
      experimental: {
        plugins: [
          [
            'swc-plugin-coverage-instrument',
            {
              instrumentLog: { level: 'warn', enableTrace: false },
              unstableExclude: ['**/node_modules/**'],
            },
          ],
        ],
      },
    },
  });
rule
  .use('ts-loader')
  .loader('ts-loader')
  .options({ appendTsSuffixTo: ['\\.vue$'], transpileOnly: true });

This problem is, the generated code doesn't include a inputSourceMap field as the babel plugin does. Here's the babel one,

import Config from 'webpack-5-chain';

const config = new Config();
const rule = config.module.rule('ts').test(/\.ts$/);
rule
  .use('babel-loader')
  .loader('babel-loader')
  .options({
    configFile: false,
    sourceType: 'unambiguous',
    sourceMaps: true,
    presets: [['@babel/preset-env']],
    plugins: [['babel-plugin-istanbul', { extension: ['.ts', '.vue'] }]],
  });
rule
  .use('ts-loader')
  .loader('ts-loader')
  .options({ appendTsSuffixTo: ['\\.vue$'], transpileOnly: true });

I can only find an option inputSourceMap which requires a sourcemap object. Is there anyway that could read sourcemap from the webpack context via swc-loader, like babel does?

kwonoj commented 2 months ago

Is there anyway that could read sourcemap from the webpack context via swc-loader, like babel does

SWC's plugin have no access to host's javascript runtime, that's the reason plugin accepts an object to consume but do not have way to access external. Unless I miss some additional context, I don't think this is an immediate actionable item on the plugin side.

satouriko commented 1 month ago

End up making https://github.com/satouriko/swc-loader-coverage-instrument as a workaround before swc makes it to pass inputSourceMap to plugins.