Skip to content

Commit 8173caa

Browse files
committed
adapt /version for ESM
1 parent 132fea9 commit 8173caa

File tree

4 files changed

+120
-10
lines changed

4 files changed

+120
-10
lines changed

package-lock.json

+83
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@babel/plugin-transform-typescript": "7.15.8",
5757
"@babel/preset-env": "7.15.8",
5858
"@babel/register": "7.15.3",
59+
"@types/babel__core": "^7.1.16",
5960
"@types/chai": "4.2.22",
6061
"@types/mocha": "9.0.0",
6162
"@types/node": "16.11.1",

resources/build-npm.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ts = require('typescript');
88
const babel = require('@babel/core');
99
const prettier = require('prettier');
1010

11+
const { getVersionFileBody } = require('./gen-version.js');
1112
const { readdirRecursive, showDirStats } = require('./utils.js');
1213

1314
const prettierConfig = JSON.parse(
@@ -31,7 +32,12 @@ if (require.main === module) {
3132
const destPath = path.join(distDirectory, filepath);
3233

3334
fs.mkdirSync(path.dirname(destPath), { recursive: true });
34-
if (filepath.endsWith('.ts')) {
35+
if (isFullESM && filepath === 'version.ts') {
36+
const js = babelTransform(getVersionFileBody(packageJSON.version), {
37+
envName: 'esm',
38+
});
39+
writeGeneratedFile(destPath.replace(/\.ts$/, '.js'), js);
40+
} else if (filepath.endsWith('.ts')) {
3541
if (isFullESM) {
3642
const js = babelBuild(srcPath, { envName: 'esm' });
3743
writeGeneratedFile(destPath.replace(/\.ts$/, '.js'), js);
@@ -117,6 +123,15 @@ function babelBuild(srcPath, options) {
117123
return code + '\n';
118124
}
119125

126+
function babelTransform(sourceCode, options) {
127+
const { code } = babel.transformSync(sourceCode, {
128+
babelrc: false,
129+
configFile: './.babelrc-npm.json',
130+
...options,
131+
});
132+
return code + '\n';
133+
}
134+
120135
function buildPackageJSON(srcFiles) {
121136
const packageJSON = JSON.parse(
122137
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),

resources/gen-version.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22

33
const fs = require('fs');
44

5-
const { version } = require('../package.json');
6-
7-
const versionMatch = /^(\d+)\.(\d+)\.(\d+)-?(.*)?$/.exec(version);
8-
if (!versionMatch) {
9-
throw new Error('Version does not match semver spec: ' + version);
5+
if (require.main === module) {
6+
fs.writeFileSync('./src/version.ts', getVersionFileBody());
107
}
118

12-
const [, major, minor, patch, preReleaseTag] = versionMatch;
9+
function getVersionFileBody(
10+
/**
11+
* @type {string | undefined}
12+
*/
13+
version,
14+
) {
15+
if (!version) version = require('../package.json').version;
16+
17+
const versionMatch = /^(\d+)\.(\d+)\.(\d+)-?(.*)?$/.exec(version);
18+
if (!versionMatch) {
19+
throw new Error('Version does not match semver spec: ' + version);
20+
}
1321

14-
const body = `
22+
const [, major, minor, patch, preReleaseTag] = versionMatch;
23+
24+
const body = `
1525
// Note: This file is autogenerated using "resources/gen-version.js" script and
1626
// automatically updated by "npm version" command.
1727
@@ -33,6 +43,7 @@ export const versionInfo = Object.freeze({
3343
});
3444
`;
3545

36-
if (require.main === module) {
37-
fs.writeFileSync('./src/version.ts', body.trim() + '\n');
46+
return body.trim() + '\n';
3847
}
48+
49+
exports.getVersionFileBody = getVersionFileBody;

0 commit comments

Comments
 (0)