Skip to content

Commit

Permalink
Resolves #129: provides dual ESM/CJS module
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Nov 12, 2022
1 parent 6f4599b commit 6c5ba72
Show file tree
Hide file tree
Showing 8 changed files with 14,141 additions and 7,897 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
3 changes: 2 additions & 1 deletion .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
node_modules
node_modules
lib/*.*js
2 changes: 1 addition & 1 deletion .prettierrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ semi: false
singleQuote: true
bracketSpacing: true
arrowParens: always
printWidth: 80
printWidth: 120
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ yarn add @metalsmith/permalinks
## Usage

```js
const Metalsmith = require('metalsmith')
const permalinks = require('@metalsmith/permalinks')
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import Metalsmith from 'metalsmith'
import permalinks from '@metalsmith/permalinks'

const __dirname = dirname(fileURLToPath(import.meta.url))

Metalsmith(__dirname).use(
permalinks({
Expand All @@ -42,10 +46,7 @@ If no pattern is provided, the files won't be remapped, but the `path` metadata
The `pattern` can also be set as such:
```js
const Metalsmith = require('metalsmith')
const permalinks = require('@metalsmith/permalinks')

Metalsmith(__dirname).use(
metalsmith.use(
permalinks({
// original options would act as the keys of a `default` linkset,
pattern: ':title',
Expand Down Expand Up @@ -91,8 +92,8 @@ would generate the file tree:
```
build
├── category1/with-category.html
└── no-category.html
├── category1/with-category/index.html
└── no-category/index.html
```
### Dates
Expand Down
21,975 changes: 14,106 additions & 7,869 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
},
"version": "2.4.1",
"license": "MIT",
"main": "lib/index.js",
"source": "src/index.js",
"main": "lib/index.cjs",
"module": "lib/index.js",
"type": "module",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
},
"scripts": {
"changelog": "auto-changelog -u --starting-version v2.4.0 --commit-limit false --ignore-commit-pattern '^((dev|chore|ci):|Release|Merge)'",
"coverage": "nyc report --reporter=text-lcov > ./coverage.info",
Expand All @@ -30,8 +37,10 @@
"lint": "eslint --fix .",
"lint:check": "eslint --fix-dry-run .",
"dev": "nodemon --exec \"npm run lint && npm test\"",
"test": "nyc mocha",
"release": "release-it ."
"release": "release-it .",
"build": "microbundle --target node --no-sourcemap -f cjs,esm --generateTypes=false",
"pretest": "npm run build",
"test": "nyc mocha"
},
"dependencies": {
"debug": "^4.3.4",
Expand All @@ -49,6 +58,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",
"nodemon": "^2.0.19",
"nyc": "^15.1.0",
Expand Down
24 changes: 9 additions & 15 deletions lib/index.js → src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'
import path from 'path'
import createDebug from 'debug'
import moment from 'moment'
import slugify from 'slugify'
import route from 'regexparam'

const path = require('path')
const createDebug = require('debug')
const debug = createDebug('@metalsmith/permalinks')
const moment = require('moment')
const slugify = require('slugify')
const route = require('regexparam')

const error = debug.extend('error')

/**
Expand Down Expand Up @@ -253,7 +251,7 @@ const replace = (pattern, data, options) => {
* @param {Options} options
* @returns {import('metalsmith').Plugin}
*/
function inititalizePermalinks(options) {
function permalinks(options) {
options = normalizeOptions(options)

const { linksets } = options
Expand Down Expand Up @@ -302,8 +300,7 @@ function inititalizePermalinks(options) {
return target
}

const makeUnique =
typeof options.unique === 'function' ? options.unique : defaultUniquePath
const makeUnique = typeof options.unique === 'function' ? options.unique : defaultUniquePath

Object.keys(files).forEach((file) => {
const data = files[file]
Expand All @@ -330,10 +327,7 @@ function inititalizePermalinks(options) {
}

// Override the path with `permalink` option
if (
Object.prototype.hasOwnProperty.call(data, 'permalink') &&
data.permalink !== false
) {
if (Object.prototype.hasOwnProperty.call(data, 'permalink') && data.permalink !== false) {
ppath = data.permalink
}

Expand Down Expand Up @@ -369,4 +363,4 @@ function inititalizePermalinks(options) {
}

// Expose `plugin`
module.exports = inititalizePermalinks
export default permalinks
File renamed without changes.

0 comments on commit 6c5ba72

Please sign in to comment.