Skip to content

Commit 1adf0c1

Browse files
🚧 progress(test): Enable power-assert when running tests.
1 parent 238a225 commit 1adf0c1

File tree

5 files changed

+144
-14
lines changed

5 files changed

+144
-14
lines changed

package.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"prepublishOnly": "pinst --disable",
5959
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6060
"test": "npm run test:src",
61-
"test-cmd": "ava",
61+
"test-cmd": "NODE_LOADER_CONFIG=test/loader/config.js ava",
6262
"test:cjs": "IMPORT_MAP_PATH=test/import-maps/dist/index.json npm run test-cmd",
6363
"test:dist": "npm run test:modern && npm run test:module && npm run test:cjs",
6464
"test:modern": "IMPORT_MAP_PATH=test/import-maps/dist/index.modern.json npm run test-cmd",
@@ -72,6 +72,8 @@
7272
"@babel/register": "7.16.0",
7373
"@commitlint/cli": "15.0.0",
7474
"@js-library/commitlint-config": "0.0.4",
75+
"@node-loader/babel": "^2.0.0",
76+
"@node-loader/core": "^2.0.0",
7577
"@node-loader/import-maps": "^1.0.3",
7678
"ava": "^4.0.0-rc.1",
7779
"babel-plugin-transform-remove-console": "6.9.4",
@@ -101,20 +103,12 @@
101103
"regenerator-runtime/runtime"
102104
],
103105
"nodeArguments": [
104-
"--experimental-loader=@node-loader/import-maps"
106+
"--experimental-loader=@node-loader/core"
105107
],
106108
"timeout": "1m"
107109
},
108110
"babel": {
109111
"sourceMaps": true,
110-
"presets": [
111-
[
112-
"@babel/preset-env",
113-
{
114-
"targets": "current node"
115-
}
116-
]
117-
],
118112
"plugins": [
119113
[
120114
"transform-remove-console",
@@ -129,6 +123,7 @@
129123
],
130124
"env": {
131125
"debug": {
126+
"sourceMaps": "both",
132127
"presets": [
133128
"babel-preset-power-assert"
134129
],
@@ -147,6 +142,7 @@
147142
]
148143
},
149144
"test": {
145+
"sourceMaps": "both",
150146
"presets": [
151147
"babel-preset-power-assert"
152148
]
@@ -206,6 +202,7 @@
206202
"unicorn"
207203
],
208204
"rules": {
205+
"unicorn/prefer-node-protocol": "off",
209206
"import/no-anonymous-default-export": "off",
210207
"unicorn/filename-case": [
211208
"error",

src/split.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from 'node:assert';
1+
import assert from 'assert';
22
import fromAsyncIterable from './fromAsyncIterable.js';
33
import exhaust from './exhaust.js';
44

test/loader/babel.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* Based on source of @node-loader/babel
3+
*
4+
* Source: https://github.com/node-loader/node-loader-babel
5+
*
6+
* with the following license:
7+
*
8+
* MIT License
9+
*
10+
* Copyright (c) 2020 node-loaders
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in all
20+
* copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
* SOFTWARE.
29+
*/
30+
31+
import process from 'process';
32+
import path from 'path';
33+
import urlModule from 'url';
34+
import babel from '@babel/core';
35+
36+
const {loadOptionsAsync, transformAsync} = babel;
37+
38+
const isBabelConfigFile = (filename) => {
39+
const basename = path.basename(filename);
40+
return (
41+
basename === '.babelrc.js' ||
42+
basename === '.babelrc.mjs' ||
43+
basename === 'babel.config.js' ||
44+
basename === 'babel.config.mjs' ||
45+
basename === '.babelrc' ||
46+
basename === '.babelrc.cjs' ||
47+
basename === 'babel.config.cjs'
48+
);
49+
};
50+
51+
const getFormatWithCertitude = (filename) => {
52+
return /\.cjs$/.test(filename)
53+
? 'commonjs'
54+
: /\.mjs$/.test(filename)
55+
? 'module'
56+
: undefined;
57+
};
58+
59+
const getFormat = (filename) => {
60+
const packageType = 'module'; // TODO query package.json
61+
const defaultFormat = 'json'; // TODO is this correct as default?
62+
63+
return (
64+
getFormatWithCertitude(filename) ??
65+
(/\.js$/.test(filename) ? packageType : defaultFormat)
66+
);
67+
};
68+
69+
const getSourceType = (filename) => {
70+
// TODO replace with getFormat once getFormat queries package.json
71+
const format = getFormatWithCertitude(filename);
72+
73+
switch (format) {
74+
case 'module':
75+
return 'module';
76+
case 'commonjs':
77+
return 'script';
78+
default:
79+
return 'unambiguous';
80+
}
81+
};
82+
83+
const skip = (url) => {
84+
return /node_modules/.test(url) || /node:/.test(url);
85+
};
86+
87+
export const load = async (url, context, defaultLoad) => {
88+
if (skip(url)) return defaultLoad(url, context, defaultLoad);
89+
90+
const {source} = await defaultLoad(url, context, defaultLoad);
91+
92+
const filename = urlModule.fileURLToPath(url);
93+
// Babel config files can themselves be ES modules,
94+
// but we cannot transform those since doing so would cause an infinite loop.
95+
if (isBabelConfigFile(filename)) {
96+
return {
97+
source,
98+
format: getFormat(filename),
99+
};
100+
}
101+
102+
const options = await loadOptionsAsync({
103+
sourceType: getSourceType(filename),
104+
root: process.cwd(),
105+
rootMode: 'root',
106+
filename,
107+
configFile: true,
108+
});
109+
const transformed = await transformAsync(source, options);
110+
111+
return {
112+
source: transformed.code,
113+
// TODO: look at babel config to see whether it will output ESM/CJS or other formats
114+
format: getFormat(filename),
115+
};
116+
};

test/loader/config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as importMapLoader from '@node-loader/import-maps';
2+
// Import * as babelLoader from "@node-loader/babel";
3+
import * as babelLoader from './babel.js';
4+
5+
export default {
6+
loaders: [importMapLoader, babelLoader],
7+
};

yarn.lock

+13-3
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,16 @@
11741174
resolved "https://registry.yarnpkg.com/@js-library/commitlint-config/-/commitlint-config-0.0.4.tgz#5243a58b0d4ad36999d4b944c7770c50658c68f5"
11751175
integrity sha512-l6un9f0HmIJZswqGe5H/HGBkENpqBQv1b36bdLZLXhZEZBTlm2Chbv3J1hJ2kNG8ogh+EFV5nUYQnL3MefLSDQ==
11761176

1177+
"@node-loader/babel@^2.0.0":
1178+
version "2.0.0"
1179+
resolved "https://registry.yarnpkg.com/@node-loader/babel/-/babel-2.0.0.tgz#59d153d0c27737faae22da9368704a46ea93961a"
1180+
integrity sha512-aEnLPbryL7NCAxMcj8NmRzGID161/2H0hTlhsjtGLuxxuw9w2900CFUmfZEIrw0XkoKOQhHwY4swF/o2WCR3sQ==
1181+
1182+
"@node-loader/core@^2.0.0":
1183+
version "2.0.0"
1184+
resolved "https://registry.yarnpkg.com/@node-loader/core/-/core-2.0.0.tgz#7f5a3a91fe137c8e71c0813f3bbaa0ee136a1308"
1185+
integrity sha512-7wV4qj2/tROXGgoYWmNvObUXbXqSv1yUD31K79Jt1q7NDuqrLz4WjjW+wVoCNgVcl8+YSj9BvLX8KsB/SrSzuw==
1186+
11771187
"@node-loader/import-maps@^1.0.3":
11781188
version "1.0.3"
11791189
resolved "https://registry.yarnpkg.com/@node-loader/import-maps/-/import-maps-1.0.3.tgz#4dff536d5dd4d9ff3510e1d2e0ddaf86630d4299"
@@ -1396,7 +1406,7 @@
13961406
dependencies:
13971407
"@types/node" "*"
13981408

1399-
"@typescript-eslint/eslint-plugin@*":
1409+
"@typescript-eslint/eslint-plugin@^5.4.0":
14001410
version "5.6.0"
14011411
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.6.0.tgz#efd8668b3d6627c46ce722c2afe813928fe120a0"
14021412
integrity sha512-MIbeMy5qfLqtgs1hWd088k1hOuRsN9JrHUPwVVKCD99EOUqScd7SrwoZl4Gso05EAP9w1kvLWUVGJOVpRPkDPA==
@@ -1422,7 +1432,7 @@
14221432
eslint-scope "^5.1.1"
14231433
eslint-utils "^3.0.0"
14241434

1425-
"@typescript-eslint/parser@*":
1435+
"@typescript-eslint/parser@^5.4.0":
14261436
version "5.6.0"
14271437
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.6.0.tgz#11677324659641400d653253c03dcfbed468d199"
14281438
integrity sha512-YVK49NgdUPQ8SpCZaOpiq1kLkYRPMv9U5gcMrywzI8brtwZjr/tG3sZpuHyODt76W/A0SufNjYt9ZOgrC4tLIQ==
@@ -3483,7 +3493,7 @@ eslint-config-prettier@^8.3.0:
34833493
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.3.0.tgz#f7471b20b6fe8a9a9254cc684454202886a2dd7a"
34843494
integrity sha512-BgZuLUSeKzvlL/VUjx/Yb787VQ26RU3gGjA3iiFvdsp/2bMfVIWUVP7tjxtjS0e+HP409cPlPvNkQloz8C91ew==
34853495

3486-
eslint-config-xo-typescript@*:
3496+
eslint-config-xo-typescript@^0.47.1:
34873497
version "0.47.1"
34883498
resolved "https://registry.yarnpkg.com/eslint-config-xo-typescript/-/eslint-config-xo-typescript-0.47.1.tgz#87b5865d8a3428fa26cc8dc3146ef4f712dfed46"
34893499
integrity sha512-BkbzIltZCWp8QLekKJKG8zJ/ZGezD8Z9FaJ+hJ5PrAVUGkIPmxXLLEHCKS3ax7oOqZLYQiG+jyKfQDIEdTQgbg==

0 commit comments

Comments
 (0)