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

BREAKING: support of es module #995

Merged
merged 10 commits into from
Oct 5, 2024
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
53 changes: 26 additions & 27 deletions copy-src.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
const mkdir = require('mkdirp');
import fs from 'fs';
import path from 'path';
import {mkdirp as mkdir} from 'mkdirp';
import {fileURLToPath} from 'url';

const readDir = dir => fs.promises.readdir(path.join(__dirname, dir), {
const readDir = dir => fs.promises.readdir(path.join(dirname, dir), {
withFileTypes: true
});

Expand All @@ -27,28 +27,27 @@ const copyIfNewer = async (src, dest) => {
return fs.promises.copyFile(src, dest);
};

(async () => {
let dir = [''], queue = [], pending = [];
queue.push(readDir('src'));
let currentDir, currentContent, nextDir, srcFile, destFile;
while (dir.length) {
currentDir = dir.shift();
currentContent = await queue.shift();
for (let dirent of currentContent) {
if (dirent.isDirectory()) {
nextDir = path.join(currentDir, dirent.name);
dir.push(nextDir);
queue.push(readDir(path.join('src', nextDir)));
} else if (dirent.isFile() && !dirent.name.endsWith('.ts')) {
if (!fs.existsSync(nextDir = path.join(__dirname, 'lib', currentDir))) {
console.debug(new Date().toLocaleString(), 'mkdir', nextDir);
mkdir.mkdirpSync(nextDir);
}
srcFile = path.join(__dirname, 'src', currentDir, dirent.name);
destFile = path.join(nextDir, dirent.name);
pending.push(copyIfNewer(srcFile, destFile));
const dirname = path.dirname(fileURLToPath(import.meta.url));
let dir = [''], queue = [], pending = [];
queue.push(readDir('src'));
let currentDir, currentContent, nextDir, srcFile, destFile;
while (dir.length) {
currentDir = dir.shift();
currentContent = await queue.shift();
for (let dirent of currentContent) {
if (dirent.isDirectory()) {
nextDir = path.join(currentDir, dirent.name);
dir.push(nextDir);
queue.push(readDir(path.join('src', nextDir)));
} else if (dirent.isFile() && !dirent.name.endsWith('.ts')) {
if (!fs.existsSync(nextDir = path.join(dirname, 'lib', currentDir))) {
console.debug(new Date().toLocaleString(), 'mkdir', nextDir);
mkdir.mkdirpSync(nextDir);
}
srcFile = path.join(dirname, 'src', currentDir, dirent.name);
destFile = path.join(nextDir, dirent.name);
pending.push(copyIfNewer(srcFile, destFile));
}
}
return Promise.all(pending);
})();
}
await Promise.all(pending);
19 changes: 5 additions & 14 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
// jest.config.ts
import {createDefaultEsmPreset} from 'ts-jest';

/** @type {import('ts-jest').JestConfigWithTsJest} */
const config = {
extensionsToTreatAsEsm: ['.ts'],
export default {
...createDefaultEsmPreset(),
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
'\\.[jt]sx?$': [
'ts-jest',
{
useESM: true,
},
],
},
};

module.exports = config;
15 changes: 10 additions & 5 deletions package-lock-resolved.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';

// true to remove the resolved property from dependencies
// false to convent the awful registry.npm.taobao.org url to registry.npmjs.org
const remove = process.env.NPM_PACKAGE_LOCK_REMOVE_RESOLVED || false;

const dirname = path.dirname(fileURLToPath(import.meta.url));
// since this is the only file reading, using sync io is acceptable
const packageLock =
JSON.parse(fs.readFileSync(path.join(__dirname, 'package-lock.json'), {
JSON.parse(fs.readFileSync(path.join(dirname, 'package-lock.json'), {
encoding: 'utf8'
}));

processDependency(packageLock);

fs.writeFileSync(path.join(__dirname, 'package-lock.json'),
fs.writeFileSync(path.join(dirname, 'package-lock.json'),
// to make github dependabot happy
JSON.stringify(packageLock, null, 2) + '\n');

Expand Down Expand Up @@ -94,6 +96,9 @@ function processDependency(dependency) {
if (dependency.dependencies) {
processDependencies(dependency.dependencies);
}
if (dependency.packages) {
processDependencies(dependency.packages);
}
}

function processDependencies(dependencies) {
Expand Down
Loading