Skip to content

Commit 8beef35

Browse files
committed
feat: Declare CJS, UMD and ES exports in package.json
Support modern Node.js environments. * Expose an ESM module. Loadable in the browsers too. * Add source maps to all exports. Node.js can them today. * Replace modules like `fs-extra` and `rimraf` with new features from `fs`. Prefer built-ins. * Convert tools and tests from CJS to ESM. Needed after inserting `type:module` to `package.json`. * Remove transpilation with `babel`. No need for it with today's Node.js and browsers. * Set up `jest` to use ES with Node.js. Not so simple, but doable. * Replace `eslint` with `denolint`. Much faster. * Replace `terser` with `swc`. Much faster. * Use ES instead of CJS in code examples. Be modern. * Move benchmark dependencies to the `perf` sub-directory. Speed up the CI build. * Simplify build. * Use global `await` in build scripts. BREAKING CHANGE: Newer Node.js required, different command-line parsing, renamed CJS exports. * Node.js 14.8 or newer is required for the `create-plural-data` script. Also supports `type`, `module`, `types` and `exports`. The sources of the library didn't change, which means that the exported modules should still work in Node.js 6 and newer, but the're no tests proving that. * Command-line argument parsing done by a custom code instead of using `commander`. Boolean arguments cannot be joined to groups like `-mpv`. They have to be passed separately, for example: `-m -p -v`. Also, the command-line script requires Node.js with the ESM support, which means >= 14.8. * CJS modules in the `dist` directory end with the file extension `.cjs` instead of `.js`. (ES modules end with `.mjs`.) Usually imported main exports are not affected, because they are imported in both CJS and ES modules using the package name.
1 parent 187b8a8 commit 8beef35

38 files changed

+1895
-5553
lines changed

.babelrc

-10
This file was deleted.

.denolint.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"tags": ["recommended"]
4+
}
5+
}

.eslintrc.ts.yml

-7
This file was deleted.

.eslintrc.yml

-1
This file was deleted.

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Test
2525
run: npm test
2626
- name: Coverage
27-
run: pnpx -y codecov
27+
uses: codecov/codecov-action@v2
2828
- name: Publish
2929
uses: cycjimmy/semantic-release-action@v2
3030
with:

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ dist
44
node_modules
55
src/data.js
66
test/browser
7-
test/typings.test.js

