Skip to content

Commit

Permalink
Rename esm to es everywhere, add systemjs alias (#3381)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Feb 19, 2020
1 parent bbc0484 commit fa273ff
Show file tree
Hide file tree
Showing 72 changed files with 291 additions and 164 deletions.
Expand Up @@ -3,11 +3,11 @@ const newAcornImport = "import * as acorn from 'acorn';\nimport { Parser } from

// by default, rollup-plugin-commonjs will translate require statements as default imports
// which can cause issues for secondary tools that use the ESM version of acorn
export default function fixAcornEsmImport() {
export default function fixAcornEsImport() {
let found = false;

return {
name: 'fix-acorn-esm-import',
name: 'fix-acorn-es-import',
renderChunk(code) {
return code.replace(expectedAcornImport, () => {
found = true;
Expand Down
2 changes: 1 addition & 1 deletion cli/help.md
Expand Up @@ -9,7 +9,7 @@ Basic options:
is unspecified, defaults to rollup.config.js)
-d, --dir <dirname> Directory for chunks (if absent, prints to stdout)
-e, --external <ids> Comma-separate list of module IDs to exclude
-f, --format <format> Type of output (amd, cjs, esm, iife, umd)
-f, --format <format> Type of output (amd, cjs, es, iife, umd, system)
-g, --globals <pairs> Comma-separate list of `moduleID:Global` pairs
-h, --help Show this help message
-i, --input <filename> Input (alternative to <entry file>)
Expand Down
2 changes: 1 addition & 1 deletion docs/00-introduction.md
Expand Up @@ -86,4 +86,4 @@ Rollup can import existing CommonJS modules [through a plugin](https://github.co

#### Publishing ES Modules

To make sure your ES modules are immediately usable by tools that work with CommonJS such as Node.js and webpack, you can use Rollup to compile to UMD or CommonJS format, and then point to that compiled version with the `main` property in your `package.json` file. If your `package.json` file also has a `module` field, ESM-aware tools like Rollup and [webpack 2+](https://webpack.js.org/) will [import the ES module version](https://github.com/rollup/rollup/wiki/pkg.module) directly.
To make sure your ES modules are immediately usable by tools that work with CommonJS such as Node.js and webpack, you can use Rollup to compile to UMD or CommonJS format, and then point to that compiled version with the `main` property in your `package.json` file. If your `package.json` file also has a `module` field, ES-module-aware tools like Rollup and [webpack 2+](https://webpack.js.org/) will [import the ES module version](https://github.com/rollup/rollup/wiki/pkg.module) directly.
4 changes: 2 additions & 2 deletions docs/01-command-line-reference.md
Expand Up @@ -128,7 +128,7 @@ export default [{
},
{
file: 'dist/bundle-b2.js',
format: 'esm'
format: 'es'
}
]
}];
Expand Down Expand Up @@ -213,7 +213,7 @@ Many options have command line equivalents. In those cases, any arguments passed
is unspecified, defaults to rollup.config.js)
-d, --dir <dirname> Directory for chunks (if absent, prints to stdout)
-e, --external <ids> Comma-separate list of module IDs to exclude
-f, --format <format> Type of output (amd, cjs, esm, iife, umd)
-f, --format <format> Type of output (amd, cjs, es, iife, umd, system)
-g, --globals <pairs> Comma-separate list of `moduleID:Global` pairs
-h, --help Show this help message
-i, --input <filename> Input (alternative to <entry file>)
Expand Down
2 changes: 1 addition & 1 deletion docs/04-tutorial.md
Expand Up @@ -378,7 +378,7 @@ Notice how both entry points import the same shared chunk. Rollup will never dup

You can build the same code for the browser via native ES modules, an AMD loader or SystemJS.

For example, with `-f esm` for native modules:
For example, with `-f es` for native modules:

```
rollup src/main.js src/main2.js -f esm -d dist
Expand Down
2 changes: 1 addition & 1 deletion docs/05-plugin-development.md
Expand Up @@ -580,7 +580,7 @@ document.body.appendChild(image);

Similar to assets, emitted chunks can be referenced from within JS code via `import.meta.ROLLUP_FILE_URL_referenceId` as well.

The following example will detect imports prefixed with `register-paint-worklet:` and generate the necessary code and separate chunk to generate a CSS paint worklet. Note that this will only work in modern browsers and will only work if the output format is set to `esm`.
The following example will detect imports prefixed with `register-paint-worklet:` and generate the necessary code and separate chunk to generate a CSS paint worklet. Note that this will only work in modern browsers and will only work if the output format is set to `es`.

```js
// plugin
Expand Down
26 changes: 13 additions & 13 deletions docs/999-big-list-of-options.md
Expand Up @@ -91,21 +91,21 @@ export default {
When using the command line interface, multiple inputs can be provided by using the option multiple times. When provided as the first options, it is equivalent to not prefix them with `--input`:

```sh
rollup --format esm --input src/entry1.js --input src/entry2.js
rollup --format es --input src/entry1.js --input src/entry2.js
# is equivalent to
rollup src/entry1.js src/entry2.js --format esm
rollup src/entry1.js src/entry2.js --format es
```

Chunks can be named by adding an `=` to the provided value:

```sh
rollup main=src/entry1.js other=src/entry2.js --format esm
rollup main=src/entry1.js other=src/entry2.js --format es
```

File names containing spaces can be specified by using quotes:

```sh
rollup "main entry"="src/entry 1.js" "src/other entry.js" --format esm
rollup "main entry"="src/entry 1.js" "src/other entry.js" --format es
```

#### output.dir
Expand All @@ -128,7 +128,7 @@ Specifies the format of the generated bundle. One of the following:

* `amd` – Asynchronous Module Definition, used with module loaders like RequireJS
* `cjs` – CommonJS, suitable for Node and other bundlers
* `esm` – Keep the bundle as an ES module file, suitable for other bundlers and inclusion as a `<script type=module>` tag in modern browsers
* `es` – Keep the bundle as an ES module file, suitable for other bundlers and inclusion as a `<script type=module>` tag in modern browsers
* `iife` – A self-executing function, suitable for inclusion as a `<script>` tag. (If you want to create a bundle for your application, you probably want to use this.)
* `umd` – Universal Module Definition, works as `amd`, `cjs` and `iife` all in one
* `system` – Native format of the SystemJS loader
Expand Down Expand Up @@ -244,11 +244,11 @@ export default {
output: [
{
file: 'bundle.js',
format: 'esm'
format: 'es'
},
{
file: 'bundle.min.js',
format: 'esm',
format: 'es',
plugins: [terser()]
}
]
Expand Down Expand Up @@ -428,7 +428,7 @@ CLI: `--chunkFileNames <pattern>`<br>
Default: `"[name]-[hash].js"`

The pattern to use for naming shared chunks created when code-splitting. Pattern supports the following placeholders:
* `[format]`: The rendering format defined in the output options, e.g. `esm` or `cjs`.
* `[format]`: The rendering format defined in the output options, e.g. `es` or `cjs`.
* `[hash]`: A hash based on the content of the chunk and the content of all its dependencies.
* `[name]`: The name of the chunk. This can be explicitly set via the [`manualChunks`](guide/en/#manualchunks) option or when the chunk is created by a plugin via [`this.emitFile`](guide/en/#thisemitfileemittedfile-emittedchunk--emittedasset--string). Otherwise it will be derived from the chunk contents.

Expand All @@ -447,7 +447,7 @@ CLI: `--entryFileNames <pattern>`<br>
Default: `"[name].js"`

The pattern to use for chunks created from entry points. Pattern supports the following placeholders:
* `[format]`: The rendering format defined in the output options, e.g. `esm` or `cjs`.
* `[format]`: The rendering format defined in the output options, e.g. `es` or `cjs`.
* `[hash]`: A hash based on the content of the entry point and the content of all its dependencies.
* `[name]`: The file name (without extension) of the entry point, unless the object form of input was used to define a different name.

Expand Down Expand Up @@ -566,7 +566,7 @@ export default ({
// will transform e.g. "src/main.js" -> "main.js"
return path.relative('src', relativePath)
},
format: 'esm',
format: 'es',
sourcemap: true
}]
});
Expand Down Expand Up @@ -717,7 +717,7 @@ Type: `boolean`<br>
CLI: `--esModule`/`--no-esModule`
Default: `true`

Whether or not to add a `__esModule: true` property when generating exports for non-ESM formats.
Whether or not to add a `__esModule: true` property when generating exports for non-ES formats.

#### output.exports
Type: `string`<br>
Expand Down Expand Up @@ -846,14 +846,14 @@ Type: `boolean`<br>
CLI: `--strict`/`--no-strict`<br>
Default: `true`

Whether to include the 'use strict' pragma at the top of generated non-ESM bundles. Strictly speaking, ES modules are *always* in strict mode, so you shouldn't disable this without good reason.
Whether to include the 'use strict' pragma at the top of generated non-ES bundles. Strictly speaking, ES modules are *always* in strict mode, so you shouldn't disable this without good reason.

#### output.dynamicImportFunction
Type: `string`<br>
CLI: `--dynamicImportFunction <name>`<br>
Default: `import`

This will rename the dynamic import function to the chosen name when outputting ESM bundles. This is useful for generating code that uses a dynamic import polyfill such as [this one](https://github.com/uupaa/dynamic-import-polyfill).
This will rename the dynamic import function to the chosen name when outputting ES bundles. This is useful for generating code that uses a dynamic import polyfill such as [this one](https://github.com/uupaa/dynamic-import-polyfill).

#### preserveSymlinks
Type: `boolean`<br>
Expand Down
8 changes: 4 additions & 4 deletions rollup.config.js
Expand Up @@ -10,7 +10,7 @@ import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript';
import addBinShebang from './build-plugins/add-bin-shebang';
import conditionalFsEventsImport from './build-plugins/conditional-fsevents-import';
import fixAcornEsmImport from './build-plugins/fix-acorn-esm-import';
import fixAcornEsImport from './build-plugins/fix-acorn-es-import.js';
import generateLicenseFile from './build-plugins/generate-license-file';
import pkg from './package.json';

Expand Down Expand Up @@ -117,10 +117,10 @@ export default command => {

const esmBuild = Object.assign({}, commonJSBuild, {
input: { 'rollup.es.js': 'src/node-entry.ts' },
plugins: [...nodePlugins, fixAcornEsmImport()],
plugins: [...nodePlugins, fixAcornEsImport()],
output: Object.assign({}, commonJSBuild.output, {
chunkFileNames: 'shared-es/[name].js',
format: 'esm',
format: 'es',
sourcemap: false
})
});
Expand All @@ -146,7 +146,7 @@ export default command => {
treeshake,
output: [
{ file: 'dist/rollup.browser.js', format: 'umd', name: 'rollup', banner },
{ file: 'dist/rollup.browser.es.js', format: 'esm', banner }
{ file: 'dist/rollup.browser.es.js', format: 'es', banner }
]
};

Expand Down
6 changes: 3 additions & 3 deletions src/Chunk.ts
Expand Up @@ -263,7 +263,7 @@ export default class Chunk {
: [options.chunkFileNames || '[name]-[hash].js', 'output.chunkFileNames'];
return makeUnique(
renderNamePattern(pattern, patternName, {
format: () => (options.format === 'es' ? 'esm' : (options.format as string)),
format: () => options.format as string,
hash: () =>
includeHash
? this.computeContentHashWithDependencies(
Expand Down Expand Up @@ -298,7 +298,7 @@ export default class Chunk {
{
ext: () => extension.substr(1),
extname: () => extension,
format: () => (options.format === 'es' ? 'esm' : (options.format as string)),
format: () => options.format as string,
name: () => this.getChunkName()
}
);
Expand Down Expand Up @@ -634,7 +634,7 @@ export default class Chunk {
if (options.dynamicImportFunction && format !== 'es') {
this.graph.warn({
code: 'INVALID_OPTION',
message: '"output.dynamicImportFunction" is ignored for formats other than "esm".'
message: '"output.dynamicImportFunction" is ignored for formats other than "es".'
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/esm.ts → src/finalisers/es.ts
Expand Up @@ -3,7 +3,7 @@ import { ChunkDependencies, ChunkExports, ImportSpecifier, ReexportSpecifier } f
import { OutputOptions } from '../rollup/types';
import { FinaliserOptions } from './index';

export default function esm(
export default function es(
magicString: MagicStringBundle,
{ intro, outro, dependencies, exports }: FinaliserOptions,
options: OutputOptions
Expand Down
4 changes: 2 additions & 2 deletions src/finalisers/index.ts
Expand Up @@ -3,7 +3,7 @@ import { ChunkDependencies, ChunkExports } from '../Chunk';
import { OutputOptions, RollupWarning } from '../rollup/types';
import amd from './amd';
import cjs from './cjs';
import esm from './esm';
import es from './es';
import iife from './iife';
import system from './system';
import umd from './umd';
Expand All @@ -29,6 +29,6 @@ export type Finaliser = (
options: OutputOptions
) => MagicStringBundle;

export default { system, amd, cjs, es: esm, iife, umd } as {
export default { system, amd, cjs, es, iife, umd } as {
[format: string]: Finaliser;
};
49 changes: 15 additions & 34 deletions src/rollup/rollup.ts
Expand Up @@ -5,7 +5,7 @@ import Graph from '../Graph';
import { createAddons } from '../utils/addons';
import { assignChunkIds } from '../utils/assignChunkIds';
import commondir from '../utils/commondir';
import { errCannotEmitFromOptionsHook, errInvalidExportOptionValue, error } from '../utils/error';
import { errCannotEmitFromOptionsHook, error } from '../utils/error';
import { writeFile } from '../utils/fs';
import getExportMode from '../utils/getExportMode';
import {
Expand Down Expand Up @@ -33,19 +33,6 @@ import {
WarningHandler
} from './types';

function checkOutputOptions(options: OutputOptions) {
if (['amd', 'cjs', 'system', 'es', 'iife', 'umd'].indexOf(options.format as string) < 0) {
return error({
message: `You must specify "output.format", which can be one of "amd", "cjs", "system", "esm", "iife" or "umd".`,
url: `https://rollupjs.org/guide/en/#output-format`
});
}

if (options.exports && !['default', 'named', 'none', 'auto'].includes(options.exports)) {
return error(errInvalidExportOptionValue(options.exports));
}
}

function getAbsoluteEntryModulePaths(chunks: Chunk[]): string[] {
const absoluteEntryModulePaths: string[] = [];
for (const chunk of chunks) {
Expand Down Expand Up @@ -391,28 +378,22 @@ function normalizeOutputOptions(
hasMultipleChunks: boolean,
outputPluginDriver: PluginDriver
): OutputOptions {
const outputOptionBase = rawOutputOptions.output || inputOptions.output || rawOutputOptions;
let outputOptions = parseOutputOptions(
outputOptionBase as GenericConfigObject,
const outputOptions = parseOutputOptions(
outputPluginDriver.hookReduceArg0Sync(
'outputOptions',
[rawOutputOptions.output || inputOptions.output || rawOutputOptions],
(outputOptions: OutputOptions, result: OutputOptions) => result || outputOptions,
pluginContext => {
const emitError = () => pluginContext.error(errCannotEmitFromOptionsHook());
return {
...pluginContext,
emitFile: emitError,
setAssetSource: emitError
};
}
) as GenericConfigObject,
inputOptions.onwarn as WarningHandler
);
const outputOptionsReducer = (outputOptions: OutputOptions, result: OutputOptions) =>
result || outputOptions;
outputOptions = outputPluginDriver.hookReduceArg0Sync(
'outputOptions',
[outputOptions],
outputOptionsReducer,
pluginContext => {
const emitError = () => pluginContext.error(errCannotEmitFromOptionsHook());
return {
...pluginContext,
emitFile: emitError,
setAssetSource: emitError
};
}
);

checkOutputOptions(outputOptions);

if (typeof outputOptions.file === 'string') {
if (typeof outputOptions.dir === 'string')
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -418,6 +418,7 @@ export type ModuleFormat =
| 'iife'
| 'module'
| 'system'
| 'systemjs'
| 'umd';

export type OptionsPaths = Record<string, string> | ((id: string) => string);
Expand Down

0 comments on commit fa273ff

Please sign in to comment.