Skip to content

Commit

Permalink
feat: support native esm plugins (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Aug 14, 2021
1 parent c73c519 commit 965cdcb
Show file tree
Hide file tree
Showing 17 changed files with 2,035 additions and 105 deletions.
9 changes: 9 additions & 0 deletions .changeset/late-paws-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'remark-preset-prettier': major
---

feat: support native esm plugins

If you're not ready for native esm or latest `remark-lint` plugins, please use versions `<1`, see [#61](https://github.com/JounQin/remark-preset-prettier/issues/61) for more details.

Or you can try something like [`yarn-deduplicate`](https://github.com/atlassian/yarn-deduplicate) with `yarn-deduplicate --strategy fewer` without guarantee.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lib
src/plugins.ts
CHANGELOG.md
!/.*.js
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "@1stg"
"extends": "@1stg",
"rules": {
"mdx/remark": 0
}
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:

- name: Build, Lint and test
run: |
yarn build
# `build` is already covered by `lint`
yarn lint
yarn typecov
env:
Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,16 @@ jobs:
${{ runner.os }}-yarn-
- name: Install Dependencies
# https://github.com/atlassian/changesets/issues/246
run: |
yarn --frozen-lockfile
rm -rf .git/hooks/commit-msg
- name: Build
run: yarn build
run: yarn --frozen-lockfile

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@master
with:
# This expects you to have a script called release which does a build for your packages and calls changeset publish
publish: yarn release
commit: 'chore: release package(s)'
title: 'chore: release package(s)'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.type-coverage
lib
node_modules
src/plugins.ts
1 change: 1 addition & 0 deletions .remarkignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHANGELOG.md
5 changes: 0 additions & 5 deletions .remarkrc

This file was deleted.

9 changes: 9 additions & 0 deletions .remarkrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
plugins: [
'remark-lint',
'remark-preset-lint-consistent',
'remark-preset-lint-markdown-style-guide',
'remark-preset-lint-recommended',
'./lib/index.js',
],
}
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
## TOC <!-- omit in toc -->

- [Notice](#notice)
- [Disabled remark-lint plugins](#disabled-remark-lint-plugins)
- [Install](#install)
- [Usage](#usage)
Expand All @@ -29,6 +30,12 @@
- [Changelog](#changelog)
- [License](#license)

## Notice

If you're not ready for native esm or latest `remark-lint` plugins, please use versions `<1`, see [#61](https://github.com/JounQin/remark-preset-prettier/issues/61) for more details.

Or you can try something like [`yarn-deduplicate`](https://github.com/atlassian/yarn-deduplicate) with `yarn-deduplicate --strategy fewer` without guarantee.

## Disabled remark-lint plugins

1. [blank-lines-1-0-2](https://www.npmjs.com/package/remark-lint-blank-lines-1-0-2)
Expand Down Expand Up @@ -111,12 +118,12 @@ remark . --use preset-lint-consistent preset-lint-markdown-style-guide preset-li
### Via Node API

```js
const report = require('vfile-reporter')
const remark = require('remark')
const consistent = require('remark-preset-lint-consistent')
const styleGuide = require('remark-preset-lint-markdown-style-guide')
const recommended = require('remark-preset-lint-recommended')
const prettier = require('remark-preset-lint-prettier')
import report from 'vfile-reporter'
import remark from 'remark'
import consistent from 'remark-preset-lint-consistent'
import styleGuide from 'remark-preset-lint-markdown-style-guide'
import recommended from 'remark-preset-lint-recommended'
import prettier from 'remark-preset-lint-prettier'

const file = remark()
.use(consistent)
Expand All @@ -138,9 +145,10 @@ If you do have problems between [retext-sentence-spacing][] and [Prettier][], yo

```js
// .remarkrc.js
const wooorm = require('retext-preset-wooorm')
import wooorm from 'retext-preset-wooorm'
import sentenceSpacing from 'retext-sentence-spacing'

module.exports = {
export default {
plugins: [
'preset-wooorm', // other preset(s) or plugin(s)
'preset-prettier',
Expand All @@ -149,7 +157,7 @@ module.exports = {
unified()
.use(wooorm) // retext preset(s)
.use({
plugins: [[require('retext-sentence-spacing'), false]],
plugins: [[sentenceSpacing, false]],
}),
],
],
Expand Down
49 changes: 0 additions & 49 deletions index.ts

This file was deleted.

54 changes: 51 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
},
"types": "lib",
"files": [
"lib",
"!lib/*.tsbuildinfo"
"lib"
],
"scripts": {
"build": "run-p build:*",
"build:r": "r -f cjs -e named",
"build:ts": "tsc -b",
"build:ts": "tsc -p tsconfig.lib.json",
"dedup": "yarn-deduplicate --strategy fewer",
"lint": "run-p lint:*",
"lint:es": "eslint . --cache -f friendly",
"lint:md": "remark .",
"lint:tsc": "tsc --noEmit",
"prebuild": "node --loader ts-node/esm scripts/build.ts",
"prelint": "yarn build",
"prepare": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prerelease": "yarn build",
"release": "changeset publish",
Expand All @@ -35,12 +37,57 @@
"peerDependencies": {
"prettier": ">=1.0.0"
},
"dependencies": {
"remark-lint-blank-lines-1-0-2": "*",
"remark-lint-blockquote-indentation": "*",
"remark-lint-books-links": "*",
"remark-lint-checkbox-character-style": "*",
"remark-lint-code-block-style": "*",
"remark-lint-definition-case": "*",
"remark-lint-definition-spacing": "*",
"remark-lint-emphasis-marker": "*",
"remark-lint-fenced-code-marker": "*",
"remark-lint-final-newline": "*",
"remark-lint-hard-break-spaces": "*",
"remark-lint-heading-style": "*",
"remark-lint-heading-whitespace": "*",
"remark-lint-link-title-style": "*",
"remark-lint-list-item-bullet-indent": "*",
"remark-lint-list-item-content-indent": "*",
"remark-lint-list-item-indent": "*",
"remark-lint-list-item-spacing": "*",
"remark-lint-maximum-line-length": "*",
"remark-lint-no-blockquote-without-marker": "*",
"remark-lint-no-consecutive-blank-lines": "*",
"remark-lint-no-heading-content-indent": "*",
"remark-lint-no-inline-padding": "*",
"remark-lint-no-long-code": "*",
"remark-lint-no-table-indentation": "*",
"remark-lint-ordered-list-marker-style": "*",
"remark-lint-ordered-list-marker-value": "*",
"remark-lint-rule-style": "*",
"remark-lint-spaces-around-number": "*",
"remark-lint-spaces-around-word": "*",
"remark-lint-strong-marker": "*",
"remark-lint-table-cell-padding": "*",
"remark-lint-table-pipe-alignment": "*",
"remark-lint-table-pipes": "*",
"remark-lint-unordered-list-marker-style": "*"
},
"devDependencies": {
"@1stg/common-config": "^2.3.1",
"@changesets/changelog-github": "^0.4.0",
"@changesets/cli": "^2.16.0",
"@pkgr/rollup": "^2.0.0",
"@types/node": "^16.6.1",
"@types/prettier": "^2.3.2",
"patch-package": "^6.4.7",
"remark-cli": "^10.0.0",
"remark-lint": "^9.0.0",
"remark-preset-lint-consistent": "^5.0.0",
"remark-preset-lint-markdown-style-guide": "^5.0.0",
"remark-preset-lint-recommended": "^6.0.0",
"ts-node": "^10.2.0",
"type-coverage": "^2.18.0",
"typescript": "^4.3.5"
},
Expand All @@ -53,6 +100,7 @@
"ignoreFiles": [
"**/*.d.ts"
],
"showRelativePath": true,
"strict": true,
"update": true
}
Expand Down
98 changes: 98 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import fs from 'fs'
import path from 'path'

import prettier from 'prettier'

const plugins = [
'blank-lines-1-0-2',
'blockquote-indentation',
'books-links',
'checkbox-character-style',
'code-block-style',
'definition-case',
'definition-spacing',
'emphasis-marker',
'fenced-code-marker',
'final-newline',
'hard-break-spaces',
'heading-style',
'heading-whitespace',
'link-title-style',
'list-item-bullet-indent',
'list-item-content-indent',
'list-item-indent',
'list-item-spacing',
'maximum-line-length',
'no-blockquote-without-marker',
'no-consecutive-blank-lines',
'no-heading-content-indent',
'no-inline-padding',
'no-long-code',
'no-table-indentation',
'ordered-list-marker-style',
'ordered-list-marker-value',
'rule-style',
'spaces-around-number',
'spaces-around-word',
'strong-marker',
'table-cell-padding',
'table-pipe-alignment',
'table-pipes',
'unordered-list-marker-style',
]

const camelCase = (val: string) =>
// type-coverage:ignore-next-line -- https://github.com/plantain-00/type-coverage/issues/96
val.replace(/-([\da-z])/g, ([_, $0]) => $0.toUpperCase())

const main = async () => {
const pkgPath = path.resolve('package.json')

const pkg = JSON.parse(await fs.promises.readFile(pkgPath, 'utf8')) as {
dependencies?: Record<string, string>
}

await fs.promises.writeFile(
pkgPath,
prettier.format(
JSON.stringify({
...pkg,
dependencies: {
...pkg.dependencies,
...plugins.reduce<Record<string, string>>(
(acc, plugin) =>
Object.assign(acc, {
[`remark-lint-${plugin}`]: '*',
}),
{},
),
},
}),
{
filepath: pkgPath,
},
),
)

const pluginsPath = path.resolve('src/plugins.ts')

await fs.promises.writeFile(
pluginsPath,
prettier.format(
[
'// @ts-nocheck',
...plugins.map(
plugin => `import ${camelCase(plugin)} from 'remark-lint-${plugin}'`,
),
`\nexport const plugins: Array<import('unified').Plugin> = [${plugins
.map(plugin => camelCase(plugin))
.join(',')}]`,
].join('\n'),
{
filepath: pluginsPath,
},
),
)
}

main().catch(console.error)
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { plugins as _plugins } from './plugins.js'

export const plugins = _plugins.map(plugin => [plugin, false] as const)

export default { plugins }
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"extends": "@1stg/tsconfig/lib",
"compilerOptions": {
"outDir": "lib"
}
"extends": "@1stg/tsconfig/lib"
}

0 comments on commit 965cdcb

Please sign in to comment.