Prefer imports over require

JavaScript pattern

Converts require statements to ES6-style import.

Transform standard require statements

BEFORE
const defaultImport = require('../../shared/default').default;
const { something, another } = require('./lib');
const { value, original: renamed } = require('something');
const otherName = require('chai').ogName;
const assert = require('chai').assert;
const conf = require('chai').config;
const starImport = require('star');
AFTER
import defaultImport from '../../shared/default';
import { something, another } from './lib';
import { value, original as renamed } from 'something';
import { ogName as otherName } from 'chai';
import { assert } from 'chai';
import { config as conf } from 'chai';
import starImport from 'star';

Handle dotenv

BEFORE
require('dotenv').config({ path: '../.env' });

// Another example
require('dotenv').config();

function doStuff() {
  // hello world
}
AFTER
import * as dotenv from 'dotenv';
dotenv.config({ path: '../.env' });

// Another example
import * as dotenv from 'dotenv';
dotenv.config();

function doStuff() {
  // hello world
}

Handle Sentry

BEFORE
const Sentry = require('@sentry/node');
AFTER
import * as Sentry from '@sentry/node';

Handle deep props

BEFORE
const assert = require('test-lib').assert,
  path = require('path'),
  hash = require('../hash'),
  {
    Some: {
      Deep: { Concerns },
    },
  } = require('@org/pkg'),
  { ancestorExport } = require('../../ancestor');

const defaultOptions = require('../../conf/default-cli-options');
const pkg = require('../../package.json');

const {
  Legacy: {
    ConfigOps,
    naming,
    CascadingConfigArrayFactory,
    IgnorePattern,
    getUsedExtractedConfigs,
    ModuleResolver,
  },
} = require('@org/pkg');

const proxyquire = require('proxyquire').noCallThru().noPreserveCache();
AFTER
import { assert } from 'test-lib';
import path from 'path';
import hash from '../hash';
import { Some } from '@org/pkg';
const {
  Deep: { Concerns },
} = Some;
import { ancestorExport } from '../../ancestor';

import defaultOptions from '../../conf/default-cli-options';
import pkg from '../../package.json';

import { Legacy } from '@org/pkg';
const {
  ConfigOps,
  naming,
  CascadingConfigArrayFactory,
  IgnorePattern,
  getUsedExtractedConfigs,
  ModuleResolver,
} = Legacy;

import __proxyquire from 'proxyquire';
const proxyquire = __proxyquire.noCallThru().noPreserveCache();