2

I'm working on a plugin for the SWC compiler and I'm trying to pass a parameter from the Next.js configuration to the plugin. However, I'm having trouble accessing this parameter within the plugin. I've been trying to find a solution for a long time, but I haven't been successful so far.

Here's how I'm passing the parameter in the Next.js configuration:

const nextConfig = {
    experimental: {
        swcPlugins: [
            [require.resolve("./plugin.wasm"), {param:"test1"}]
        ],
    },
}

Could someone please guide me on how to retrieve this parameter inside the plugin? Any help would be greatly appreciated. Thank you!

Roman
  • 175
  • 2
  • 3
  • 15

1 Answers1

0

To access the parameter pased from Next.js config inside your SWC compiler plugin:

First install @swc/core package in your project:

npm install @swc/core

Then in your SWC compiler plugin you can access the parameter by accessing plugin.options object.

Example:

module.exports = function plugin({ types: t }) {
  return {
    visitor: {
      Program(path, state) {
        const param = state.opts.param;
        console.log('param:', param);
        
        // You can use the param value in your plugin logic
      },
    },
  };
};

The state.opts object contains the options passed to the plugin. You can access param parameter using state.opts.param, then use the value of param in plugin logic.

Make sure your plugin code is properly transformed using SWC before using it in Next.js. :)

Qasim
  • 658
  • 1
  • 5
  • 10