Simple require imports, for example const bar = require("foo")
:
- Regex:
(const|var|let)\b\s+(\w+)\s+=\s+require\s*\(["']([\/\w\-]+)["']\)\n
- Replace:
import $2 from "$3"\n
One-liner require & config/compute, for example const router = require("express").Router({ mergeParams: true })
:
- Regex:
(const|var|let)\b\s+(\w+)\s+=\s+require\s*\(["'](\w+)["']\)(?=\S+)
- Replace:
import $3 from "$3" \n$1 $2 = $3
WARNING: Using this regex one can end up with duplicate variable names (when $2 === $3).
Relative module imports, for example const bar = require("./bar/foo")
:
- Regex:
(const|var|let)\b\s+(\w+)\s+=\s+require\s*\(["']([\.\/\w\-]+)["']\)\n
- Replace:
import $2 from "$3"\n
One-line import, no variable, for example: require("module")
:
- Regex:
require\s*\(["']([\/\w\-]+)["']\)\n
- Replace:
import "$1"\n
Append .mjs or .js to import statements:
- Regex:
import ([\w\-_]+)\s+from\s+["'](?=\.)([\w\-\.\/]+)["']
- Replace:
import $1 from "$2.js"
module.exports =
withexport default
exports.([\w\-_]+)
withexport const $1