Skip to content

Commit

Permalink
Create an ESM version of the package
Browse files Browse the repository at this point in the history
  • Loading branch information
elchininet committed Mar 26, 2022
1 parent 89a20bf commit 6d29e3f
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 5,220 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ node_modules/
dist/
.DS_Store
coverage/
/@types/
/constants/
/data/
/parsers/
/utilities/
/esm/
/index.d.ts
/index.js
/options.d.ts
Expand Down
5 changes: 5 additions & 0 deletions config.replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
files: 'index.d.ts,esm/index.d.ts',
from: /declare namespace[^}]*\}[^}]*\};/g,
to: '\nexport = postcssRTLCSS',
};
3 changes: 3 additions & 0 deletions package.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
37 changes: 25 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
"rtlcss"
],
"main": "index.js",
"module": "esm/index.js",
"types": "index.d.ts",
"exports": {
".": {
"require": "./index.js",
"import": "./esm/index.js"
},
"./options": {
"require": "./options.js",
"import": "./esm/options.js"
}
},
"files": [
"@types/**/*",
"constants/**/*",
"data/**/*",
"parsers/**/*",
"utilities/**/*",
"esm/**/*",
"index.d.ts",
"index.js",
"options.d.ts",
Expand All @@ -29,8 +36,11 @@
"scripts": {
"test": "jest --clearCache && jest --verbose",
"lint": "eslint src/**/*.ts",
"copy": "cp -r ./dist/. ./",
"build": "webpack && tsconfig-replace-paths -p tsconfig.json -s ./src -o ./dist && yarn copy",
"clean": "./scripts/clean.sh",
"copy": "./scripts/copy.sh",
"modify-dts": "replace-in-file --configFile=config.replace.js",
"build-dts": "rollup --config rollup.dts.config.js",
"build": "yarn clean && rollup --config rollup.config.js && yarn copy && yarn build-dts && yarn modify-dts",
"prepare": "yarn build && yarn copy",
"prepublishOnly": "npm run lint && npm run test",
"version": "git add .",
Expand All @@ -46,24 +56,27 @@
"rtlcss": "^3.5.0"
},
"devDependencies": {
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-typescript": "^8.3.1",
"@types/eslint": "^8.2.2",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.10",
"@types/rtlcss": "^3.1.2",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"clean-webpack-plugin": "^4.0.0",
"coveralls": "^3.1.1",
"eslint": "^7.32.0",
"eslint-plugin-jest": "^24.4.2",
"jest": "^27.4.7",
"postcss": "^8.3.11",
"replace-in-file": "^6.3.2",
"rollup": "^2.70.1",
"rollup-plugin-dts": "^4.2.0",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "^27.1.3",
"ts-loader": "^9.2.6",
"tsconfig-replace-paths": "^0.0.11",
"typescript": "4.4.4",
"webpack": "^5.66.0",
"webpack-cli": "^4.9.1"
"tslib": "^2.3.1",
"typescript": "4.4.4"
},
"peerDependencies": {
"postcss": "^8.0.0"
Expand Down
55 changes: 55 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import typescript from '@rollup/plugin-typescript';
import json from '@rollup/plugin-json';
import { terser } from "rollup-plugin-terser";

const getPlugins = (includeJson) => {
const plugins = includeJson
? [
json()
]
: [];
return [
...plugins,
typescript({
tsconfig: './tsconfig.json',
declaration: true,
outDir: './'
}),
terser({
output: {
comments: false
}
})
];
};

const getConfig = (name, defaults = true) => ({
input: `src/${name}.ts`,
external: [
'postcss',
'rtlcss'
],
output: [
{
file: `dist/${name}.js`,
format: 'cjs',
exports: defaults ? 'default' : 'named'
},
{
file: `dist/esm/${name}.js`,
format: 'es',
exports: defaults ? 'default' : 'named'
}
]
});

export default [
{
plugins: getPlugins(true),
...getConfig('index')
},
{
plugins: getPlugins(),
...getConfig('options', false)
}
];
26 changes: 26 additions & 0 deletions rollup.dts.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import dts from 'rollup-plugin-dts';
import tsconfig from './tsconfig.json';

const getConfig = (name) => ({
plugins: [
dts({
compilerOptions: {
baseUrl: './dist',
paths: tsconfig.compilerOptions.paths,
}
})
],
input: `dist/${name}.d.ts`,
output: [
{
file: `./${name}.d.ts`
}
]
});

export default [
getConfig('index'),
getConfig('options'),
getConfig('esm/index'),
getConfig('esm/options')
];
10 changes: 10 additions & 0 deletions scripts/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /bin/sh

RIMRAF="./node_modules/rimraf/bin.js"

$RIMRAF dist/
$RIMRAF esm/
$RIMRAF index.js
$RIMRAF index.d.ts
$RIMRAF options.js
$RIMRAF options.d.ts
14 changes: 14 additions & 0 deletions scripts/copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /bin/sh

mkdir esm

## index
cp dist/index.js index.js
cp dist/esm/index.js esm/index.js

# options
cp dist/options.js options.js
cp dist/esm/options.js esm/options.js

# esm package
cp package.esm.json esm/package.json
16 changes: 14 additions & 2 deletions src/data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
StringMap,
PluginStringMap
} from '@types';
import { getKeyFramesStringMap, getKeyFramesRegExp } from '@parsers/atrules';
import {
BOOLEAN_TYPE,
REG_EXP_CHARACTERS_REG_EXP,
Expand Down Expand Up @@ -154,7 +153,7 @@ const store: Store = {
rulesPrefixRegExp: defaultRegExp
};

export const normalizeOptions = (options: PluginOptions): PluginOptionsNormalized => {
const normalizeOptions = (options: PluginOptions): PluginOptionsNormalized => {
const returnOptions: PluginOptionsNormalized = {...defaultOptions()};
if (options.mode && ModeValuesArray.includes(options.mode)) {
returnOptions.mode = options.mode;
Expand Down Expand Up @@ -223,6 +222,19 @@ const initStore = (options: PluginOptions): void => {
store.rulesPrefixRegExp = createRulesPrefixesRegExp(store.options);
};

const getKeyFramesStringMap = (keyframes: AtRulesObject[]): AtRulesStringMap => {
const stringMap: AtRulesStringMap = {};
keyframes.forEach((obj: AtRulesObject): void => {
stringMap[obj.atRuleParams] = {
name: obj.atRule.params,
nameFlipped: obj.atRuleFlipped.params
};
});
return stringMap;
};

const getKeyFramesRegExp = (stringMap: AtRulesStringMap): RegExp => new RegExp(`(^|[^\\w-]| )(${ Object.keys(stringMap).join('|') })( |[^\\w-]|$)`, 'g');

const initKeyframesData = (): void => {
store.keyframesStringMap = getKeyFramesStringMap(store.keyframes);
store.keyframesRegExp = getKeyFramesRegExp(store.keyframesStringMap);
Expand Down
26 changes: 14 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { parseKeyFrames, parseAtRules } from '@parsers/atrules';
import { parseRules } from '@parsers/rules';
import { appendRules, appendKeyFrames, appendAutorenameRules } from '@utilities/rules';

const postcssRTLCSS = (options: PluginOptions = {}): Plugin => ({
postcssPlugin: 'postcss-rtlcss',
Once(css: Root): void {
initStore(options);
parseKeyFrames(css);
parseAtRules(css);
parseRules(css);
appendRules();
appendKeyFrames();
appendAutorenameRules();
}
});
function postcssRTLCSS (options: PluginOptions = {}): Plugin {
return ({
postcssPlugin: 'postcss-rtlcss',
Once(css: Root): void {
initStore(options);
parseKeyFrames(css);
parseAtRules(css);
parseRules(css);
appendRules();
appendKeyFrames();
appendAutorenameRules();
}
});
};

postcssRTLCSS.postcss = true;

Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Mode, Source, Autorename } from '@types';

export default {
export {
Mode,
Source,
Autorename
Expand Down
30 changes: 14 additions & 16 deletions src/parsers/atrules.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import postcss, { Root, Container, Node, AtRule, Comment } from 'postcss';
import rtlcss from 'rtlcss';
import { AtRulesObject, AtRulesStringMap, Source, ControlDirective } from '@types';
import { AT_RULE_TYPE, RULE_TYPE, KEYFRAMES_NAME, CONTROL_DIRECTIVE } from '@constants';
import { store, initKeyframesData } from '@data/store';
import {
Source,
ControlDirective
} from '@types';
import {
AT_RULE_TYPE,
RULE_TYPE,
KEYFRAMES_NAME,
CONTROL_DIRECTIVE
} from '@constants';
import {
store,
initKeyframesData
} from '@data/store';
import { walkContainer } from '@utilities/containers';
import {
isIgnoreDirectiveInsideAnIgnoreBlock,
Expand All @@ -12,19 +23,6 @@ import {
import { vendor } from '@utilities/vendor';
import { parseRules } from '@parsers/rules';

export const getKeyFramesStringMap = (keyframes: AtRulesObject[]): AtRulesStringMap => {
const stringMap: AtRulesStringMap = {};
keyframes.forEach((obj: AtRulesObject): void => {
stringMap[obj.atRuleParams] = {
name: obj.atRule.params,
nameFlipped: obj.atRuleFlipped.params
};
});
return stringMap;
};

export const getKeyFramesRegExp = (stringMap: AtRulesStringMap): RegExp => new RegExp(`(^|[^\\w-]| )(${ Object.keys(stringMap).join('|') })( |[^\\w-]|$)`, 'g');

export const parseAtRules = (container: Container): void => {

const controlDirectives: Record<string, ControlDirective> = {};
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"outDir": "./dist/",
"module": "commonjs",
"module": "esnext",
"target": "ES5",
"moduleResolution": "node",
"esModuleInterop": true,
Expand Down
43 changes: 0 additions & 43 deletions webpack.config.js

This file was deleted.

0 comments on commit 6d29e3f

Please sign in to comment.