Skip to content

Commit 6f39d93

Browse files
committed
Create an ESM version of the package
1 parent 2c00258 commit 6f39d93

18 files changed

+339
-5223
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ node_modules/
22
dist/
33
.DS_Store
44
coverage/
5-
/@types/
6-
/constants/
7-
/data/
8-
/parsers/
9-
/utilities/
5+
/esm/
106
/index.d.ts
117
/index.js
128
/options.d.ts

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [3.5.4] - 2022-03-26
4+
5+
- Build the package bundle using rollup and created an ESM version of the package
6+
37
## [3.5.3] - 2022-03-11
48

59
- Fixed a bug that was removing rules without declarations but with atRules

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ const { postcssRTLCSS, Mode, Source, Autorename } = require('postcss-rtlcss');
6464
```javascript
6565
import postcss from 'postcss';
6666
import postcssRTLCSS from 'postcss-rtlcss';
67-
import postcssRTLCSSOptions from 'postcss-rtlcss/options';
68-
69-
const { Mode, Source, Autorename } = postcssRTLCSSOptions;
67+
import { Mode, Source, Autorename } from 'postcss-rtlcss/options';
7068

7169
const options = { ... available options ... };
7270
const result = postcss([

config.replace.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
files: 'index.d.ts',
3+
from: /declare namespace[^}]*\}[^}]*\};/g,
4+
to: '\nexport = postcssRTLCSS',
5+
};

package.esm.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

package.json

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@
1414
"rtlcss"
1515
],
1616
"main": "index.js",
17+
"module": "esm/index.js",
1718
"types": "index.d.ts",
19+
"exports": {
20+
".": {
21+
"require": "./index.js",
22+
"import": "./esm/index.js"
23+
},
24+
"./options": {
25+
"require": "./options.js",
26+
"import": "./esm/options.js"
27+
}
28+
},
1829
"files": [
19-
"@types/**/*",
20-
"constants/**/*",
21-
"data/**/*",
22-
"parsers/**/*",
23-
"utilities/**/*",
30+
"esm/**/*",
2431
"index.d.ts",
2532
"index.js",
2633
"options.d.ts",
@@ -29,8 +36,11 @@
2936
"scripts": {
3037
"test": "jest --clearCache && jest --verbose",
3138
"lint": "eslint src/**/*.ts",
32-
"copy": "cp -r ./dist/. ./",
33-
"build": "webpack && tsconfig-replace-paths -p tsconfig.json -s ./src -o ./dist && yarn copy",
39+
"clean": "./scripts/clean.sh",
40+
"copy": "./scripts/copy.sh",
41+
"build-dts": "rollup --config rollup.dts.config.js",
42+
"modify-dts": "replace-in-file --configFile=config.replace.js",
43+
"build": "yarn clean && rollup --config rollup.config.js && yarn copy && yarn build-dts && yarn modify-dts",
3444
"prepare": "yarn build && yarn copy",
3545
"prepublishOnly": "npm run lint && npm run test",
3646
"version": "git add .",
@@ -46,24 +56,27 @@
4656
"rtlcss": "^3.5.0"
4757
},
4858
"devDependencies": {
59+
"@rollup/plugin-json": "^4.1.0",
60+
"@rollup/plugin-typescript": "^8.3.1",
4961
"@types/eslint": "^8.2.2",
5062
"@types/jest": "^27.4.0",
5163
"@types/node": "^17.0.10",
5264
"@types/rtlcss": "^3.1.2",
5365
"@typescript-eslint/eslint-plugin": "^4.31.1",
5466
"@typescript-eslint/parser": "^4.31.1",
55-
"clean-webpack-plugin": "^4.0.0",
5667
"coveralls": "^3.1.1",
5768
"eslint": "^7.32.0",
5869
"eslint-plugin-jest": "^24.4.2",
5970
"jest": "^27.4.7",
6071
"postcss": "^8.3.11",
72+
"replace-in-file": "^6.3.2",
73+
"rollup": "^2.70.1",
74+
"rollup-plugin-dts": "^4.2.0",
75+
"rollup-plugin-terser": "^7.0.2",
6176
"ts-jest": "^27.1.3",
6277
"ts-loader": "^9.2.6",
63-
"tsconfig-replace-paths": "^0.0.11",
64-
"typescript": "4.4.4",
65-
"webpack": "^5.66.0",
66-
"webpack-cli": "^4.9.1"
78+
"tslib": "^2.3.1",
79+
"typescript": "4.4.4"
6780
},
6881
"peerDependencies": {
6982
"postcss": "^8.0.0"

rollup.config.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
import json from '@rollup/plugin-json';
3+
import { terser } from "rollup-plugin-terser";
4+
5+
const getPlugins = (includeJson) => {
6+
const plugins = includeJson
7+
? [
8+
json()
9+
]
10+
: [];
11+
return [
12+
...plugins,
13+
typescript({
14+
tsconfig: './tsconfig.json',
15+
declaration: true,
16+
outDir: './'
17+
}),
18+
terser({
19+
output: {
20+
comments: false
21+
}
22+
})
23+
];
24+
};
25+
26+
const getConfig = (name, defaults = true) => ({
27+
input: `src/${name}.ts`,
28+
external: [
29+
'postcss',
30+
'rtlcss'
31+
],
32+
output: [
33+
{
34+
file: `dist/${name}.js`,
35+
format: 'cjs',
36+
exports: defaults ? 'default' : 'named'
37+
},
38+
{
39+
file: `dist/esm/${name}.js`,
40+
format: 'es',
41+
exports: defaults ? 'default' : 'named'
42+
}
43+
]
44+
});
45+
46+
export default [
47+
{
48+
plugins: getPlugins(true),
49+
...getConfig('index')
50+
},
51+
{
52+
plugins: getPlugins(),
53+
...getConfig('options', false)
54+
}
55+
];

rollup.dts.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import dts from 'rollup-plugin-dts';
2+
import tsconfig from './tsconfig.json';
3+
4+
const getConfig = (name) => ({
5+
plugins: [
6+
dts({
7+
compilerOptions: {
8+
baseUrl: './dist',
9+
paths: tsconfig.compilerOptions.paths,
10+
}
11+
})
12+
],
13+
input: `dist/${name}.d.ts`,
14+
output: [
15+
{
16+
file: `./${name}.d.ts`
17+
}
18+
]
19+
});
20+
21+
export default [
22+
getConfig('index'),
23+
getConfig('options')
24+
];

scripts/clean.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#! /bin/sh
2+
3+
RIMRAF="./node_modules/rimraf/bin.js"
4+
5+
$RIMRAF dist/
6+
$RIMRAF esm/
7+
$RIMRAF index.js
8+
$RIMRAF index.d.ts
9+
$RIMRAF options.js
10+
$RIMRAF options.d.ts

scripts/copy.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#! /bin/sh
2+
3+
mkdir esm
4+
5+
## index
6+
cp dist/index.js index.js
7+
cp dist/esm/index.js esm/index.js
8+
9+
# options
10+
cp dist/options.js options.js
11+
cp dist/esm/options.js esm/options.js
12+
13+
# esm package
14+
cp package.esm.json esm/package.json

0 commit comments

Comments
 (0)