CHANGELOG.md

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
1-
# Changes
1+
## [1.0.2](https://github.com/prantlf/plural-rules/compare/v1.0.1...v1.0.2) (2022-01-28)
2+
3+
### Bug Fixes
4+
5+
* Adapt sources after upgrading the build environment ([9f4021e](https://github.com/prantlf/plural-rules/commit/9f4021e7c7c272a2df7157f105b0638d3ea09eb1))
6+
7+
## [1.0.1](https://github.com/prantlf/plural-rules/compare/v1.0.0...v1.0.1) (2019-09-24)
8+
9+
### Bug Fixes
10+
11+
* Upgrade package dependencies ([bf8bfa6](https://github.com/prantlf/plural-rules/commit/bf8bfa6))
12+
13+
# [1.0.0](https://github.com/prantlf/plural-rules/compare/v0.1.0...v1.0.0) (2019-08-03)
14+
15+
### Features
16+
17+
* Improve the data compression by serialising plural form objects to strings ([858c2a0](https://github.com/prantlf/plural-rules/commit/858c2a0))
18+
19+
### BREAKING CHANGES
20+
21+
* Plural data created by this version of the library cannot be consumed by *older versions*. It would be unusual, if you created data by tis version and fed them to an older version of the library, but nevertheless, be aware of it. If you create data by this version, use them with the same major version or a newer one, as long as it is documented to be feasible.
22+
23+
Plural data created by older versios of this library can still be used with this version. The code is beckwards compatible.
224

325
## 2019-08-03 v1.0.0
426

527
Decrease the size of the packed plural rules and forms
628

7-
## 2019-08-03 v0.1.0
29+
# [0.1.0](https://github.com/prantlf/plural-rules/compare/v0.0.2...v0.1.0) (2019-08-03)
30+
31+
### Bug Fixes
32+
33+
* Transpile the CJS module output for Node.js 6 or newer ([6fb15aa](https://github.com/prantlf/plural-rules/commit/6fb15aa))
34+
35+
### Features
36+
37+
* Upgrade CLDR data to the version 35.1
38+
* Provide both minificated and only cleaned-up UMD scripts for the browser ([460efad](https://github.com/prantlf/plural-rules/commit/460efad))
39+
40+
## [0.0.2](https://github.com/prantlf/plural-rules/compare/v0.0.1...v0.0.2) (2019-06-07)
41+
42+
### Bug Fixes
843

9-
Upgrade CLDR data to the version 35.1
44+
* Upgrade module dependencies ([a76acb4](https://github.com/prantlf/plural-rules/commit/a76acb4))
1045

1146
## 2018-11-05 v0.0.1
1247

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ If you are looking for a smaller and [faster](https://github.com/prantlf/fast-pl
2828
## Synopsis
2929

3030
```js
31-
const { getPluralFormForCardinal } = require('plural-rules')
31+
import { getPluralFormForCardinal } from 'plural-rules'
3232

3333
// Returns index of the plural form for the specified locale and cardinal.
3434
getPluralFormForCardinal('en', 1) // Returns "one"; "1 file"
@@ -81,10 +81,10 @@ $ yarn add plural-rules
8181
Functions are exposed as named exports, for example:
8282

8383
```js
84-
const { getPluralFormForCardinal } = require('plural-rules')
84+
import { getPluralFormForCardinal } from 'plural-rules'
8585
```
8686

87-
You can read more about the [module loading](./docs/API.md#loading) in other environments, like with ES6 or in web browsers. [Usage scenarios](./docs/usage.md#usage-scenarios) demonstrate applications of this library in typical real-world situations. [Design concepts](./docs/design.md#design-concepts) explain the approach to the correct internationalization of messages with cardinals taken by this library. Translators will read about [plural rules for supported languages](./docs/languages.md#supported-languages) to be able to write the right plural forms to language packs. [Data genrator](#./API.md#data-generator) enables customizing the the amount of recognized languages and thus shrink the library size. Finally, the [API reference](./docs/API.md#api-reference) lists all functions with a description of their functionality.
87+
You can read more about the [module loading](./docs/API.md#loading) in other environments, like with ESM or in web browsers. [Usage scenarios](./docs/usage.md#usage-scenarios) demonstrate applications of this library in typical real-world situations. [Design concepts](./docs/design.md#design-concepts) explain the approach to the correct internationalization of messages with cardinals taken by this library. Translators will read about [plural rules for supported languages](./docs/languages.md#supported-languages) to be able to write the right plural forms to language packs. [Data genrator](#./API.md#data-generator) enables customizing the the amount of recognized languages and thus shrink the library size. Finally, the [API reference](./docs/API.md#api-reference) lists all functions with a description of their functionality.
8888

8989
## Contributing
9090

bin/create-plural-data

-62
This file was deleted.

bin/create-plural-data.js

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env node
2+
3+
import { createPluralData } from '../util/data-creator.js'
4+
import { readFileSync } from 'fs'
5+
import { dirname, join } from 'path'
6+
import { fileURLToPath } from 'url'
7+
8+
function usage () {
9+
console.log(`Generates plural rules and forms for selected locales.
10+
11+
Usage: create-plural-data [options] <locale> [<locale> ...]
12+
13+
Options:
14+
-a|--all-locales incudes all available locales
15+
-c|--as-cjs-module format the plural data as a CommonJS module
16+
-d|--as-amd-module format the plural data as an AMD module
17+
-m|--as-module format the plural data as an ES module
18+
-n|--umd-name <name> UMD global export name, if not "pluralData"
19+
-o|--output-file <file> write the plural data to a file
20+
-p|--packed pack the plural rules in plural forms
21+
-u|--as-umd-module format the plural data as an UMD module
22+
-v|--include-version include version of the CLDR source
23+
24+
Plural data are printed on the standard output as JSON by default.
25+
Packed plural data must be used as a single module; not for merging.
26+
27+
Examples:
28+
create-pural-data cs
29+
create-plural-data -m -p -v -o custom-data.js en de cs pl hu ru`)
30+
}
31+
32+
function version() {
33+
const __dirname = dirname(fileURLToPath(import.meta.url))
34+
const { version } = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf8'))
35+
console.log(version)
36+
}
37+
38+
const { argv } = process
39+
let allLocales, asModule, asCjsModule, asAmdModule, asUmdModule, umdName,
40+
packed, includeVersion, outputFile
41+
const locales = []
42+
43+
for (let i = 2, l = argv.length; i < l; ++i) {
44+
const arg = argv[i]
45+
const match = /^(?:-|--)([a-zA-Z][-a-z]*)$/.exec(arg)
46+
if (match) {
47+
switch (match[1]) {
48+
case 'a': case 'all-locales':
49+
allLocales = true
50+
continue
51+
case 'c': case 'as-cjs-module':
52+
asCjsModule = true
53+
continue
54+
case 'd': case 'as-amd-module':
55+
asAmdModule = true
56+
continue
57+
case 'm': case 'as-module':
58+
asModule = true
59+
continue
60+
case 'n': case 'umd-name':
61+
umdName = argv[++i]
62+
continue
63+
case 'o': case 'output-file':
64+
outputFile = argv[++i]
65+
continue
66+
case 'p': case 'packed':
67+
packed = true
68+
continue
69+
case 'u': case 'as-umd-module':
70+
asUmdModule = true
71+
continue
72+
case 'v': case 'include-version':
73+
includeVersion = true
74+
continue
75+
case 'V': case 'version':
76+
version()
77+
process.exit(0)
78+
/* falls through */
79+
case 'h': case 'help':
80+
usage()
81+
process.exit(0)
82+
}
83+
console.error(`Unknown argument: "${arg}".`)
84+
process.exit(1)
85+
}
86+
locales.push(arg)
87+
}
88+
89+
if (!locales.length && !allLocales) {
90+
usage()
91+
process.exit(0)
92+
}
93+
94+
try {
95+
const pluralData = await createPluralData({
96+
locales,
97+
asModule,
98+
asCjsModule,
99+
asAmdModule,
100+
asUmdModule,
101+
umdName,
102+
packed,
103+
includeVersion,
104+
outputFile
105+
})
106+
if (!outputFile) {
107+
console.log(pluralData)
108+
}
109+
} catch (error) {
110+
console.error(error.message)
111+
process.exitCode = 1
112+
}

0 commit comments

Comments
 (0)