Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to ESM only #1002

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions .eslintrc
Expand Up @@ -2,7 +2,7 @@
"parser": "@babel/eslint-parser",
"extends": ["eslint:recommended", "eslint-config-prettier"],
"parserOptions": {
"ecmaVersion": 2017,
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
Expand All @@ -12,7 +12,6 @@
"linebreak-style": ["error", "unix"],
"max-len": ["error", 110, 2],
"new-cap": "off",
"no-case-declarations": "error",
"no-cond-assign": "off",
"no-confusing-arrow": "error",
"no-console": "off",
Expand All @@ -28,8 +27,6 @@
"no-underscore-dangle": "off",
"no-unreachable": "off",
"no-use-before-define": "off",
"no-var": "error",
"prefer-const": "error",
"strict": "off",
"prettier/prettier": "error"
},
Expand Down
13 changes: 6 additions & 7 deletions .github/workflows/ci.yml
Expand Up @@ -11,11 +11,11 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [14, 16, 18]
webpack-version: ['5']
node-version: [16, 18, 20]
webpack-version: ["5"]
include:
- node-version: "14.15.0" # The minimum supported node version
webpack-version: latest
- node-version: "16.17.0" # The minimum supported node version
webpack-version: "5.80.0" # The minimum supported webpack version
os: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -24,7 +24,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
cache: "yarn"
- name: Install dependencies
run: yarn
- name: Install webpack ${{ matrix.webpack-version }}
Expand All @@ -47,7 +47,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
cache: "yarn"
- name: Install dependencies
run: yarn
- name: Run lint and coverage tests
Expand All @@ -56,4 +56,3 @@ jobs:
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}

15 changes: 9 additions & 6 deletions babel.config.json
@@ -1,12 +1,15 @@
{
"targets": {
"node": "14.15.0"
"node": "16.17.0"
},
"presets": [
["@babel/preset-env", {
"loose": true,
"bugfixes": true,
"modules": false
}]
[
"@babel/preset-env",
{
"loose": true,
"bugfixes": true,
"modules": false
}
]
]
}
21 changes: 10 additions & 11 deletions package.json
Expand Up @@ -7,23 +7,27 @@
],
"main": "lib/index.js",
"engines": {
"node": ">= 14.15.0"
"node": ">=18 || ^16.17.0"
},
"type": "module",
"exports": {
".": "./lib/index.js",
"./package.json": "./package.json"
},
"dependencies": {
"find-cache-dir": "^4.0.0",
"schema-utils": "^4.0.0"
},
"peerDependencies": {
"@babel/core": "^7.12.0",
"webpack": ">=5"
"webpack": "^5.80.0"
},
"devDependencies": {
"@ava/babel": "^1.0.1",
"@babel/cli": "^7.22.9",
"@babel/core": "^7.22.9",
"@babel/eslint-parser": "^7.22.9",
"@babel/preset-env": "^7.22.9",
"ava": "^3.13.0",
"ava": "^5.3.1",
"babel-plugin-react-intl": "^8.2.25",
"c8": "^8.0.0",
"eslint": "^8.45.0",
Expand All @@ -38,7 +42,7 @@
"react-intl-webpack-plugin": "^0.3.0",
"rimraf": "^5.0.1",
"semver": "7.5.2",
"webpack": "^5.74.0"
"webpack": "^5.88.2"
},
"scripts": {
"clean": "rimraf lib/",
Expand Down Expand Up @@ -89,12 +93,7 @@
"test/**/*.test.js",
"!test/fixtures/**/*",
"!test/helpers/**/*"
],
"babel": {
"compileAsTests": [
"test/helpers/**/*"
]
}
]
},
"lint-staged": {
"scripts/*.js": [
Expand Down
2 changes: 1 addition & 1 deletion src/Error.js
Expand Up @@ -32,4 +32,4 @@ class LoaderError extends Error {
}
}

module.exports = LoaderError;
export default LoaderError;
18 changes: 9 additions & 9 deletions src/cache.js
Expand Up @@ -7,15 +7,15 @@
* @see https://github.com/babel/babel-loader/issues/34
* @see https://github.com/babel/babel-loader/pull/41
*/
const os = require("os");
const path = require("path");
const zlib = require("zlib");
const crypto = require("crypto");
const { promisify } = require("util");
const { readFile, writeFile, mkdir } = require("fs/promises");
import os from "node:os";
import path from "node:path";
import zlib from "node:zlib";
import crypto from "node:crypto";
import { promisify } from "node:util";
import { readFile, writeFile, mkdir } from "node:fs/promises";
const findCacheDirP = import("find-cache-dir");

const transform = require("./transform");
import transform from "./transform.js";
// Lazily instantiated when needed
let defaultCacheDirectory = null;

Expand Down Expand Up @@ -162,7 +162,7 @@ const handleCache = async function (directory, params) {
* });
*/

module.exports = async function (params) {
export default async function cache(params) {
let directory;

if (typeof params.cacheDirectory === "string") {
Expand All @@ -178,4 +178,4 @@ module.exports = async function (params) {
}

return await handleCache(directory, params);
};
}
21 changes: 12 additions & 9 deletions src/index.js
@@ -1,6 +1,6 @@
let babel;
try {
babel = require("@babel/core");
babel = await import("@babel/core");
} catch (err) {
if (err.code === "MODULE_NOT_FOUND") {
err.message +=
Expand All @@ -19,23 +19,26 @@ if (/^6\./.test(babel.version)) {
);
}

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

const { version } = require("../package.json");
const cache = require("./cache");
const transform = require("./transform");
const injectCaller = require("./injectCaller");
const schema = require("./schema");
import cache from "./cache.js";
import transform from "./transform.js";
import injectCaller from "./injectCaller.js";
const schema = require("./schema.json");

const { isAbsolute } = require("path");
const validateOptions = require("schema-utils").validate;
import { isAbsolute } from "node:path";
import { validate as validateOptions } from "schema-utils";

function subscribe(subscriber, metadata, context) {
if (context[subscriber]) {
context[subscriber](metadata);
}
}

module.exports = makeLoader();
module.exports.custom = makeLoader;
export default makeLoader();
export const custom = makeLoader;

function makeLoader(callback) {
const overrides = callback ? callback(babel) : undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/injectCaller.js
@@ -1,6 +1,6 @@
const babel = require("@babel/core");
import babel from "@babel/core";

module.exports = function injectCaller(opts, target) {
export default function injectCaller(opts, target) {
if (!supportsCallerOption()) return opts;

return Object.assign({}, opts, {
Expand All @@ -24,7 +24,7 @@ module.exports = function injectCaller(opts, target) {
opts.caller,
),
});
};
}

// TODO: We can remove this eventually, I'm just adding it so that people have
// a little time to migrate to the newer RCs of @babel/core without getting
Expand Down
15 changes: 6 additions & 9 deletions src/transform.js
@@ -1,13 +1,10 @@
const babel = require("@babel/core");
const { promisify } = require("util");
const LoaderError = require("./Error");
import { transformAsync } from "@babel/core";
import LoaderError from "./Error.js";

const transform = promisify(babel.transform);

module.exports = async function (source, options) {
export default async function (source, options) {
let result;
try {
result = await transform(source, options);
result = await transformAsync(source, options);
} catch (err) {
throw err.message && err.codeFrame ? new LoaderError(err) : err;
}
Expand All @@ -34,6 +31,6 @@ module.exports = async function (source, options) {
// Convert it from a Set to an Array to make it JSON-serializable.
externalDependencies: Array.from(externalDependencies || []),
};
};
}

module.exports.version = babel.version;
export { version } from "@babel/core";