Skip to content

Commit

Permalink
Merge branch 'master' into stricter-test-module
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 18, 2020
2 parents 4e931be + 238e399 commit 21e64f1
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 32 deletions.
70 changes: 45 additions & 25 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ jobs:
- "12"
- "11"
- "10"
- "9"
- "8"
- "7"
- "6"
- "5"
- "4"
- "0.12"
- "0.10"

name: Node.js ${{ matrix.node_version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand All @@ -45,30 +37,13 @@ jobs:
node-version: ${{ matrix.node_version }}

- name: Install Dependencies
if: matrix.node_version == '12'
run: yarn

- name: Run Test
if: matrix.node_version != '12'
run: node ./test.js

- name: Run Test with --experimental-modules
if: |
matrix.node_version != '7' &&
matrix.node_version != '6' &&
matrix.node_version != '5' &&
matrix.node_version != '4' &&
matrix.node_version != '0.12' &&
matrix.node_version != '0.10'
run: node --experimental-modules ./test.js

- name: Run Test Coverage
if: matrix.node_version == '12'
run: yarn test-coverage

- name: Upload Coverage
uses: coverallsapp/github-action@master
if: matrix.node_version == '12'
continue-on-error: true
with:
github-token: ${{ secrets.github_token }}
Expand All @@ -85,6 +60,51 @@ jobs:
github-token: ${{ secrets.github_token }}
parallel-finished: true

legacy:
strategy:
fail-fast: false
matrix:
os:
- "ubuntu-latest"
- "macos-latest"
- "windows-latest"
node_version:
- "9"
- "8"
- "7"
- "6"
- "5"
- "4"
- "0.12"
- "0.10"

name: Node.js ${{ matrix.node_version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@master
with:
depth: 1

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

- name: Run Test
run: node ./test.js

- name: Run Test with --experimental-modules
# those version don't support `--experimental-modules`
if: |
matrix.node_version != '7' &&
matrix.node_version != '6' &&
matrix.node_version != '5' &&
matrix.node_version != '4' &&
matrix.node_version != '0.12' &&
matrix.node_version != '0.10'
run: node --experimental-modules ./test.js

lint:
name: Lint
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions fixtures/commonjs-package/name.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {dirname, basename} from 'path'

export const name = basename(dirname(import.meta.url))
6 changes: 6 additions & 0 deletions fixtures/commonjs-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "commonjs-package",
"version": "0.0.0",
"private": true,
"type": "commonjs"
}
2 changes: 2 additions & 0 deletions fixtures/foo.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {}
export const name = 'foo'
3 changes: 3 additions & 0 deletions fixtures/module-package/name.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {dirname, basename} from 'path'

export const name = basename(dirname(import.meta.url))
6 changes: 6 additions & 0 deletions fixtures/module-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-package",
"version": "0.0.0",
"private": true,
"type": "module"
}
1 change: 0 additions & 1 deletion fixtures/test.mjs

This file was deleted.

3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ var TEST_MODULE = 'data:text/javascript,export default ' + RANDOM_NUMBER + ';'

function check() {
try {
// eslint-disable-next-line no-new-func
return new Function('return import("' + TEST_MODULE + '")')().then(
return require('./import')(TEST_MODULE).then(
function(module) {
return module && module.default === RANDOM_NUMBER
},
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "import-esm",
"version": "0.0.0",
"version": "0.0.2",
"description": "Import ECMAScript modules in the safe way",
"keywords": [
"esm",
"ES Module",
"ECMAScript Modules",
"Dynamic import",
"import",
"load",
"mjs"
],
"repository": "fisker/import-esm",
"funding": "https://github.com/fisker/import-esm?sponsor=1",
"license": "MIT",
"author": {
"name": "fisker Cheung",
Expand Down Expand Up @@ -47,6 +50,12 @@
"path": "./node_modules/cz-conventional-changelog"
}
},
"nyc": {
"reporter": [
"text",
"lcov"
]
},
"devDependencies": {
"@commitlint/cli": "8.3.5",
"@fisker/commitlint-config": "1.1.7",
Expand Down
19 changes: 17 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# import-esm

> Import ES Module in the safe way
[![Build Status][github_actions_badge]][github_actions_link]
[![Coverage][coveralls_badge]][coveralls_link]
[![Npm Version][package_version_badge]][package_link]
[![MIT License][license_badge]][license_link]

[github_actions_badge]: https://img.shields.io/github/workflow/status/fisker/import-esm/CI/master?style=flat-square
[github_actions_link]: https://github.com/fisker/import-esm/actions?query=branch%3Amaster
[coveralls_badge]: https://img.shields.io/coveralls/github/fisker/import-esm/master?style=flat-square
[coveralls_link]: https://coveralls.io/github/fisker/import-esm?branch=master
[license_badge]: https://img.shields.io/npm/l/import-esm.svg?style=flat-square
[license_link]: https://github.com/fisker/import-esm/blob/master/license
[package_version_badge]: https://img.shields.io/npm/v/import-esm.svg?style=flat-square
[package_link]: https://www.npmjs.com/package/import-esm

> Import ECMAScript modules in the safe way
## Motivation

Expand Down Expand Up @@ -69,4 +83,5 @@ const importEsm = require('import-esm')

## Known Issue(s)

On `Node.js@<=0.10`, you need polyfill `Promise` yourself.
- On `Node.js@<=0.10`, you need polyfill `Promise` yourself.
- ESM support in Node.js is experimental, though enabled by default in Node.js 13. _You will see messages like `ExperimentalWarning: The ESM module loader is experimental` in console. These are emitted by Node.js, not from this module._
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ check.then(function(result) {
equal(result, supported)
})

var load = importEsm('./fixtures/test.mjs')
var load = importEsm('./fixtures/foo.mjs')
equal(isPromise(load), true)
equal(typeof load.then, 'function')
if (supported) {
load.then(function(module) {
equal(typeof module, 'object')
equal('default' in module, true)
equal(module.name, 'foo')
})
importEsm('./fixtures/commonjs-package/name.mjs').then(function(module) {
equal(module.name, 'commonjs-package')
})
importEsm('./fixtures/module-package/name.mjs').then(function(module) {
equal(module.name, 'module-package')
})
} else {
load.then(null, function(error) {
Expand Down

0 comments on commit 21e64f1

Please sign in to comment.