Skip to content

Commit cc2994c

Browse files
committed
feat: enhance UMD module support with legacy version and update build scripts
1 parent 6261f24 commit cc2994c

File tree

10 files changed

+114
-82
lines changed

10 files changed

+114
-82
lines changed

.fatherrc.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { defineConfig } from 'father';
22

3+
const legacy = process.env.LEGACY === '1';
34
export default defineConfig({
4-
esm: { input: 'src', output: 'es', transformer: 'babel' },
5-
umd: { entry: 'src', output: 'umd' },
5+
esm: { input: 'src', output: legacy ? 'es-legacy' : 'es', transformer: 'babel' },
6+
umd: {
7+
entry: 'src',
8+
name: 'EnumPlus',
9+
output: {
10+
path: 'umd',
11+
filename: legacy ? 'enum-plus-legacy.min' : 'enum-plus.min',
12+
},
13+
},
614
sourcemap: true,
7-
targets: { chrome: 80 },
8-
plugins: ['./scripts/plugin-legacy-mode.ts'],
15+
targets: legacy ? { ie: 11 } /* ES2015 */ : { chrome: 80 } /* ES2020 */,
916
});
File renamed without changes.

.github/workflows/npm-publish.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,35 @@ jobs:
4343
run: |
4444
cp -r umd/ umd-zip/
4545
mv umd-zip/enum-plus.min.js umd-zip/enum-plus.umd.min.js
46+
mv umd-zip/enum-plus-legacy.min.js umd-zip/enum-plus-legacy.umd.min.js
4647
gzip -9 umd-zip/enum-plus.umd.min.js
48+
gzip -9 umd-zip/enum-plus-legacy.umd.min.js
4749
cp -r umd/ umd-zip-full/
48-
cd umd-zip-full
49-
tar -cvf ../enum-plus.umd.tar.gz *
50-
cd ..
50+
mkdir -p umd-zip-full/modern
51+
mkdir -p umd-zip-full/legacy
52+
mv umd-zip-full/enum-plus.min.js* umd-zip-full/modern
53+
mv umd-zip-full/enum-plus-legacy.min.js* umd-zip-full/legacy
54+
cd umd-zip-full/modern
55+
tar -cvf ../../enum-plus.umd.tar.gz *
56+
cd ../legacy
57+
tar -cvf ../../enum-plus-legacy.umd.tar.gz *
58+
cd ../..
5159
5260
- name: Add Assets to Release
5361
uses: softprops/action-gh-release@v1
5462
with:
5563
files: |
5664
umd-zip/enum-plus.umd.min.js.gz
65+
umd-zip/enum-plus-legacy.umd.min.js.gz
5766
enum-plus.umd.tar.gz
67+
enum-plus-legacy.umd.tar.gz
5868
token: ${{ secrets.GITHUB_TOKEN }}
5969

6070
- name: Replace relative image url to cdn.jsdelivr.net
6171
uses: ./.github/actions/replace-img-url
6272

6373
- name: Replace download url to the releasing version
64-
uses: ./.github/actions/replace-url
74+
uses: ./.github/actions/replace-path-url
6575
with:
6676
version: ${{ github.event.release.tag_name }}
6777

