snowpack-plugin-selective-import-to-require

A Snowpack plugin for selectively converting imports to requires based on module names.

Usage no npm install needed!

<script type="module">
  import snowpackPluginSelectiveImportToRequire from 'https://cdn.skypack.dev/snowpack-plugin-selective-import-to-require';
</script>

README

snowpack-plugin-selective-import-to-require

Selectively convert import calls into require calls. Useful for running snowpack with Electron.

npm install --save-dev snowpack-plugin-selective-import-to-require
// snowpack.config.json
module.exports = {
  "plugins": [
    [
      "snowpack-plugin-selective-import-to-require",
      {
        "modules": ["os", "child_process"],
        "types": ['.js'],
      }
    ]
  ]
}

Plugin Options

Name Type Description
modules string[] An array of module names to match against, such as ['os', 'child_process'].
types string[] (optional) By default, this plugin matches against ['.js']. Provide an array if you need to match other extensions.

Match Built-in Node Modules

It may be convenient to match all built-in node modules.

npm install builtin-modules --save-dev
// snowpack.config.json
const builtinModules = require('builtin-modules')

module.exports = {
  "plugins": [
    [
      "snowpack-plugin-selective-import-to-require",
      {
        "modules": builtinModules,
        "types": ['.js'],
      }
    ]
  ],
  "packageOptions": {
    "external": builtinModules
  },
}