Skip to content

Commit cff4648

Browse files
committed
Convert to ES modules and modernize codebase
- Migrate from CommonJS require/module.exports to ES import/export syntax - Update package.json to use "type": "module" and require Node.js >=18 - Add Babel configuration for Jest testing with ES modules - Update GitHub Actions workflow to test Node.js 18, 20, 22 - Modernize Node.js imports to use node: protocol - Update version to 2.0.0 reflecting breaking changes
1 parent d005d27 commit cff4648

File tree

14 files changed

+62
-66
lines changed

14 files changed

+62
-66
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env"]
3+
}

.github/workflows/workflow.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ jobs:
77
test:
88
name: Run tests and collect coverage
99
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version: [18, 20, 22]
1013
steps:
1114
- name: Checkout
1215
uses: actions/checkout@v4
1316
with:
1417
fetch-depth: 2
1518

16-
- name: Set up Node
19+
- name: Set up Node ${{ matrix.node-version }}
1720
uses: actions/setup-node@v4
21+
with:
22+
node-version: ${{ matrix.node-version }}
1823

1924
- name: Install dependencies
2025
run: npm install

index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import Layout from './lib/layout.js';
2+
import helpers from './lib/helper.js';
3+
import * as utils from './lib/utils.js';
24

3-
const Layout = require('./lib/layout');
4-
const helpers = require('./lib/helper');
5-
const utils = require('./lib/utils');
6-
7-
const VERSION = '1.3.8';
5+
const VERSION = '2.0.0';
86

97
const TEMPLATE_OUT = '__templateOut__';
108
const TEMPLATE_VAR_NAME = '__templateVarName__';
@@ -284,9 +282,9 @@ const core = {
284282

285283
};
286284

287-
288-
module.exports = Object.assign({
285+
export default {
286+
...core,
289287
getInstance() {
290-
return Object.assign({}, core);
288+
return { ...core };
291289
}
292-
}, core);
290+
};

jest.config.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
module.exports = {
1+
export default {
22
testEnvironment: 'node',
3+
transform: {
4+
'\\.[jt]sx?$': 'babel-jest'
5+
},
36
collectCoverageFrom: [
47
'index.js',
58
'lib/**/*.js',
69
'!node_modules/**'
710
],
811
coverageDirectory: 'coverage',
9-
coverageReporters: ['text', 'lcov', 'html'],
1012
testMatch: [
1113
'<rootDir>/test/**/*.test.js'
1214
],
13-
testPathIgnorePatterns: [
14-
'/node_modules/'
15-
],
16-
moduleFileExtensions: ['js', 'json'],
1715
verbose: true
18-
};
16+
};

lib/helper.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
// 辅助函数
1+
// Helper functions
42
const helpers = {
53
// run wrapper
64
run(func) {
@@ -90,4 +88,4 @@ const helpers = {
9088
}
9189
};
9290

93-
module.exports = helpers;
91+
export default helpers;

lib/layout.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
1+
import os from 'node:os';
2+
import fs from 'node:fs';
3+
import fsPromises from 'node:fs/promises';
4+
import path from 'node:path';
25

3-
const os = require('os');
4-
const fs = require('fs');
5-
const fsPromises = require('fs').promises;
6-
const path = require('path');
7-
8-
const lockfile = require('./lockfile');
9-
const utils = require('./utils');
6+
import * as lockfile from './lockfile.js';
7+
import * as utils from './utils.js';
108

119
// Template layout class
1210
class Layout {
@@ -554,4 +552,4 @@ class Layout {
554552

555553
};
556554

557-
module.exports = Layout;
555+
export default Layout;

lib/lockfile.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const fs = require('fs');
1+
import fs from 'node:fs';
42

53
// Lock timeout duration
64
const lockTimeout = 1000 * 60 * 5;
@@ -121,7 +119,7 @@ const isLocked = (file, callback) => {
121119
};
122120

123121

124-
module.exports = {
122+
export {
125123
lock,
126124
unlock,
127125
isLocked

lib/utils.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict';
2-
3-
const crypto = require('crypto');
4-
const fs = require('fs');
5-
const path = require('path');
1+
import crypto from 'node:crypto';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
64

75
const _0777 = parseInt('0777', 8);
86

@@ -92,7 +90,7 @@ const mkdirp = (p, callback, opts, made) => {
9290
});
9391
};
9492

95-
module.exports = {
93+
export {
9694
getHash,
9795
encodeReg,
9896
fileExists,

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
"name": "cb-template",
33
"version": "2.0.0",
44
"description": "Changba Template Engine",
5+
"type": "module",
56
"main": "index.js",
67
"types": "index.d.ts",
8+
"engines": {
9+
"node": ">=18.0.0"
10+
},
711
"files": [
812
"index.js",
913
"index.d.ts",
@@ -38,7 +42,9 @@
3842
},
3943
"homepage": "https://github.com/ChangbaFE/cbT#readme",
4044
"devDependencies": {
41-
"eslint": "^6.8.0",
45+
"@babel/preset-env": "^7.28.0",
46+
"babel-jest": "^30.0.4",
47+
"eslint": "^8.57.1",
4248
"jest": "^30.0.4",
4349
"typescript": "^5.8.3"
4450
}

test/helper.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const helpers = require('../lib/helper');
1+
import helpers from '../lib/helper.js';
42

53
describe('helper.js', () => {
64
describe('run', () => {

0 commit comments

Comments
 (0)