Skip to content

Commit

Permalink
Rename @std/esm to esm.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Mar 8, 2018
1 parent 68e18d2 commit f73561d
Show file tree
Hide file tree
Showing 28 changed files with 62 additions and 95 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright @std/esm contributors
Copyright esm contributors

Based on reify, copyright Ben Newman <https://github.com/benjamn/reify>

Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @std/esm
# esm

This fast, small, zero-dependency package is all you need to enable
ES modules in Node 6+ today!
Expand All @@ -10,53 +10,53 @@ for all the details.
Getting started
---

Run `npm i --save @std/esm` in your app or package directory.
Run `npm i --save esm` in your app or package directory.

There are three ways to enable ESM with `@std/esm`.
There are three ways to enable ESM with `esm`.

1. Enable ESM with a CJS bridge:

**index.js**
```js
// Provide options as a parameter, environment variable, or rc file.
require = require("@std/esm")(module/*, options*/)
require = require("esm")(module/*, options*/)
module.exports = require("./main.mjs").default
```

2. Enable ESM in the Node CLI with the [`-r` option](https://nodejs.org/api/cli.html#cli_r_require_module):

```shell
node -r @std/esm main.mjs
node -r esm main.mjs
```

3. Enable ESM in the Node REPL:

```shell
node -r @std/esm
node -r esm
```

or upon entering:

```shell
$ node
> require("@std/esm")
@std/esm enabled
> require("esm")
esm enabled
```

*Note: All `"cjs"` options are [unlocked](#unlockables) in the Node REPL.*

Standard Features
---

The `@std/esm` loader is as spec-compliant
The `esm` loader is as spec-compliant
as possible and follows Node’s [ESM rules](https://github.com/nodejs/node-eps/blob/master/002-es-modules.md).

:point_right: This means, by default, ESM requires the use of the `.mjs` file
extension.<br>
:unlock: You can [unlock](#unlockables) ESM with the `.js` file extension using
the `"js"` ESM mode.

Out of the box `@std/esm` just works, no configuration necessary, and supports:
Out of the box `esm` just works, no configuration necessary, and supports:

* [`import`](https://ponyfoo.com/articles/es6-modules-in-depth#import) / [`export`](https://ponyfoo.com/articles/es6-modules-in-depth#export)
* [`import.meta`](https://github.com/tc39/proposal-import-meta)
Expand All @@ -73,15 +73,15 @@ Unlockables

Unlock features with options specified as one of the following:

* The `"@std/esm"` field in your package.json
* The `"esm"` field in your package.json
* [JSON6](https://github.com/d3x0r/json6) in an .esmrc or .esmrc.json file
* JSON6 or file path in the `ESM_OPTIONS` environment variable
* CJS/ESM in an .esmrc.js or .esmrc.mjs file

Commonly used options may be specified in shorthand form:

* `"@std/esm":"js"` is shorthand for `"@std/esm":{"mode":"js"}`
* `"@std/esm":"cjs"` is shorthand for `"@std/esm":{"cjs":true,"mode":"js"}`
* `"esm":"js"` is shorthand for `"esm":{"mode":"js"}`
* `"esm":"cjs"` is shorthand for `"esm":{"cjs":true,"mode":"js"}`

<table>
<tr>
Expand Down Expand Up @@ -205,20 +205,20 @@ DevOpts

Tips
---
* Load `@std/esm` before
* Load `esm` before
[`@babel/register`](https://github.com/babel/babel/tree/master/packages/babel-register) v7+
* Load `@std/esm` with the *“require”* option of
* Load `esm` with the *“require”* option of
[`ava`](https://github.com/avajs/ava#options),
[`mocha`](https://mochajs.org/#-r---require-module-name),
[`nyc`](https://github.com/istanbuljs/nyc#require-additional-modules), and
[`tape`](https://github.com/substack/tape#preloading-modules)
* Load `@std/esm` with the `--node-arg=-r --node-arg=@std/esm` option of
* Load `esm` with the `--node-arg=-r --node-arg=esm` option of
[`node-tap`](http://www.node-tap.org/cli/)
* Load `@std/esm` with the `--node-args="-r @std/esm"` option of
* Load `esm` with the `--node-args="-r esm"` option of
[`pm2`](http://pm2.keymetrics.io/docs/usage/quick-start/#options)
* Load `@std/esm` with [`wallaby.js`](https://wallabyjs.com/docs/integration/node.html#es-modules)
* Use `@std/esm` to load [`jasmine`](https://jasmine.github.io/setup/nodejs.html#a-simple-example-using-the-library)
* Use `"@std/esm":"cjs"` for the `--watch` and `--watch-extensions` options of
* Load `esm` with [`wallaby.js`](https://wallabyjs.com/docs/integration/node.html#es-modules)
* Use `esm` to load [`jasmine`](https://jasmine.github.io/setup/nodejs.html#a-simple-example-using-the-library)
* Use `"esm":"cjs"` for the `--watch` and `--watch-extensions` options of
[`mocha`](https://mochajs.org/#-w---watch)
* Use `"@std/esm":"cjs"` for [`ava`](https://github.com/avajs/ava) and [`webpack`](https://github.com/webpack/webpack)
* When in doubt, use `"@std/esm":"cjs"`
* Use `"esm":"cjs"` for [`ava`](https://github.com/avajs/ava) and [`webpack`](https://github.com/webpack/webpack)
* When in doubt, use `"esm":"cjs"`
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ __shared__.global = global

defineProperty(makeRequireFunction, inspectKey, {
__proto__: null,
value: () => "@std/esm enabled"
value: () => "esm enabled"
})

freeze(makeRequireFunction)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@std/esm",
"name": "esm",
"version": "0.25.2",
"description": "Enable ES modules in Node today!",
"keywords": "commonjs, ecmascript, export, import, modules, node, require",
Expand All @@ -8,7 +8,7 @@
"author": "John-David Dalton <[email protected]>",
"main": "index.js",
"private": true,
"@std/esm": true,
"esm": true,
"engines": {
"node": ">=6"
},
Expand Down
2 changes: 1 addition & 1 deletion script/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const defaultScripts = `,
}`

const fieldsToRemove = [
"@std/esm",
"devDependencies",
"esm",
"private"
]

Expand Down
2 changes: 1 addition & 1 deletion src/constant/prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import encodeId from "../util/encode-id.js"

const PREFIX = {
__proto__: null,
STD_ESM: encodeId("@std/esm")
ESM_PKG: encodeId("esm")
}

export default PREFIX
4 changes: 2 additions & 2 deletions src/own/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assign from "../util/assign.js"
import shared from "../shared.js"

const {
STD_ESM
ESM_PKG
} = PREFIX

const markerDescriptor = {
Expand All @@ -23,7 +23,7 @@ function init() {

proto[shared.symbol.inspect] = customInspect
handler = { __proto__: proto }
Reflect.defineProperty(handler, STD_ESM + ":proxy", markerDescriptor)
Reflect.defineProperty(handler, ESM_PKG + ":proxy", markerDescriptor)

const proxy = new Proxy(target, handler)

Expand Down
20 changes: 8 additions & 12 deletions src/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Package {
if (typeof options.cache === "string") {
cachePath = resolve(dirPath, options.cache)
} else if (options.cache !== false) {
cachePath = resolve(dirPath, "node_modules/.cache/@std/esm")
cachePath = resolve(dirPath, "node_modules/.cache/esm")
} else {
cachePath = ""
}
Expand Down Expand Up @@ -289,8 +289,8 @@ function getRange(json, name) {
if (has(json, name)) {
const object = json[name]

if (has(object, "@std/esm")) {
return validRange(object["@std/esm"])
if (has(object, "esm")) {
return validRange(object["esm"])
}
}

Expand Down Expand Up @@ -382,13 +382,9 @@ function readInfo(dirPath, force) {
pkgParsed = true
pkgJSON = parseJSON(pkgJSON)

if (has(pkgJSON, "@std/esm")) {
if (has(pkgJSON, "esm")) {
optionsFound = true
options = pkgJSON["@std/esm"]
} else if (has(pkgJSON, "@std") &&
has(pkgJSON["@std"], "esm")) {
optionsFound = true
options = pkgJSON["@std"].esm
options = pkgJSON["esm"]
}
}

Expand All @@ -405,9 +401,9 @@ function readInfo(dirPath, force) {
pkgJSON = parseJSON(pkgJSON)
}

// A package.json may have `@std/esm` in its "devDependencies" object
// because it expects another package or application to enable ESM loading
// in production, but needs `@std/esm` during development.
// A package.json may have `esm` in its "devDependencies" object because
// it expects another package or application to enable ESM loading in
// production, but needs `esm` during development.
range =
getRange(pkgJSON, "dependencies") ||
getRange(pkgJSON, "peerDependencies")
Expand Down
12 changes: 6 additions & 6 deletions src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import satisfies from "./util/satisfies.js"
import setDeferred from "./util/set-deferred.js"

const {
STD_ESM
ESM_PKG
} = PREFIX

let shared
Expand All @@ -25,10 +25,10 @@ if (__shared__) {

const symbol = {
__proto__: null,
_compile: Symbol.for(STD_ESM + ":module._compile"),
_compile: Symbol.for(ESM_PKG + ":module._compile"),
inspect: inspect.custom,
mjs: Symbol.for(STD_ESM + ':Module._extensions[".mjs"]'),
wrapper: Symbol.for(STD_ESM + ":wrapper")
mjs: Symbol.for(ESM_PKG + ':Module._extensions[".mjs"]'),
wrapper: Symbol.for(ESM_PKG + ":wrapper")
}

shared = {
Expand Down Expand Up @@ -143,11 +143,11 @@ if (__shared__) {
)

setDeferred(support, "inspectProxies", () => {
const proxy = new Proxy({ __proto__: null }, { __proto__: null, [STD_ESM]: 1 })
const proxy = new Proxy({ __proto__: null }, { __proto__: null, [ESM_PKG]: 1 })
const inspected = shared.inspect(proxy, { __proto__: null, showProxy: true })

return inspected.startsWith("Proxy") &&
inspected.indexOf(STD_ESM) !== -1
inspected.indexOf(ESM_PKG) !== -1
})

setDeferred(support, "internalModuleReadFile", () =>
Expand Down
4 changes: 2 additions & 2 deletions src/util/is-own-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isObjectLike from "./is-object-like.js"
import shared from "../shared.js"

const {
STD_ESM
ESM_PKG
} = PREFIX

const inspectOptions = {
Expand Down Expand Up @@ -34,7 +34,7 @@ function isOwnProxy(value) {
const inspected = shared.inspect(value, inspectOptions)

return inspected.startsWith("Proxy") &&
inspected.endsWith("'" + STD_ESM + ":proxy': 1 } ]")
inspected.endsWith("'" + ESM_PKG + ":proxy': 1 } ]")
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import SemVer from "semver"

// The `process.env.ESM_VERSION` reference is replaced, at build time, with the
// `@std/esm` version string. See https://webpack.js.org/plugins/environment-plugin/.
// `esm` version string. See https://webpack.js.org/plugins/environment-plugin/.
const semver = new SemVer(process.env.ESM_VERSION)
const { major, minor, patch, version } = semver
const name = "@std/esm@" + version
const name = "esm@" + version

export { name, major, minor, patch, version }
export default semver
2 changes: 1 addition & 1 deletion src/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This module is important for `@std/esm` versioning support and should be
// This module is important for `esm` versioning support and should be
// changed as little as possible. Please ensure any changes are backwards
// compatible.

Expand Down
2 changes: 1 addition & 1 deletion test/compiler-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe("compiler", () => {

it("should support shebangs", () => {
const code = [
"#!/usr/bin/env node -r @std/esm",
"#!/usr/bin/env node -r esm",
'import a from "a"'
].join("\n")

Expand Down
1 change: 0 additions & 1 deletion test/fixture/options-file/@std-object/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions test/fixture/options-file/@std-object/package.json

This file was deleted.

1 change: 0 additions & 1 deletion test/fixture/options-file/@std-string-cjs/index.mjs

This file was deleted.

7 changes: 0 additions & 7 deletions test/fixture/options-file/@std-string-cjs/package.json

This file was deleted.

1 change: 0 additions & 1 deletion test/fixture/options-file/@std-string-js/index.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/fixture/options-file/@std-string-js/package.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@std-esm-object",
"private": true,
"@std/esm": {
"esm": {
"mode": "js"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@std-esm-string-cjs",
"private": true,
"@std/esm": "cjs"
"esm": "cjs"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@std-esm-string-js",
"private": true,
"@std/esm": "js"
"esm": "js"
}
2 changes: 1 addition & 1 deletion test/fixture/options-priority/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "options-priority",
"private": true,
"@std/esm": "mjs"
"esm": "mjs"
}
2 changes: 1 addition & 1 deletion test/main-hook-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const pkgPath = path.resolve("../index.js")
const pkgJSON = JSON6.parse(fs.readFileSync("../package.json"))
const pkgOptions = fs.pathExistsSync(".esmrc")
? JSON6.parse(fs.readFileSync(".esmrc"))
: pkgJSON["@std/esm"]
: pkgJSON["esm"]

const testPath = path.resolve(".")
const testURL = fileProtocol + testPath.replace(/\\/g, "/")
Expand Down
Loading

0 comments on commit f73561d

Please sign in to comment.