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

Migrate to ES Module #1309

Merged
merged 6 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions .circleci/config.yml

This file was deleted.

File renamed without changes.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

58 changes: 26 additions & 32 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
push:
branches:
- master
- main
pull_request:
# schedule:
# - cron: "0 23 * * 6"
Expand All @@ -15,12 +15,12 @@ jobs:
matrix:
os:
- "ubuntu-latest"
# - "macos-latest"
# - "windows-latest"
- "macos-latest"
- "windows-latest"
node_version:
- "16"
- "14"
- "12"
# - "10"
# exclude:
# - os: "macos-latest"
# node_version: "12"
Expand All @@ -30,20 +30,18 @@ jobs:
# needs: [build]
steps:
- name: Checkout
uses: actions/checkout@master
with:
depth: 1
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@master
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node_version }}

- name: Install Dependencies
run: yarn

# - name: Download Artifact
# uses: actions/download-artifact@master
# uses: actions/download-artifact@v2
# with:
# name: dist
# path: dist
Expand Down Expand Up @@ -75,39 +73,35 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 1
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@master
uses: actions/setup-node@v2

- name: Install Dependencies
run: yarn

- name: Run Lint
run: yarn lint

build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 1
# build:
# name: Build
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@master
# - name: Setup Node.js
# uses: actions/setup-node@v2

- name: Install Dependencies
run: yarn
# - name: Install Dependencies
# run: yarn

- name: Build Package
run: yarn build
# - name: Build Package
# run: yarn build

- name: Upload Artifact
uses: actions/upload-artifact@master
with:
name: dist
path: dist
# - name: Upload Artifact
# uses: actions/upload-artifact@v2
# with:
# name: dist
# path: dist
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import load from './load.js'
import parse from './parse.js'
import stringify from './stringify.js'

export default {
load,
parse,
stringify,
}

export {load, parse, stringify}
4 changes: 2 additions & 2 deletions src/load.js → lib/load.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as loader from './loader'
import {getFileType} from './utils'
import * as loader from './loader/index.js'
import {getFileType} from './utils/index.js'

function load(file, options) {
if (typeof options === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion src/loader/cjs.js → lib/loader/cjs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importFresh from 'import-fresh'

function loadCjs(file /* , options */) {
async function loadCjs(file /* , options */) {
return importFresh(file)
}

Expand Down
15 changes: 15 additions & 0 deletions lib/loader/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {pathToFileURL} from 'node:url'

function interopDefault(module) {
if (typeof module === 'object' && 'default' in module) {
return module.default
}

return module
}

async function loadEsm(file /* , options */) {
return interopDefault(await import(pathToFileURL(file)))
}

export default loadEsm
23 changes: 23 additions & 0 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {parserToLoader} from '../utils/index.js'
import * as parsers from '../parser/index.js'
import cjs from './cjs.js'
import esm from './esm.js'

const yaml = parserToLoader(parsers.yaml)
const json5 = parserToLoader(parsers.json5)
const toml = parserToLoader(parsers.toml)
const ini = parserToLoader(parsers.ini)
const json = parserToLoader(parsers.json)
const js = async (...arguments_) => {
try {
await esm(...arguments_)
} catch (esmLoadError) {
try {
await cjs(...arguments_)
} catch {
throw esmLoadError
}
}
}

export {esm, yaml, json5, toml, js, cjs, json, ini}
4 changes: 2 additions & 2 deletions src/parse.js → lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as parser from './parser'
import {getFileType} from './utils'
import * as parser from './parser/index.js'
import {getFileType} from './utils/index.js'

function parse(string, options) {
if (typeof options === 'string') {
Expand Down
5 changes: 5 additions & 0 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {default as yaml} from './yaml.js'
export {default as json5} from './json5.js'
export {default as json} from './json.js'
export {default as toml} from './toml.js'
export {default as ini} from './ini.js'
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/parser/json5.js → lib/parser/json5.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {parse} from 'json5'
import json5 from 'json5'

function parseJson5(content /* , options */) {
return parse(content)
return json5.parse(content)
}

export default parseJson5
File renamed without changes.
4 changes: 2 additions & 2 deletions src/parser/yaml.js → lib/parser/yaml.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {safeLoad} from 'js-yaml'
import {load} from 'js-yaml'

function parseYaml(content, options) {
const {filename} = {
filename: '',
...options,
}
return safeLoad(content, {filename})
return load(content, {filename})
}

export default parseYaml
2 changes: 1 addition & 1 deletion src/stringifier/cjs.js → lib/stringifier/cjs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json5Stringify from './json5'
import json5Stringify from './json5.js'

function stringifyCjs(data, options) {
return `module.exports = ${json5Stringify(data, options)};`
Expand Down
2 changes: 1 addition & 1 deletion src/stringifier/esm.js → lib/stringifier/esm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json5Stringify from './json5'
import json5Stringify from './json5.js'

function stringifyEsm(data, options) {
return `export default ${json5Stringify(data, options)};`
Expand Down
7 changes: 7 additions & 0 deletions lib/stringifier/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {default as json} from './json.js'
export {default as json5} from './json5.js'
export {default as yaml} from './yaml.js'
export {default as toml} from './toml.js'
export {default as cjs} from './cjs.js'
export {default as esm} from './esm.js'
export {default as ini} from './ini.js'
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/stringifier/json5.js → lib/stringifier/json5.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {stringify} from 'json5'
import json5 from 'json5'

function stringifyJson5(data, options) {
const {pretty} = {
pretty: false,
...options,
}
return pretty ? stringify(data, undefined, 2) : stringify(data)
return pretty ? json5.stringify(data, undefined, 2) : json5.stringify(data)
}

export default stringifyJson5
File renamed without changes.
4 changes: 2 additions & 2 deletions src/stringifier/yaml.js → lib/stringifier/yaml.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {safeDump} from 'js-yaml'
import {dump} from 'js-yaml'

function stringifyYaml(data, options) {
const {pretty, sort} = {
Expand All @@ -7,7 +7,7 @@ function stringifyYaml(data, options) {
...options,
}

return safeDump(data, {
return dump(data, {
noArrayIndent: !pretty,
sortKeys: sort,
lineWidth: pretty ? 80 : -1,
Expand Down
2 changes: 1 addition & 1 deletion src/stringify.js → lib/stringify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as stringifier from './stringifier'
import * as stringifier from './stringifier/index.js'

function stringify(data, options) {
if (typeof options === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/get-file-type.js → lib/utils/get-file-type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path'
import path from 'node:path'

const fileTypes = {
'.mjs': 'esm',
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as parserToLoader} from './parser-to-loader.js'
export {default as getFileType} from './get-file-type.js'
14 changes: 14 additions & 0 deletions lib/utils/parser-to-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {promises as fs} from 'node:fs'

function parserToLoader(parser) {
return async function loader(filename, options) {
const content = await fs.readFile(filename, 'utf8')

return parser(content, {
filename,
...options,
})
}
}

export default parserToLoader
File renamed without changes.
Loading