Skip to content

Commit

Permalink
fix: use esm module
Browse files Browse the repository at this point in the history
issue: #150
  • Loading branch information
hosseinmd committed Mar 31, 2022
1 parent c71f898 commit 28ac56e
Show file tree
Hide file tree
Showing 5 changed files with 544 additions and 84 deletions.
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@
"unpkg": "dist/index.umd.min.js",
"types": "dist/index.d.ts",
"files": [
"dist"
"dist/index.js",
"dist/index.min.mjs",
"dist/index.umd.min.js",
"dist/index.d.ts"
],
"scripts": {
"prepare": "yarn build",
"lint": "eslint --ext '.ts' ./src",
"test": "yarn prepare && jest",
"test": "yarn build --test && jest",
"release": "standard-version && npm publish && git push --follow-tags origin master",
"prettierAll": "prettier --write \"**/*.ts\"",
"clean": "rm -fr dist",
"build": "npm run clean && npm run lint && tsc --project tsconfig.json && npm run bundle:esm && npm run bundle:esm:min && npm run bundle:umd && npm run bundle:umd:min && npm run build:stats && tsc --project tsconfig.json --module commonjs",
"build:stats": "(echo '\\033[35;3m' ; cd dist && ls -lh index*js index*gz | tail -n +2 | awk '{print $5,$9}')",
"bundle:esm": "rollup dist/index.js --file dist/index.mjs --format esm",
"bundle:esm:min": "terser --ecma 6 --compress --mangle --module -o dist/index.min.mjs -- dist/index.mjs && gzip -9 -c dist/index.min.mjs > dist/index.min.mjs.gz",
"bundle:umd": "rollup dist/index.js --file dist/index.umd.js --format umd --name sayHello",
"bundle:umd:min": "terser --ecma 6 --compress --mangle -o dist/index.umd.min.js -- dist/index.umd.js && gzip -9 -c dist/index.umd.min.js > dist/index.umd.min.js.gz"
"build": "node script.mjs"
},
"keywords": [
"prettier",
Expand All @@ -45,6 +43,9 @@
},
"devDependencies": {
"@commitlint/config-conventional": "^14.1.0",
"@rollup/plugin-commonjs": "^21.0.3",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.1.3",
"@types/jest": "^27.4.1",
"@types/mdast": "^3.0.10",
"@typescript-eslint/eslint-plugin": "^5.3.0",
Expand All @@ -70,7 +71,8 @@
"binary-searching": "^2.0.5",
"comment-parser": "^1.3.1",
"linguist-languages": "^7.15.0",
"mdast-util-from-markdown": "^0.8.5"
"remark-parse": "^10.0.1",
"unified": "^10.1.2"
},
"engines": {
"node": ">=12.0.0"
Expand Down
19 changes: 19 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";

export default {
input: "dist/index.js",
output: {
file: "dist/index.js",
format: "commonjs",
},
external: ["prettier", "comment-parser", "binary-searching"],
plugins: [
commonjs({}),
nodeResolve({}),
json({
preferConst: true,
}),
],
};
37 changes: 37 additions & 0 deletions script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { execSync } from "child_process";
import { default as chalk } from "chalk";

const __TEST__ = process.argv.includes("--test");

function $(commands) {
if (typeof commands === "string") {
console.log(chalk.gray("$", commands));
return execSync(commands).toString();
}

return commands.map((command) => {
console.log(chalk.gray("$", commands));
execSync(command).toString();
});
}

const lint = "npm run clean && npm run lint && tsc --project tsconfig.json";

const bundleEsm = "rollup --config rollup.config.js";
const bundleEsmMin =
"terser --ecma 6 --compress --mangle --module -o dist/index.min.mjs -- dist/index.js && gzip -9 -c dist/index.min.mjs > dist/index.min.mjs.gz";
const bundleUmd =
"rollup dist/index.js --file dist/index.umd.js --format umd --name sayHello";
const bundleUmdMin =
"terser --ecma 6 --compress --mangle -o dist/index.umd.min.js -- dist/index.umd.js && gzip -9 -c dist/index.umd.min.js > dist/index.umd.min.js.gz";
const buildStats =
"(echo '\\033[35;3m' ; cd dist && ls -lh index*.js | index*.gz | tail -n +2 | awk '{print $5,$9}')";

$(`${lint}`);
$(`${bundleEsm}`);
if (!__TEST__) {
$(`${bundleEsmMin}`);
$(`${bundleUmd}`);
$(`${bundleUmdMin}`);
$(`${buildStats}`);
}
8 changes: 6 additions & 2 deletions src/descriptionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { format, BuiltInParserName } from "prettier";
import { DESCRIPTION, EXAMPLE, TODO } from "./tags";
import { AllOptions } from "./types";
import { capitalizer, formatCode } from "./utils";
import fromMarkdown from "mdast-util-from-markdown";
import { unified } from "unified";
import remarkParse from "remark-parse";
import { Root, Content, Link, Image, Text, List } from "mdast";
import { TAGS_PEV_FORMATE_DESCRIPTION } from "./roles";

const fromMarkdown = unified().use(remarkParse);

const TABLE = "2@^5!~#sdE!_TABLE";

interface DescriptionEndLineParams {
Expand Down Expand Up @@ -109,6 +112,7 @@ function formatDescription(
return `\n\n${TABLE}\n\n${_3 ? _3.slice(1) : ""}`;
},
);

if (
options.jsdocCapitalizeDescription &&
!TAGS_PEV_FORMATE_DESCRIPTION.includes(tag)
Expand All @@ -122,7 +126,7 @@ function formatDescription(

let tableIndex = 0;

const rootAst = fromMarkdown(text);
const rootAst = fromMarkdown.parse(text);

function stringifyASTWithoutChildren(
mdAst: Content | Root,
Expand Down
Loading

0 comments on commit 28ac56e

Please sign in to comment.