Skip to content

Commit

Permalink
Avoid using require in src files
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 4, 2021
1 parent 468e8c3 commit 98b5273
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.cjs
Expand Up @@ -46,7 +46,6 @@ module.exports = {
"@babel/development/no-deprecated-clone": "error",
"guard-for-in": "error",
"import/extensions": ["error", { json: "always", cjs: "always" }],
"no-restricted-globals": ["error", ...cjsGlobals],
},
globals: { PACKAGE_JSON: "readonly" },
},
Expand All @@ -73,6 +72,9 @@ module.exports = {
},
{
files: [
"packages/*/src/**/*.{js,ts}",
"codemods/*/src/**/*.{js,ts}",
"eslint/*/src/**/*.{js,ts}",
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
Expand All @@ -81,7 +83,7 @@ module.exports = {
],
excludedFiles: [
// @babel/register is the require() hook, so it will always be CJS-based
"packages/babel-register/test/**/*.js",
"packages/babel-register/**/*.js",
],
rules: {
"no-restricted-globals": ["error", ...cjsGlobals],
Expand Down
25 changes: 25 additions & 0 deletions babel.config.js
Expand Up @@ -473,12 +473,18 @@ function pluginImportMetaUrl({ types: t }) {
t.isIdentifier(node.meta, { name: "import" }) &&
t.isIdentifier(node.property, { name: "meta" });

const isImportMetaUrl = node =>
t.isMemberExpression(node, { computed: false }) &&
t.isIdentifier(node.property, { name: "url" }) &&
isImportMeta(node.object);

return {
visitor: {
Program(programPath) {
// We must be sure to run this before the instanbul plugins, because its
// instrumentation breaks our detection.
programPath.traverse({
// fileURLToPath(import.meta.url)
CallExpression(path) {
const { node } = path;

Expand All @@ -501,6 +507,25 @@ function pluginImportMetaUrl({ types: t }) {

path.replaceWith(t.identifier("__filename"));
},

// const require = createRequire(import.meta.url)
VariableDeclarator(path) {
const { node } = path;

if (
!t.isIdentifier(node.id, { name: "require" }) ||
!t.isCallExpression(node.init) ||
!t.isIdentifier(node.init.callee, { name: "createRequire" }) ||
node.init.arguments.length !== 1 ||
!isImportMetaUrl(node.init.arguments[0])
) {
return;
}

// Let's just remove this declaration to unshadow the "global" cjs require.
path.remove();
},

MetaProperty(path) {
if (isImportMeta(path.node)) {
throw path.buildCodeFrameError("Unsupported import.meta");
Expand Down
4 changes: 4 additions & 0 deletions lib/third-party-libs.js.flow
Expand Up @@ -2,6 +2,10 @@
* Basic declarations for the npm modules we use.
*/

declare module "module" {
declare export function createRequire(url: any): any;
}

declare module "debug" {
declare export default (namespace: string) => (formatter: string, ...args: any[]) => void;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-cli/src/babel/util.js
Expand Up @@ -4,6 +4,7 @@ import readdirRecursive from "fs-readdir-recursive";
import * as babel from "@babel/core";
import path from "path";
import fs from "fs";
import { createRequire } from "module";

export function chmod(src: string, dest: string): void {
try {
Expand Down Expand Up @@ -119,6 +120,9 @@ process.on("uncaughtException", function (err) {
});

export function requireChokidar(): Object {
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url);

try {
// todo(babel 8): revert `@nicolo-ribaudo/chokidar-2` hack
return parseInt(process.versions.node) >= 8
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-core/src/config/files/configuration.js
Expand Up @@ -18,6 +18,10 @@ import type { CallerMetadata } from "../validation/options";

import * as fs from "../../gensync-utils/fs";

import { createRequire } from "module";
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url);

const debug = buildDebug("babel:config:loading:files:configuration");

export const ROOT_CONFIG_FILENAMES = [
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-core/src/config/files/module-types.js
Expand Up @@ -2,6 +2,9 @@ import { isAsync, waitFor } from "../../gensync-utils/async";
import type { Handler } from "gensync";
import path from "path";
import { pathToFileURL } from "url";
import { createRequire } from "module";

const require = createRequire(import.meta.url);

let import_;
try {
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-core/src/config/files/plugins.js
Expand Up @@ -9,6 +9,10 @@ import path from "path";
import { type Handler } from "gensync";
import loadCjsOrMjsDefault from "./module-types";

import { createRequire } from "module";
// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504
const require = createRequire(import /*::("")*/.meta.url);

const debug = buildDebug("babel:config:loading:files:plugins");

const EXACT_RE = /^module:/;
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-helper-fixtures/src/index.ts
Expand Up @@ -3,6 +3,9 @@ import semver from "semver";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import { createRequire } from "module";

const require = createRequire(import.meta.url);

const nodeVersion = semver.clean(process.version.slice(1));

Expand Down
Expand Up @@ -17,6 +17,9 @@ import QuickLRU from "quick-lru";
import escapeRegExp from "./escape-regexp.cjs";
import { fileURLToPath } from "url";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

import _checkDuplicatedNodes from "babel-check-duplicated-nodes";
const checkDuplicatedNodes = _checkDuplicatedNodes.default;

Expand Down
3 changes: 3 additions & 0 deletions packages/babel-node/src/_babel-node.js
Expand Up @@ -10,6 +10,9 @@ import "regenerator-runtime/runtime";
import register from "@babel/register";
import { fileURLToPath } from "url";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

const program = new commander.Command("babel-node");

function collect(value, previousValue): Array<string> {
Expand Down
@@ -1,5 +1,8 @@
import path from "path";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

export default function (moduleName, dirname, absoluteRuntime) {
if (absoluteRuntime === false) return moduleName;

Expand Down

0 comments on commit 98b5273

Please sign in to comment.