Skip to content

Commit

Permalink
Add explicit .ts/.js extension to all imports in src (#15892)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 31, 2023
1 parent 8ba5c67 commit 5ebab54
Show file tree
Hide file tree
Showing 292 changed files with 1,096 additions and 1,093 deletions.
2 changes: 1 addition & 1 deletion Gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function generateStandalone() {
through.obj(async (file, enc, callback) => {
log("Generating @babel/standalone files");
const pluginConfig = JSON.parse(file.contents);
let imports = `import makeNoopPlugin from "../make-noop-plugin";`;
let imports = `import makeNoopPlugin from "../make-noop-plugin.ts";`;
let exportDecls = "";
let exportsList = "";
let allList = "";
Expand Down
60 changes: 7 additions & 53 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ module.exports = function (api) {

let targets = {};
let convertESM = outputType === "script";
/** @type {false | "externals" | "always"} */
let addImportExtension = convertESM ? false : "always";
let replaceTSImportExtension = true;
let ignoreLib = true;
let includeRegeneratorRuntime = false;
let needsPolyfillsForOldNode = false;
Expand Down Expand Up @@ -107,7 +106,7 @@ module.exports = function (api) {
case "standalone":
includeRegeneratorRuntime = true;
convertESM = false;
addImportExtension = false;
replaceTSImportExtension = false;
ignoreLib = false;
// rollup-commonjs will converts node_modules to ESM
unambiguousSources.push(
Expand All @@ -120,7 +119,7 @@ module.exports = function (api) {
break;
case "rollup":
convertESM = false;
addImportExtension = "externals";
replaceTSImportExtension = false;
ignoreLib = false;
// rollup-commonjs will converts node_modules to ESM
unambiguousSources.push(
Expand Down Expand Up @@ -230,9 +229,7 @@ module.exports = function (api) {
assumptions: sourceAssumptions,
plugins: [
transformNamedBabelTypesImportToDestructuring,
addImportExtension
? [pluginAddImportExtension, { when: addImportExtension }]
: null,
replaceTSImportExtension ? pluginReplaceTSImportExtension : null,

[
pluginToggleBooleanFlag,
Expand Down Expand Up @@ -787,56 +784,13 @@ function pluginImportMetaUrl({ types: t, template }) {
};
}

function pluginAddImportExtension(api, { when }) {
function pluginReplaceTSImportExtension() {
return {
visitor: {
"ImportDeclaration|ExportDeclaration"({ node }) {
const { source } = node;
if (!source) return;

if (
when === "always" &&
source.value.startsWith(".") &&
!/\.[a-z]+$/.test(source.value)
) {
const dir = pathUtils.dirname(this.filename);

try {
const pkg = JSON.parse(
fs.readFileSync(
pathUtils.join(dir, `${source.value}/package.json`)
),
"utf8"
);

if (pkg.main) source.value = pathUtils.join(source.value, pkg.main);
} catch (_) {}

try {
if (fs.statSync(pathUtils.join(dir, source.value)).isFile()) return;
} catch (_) {}

for (const [src, lib = src] of [["ts", "js"], ["js"], ["cjs"]]) {
try {
fs.statSync(pathUtils.join(dir, `${source.value}.${src}`));
source.value += `.${lib}`;
return;
} catch (_) {}
}

source.value += "/index.js";
}
if (
source.value.startsWith("lodash/") ||
source.value.startsWith("core-js-compat/") ||
source.value === "core-js/stable/index" ||
source.value === "regenerator-runtime/runtime" ||
source.value === "babel-plugin-dynamic-import-node/utils"
) {
source.value += ".js";
}
if (source.value.startsWith("@babel/preset-modules/")) {
source.value += "/index.js";
if (source) {
source.value = source.value.replace(/(\.[mc]?)ts$/, "$1js");
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ module.exports = [
"@babel/development/no-undefined-identifier": "error",
"@babel/development/no-deprecated-clone": "error",
"guard-for-in": "error",
"import/extensions": ["error", { json: "always", cjs: "always" }],
"import/extensions": ["error", "ignorePackages"],
},
},
...compat.extends("plugin:jest/recommended").map(config => {
Expand Down
54 changes: 28 additions & 26 deletions lib/third-party-libs.d.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,69 @@
/* eslint-disable import/no-extraneous-dependencies */

declare module "js-tokens" {
// TODO(Babel 8): Remove this
export { default } from "js-tokens-BABEL_8_BREAKING-true";
export * from "js-tokens-BABEL_8_BREAKING-true";
}

declare module "@babel/preset-modules/lib/plugins/transform-async-arrows-in-class" {
import { declare } from "@babel/helper-plugin-utils";
declare module "@babel/preset-modules/lib/plugins/transform-async-arrows-in-class/index.js" {
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "@babel/preset-modules/lib/plugins/transform-edge-default-parameters" {
import { declare } from "@babel/helper-plugin-utils";
declare module "@babel/preset-modules/lib/plugins/transform-edge-default-parameters/index.js" {
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "@babel/preset-modules/lib/plugins/transform-edge-function-name" {
import { declare } from "@babel/helper-plugin-utils";
declare module "@babel/preset-modules/lib/plugins/transform-edge-function-name/index.js" {
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "@babel/preset-modules/lib/plugins/transform-tagged-template-caching" {
import { declare } from "@babel/helper-plugin-utils";
declare module "@babel/preset-modules/lib/plugins/transform-tagged-template-caching/index.js" {
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "@babel/preset-modules/lib/plugins/transform-safari-block-shadowing" {
import { declare } from "@babel/helper-plugin-utils";
declare module "@babel/preset-modules/lib/plugins/transform-safari-block-shadowing/index.js" {
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "@babel/preset-modules/lib/plugins/transform-safari-for-shadowing" {
import { declare } from "@babel/helper-plugin-utils";
declare module "@babel/preset-modules/lib/plugins/transform-safari-for-shadowing/index.js" {
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "babel-plugin-polyfill-corejs2" {
import { declare } from "@babel/helper-plugin-utils";
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "babel-plugin-polyfill-corejs3" {
import { declare } from "@babel/helper-plugin-utils";
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
declare module "babel-plugin-polyfill-regenerator" {
import { declare } from "@babel/helper-plugin-utils";
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}

declare module "regenerator-transform" {
import { declare } from "@babel/helper-plugin-utils";
import type { declare } from "@babel/helper-plugin-utils";
let plugin: ReturnType<typeof declare>;
let exports: {default: typeof plugin};
let exports: { default: typeof plugin };
export = exports;
}
6 changes: 3 additions & 3 deletions packages/babel-cli/src/babel/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import slash from "slash";
import path from "path";
import fs from "fs";

import * as util from "./util";
import * as watcher from "./watcher";
import type { CmdOptions } from "./options";
import * as util from "./util.ts";
import * as watcher from "./watcher.ts";
import type { CmdOptions } from "./options.ts";

const FILE_TYPE = Object.freeze({
NON_COMPILABLE: "NON_COMPILABLE",
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-cli/src/babel/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import slash from "slash";
import path from "path";
import fs from "fs";

import * as util from "./util";
import type { CmdOptions } from "./options";
import * as watcher from "./watcher";
import * as util from "./util.ts";
import type { CmdOptions } from "./options.ts";
import * as watcher from "./watcher.ts";

import type {
EncodedSourceMap,
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-cli/src/babel/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

import parseArgv from "./options";
import dirCommand from "./dir";
import fileCommand from "./file";
import parseArgv from "./options.ts";
import dirCommand from "./dir.ts";
import fileCommand from "./file.ts";

const opts = parseArgv(process.argv);

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-cli/src/babel/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as babel from "@babel/core";
import path from "path";
import fs from "fs";

import * as watcher from "./watcher";
import * as watcher from "./watcher.ts";

import type { FileResult, InputOptions } from "@babel/core";

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/src/config/cache-contexts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Targets } from "@babel/helper-compilation-targets";

import type { ConfigContext } from "./config-chain";
import type { CallerMetadata } from "./validation/options";
import type { ConfigContext } from "./config-chain.ts";
import type { CallerMetadata } from "./validation/options.ts";

export type { ConfigContext as FullConfig };

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/src/config/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
onFirstPause,
waitFor,
isThenable,
} from "../gensync-utils/async";
import { isIterableIterator } from "./util";
} from "../gensync-utils/async.ts";
import { isIterableIterator } from "./util.ts";

export type { CacheConfigurator };

Expand Down
26 changes: 13 additions & 13 deletions packages/babel-core/src/config/config-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import path from "path";
import buildDebug from "debug";
import type { Handler } from "gensync";
import { validate } from "./validation/options";
import { validate } from "./validation/options.ts";
import type {
ValidatedOptions,
IgnoreList,
ConfigApplicableTest,
BabelrcSearch,
CallerMetadata,
IgnoreItem,
} from "./validation/options";
import pathPatternToRegex from "./pattern-to-regex";
import { ConfigPrinter, ChainFormatter } from "./printer";
import type { ReadonlyDeepArray } from "./helpers/deep-array";
} from "./validation/options.ts";
import pathPatternToRegex from "./pattern-to-regex.ts";
import { ConfigPrinter, ChainFormatter } from "./printer.ts";
import type { ReadonlyDeepArray } from "./helpers/deep-array.ts";

import { endHiddenCallStack } from "../errors/rewrite-stack-trace";
import ConfigError from "../errors/config-error";
import type { PluginAPI, PresetAPI } from "./helpers/config-api";
import { endHiddenCallStack } from "../errors/rewrite-stack-trace.ts";
import ConfigError from "../errors/config-error.ts";
import type { PluginAPI, PresetAPI } from "./helpers/config-api.ts";

const debug = buildDebug("babel:config:config-chain");

Expand All @@ -27,20 +27,20 @@ import {
findRelativeConfig,
findRootConfig,
loadConfig,
} from "./files";
import type { ConfigFile, IgnoreFile, FilePackageData } from "./files";
} from "./files/index.ts";
import type { ConfigFile, IgnoreFile, FilePackageData } from "./files/index.ts";

import { makeWeakCacheSync, makeStrongCacheSync } from "./caching";
import { makeWeakCacheSync, makeStrongCacheSync } from "./caching.ts";

import {
createCachedDescriptors,
createUncachedDescriptors,
} from "./config-descriptors";
} from "./config-descriptors.ts";
import type {
UnloadedDescriptor,
OptionsAndDescriptors,
ValidatedFile,
} from "./config-descriptors";
} from "./config-descriptors.ts";

export type ConfigChain = {
plugins: Array<UnloadedDescriptor<PluginAPI>>;
Expand Down
16 changes: 8 additions & 8 deletions packages/babel-core/src/config/config-descriptors.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import gensync, { type Handler } from "gensync";
import { once } from "../gensync-utils/functional";
import { once } from "../gensync-utils/functional.ts";

import { loadPlugin, loadPreset } from "./files";
import { loadPlugin, loadPreset } from "./files/index.ts";

import { getItemDescriptor } from "./item";
import { getItemDescriptor } from "./item.ts";

import {
makeWeakCacheSync,
makeStrongCacheSync,
makeStrongCache,
} from "./caching";
import type { CacheConfigurator } from "./caching";
} from "./caching.ts";
import type { CacheConfigurator } from "./caching.ts";

import type {
ValidatedOptions,
PluginList,
PluginItem,
} from "./validation/options";
} from "./validation/options.ts";

import { resolveBrowserslistConfigFile } from "./resolve-targets";
import type { PluginAPI, PresetAPI } from "./helpers/config-api";
import { resolveBrowserslistConfigFile } from "./resolve-targets.ts";
import type { PluginAPI, PresetAPI } from "./helpers/config-api.ts";

// Represents a config object and functions to lazily load the descriptors
// for the plugins and presets so we don't load the plugins/presets unless
Expand Down
Loading

0 comments on commit 5ebab54

Please sign in to comment.