Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use named exports for ESM #243

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions .eslintignore
@@ -1,2 +1,3 @@
coverage/
dist/
/coverage
/dist
/lib/unicode.js
119 changes: 111 additions & 8 deletions .gitignore
Expand Up @@ -2,42 +2,145 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

.vscode/
dist/
# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

test/output.json
test/test.json
/test/output.json
/test/test.json
4 changes: 4 additions & 0 deletions .husky/pre-commit
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

node build/check-package.js
9 changes: 9 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,14 @@
[c-unreleased]: https://github.com/json5/json5/tree/main
[d-unreleased]: https://github.com/json5/json5/compare/v2.2.1...HEAD

- New: `parse` and `stringify` can now be imported as named exports when using
JavaScript modules. ([#243])
- New: Browser bundles use more descriptive names (e.g. `dist/json5.umd.js` and
`dist/json5.esm.js`). Legacy `dist/index.*` names are deprecated, and will be
removed in the next major version. ([#243])
- Fix: CommonJS and module exports in Node.js refer to the same instance.
([#243])

### v2.2.1 [[code][c2.2.1], [diff][d2.2.1]]

[c2.2.1]: https://github.com/json5/json5/tree/v2.2.1
Expand Down Expand Up @@ -366,5 +374,6 @@ parser for the regular JSON format.
[#228]: https://github.com/json5/json5/issues/228
[#229]: https://github.com/json5/json5/issues/229
[#236]: https://github.com/json5/json5/issues/236
[#243]: https://github.com/json5/json5/issues/243
[#244]: https://github.com/json5/json5/issues/244
[#266]: https://github.com/json5/json5/issues/266
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -92,13 +92,13 @@ import JSON5 from 'json5'
#### UMD
```html
<!-- This will create a global `JSON5` variable. -->
<script src="https://unpkg.com/json5@2/dist/index.min.js"></script>
<script src="https://unpkg.com/json5@2/dist/json5.umd.min.js"></script>
```

#### Modules
```html
<script type="module">
import JSON5 from 'https://unpkg.com/json5@2/dist/index.min.mjs'
import JSON5 from 'https://unpkg.com/json5@2/dist/json5.esm.min.js'
</script>
```

Expand Down
14 changes: 14 additions & 0 deletions build/check-package.js
@@ -0,0 +1,14 @@
const assert = require('assert')
const fs = require('fs')
const pkg = require('../package.json')

const JSON5 = require('..')
const pkg5JSON5 = fs.readFileSync('package.json5', 'utf8')
const pkg5 = JSON5.parse(pkg5JSON5)

try {
assert.deepStrictEqual(pkg5, pkg, 'package.json5 does not match package.json.\nRun `npm run build-package` to update package.json5.')
} catch (err) {
console.error(err.message)
process.exitCode = 1
}
2 changes: 1 addition & 1 deletion build/es5.js
@@ -1,6 +1,6 @@
require('core-js/fn/string/code-point-at')
require('core-js/fn/string/from-code-point')

const JSON5 = require('../lib')
const JSON5 = require('../lib/index.js')

module.exports = JSON5
2 changes: 1 addition & 1 deletion build/package.js
@@ -1,7 +1,7 @@
const fs = require('fs')
const path = require('path')

const JSON5 = require('../lib')
const JSON5 = require('../lib/index.js')

const pkg = require('../package.json')

Expand Down
2 changes: 1 addition & 1 deletion lib/cli.js
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs')
const path = require('path')
const pkg = require('../package.json')
const JSON5 = require('./')
const JSON5 = require('./index.js')

const argv = parseArgs()

Expand Down
3 changes: 3 additions & 0 deletions lib/index.d.ts
Expand Up @@ -2,3 +2,6 @@ import parse = require('./parse')
import stringify = require('./stringify')

export {parse, stringify}

const JSON5 = {parse, stringify}
export default JSON5
5 changes: 5 additions & 0 deletions lib/index.js
Expand Up @@ -6,4 +6,9 @@ const JSON5 = {
stringify,
}

Object.defineProperty(JSON5, 'default', {
enumerable: false,
value: JSON5,
})

module.exports = JSON5
7 changes: 7 additions & 0 deletions lib/index.mjs
@@ -0,0 +1,7 @@
/* eslint-disable node/no-unsupported-features/es-syntax */

import JSON5 from './index.js'

export const parse = JSON5.parse
export const stringify = JSON5.stringify
export default JSON5
2 changes: 1 addition & 1 deletion lib/register.js
@@ -1,5 +1,5 @@
const fs = require('fs')
const JSON5 = require('./')
const JSON5 = require('./index.js')

// eslint-disable-next-line node/no-deprecated-api
require.extensions['.json5'] = function (module, filename) {
Expand Down