Overview
Editions are the best way to produce and consume the JavaScript packages you care about. With Editions you can produce packages beautifully, and consume packages perfectly.
Last updated
Was this helpful?
Editions are the best way to produce and consume the JavaScript packages you care about. With Editions you can produce packages beautifully, and consume packages perfectly.
Last updated
Was this helpful?
Editions is for describing the ways in which your project has been produced to accelerate manual consumption, as well as automatic consumption through .
JavaScript production and consumption has gotten difficult over the years.
Production use to be as easy as publishing your source code, which worked across all environments, with a few minor tweaks. Consumption was as easy as including the package, and you are done.
An edition is each variation of your code. Usually this comes in the form of your source edition, as well as compiled editions for each environment you wish to support.
Practically, editions are specified in descending order of preference in the editions
field of your package.json
, with each edition being composed of the following fields:
a description
field to describe the edition
a directory
field for where the edition is located
a entry
field for the default file inside the directory
to be loaded
You can find the full technical specification here:
For a project that has a source edition written in ESNext using require('some-package')
syntax, with a compiled edition for the default browsers, as well as a compiled edition for older node versions, then a compatible editions definition for it would look like so:
{
"editions": [
{
"description": "esnext source code with require for modules",
"directory": "source",
"entry": "index.js",
"tags": [
"javascript",
"esnext",
"require"
],
"engines": {
"node": ">=6",
"browsers": false
}
},
{
"description": "esnext compiled for browsers with require for modules",
"directory": "edition-browsers",
"entry": "index.js",
"tags": [
"javascript",
"require"
],
"engines": {
"node": false,
"browsers": "defaults"
}
},
{
"description": "esnext compiled for node.js >=0.8 with require for modules",
"directory": "edition-node-0.8",
"entry": "index.js",
"tags": [
"javascript",
"require"
],
"engines": {
"node": ">=0.8",
"browsers": false
}
}
]
}
Editions can be consumed in multiple ways, here are the options:
For producers who only want one edition to be used by default, they can specify the default edition to be loaded via the standard main
property of the package.json
file:
{
"main": "edition-node-0.8/index.js"
}
Inside your project, install the Editions Autoloader package via npm install --save editions
Create a root index.js
file that uses the Editions Autoloader to load the best edition from our available compatible editions.
'use strict'
/** @type {typeof import("./source/index.js") } */
module.exports = require('editions').requirePackage(__dirname, require)
Set the package.json
property main
to point to the index.js
file above, instead of a specfic edition.
{
"main": "index.js"
}
For binary executables or testing, we then we would want to specify a non-default entry to load for the editions. We can do this by passing the custom entry point as an extra argument to the requirePackage
function.
To specify a bin.js
custom entry, then we would perform the following.
Create a root bin.js
file that uses the Editions Autoloader to load the best bin.js
script from our available compatible editions.
#!/usr/bin/env node
'use strict'
/** @type {typeof import("./source/bin.js") } */
module.exports = require('editions').requirePackage(__dirname, require, 'bin.js')
Set the bin
property in our package.json
file to point to the root bin.js
file we just created.
{
"bin": "bin.js"
}
To specify a test.js
custom entry, then we would perform the following.
Create a root test.js
file that uses the Editions Autoloader to load the best test.js
script from our available compatible editions.
'use strict'
/** @type {typeof import("./source/test.js") } */
module.exports = require('editions').requirePackage(__dirname, require, 'test.js')
Set the scripts.test
property in our package.json
file to point to the root test.js
file we just created.
{
"scripts": {
"test": "node ./test.js"
}
}
// using require
require('a-editioned-package/a-custom-edition/')
// using import
import blah from 'a-editioned-package/a-custom-edition/'
{
"browser": "edition-browsers/index.js"
}
This usage of the browser
field tells most tools like Browserify, WebPack, and Rollup to specifically use the edition that we precompiled for the browsers we target for.
You can find more information about the browser
field and its equivalents here:
You can find tooling that already has builtin support for the Editions specification here:
{
"main": "editions-node-0.8/index.js"
}
This will have Projectz turn the <!-- INSTALL -->
comment in our README.md
file into the following rendered output:
require('project')
aliasesrequire('project/edition-node-0.8')
However, these days, code may be run anywhere, in all sorts of browsers, desktop environments, and devices, of varying capabilities, not always known by the producer. JavaScript has also evolved, incorporating a lot of modern features that save developers time, but not supported across all possible environments - either requiring abstinence of time saving features, or eliminating environment support, or compilation on either the producer or consumer side - .
Editions comes in to solve this problem, elegantly, and , that works with current environments and development setups. Producers are able to produce their packages in their ideal configurations, then publish the package with multiple editions for the consumers to consume at their digression. Consumers are made aware of this through , and can - and by default, . All the complexity of modern JavaScript publishing is solved, for the consumer and publisher.
If you wish to delve further, refer to the document for a complete understanding of the problem space and why editions is a superior solution in it.
an optional tags
field for to describe the edition that tooling can utilise
Tools like can automatically create for you the editions definition as well as the editions themselves.
For producers who want consumers to automatically load the best edition for the consumers particular environment, you can make use of the package.
For consumers who , they can opt into a non-standard edition by specifying it manually in their consumption.
For our , we would define the browser
field in our package.json
like so:
If producers wish to inform consumers of the editions they provide, which is not necessary for consumption, but useful to the consumer, then producers can utilise to inject the appropriate editions information into your README.md
file via the HTML comment <!-- INSTALL -->
.
If we partner our with the following package.json
fields:
require('project/source')
is source code with
require('project/edition-browsers')
is compiled for browsers with
require('project/edition-node-0.8')
is compiled for >=0.8 with
If we partner our to , then Projectz will turn the <!-- INSTALL -->
comment in our README.md
file into the following rendered output:
require('project')
aliases require('project/index.js')
which uses the to automatically select the correct edition for the consumers environment
require('project/source')
is source code with
require('project/edition-browsers')
is compiled for browsers with
require('project/edition-node-0.8')
is compiled for >=0.8 with