Skip to content

Commit

Permalink
Provides dual ESM/CJS module
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Nov 4, 2022
1 parent 33efb24 commit a3b6271
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
os: ['ubuntu-latest', 'windows-latest']
node: ['12.0']
node: ['12.17.0']
name: Testing Node ${{ matrix.node }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -43,7 +43,7 @@ jobs:
strategy:
matrix:
os: ['ubuntu-latest', 'windows-latest']
node: ['12.0', '14.0', '16.0']
node: ['12.17.0', '14.0', '16.0']
name: Testing Node ${{ matrix.node }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ npm-debug.log*
.nyc_output
coverage.info
test/fixtures/*/build
lib
node_modules
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ yarn add @metalsmith/markdown
## Usage

```js
const markdown = require('@metalsmith/markdown')
import markdown from '@metalsmith/markdown'

metalsmith.use(
markdown({
Expand Down Expand Up @@ -111,8 +111,8 @@ would be transformed into:
You can use a custom renderer by using `marked.Renderer()`

```js
const markdown = require('@metalsmith/markdown')
const marked = require('marked')
import markdown from '@metalsmith/markdown'
import { marked } from 'marked'
const markdownRenderer = new marked.Renderer()

markdownRenderer.image = function (href, title, text) {
Expand Down
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
"maintainers": [
"Kevin Van Lierde (https://webketje.com)"
],
"main": "lib/index.js",
"source": "src/index.js",
"main": "lib/index.cjs",
"module": "lib/index.js",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"type": "module",
"files": [
"lib/*.js"
],
Expand All @@ -36,8 +43,10 @@
"format:check": "prettier --list-different \"**/*.{yml,md,js,json}\"",
"lint": "eslint --fix .",
"lint:check": "eslint --fix-dry-run .",
"release": "release-it .",
"test": "nyc mocha"
"release": "npm run build && release-it .",
"build": "microbundle --target node --no-sourcemap -f cjs,esm",
"test": "nyc mocha",
"pretest": "npm run build"
},
"dependencies": {
"dlv": "^1.1.3",
Expand All @@ -52,6 +61,7 @@
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"metalsmith": "^2.5.0",
"microbundle": "^0.15.1",
"mocha": "^9.2.2",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
Expand All @@ -64,6 +74,6 @@
"access": "public"
},
"engines": {
"node": ">=12"
"node": ">=12.17.0 <13.0.0-0||>=13.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const get = require('dlv')
import get from 'dlv'

function error(name, msg) {
const err = new Error(msg)
Expand Down Expand Up @@ -63,4 +63,4 @@ function expandWildcardKeypath(root, keypaths, char) {
return expanded
}

module.exports = expandWildcardKeypath
export default expandWildcardKeypath
12 changes: 6 additions & 6 deletions lib/index.js → src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { basename, dirname, extname, join } = require('path')
const get = require('dlv')
const set = require('dset').dset
const { marked } = require('marked')
const expandWildcardKeypaths = require('./expand-wildcard-keypath')
import { basename, dirname, extname, join } from 'path'
import get from 'dlv'
import { dset as set } from 'dset'
import { marked } from 'marked'
import expandWildcardKeypaths from './expand-wildcard-keypath.js'

function render(data, key, options) {
const value = get(data, key)
Expand Down Expand Up @@ -72,4 +72,4 @@ function initMarkdown(options = defaultOptions) {
}
}

module.exports = initMarkdown
export default initMarkdown
8 changes: 7 additions & 1 deletion test/index.js → test/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ const equal = require('assert-dir-equal')
const Metalsmith = require('metalsmith')
const { name } = require('../package.json')
const markdown = require('..')
const expandWildcardKeypath = require('../lib/expand-wildcard-keypath')
let expandWildcardKeypath
const path = require('path')

function msCommon(dir) {
return Metalsmith(dir).env('DEBUG', process.env.DEBUG)
}

describe('@metalsmith/markdown', function () {
before(function (done) {
import('../src/expand-wildcard-keypath.js').then(imported => {
expandWildcardKeypath = imported
done()
})
})
it('should export a named plugin function matching package.json name', function () {
const namechars = name.split('/')[1]
const camelCased = namechars.split('').reduce((str, char, i) => {
Expand Down

0 comments on commit a3b6271

Please sign in to comment.