@@ -125,7 +135,7 @@ jobs:
125135
126136
- name: Replace download url to the NPM latest version
127137
if: github.event.release.prerelease == false
128-
uses: ./.github/actions/replace-url
138+
uses: ./.github/actions/replace-path-url
129139
with:
130140
version: ${{ steps.get_latest_version_after_publish.outputs.LATEST_VERSION }}
131141

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
- 🔥 introduce an official plugin `enum-plus-plugin` that provides a collection of highly shareable plugins.
1515
- These plugins are designed to work in all scenarios and environments (including browser and Node.js), without introducing dependencies on third-party frameworks, component libraries, or platforms.
1616
- If you have a good idea that might be specific to a certain framework or platform rather than universally applicable, we recommend creating a new plugin project (e.g., `enum-plus-plugin-xxx`). We are happy to see that, and hope that you submit a PR to `enum-plus-plugin` to link your project back.
17-
- ✨ Includes the `UMD` module format in this release, which allows you to use `enum-plus` in a browser environment without the need for a module bundler. This is particularly useful for quick prototyping or when working with legacy codebases that do not support modern module systems. You can find them in the `umd` directory of the package. They can also be downloaded in [github release](https://github.com/shijistar/enum-plus/releases) assets.
17+
- ✨ Add the `UMD` module format in this release, which allows you to use `enum-plus` in a browser environment without the need for a module bundler. Two versions are provided: `enum-plus.min.js` (ES2020) for modern browsers and `enum-plus-legacy.min.js` (ES2015) for legacy browsers.
18+
. This is particularly useful for quick prototyping or when working with legacy codebases that do not support modern module systems. You can find them in the `umd` directory of the package. They can also be downloaded in [github release](https://github.com/shijistar/enum-plus/releases) assets.
1819

1920
### Breaking Changes
2021

@@ -26,6 +27,10 @@
2627
- `enums.valuesEnum`
2728
- Please use the new methods `Enum.items`, `Enum.toSelect`, `Enum.toMenu`, `Enum.toFilter`, and `Enum.toValueMap` instead, which are introduced since `v2.1.0`.
2829

30+
### Bug Fixes
31+
32+
- 🐞 Fix sourcemap resolution issues in CommonJS mode
33+
2934
## 2.3.0
3035

3136
2025-5-23

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
[![npm downloads](https://img.shields.io/npm/dm/enum-plus.svg?color=007ec6&cacheSeconds=86400)](https://www.npmjs.com/package/enum-plus)
2020
![GitHub License](https://img.shields.io/github/license/shijistar/enum-plus?label=License&color=%23F68F1E&cacheSeconds=86400)
2121

22-
⬇️  [Introduction](#introduction) | [Features](#features) | [Installation](#installation) | [Enum Initialization](#enum-initialization) | [API](#api) | [Usage](#usage) | [Naming Conventions](#naming-convention-best-practices) | [Localization](#localization) | [Extensibility](#extensibility) | [Q&A](#qa)  ⬇️
22+
⬇️  [Introduction](#introduction) | [Features](#features) | [Installation](#installation) | [Enum Initialization](#enum-initialization) | [API](#api) | [Usage](#usage) | [Naming Conventions](#naming-convention-best-practices) | [Localization](#localization) | [Extensibility](#extensibility) | [Compatibility](#compatibility) | [Q&A](#qa)  ⬇️
2323

2424
## Introduction
2525

@@ -78,19 +78,27 @@ yarn add enum-plus
7878
- The specific version:
7979

8080
```html
81+
<!-- ES2020 modern version -->
8182
<script src="https://cdn.jsdelivr.net/npm/enum-plus@v3.0.0/umd/enum-plus.min.js"></script>
83+
<!-- ES2015 legacy version -->
84+
<script src="https://cdn.jsdelivr.net/npm/enum-plus@v3.0.0/umd/enum-plus-legacy.min.js"></script>
8285
```
8386

8487
- The latest version:
8588

8689
```html
90+
<!-- ES2020 modern version -->
8791
<script src="https://cdn.jsdelivr.net/npm/enum-plus/umd/enum-plus.min.js"></script>
92+
<!-- ES2015 legacy version -->
93+
<script src="https://cdn.jsdelivr.net/npm/enum-plus/umd/enum-plus-legacy.min.js"></script>
8894
```
8995

9096
⬇️ **Download**:
9197

92-
- [enum-plus.umd.min.js.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus.umd.min.js.gz) (~2kB gzipped)
98+
- [enum-plus.umd.min.js.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus.umd.min.js.gz)
9399
- [enum-plus.umd.tar.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus.umd.tar.gz) (Full package with sourcemap)
100+
- [enum-plus-legacy.umd.min.js.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus-legacy.umd.min.js.gz)
101+
- [enum-plus-legacy.umd.tar.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus-legacy.umd.tar.gz) (Full package with sourcemap)
94102

95103
> You can also download them in [github release](https://github.com/shijistar/enum-plus/releases) assets
96104
@@ -912,19 +920,27 @@ If you want to provide more friendly type hints in the extension methods, you ma
912920
913921
## Compatibility
914922
915-
- **Browser Environments**:
923+
enum-plus is designed to be compatible with a wide range of environments, including modern browsers, Node.js, and various build tools. Below are the compatibility details for different environments:
916924
917-
- **Modern Bundlers**: With bundlers supporting the [exports](https://nodejs.org/api/packages.html#exports-sugar) field (Webpack 5+, Vite, Rollup), enum-plus targets `ES2020`. For broader browser support, transpile to earlier syntax using `@babel/preset-env` during your build process.
925+
### Browser Environments
918926
919-
- **Legacy Bundlers**: For tools without `exports` field support (like Webpack 4), enum-plus automatically falls back to the `main` field entry point, which targets `ES2016`.
927+
- **Modern Bundlers**: For bundlers supporting the [exports](https://nodejs.org/api/packages.html#exports-sugar) field (such as webpack 5+, vite, rollup), `es` directory is imported, which targets `ES2020`. For broader browser support, you can transpile to earlier syntax using `@babel/preset-env` during your build process.
920928
921-
- **Polyfill Strategy**: enum-plus ships without polyfills to minimize bundle size. For legacy browser support, incorporate:
929+
- **Legacy Bundlers**: For bundlers without [exports](https://nodejs.org/api/packages.html#exports-sugar) field support (like Webpack 4), this library automatically falls back to the `main` field entry point, and `es-legacy` directory is imported, which targets `ES2015`.
922930
923-
- `core-js`
924-
- `@babel/preset-env` with appropriate `useBuiltIns` settings
925-
- Alternative polyfill implementations
931+
- **UMD Version**: For direct browser usage or static projects without bundlers, enum-plus provides UMD version that can be included via a `<script>` tag. The `umd` directory contains two versions:
932+
- `enum-plus.min.js`: Targets **`ES2020`**, suitable for modern browsers.
933+
- `enum-plus-legacy.min.js`: Targets **`ES2015`**, suitable for older browsers.
926934
927-
- **Node.js Compatibility**: enum-plus requires a minimum of `ES2016` features, compatible with Node.js `v7.x` and above.
935+
> **Polyfill Strategy**: enum-plus ships without polyfills to minimize bundle size. For legacy browser support, you can include the following tools as needed:
936+
>
937+
> - `core-js`
938+
> - `@babel/preset-env` with appropriate `useBuiltIns` settings
939+
> - Alternative polyfill implementations
940+
941+
### **Node.js Environments**
942+
943+
enum-plus requires a minimum of `ES2016` features, compatible with Node.js `v7.x` and above
928944
929945
---
930946

README.zh-CN.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,29 @@ yarn add enum-plus
7878
- 特定版本:
7979

8080
```html
81+
<!-- ES2020 现代版本 -->
8182
<script src="https://cdn.jsdelivr.net/npm/enum-plus@v3.0.0/umd/enum-plus.min.js"></script>
83+
<!-- ES2015 兼容版本 -->
84+
<script src="https://cdn.jsdelivr.net/npm/enum-plus@v3.0.0/umd/enum-plus-legacy.min.js"></script>
8285
```
8386

8487
- 最新版本:
8588

8689
```html
90+
<!-- ES2020 现代版本 -->
8791
<script src="https://cdn.jsdelivr.net/npm/enum-plus/umd/enum-plus.min.js"></script>
92+
<!-- ES2015 兼容版本 -->
93+
<script src="https://cdn.jsdelivr.net/npm/enum-plus/umd/enum-plus-legacy.min.js"></script>
8894
```
8995

9096
⬇️ **下载文件**:
9197

92-
- [enum-plus.umd.min.js.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus.umd.min.js.gz) (~2kB gzipped)
98+
- [enum-plus.umd.min.js.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus.umd.min.js.gz)
9399
- [enum-plus.umd.tar.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus.umd.tar.gz) (完整包,包含 sourcemap)
100+
- [enum-plus-legacy.umd.min.js.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus-legacy.umd.min.js.gz)
101+
- [enum-plus-legacy.umd.tar.gz](https://github.com/shijistar/enum-plus/releases/download/v3.0.0/enum-plus-legacy.umd.tar.gz) (完整包,包含 sourcemap)
94102

95-
> 你也可以去 [Github 发布](https://github.com/shijistar/enum-plus/releases) 中下载这些文件
103+
> 你也可以在 [Github 发布](https://github.com/shijistar/enum-plus/releases) 中下载这些文件
96104
97105
## 枚举定义
98106

@@ -910,22 +918,28 @@ declare global {
910918

911919
## 兼容性
912920

913-
enum-plus 提供了完善的兼容性支持。
921+
enum-plus 设计之初就考虑了广泛的兼容性需求,可无缝运行于各类环境,包括现代浏览器、Node.js 以及多种构建工具。下面详细说明各环境的兼容性情况:
914922

915-
- **浏览器环境**
923+
### 浏览器环境
916924

917-
- **现代打包工具**:对于支持 [exports](https://nodejs.org/api/packages.html#exports-sugar) 字段的打包工具(如 Webpack 5+、Vite、Rollup),enum-plus 的目标是 **`ES2020`**。如果需要更广泛的浏览器支持,可以在构建过程中使用 `@babel/preset-env` 转译为更早期的语法。
925+
- **现代打包工具**:对于支持 [exports](https://nodejs.org/api/packages.html#exports-sugar) 字段的打包工具(如 webpack 5+、vite、rollup),引入的是`es`目录,对应的 EcmaScript 版本是 **`ES2020`**。如果需要更广泛的浏览器支持,可以在构建过程中使用 `@babel/preset-env` 转译为更早期的语法。
918926

919-
- **旧版打包工具**:对于不支持 `exports` 字段的工具(如 Webpack 4),enum-plus 会自动回退到 `main` 字段的入口点,其目标是 **`ES2016`**
927+
- **旧版打包工具**:对于不支持 [exports](https://nodejs.org/api/packages.html#exports-sugar) 字段的工具(如 webpack 4),enum-plus 会自动回退到 `main` 字段的入口点,引入的是`es-legacy`目录,对应的 EcmaScript 版本是 **`ES2015`**。
920928

921-
- **Polyfill 策略**为了最小化包的体积,enum-plus 不包含任何 polyfill。如果需要支持旧版浏览器,可以引入以下内容
929+
- **UMD版本**为了方便在浏览器中直接使用,或者方便在没有打包工具的静态项目中使用,enum-plus 还提供了 UMD 版本,可以通过 `<script>` 标签引入。`umd` 目录下提供了两种版本
922930

923-
- `core-js`
924-
- 配置适当的 `@babel/preset-env``useBuiltIns` 设置
925-
- 其他替代的 polyfill 实现
926-
-
931+
- `enum-plus.min.js`:对应的 EcmaScript 版本是 **`ES2020`**,适用于现代浏览器
932+
- `enum-plus-legacy.min.js`:对应的 EcmaScript 版本是 **`ES2015`**,适用于旧版浏览器
927933

928-
- **Node.js 兼容性**enum-plus 需要至少 **`ES2016`** 的特性,兼容 Node.js `v7.x` 及以上版本。
934+
> **Polyfill 策略**:为了最小化包的体积,enum-plus 不包含任何 polyfill。如果需要支持旧版浏览器,可以自行引入以下工具:
935+
>
936+
> - `core-js`
937+
> - 配置适当的 `@babel/preset-env``useBuiltIns` 设置
938+
> - 其他替代的 polyfill 实现
939+
940+
### Node.js 环境
941+
942+
Node.js 环境下,enum-plus 提供的 EcmaScript 版本是 **`ES2016`**,可以兼容 Node.js `v7.x` 及以上版本
929943

930944
---
931945

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "enum-plus",
3-
"version": "3.0.0",
3+
"version": "3.0.0-alpha.0",
44
"description": "A drop-in replacement for native enum. Like native enum but much better!",
55
"keywords": [
66
"enum",
@@ -87,8 +87,8 @@
8787
],
8888
"scripts": {
8989
"build": "run-p build:*",
90-
"build:es": "run-s task:build-es task:add-umd-header",
91-
"build:es-legacy": "cross-env LEGACY=1 father build",
90+
"build:es": "run-s task:build-es task:add-umd-banner",
91+
"build:es-legacy": "cross-env LEGACY=1 run-s task:build-es-legacy task:add-umd-banner",
9292
"build:lib": "run-s ts2lib task:copy-dts task:copy-lib",
9393
"e2e": "run-s task:bundle-e2e task:run-e2e",
9494
"e2e:debug": "run-s task:bundle-e2e task:run-e2e-debug",
@@ -97,8 +97,9 @@
9797
"prepare-legacy-node13": "node ./scripts/prepare-legacy.js node13",
9898
"prepare-legacy-node15": "node ./scripts/prepare-legacy.js node15",
9999
"prepublishOnly": "run-s build test",
100-
"task:add-umd-header": "tsx scripts/add-umd-header-comments.ts",
100+
"task:add-umd-banner": "tsx scripts/add-umd-banner.ts",
101101
"task:build-es": "father build",
102+
"task:build-es-legacy": "father build",
102103
"task:bundle-e2e": "tsx scripts/make-e2e-bundle.ts",
103104
"task:copy-dts": "shx cp ./src/*.d.ts ./lib",
104105
"task:copy-lib": "tsx scripts/copy-lib.ts",

scripts/add-umd-banner.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { existsSync, readFileSync, writeFileSync } from 'fs';
2+
import packageJson from '../package.json';
3+
4+
const headerComment = (legacy: boolean) => `/**
5+
* ${packageJson.name} (${legacy ? 'ES2015' : 'ES2020'} version)
6+
* ${packageJson.description}
7+
*
8+
* @version: ${packageJson.version}
9+
* @author: ${packageJson.author}
10+
* @link ${packageJson.homepage}
11+
*/`;
12+
13+
if (process.env.LEGACY === '1') {
14+
const legacyFilePath = './umd/enum-plus-legacy.min.js';
15+
if (existsSync(legacyFilePath)) {
16+
const legacyJsContent = readFileSync(legacyFilePath, 'utf8');
17+
writeFileSync(legacyFilePath, `${headerComment(true)}\n${legacyJsContent}`);
18+
}
19+
} else {
20+
const filePath = './umd/enum-plus.min.js';
21+
if (existsSync(filePath)) {
22+
const umdJsContent = readFileSync(filePath, 'utf8');
23+
writeFileSync(filePath, `${headerComment(false)}\n${umdJsContent}`);
24+
}
25+
}

scripts/add-umd-header-comments.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/plugin-legacy-mode.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)