Skip to content

Support for Node 16.12 #6

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

Merged
merged 3 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "important-stuff",
"parser": "@babel/eslint-parser"
"extends": "node-important-stuff",
"parser": "@babel/eslint-parser",
"rules": {
"node/no-unpublished-import": "off"
}
}
15 changes: 10 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
name: Build and Test

on:
- push
- pull_request
push:
branches:
- main
pull_request:
branches:
- "*"

jobs:
build:
Expand All @@ -12,10 +16,11 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- uses: pnpm/action-setup@v1.2.1
node-version: "16.12"
- uses: pnpm/action-setup@v2.0.1
with:
version: 6.9.1
version: 6.20.3
- run: pnpm install --frozen-lockfile
- run: pnpm test
- run: pnpm run check-format
- run: pnpm run lint
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm test
pnpm exec pretty-quick -- --staged && pnpm test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ npm install --save @node-loader/babel
yarn add --save @node-loader/babel
```

If using Node<16.12, use `@node-loader/babel@1`. Otherwise, use `@node-loader/babel@latest`

## Usage

Run node with the `--experimental-loader` flag:
Expand Down
54 changes: 35 additions & 19 deletions lib/node-loader-babel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import babel from "@babel/core";
import path from "path";
import url from "url";
import urlModule from "url";

const { loadOptionsAsync, transformAsync } = babel;

Expand All @@ -17,25 +17,41 @@ function isBabelConfigFile(filename) {
);
}

export async function transformSource(source, context, defaultGetSource) {
const filename = url.fileURLToPath(context.url);
// Babel config files can themselves be ES modules,
// but we cannot transform those since doing so would cause an infinite loop.
if (isBabelConfigFile(filename)) {
return { source };
}
export async function load(url, context, defaultLoad) {
if (useLoader(url)) {
const { source } = await defaultLoad(url, context, defaultLoad);

const filename = urlModule.fileURLToPath(url);
// Babel config files can themselves be ES modules,
// but we cannot transform those since doing so would cause an infinite loop.
if (isBabelConfigFile(filename)) {
return {
source,
format: /\.(c|m)?js$/.test(filename) ? "module" : "json",
};
}

const options = await loadOptionsAsync({
sourceType: "module",
root: process.cwd(),
rootMode: "root",
filename: filename,
configFile: true,
});

const options = await loadOptionsAsync({
sourceType: "module",
root: process.cwd(),
rootMode: "root",
filename: filename,
configFile: true,
});
const transformed = await transformAsync(source, options);

const transformed = await transformAsync(source, options);
return {
source: transformed.code,
// Maybe a shaky assumption
// TODO: look at babel config to see whether it will output ESM/CJS or other formats
format: "module",
};
} else {
return defaultLoad(url, context, defaultLoad);
}
}

return {
source: transformed.code,
};
function useLoader(url) {
return !/node_modules/.test(url) && !/node:/.test(url);
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "module",
"scripts": {
"format": "prettier --write .",
"lint": "prettier --check . && eslint lib",
"lint": "eslint lib",
"check-format": "prettier --check .",
"test": "node --experimental-loader ./lib/node-loader-babel.js ./test/run-tests.js",
"prepare": "husky install"
},
Expand All @@ -28,8 +29,7 @@
"@babel/eslint-parser": "^7.14.7",
"@babel/plugin-transform-react-jsx": "^7.10.4",
"eslint": "^7.30.0",
"eslint-config-important-stuff": "^1.1.0",
"eslint-config-node-important-stuff": "^1.1.0",
"eslint-config-node-important-stuff": "^2.0.0",
"husky": "^7.0.0",
"mocha": "^9.0.2",
"prettier": "^2.1.2",
Expand Down
10 changes: 4 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.