Skip to content

Commit

Permalink
Merge pull request #10 from ksoichiro/fix-config-for-ts-and-cjs
Browse files Browse the repository at this point in the history
Keep previous usage, fix package config, and add tests to confirm examples work
  • Loading branch information
ksoichiro authored Jan 22, 2022
2 parents aa62305 + aac0672 commit 7a51046
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 6 deletions.
13 changes: 13 additions & 0 deletions examples/cjs/example.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
beforeAll(() => {
process.chdir('examples/cjs')
})

afterAll(() => {
process.chdir('../..')
})

test('example works', () => {
console.log = jest.fn()
require('./example.js')
expect(console.log).toHaveBeenCalledWith(`<p><img src="img.png" alt="" width="640" height="480"></p>`)
})
1 change: 1 addition & 0 deletions examples/cjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"remark-rehype": "^8.1.0",
"to-vfile": "^6.1.0",
"unified": "^9.2.2"
},
"scripts": {
"test": "jest"
}
}
15 changes: 15 additions & 0 deletions examples/esm/example.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {jest} from '@jest/globals'

beforeAll(() => {
process.chdir('examples/cjs')
})

afterAll(() => {
process.chdir('../..')
})

test('example works', async () => {
console.log = jest.fn()
await import('./example.js')
expect(console.log).toHaveBeenCalledWith(`<p><img src="img.png" alt="" width="640" height="480"></p>`)
})
1 change: 1 addition & 0 deletions examples/esm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"type": "module",
"devDependencies": {
"rehype-img-size": "file:../.."
},
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
}
}
2 changes: 2 additions & 0 deletions examples/ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/example.js
15 changes: 15 additions & 0 deletions examples/ts/example.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {jest} from '@jest/globals'

beforeAll(() => {
process.chdir('examples/cjs')
})

afterAll(() => {
process.chdir('../..')
})

test('example works', async () => {
console.log = jest.fn()
await import('./example.js')
expect(console.log).toHaveBeenCalledWith(`<p><img src="img.png" alt="" width="640" height="480"></p>`)
})
17 changes: 17 additions & 0 deletions examples/ts/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {unified} from 'unified'
import parse from 'remark-parse'
import remark2rehype from 'remark-rehype'
import stringify from 'rehype-stringify'
import * as vfile from 'to-vfile'
import rehypeImgSize from 'rehype-img-size'

unified()
.use(parse)
.use(remark2rehype)
.use(rehypeImgSize)
.use(stringify)
.process(vfile.readSync('index.md'), function(err, file) {
if (err) throw err
if (!file) return
console.log(file.value)
})
Binary file added examples/ts/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/ts/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![](img.png)
117 changes: 117 additions & 0 deletions examples/ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"type": "module",
"devDependencies": {
"@types/node": "^17.0.10",
"rehype-img-size": "file:../..",
"typescript": "^4.5.5"
},
"scripts": {
"build": "tsc -p .",
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
}
}
9 changes: 9 additions & 0 deletions examples/ts/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Example of usage as TypeScript

To try example, run following:

```
npm install
npm run build
node example.js
```
8 changes: 8 additions & 0 deletions examples/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es2016",
"module": "es2015",
"skipLibCheck": true,
"moduleResolution": "node"
}
}
2 changes: 0 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ import type {Plugin} from 'unified'
declare const rehypeImgSize: Plugin<[Options] | [], Root, string>
export default rehypeImgSize
export type {Options}

export function getImageSize(src: string, dir?: string): ISize;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default setImageSize
*/
const absolutePathRegex = /^(?:[a-z]+:)?\/\//;

export function getImageSize(src, dir) {
function getImageSize(src, dir) {
if (absolutePathRegex.exec(src)) {
return
}
Expand Down
8 changes: 7 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import stringify from 'rehype-stringify'
import * as vfile from 'to-vfile'
import rehypeImgSize from './index'

process.chdir('fixtures')
beforeAll(() => {
process.chdir('fixtures')
})

afterAll(() => {
process.chdir('..')
})

test('images in the same directory', (done) => {
unified()
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"types": "./index.d.ts",
"files": [
"index.js",
"index.d.ts",
"cjs/index.cjs"
],
"devDependencies": {
Expand All @@ -48,10 +49,19 @@
"unist-util-visit": "^4.1.0"
},
"scripts": {
"postinstall": "(cd examples/cjs && npm i); (cd examples/esm && npm i); (cd examples/ts && npm i)",
"pretest": "(npm run build) && (cd examples/cjs && npm run build --if-present); (cd examples/esm && npm run build --if-present); (cd examples/ts && npm run build --if-present)",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
"cjs": "rollup --config --file cjs/index.cjs"
"build": "rollup --config --file cjs/index.cjs"
},
"jest": {
"transform": {}
"collectCoverageFrom": [
"*.js"
],
"transform": {},
"testPathIgnorePatterns": [
"<rootDir>/node_modules/",
"<rootDir>/cjs"
]
}
}

0 comments on commit 7a51046

Please sign in to comment.