#swc-plugin #swc #plugin #order #export #exported #named

swc-export-order

SWC plugin for injecting export order

1 unstable release

new 0.1.0 Jan 13, 2025

#1845 in Web programming

MIT/Apache

10KB
177 lines

SWC export order

This is an SWC plugin which injects an exported constant named __namedExportsOrder. This constant is defined as a string array of exported names in the module, in the order they were exported. Thus, the following example:

const a = 5

export const b = 'foo'

export { a }

becomes

const a = 5

export const b = 'foo'

export { a }
export const __namedExportsOrder = ['b', 'a']

As you can see, the order is dictated by when a name is exported, not by when it is defined.

Using the plugin

The plugin targets swc_core 5.0, meaning that it supports @swc/core 1.9. See https://plugins.swc.rs/ for details. Assuming you're using rspack, you can follow its documentation for an example for adding a plugin. You can also consult its reference for futher details. In short, you'll want to set your loader options to something like

module.exports = {
  module: {
    rules: [
      {
        // Which modules should be match
        test: /\.js$/,
        use: {
          loader: 'builtin:swc-loader',
          options: {
            jsc: {
              experimental: {
                plugins: [
                  [
                    // Name of NPM package
                    'swc-export-order',
                    // Plugin configuration (not used for this plugin, may be subject to change)
                    {},
                  ],
                ],
              },
            },
          },
        },
      },
    ],
  },
};

Dependencies

~3.5–4.5MB
~82K SLoC