Skip to content

Commit

Permalink
feat: add support for modules
Browse files Browse the repository at this point in the history
This large commit enhances support for JavaScript modules.

This adds `lib/index.mjs`, which re-exports `lib/index.js` as a default
export and `parse` and `stringify` as named exports. The `module` field
in `package.json` now points to this file as the entry point for
`import` statements in versions of Node.js that support modules. With
this change, `import` statements in Node.js no longer need to use the
path `json5/dist/index.mjs`, and should import the module as `json5`
instead. `test/index.mjs` ensures that `lib/index.js` and
`lib/index.mjs` export the same instance. This follows the guidance in
approach json5#1 of [Writing dual packages while avoiding or minimizing
hazards][guidance].

`rollup.config.js` now uses `lib/index.mjs` as its entry point for
building browser bundles for JavaScript modules environments. It now
writes browser bundles to `dist/json5.umd.js` and `dist/json5.esm.js`,
as well as minified versions. These are the new canonical paths for
browser bundles. The legacy `dist/index.*` paths still exist for
backward compatibility, but they will be removed in the next major
version.

All files in `build` and `lib` now explicitly require `lib/index.js` to
ensure that `lib/index.mjs` is not imported. All files in `test` now
require the package root to simulate the default behavior of the version
of Node.js being tested.

[guidance]:
  https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#writing-dual-packages-while-avoiding-or-minimizing-hazards

squash
  • Loading branch information
jordanbtucker committed Aug 12, 2022
1 parent 4798b9d commit ecb408a
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 33 deletions.
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
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
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
5 changes: 5 additions & 0 deletions lib/index.mjs.d.ts
@@ -0,0 +1,5 @@
import parse = require('./parse')
import stringify = require('./stringify')

export {parse, stringify}
export default {parse, stringify}
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
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -3,9 +3,10 @@
"version": "2.2.1",
"description": "JSON for Humans",
"main": "lib/index.js",
"module": "dist/index.mjs",
"module": "lib/index.mjs",
"bin": "lib/cli.js",
"browser": "dist/index.js",
"browser": "dist/json5.umd.js",
"unpkg": "dist/json5.umd.js",
"types": "lib/index.d.ts",
"files": [
"lib/",
Expand Down
5 changes: 3 additions & 2 deletions package.json5
Expand Up @@ -4,9 +4,10 @@
version: '2.2.1',
description: 'JSON for Humans',
main: 'lib/index.js',
module: 'dist/index.mjs',
module: 'lib/index.mjs',
bin: 'lib/cli.js',
browser: 'dist/index.js',
browser: 'dist/json5.umd.js',
unpkg: 'dist/json5.umd.js',
types: 'lib/index.d.ts',
files: [
'lib/',
Expand Down
70 changes: 50 additions & 20 deletions rollup.config.js
Expand Up @@ -8,11 +8,19 @@ module.exports = [
// ES5 Non-minified
{
input: 'build/es5.js',
output: {
file: pkg.browser,
format: 'umd',
name: 'JSON5',
},
output: [
{
file: pkg.browser,
format: 'umd',
name: 'JSON5',
},
{
// Legacy path
file: 'dist/index.js',
format: 'umd',
name: 'JSON5',
},
],
plugins: [
resolve(),
commonjs(),
Expand All @@ -22,11 +30,19 @@ module.exports = [
// ES5 Minified
{
input: 'build/es5.js',
output: {
file: pkg.browser.replace(/\.js$/, '.min.js'),
format: 'umd',
name: 'JSON5',
},
output: [
{
file: pkg.browser.replace(/\.js$/, '.min.js'),
format: 'umd',
name: 'JSON5',
},
{
// Legacy path
file: 'dist/index.min.js',
format: 'umd',
name: 'JSON5',
},
],
plugins: [
resolve(),
commonjs(),
Expand All @@ -36,23 +52,37 @@ module.exports = [
},
// ES6 Modules Non-minified
{
input: 'lib/index.js',
output: {
file: pkg.browser.replace(/\.js$/, '.mjs'),
format: 'esm',
},
input: 'lib/index.mjs',
output: [
{
file: pkg.browser.replace(/\.umd\.js$/, '.esm.js'),
format: 'esm',
},
{
// Legacy path
file: 'dist/index.mjs',
format: 'esm',
},
],
plugins: [
resolve(),
commonjs(),
],
},
// ES6 Modules Minified
{
input: 'lib/index.js',
output: {
file: pkg.browser.replace(/\.js$/, '.min.mjs'),
format: 'esm',
},
input: 'lib/index.mjs',
output: [
{
file: pkg.browser.replace(/\.umd\.js$/, '.esm.min.mjs'),
format: 'esm',
},
{
// Legacy path
file: 'dist/index.min.mjs',
format: 'esm',
},
],
plugins: [
resolve(),
commonjs(),
Expand Down
2 changes: 1 addition & 1 deletion test/errors.js
@@ -1,5 +1,5 @@
const assert = require('assert')
const JSON5 = require('../lib')
const JSON5 = require('..')

require('tap').mochaGlobals()

Expand Down
8 changes: 8 additions & 0 deletions test/index.mjs
@@ -0,0 +1,8 @@
/* eslint-disable import/no-duplicates, node/no-unsupported-features/es-syntax */

import JSON5Modules from '..'
import JSON5CommonJS from '../lib/index.js'

import t from 'tap'

t.strictSame(JSON5Modules, JSON5CommonJS, 'Modules export and CommonJS export are the same')
2 changes: 1 addition & 1 deletion test/parse.js
@@ -1,6 +1,6 @@
const assert = require('assert')
const sinon = require('sinon')
const JSON5 = require('../lib')
const JSON5 = require('..')

require('tap').mochaGlobals()

Expand Down
2 changes: 1 addition & 1 deletion test/stringify.js
@@ -1,5 +1,5 @@
const assert = require('assert')
const JSON5 = require('../lib')
const JSON5 = require('..')

require('tap').mochaGlobals()

Expand Down

0 comments on commit ecb408a

Please sign in to comment.