Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rollup/plugins
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d9a127755f5202741c6409d0b568885b6542d308
Choose a base ref
...
head repository: rollup/plugins
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 026afa307aa14e7a877da493c7c8f4bf138cead4
Choose a head ref
  • 8 commits
  • 41 files changed
  • 4 contributors

Commits on Apr 4, 2023

  1. chore(release): alias v4.0.4

    Release Workflow committed Apr 4, 2023
    1
    Copy the full SHA
    41686bc View commit details
  2. feat(swc): initial release (#1412)

    * feat(swc): plugin for the speedy web compiler
    
    * fix(swc): types definition
    
    * fix(swc): use type infering for renderChunk
    
    * types(swc): remove unnecessary type import
    
    * types(swc): remove unnecessary type import
    
    * style(swc): fix linting issues
    
    * style(swc): capitalize babel string
    
    * docs(swc): mention alternatives in README.md
    
    * fix(swc): use transform instead renderChunk hook
    
    * fix(swc): move swc options to object property
    
    * test(swc): add typescript test for verifying transformation
    
    * style(swc): remove logging
    
    * style(swc): fix styling and spacing in README.md
    
    * build: define swc as dep instead peer-dep
    
    * fix: set engine version to >= v14.0
    
    * fix: set swc as peer-dep + specified rollup v3 as min version
    
    * docs: updated min node engine version in README
    tada5hi authored Apr 4, 2023
    2
    Copy the full SHA
    f8b251c View commit details
  3. chore(release): swc v0.1.0

    Release Workflow committed Apr 4, 2023
    2
    Copy the full SHA
    93a95d8 View commit details
  4. feat(esm-shim): initial release (#1415)

    * feat(commonjs-shim): initial release
    
    * build(commonjs-shim): fix pnpm lock file
    
    * build(commonjs-shim): fix type generation
    
    * build(commonjs-shim): use default procedure to provide types
    
    * fix: refactor & implemented requested changes
    
    * build: fix frozen lock file
    
    * docs: add unbuild reference + use tsconfig file link
    
    * fix: renderChunk arg + add match-all polyfill
    tada5hi authored Apr 4, 2023
    2
    Copy the full SHA
    3b20853 View commit details
  5. chore(release): esm-shim v0.1.0

    Release Workflow committed Apr 4, 2023
    1
    Copy the full SHA
    b4abf23 View commit details
  6. 1
    Copy the full SHA
    38e5ff0 View commit details
  7. feat(alias)!: resolve entry id (#1402)

    BREAKING CHANGE: Aliased path without an importer (e.g. Rollup input) will also be resolved
    bluwy authored Apr 4, 2023
    1
    Copy the full SHA
    a0242b2 View commit details
  8. chore(release): alias v5.0.0

    Release Workflow committed Apr 4, 2023
    Copy the full SHA
    026afa3 View commit details
Showing with 882 additions and 14 deletions.
  1. +2 βˆ’0 CODEOWNERS
  2. +2 βˆ’0 README.md
  3. +16 βˆ’0 packages/alias/CHANGELOG.md
  4. +1 βˆ’1 packages/alias/package.json
  5. +0 βˆ’3 packages/alias/src/index.ts
  6. +2 βˆ’2 packages/alias/test/test.mjs
  7. +9 βˆ’0 packages/esm-shim/CHANGELOG.md
  8. +57 βˆ’0 packages/esm-shim/README.md
  9. +70 βˆ’0 packages/esm-shim/package.json
  10. +10 βˆ’0 packages/esm-shim/rollup.config.mjs
  11. +14 βˆ’0 packages/esm-shim/src/constants.ts
  12. +10 βˆ’0 packages/esm-shim/src/index.ts
  13. +16 βˆ’0 packages/esm-shim/src/module.ts
  14. +6 βˆ’0 packages/esm-shim/src/types.ts
  15. +50 βˆ’0 packages/esm-shim/src/utils.ts
  16. +1 βˆ’0 packages/esm-shim/test/fixtures/child.js
  17. +3 βˆ’0 packages/esm-shim/test/fixtures/cjs.js
  18. +51 βˆ’0 packages/esm-shim/test/snapshots/test.js.md
  19. BIN packages/esm-shim/test/snapshots/test.js.snap
  20. +43 βˆ’0 packages/esm-shim/test/test.js
  21. +14 βˆ’0 packages/esm-shim/test/types.ts
  22. +4 βˆ’0 packages/esm-shim/tsconfig.json
  23. +8 βˆ’0 packages/esm-shim/types/index.d.ts
  24. +21 βˆ’0 packages/esm-shim/unbuild.license
  25. +2 βˆ’2 packages/node-resolve/src/resolveImportSpecifiers.js
  26. +9 βˆ’0 packages/swc/CHANGELOG.md
  27. +70 βˆ’0 packages/swc/README.md
  28. +73 βˆ’0 packages/swc/package.json
  29. +10 βˆ’0 packages/swc/rollup.config.mjs
  30. +5 βˆ’0 packages/swc/src/index.ts
  31. +36 βˆ’0 packages/swc/src/module.ts
  32. +5 βˆ’0 packages/swc/src/type.ts
  33. +1 βˆ’0 packages/swc/test/fixtures/export-default.js
  34. +1 βˆ’0 packages/swc/test/fixtures/export.js
  35. +3 βˆ’0 packages/swc/test/fixtures/test.ts
  36. +7 βˆ’0 packages/swc/test/fixtures/unminified.js
  37. +85 βˆ’0 packages/swc/test/test.js
  38. +14 βˆ’0 packages/swc/test/types.ts
  39. +8 βˆ’0 packages/swc/tsconfig.json
  40. +11 βˆ’0 packages/swc/types/index.d.ts
  41. +132 βˆ’6 pnpm-lock.yaml
2 changes: 2 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
* @shellscape

/packages/babel/ @Andarist
/packages/esm-shim/ @tada5hi
/packages/image/ @tjenkinson
/packages/node-resolve/ @tjenkinson
/packages/swc/ @tada5hi
/packages/terser/ @tada5hi
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ This repository houses plugins that Rollup considers critical to every day use o
| [dsv](packages/dsv) | Convert .csv and .tsv files into JavaScript modules with d3-dsv |
| [dynamic-import-vars](packages/dynamic-import-vars) | Resolving dynamic imports that contain variables. |
| [eslint](packages/eslint) | Verify entry point and all imported files with ESLint |
| [esm-shim](packages/esm-shim) | Replace cjs syntax for esm output bundles |
| [graphql](packages/graphql) | Convert .gql/.graphql files to ES6 modules |
| [html](packages/html) | Create HTML files to serve Rollup bundles |
| [image](packages/image) | Import JPG, PNG, GIF, SVG, and WebP files |
@@ -42,6 +43,7 @@ This repository houses plugins that Rollup considers critical to every day use o
| [run](packages/run) | Run your bundles in Node once they're built |
| [strip](packages/strip) | Remove debugger statements and functions like assert.equal and console.log from your code |
| [sucrase](packages/sucrase) | Compile TypeScript, Flow, JSX, etc with Sucrase |
| [swc](packages/swc) | Transpile TypeScript/JavaScript with the speedy-web-compiler |
| [terser](packages/terser) | Generate a minified output bundle with terser |
| [typescript](packages/typescript) | Integration between Rollup and Typescript |
| [url](packages/url) | Import files as data-URIs or ES Modules |
16 changes: 16 additions & 0 deletions packages/alias/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# @rollup/plugin-alias ChangeLog

## v5.0.0

_2023-04-04_

### Breaking Changes

- feat: resolve entry id [#1402](https://github.com/rollup/plugins/pull/1402)

## v4.0.4

_2023-04-04_

### Updates

- docs: fix README esm error [#1451](https://github.com/rollup/plugins/pull/1451)

## v4.0.3

_2023-01-20_
2 changes: 1 addition & 1 deletion packages/alias/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rollup/plugin-alias",
"version": "4.0.3",
"version": "5.0.0",
"publishConfig": {
"access": "public"
},
3 changes: 0 additions & 3 deletions packages/alias/src/index.ts
Original file line number Diff line number Diff line change
@@ -81,9 +81,6 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
);
},
resolveId(importee, importer, resolveOptions) {
if (!importer) {
return null;
}
// First match is supposed to be the correct one
const matchedEntry = entries.find((entry) => matches(entry.find, importee));
if (!matchedEntry) {
4 changes: 2 additions & 2 deletions packages/alias/test/test.mjs
Original file line number Diff line number Diff line change
@@ -163,14 +163,14 @@ test('Will not confuse modules with similar names', (t) =>
]
).then((result) => t.deepEqual(result, [null, null, null])));

test('Leaves entry file untouched if matches alias', (t) =>
test('Alias entry file', (t) =>
resolveAliasWithRollup(
{
entries: [{ find: 'abacaxi', replacement: './abacaxi' }]
},
// eslint-disable-next-line no-undefined
[{ source: 'abacaxi/entry.js' }]
).then((result) => t.deepEqual(result, [null])));
).then((result) => t.deepEqual(result, ['./abacaxi/entry.js'])));

test('i/am/a/file', (t) =>
resolveAliasWithRollup(
9 changes: 9 additions & 0 deletions packages/esm-shim/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @rollup/plugin-esm-shim ChangeLog

## v0.1.0

_2023-04-04_

### Features

- feat: initial release [#1415](https://github.com/rollup/plugins/pull/1415)
57 changes: 57 additions & 0 deletions packages/esm-shim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[npm]: https://img.shields.io/npm/v/@rollup/plugin-esm-shim
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-esm-shim
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-esm-shim
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-esm-shim

[![npm][npm]][npm-url]
[![size][size]][size-url]
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)

# @rollup/plugin-esm-shim

🍣 A Rollup plugin to replace CJS global variables within esm output bundles.

The list of global variables that are shimmed are:

- `__filename`: This variable corresponds to the file path of the generated bundle file.
- `__dirname`: This variable corresponds to the folder path of the created bundle file.
- `require`: Corresponds to a function to import modules in a synchronous way.

## Requirements

This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v2.0+.

## Install

Using npm:

```console
npm install @rollup/plugin-esm-shim --save-dev
```

## Usage

Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:

```typescript
import esmShim from '@rollup/plugin-esm-shim';

export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [esmShim()]
};
```

Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)

[LICENSE (MIT)](/LICENSE)

Portions of this code were used with permission from [unbuild](https://github.com/unjs/unbuild). [unbuild LICENSE](./unbuild.LICENSE)
70 changes: 70 additions & 0 deletions packages/esm-shim/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "@rollup/plugin-esm-shim",
"version": "0.1.0",
"publishConfig": {
"access": "public"
},
"description": "Generate minified bundle",
"license": "MIT",
"repository": {
"url": "rollup/plugins",
"directory": "packages/esm-shim"
},
"author": "Peter Placzek <peter.placzek1996@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/esm-shim#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
"exports": {
"types": "./types/index.d.ts",
"import": "./dist/es/index.js",
"default": "./dist/cjs/index.js"
},
"engines": {
"node": ">=14.0.0"
},
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:lint": "pnpm build && pnpm lint",
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm test -- --verbose && pnpm test:ts",
"prebuild": "del-cli dist",
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
"prerelease": "pnpm build",
"pretest": "pnpm build",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
"test": "ava",
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
},
"files": [
"dist",
"!dist/**/*.map",
"src",
"types",
"README.md"
],
"keywords": [
"rollup",
"plugin",
"esm-shim",
"npm",
"modules"
],
"peerDependencies": {
"rollup": "^2.x || ^3.x"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"magic-string": "^0.27.0"
},
"devDependencies": {
"rollup": "^3.0.0-7",
"typescript": "^4.8.3"
},
"types": "./types/index.d.ts"
}
10 changes: 10 additions & 0 deletions packages/esm-shim/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { readFileSync } from 'node:fs';

import { createConfig } from '../../shared/rollup.config.mjs';

export default {
...createConfig({
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), { encoding: 'utf-8' }))
}),
input: 'src/index.ts'
};
14 changes: 14 additions & 0 deletions packages/esm-shim/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const CJSyntaxRegex = /__filename|__dirname|require\(|require\.resolve\(/;

export const ESMShim = `
// -- Shims --
import cjsUrl from 'url';
import cjsPath from 'path';
import cjsModule from 'module';
const __filename = cjsUrl.fileURLToPath(import.meta.url);
const __dirname = cjsPath.dirname(__filename);
const require = cjsModule.createRequire(import.meta.url);
`;

export const ESMStaticImportRegex =
/(?<=\s|^|;)import\s*([\s"']*(?<imports>[\w\t\n\r $*,/{}]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gm;
10 changes: 10 additions & 0 deletions packages/esm-shim/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2023.
* The core of this plugin was conceived by pi0 and is taken from the following repository:
* https://github.com/unjs/unbuild/blob/main/src/builder/plugins/cjs.ts
*/
import { esmShim } from './module';

export * from './types';
export * from './utils';
export default esmShim;
16 changes: 16 additions & 0 deletions packages/esm-shim/src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Plugin } from 'rollup';

import { provideCJSSyntax } from './utils';

export function esmShim(): Plugin {
return {
name: 'esm-shim',
renderChunk(code, _chunk, opts) {
if (opts.format === 'es') {
return provideCJSSyntax(code);
}

return null;
}
};
}
6 changes: 6 additions & 0 deletions packages/esm-shim/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { SourceMapInput } from 'rollup';

export interface Output {
code: string;
map?: SourceMapInput;
}
50 changes: 50 additions & 0 deletions packages/esm-shim/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import MagicString from 'magic-string';

// Shim __dirname, __filename and require
import { ESMShim, CJSyntaxRegex, ESMStaticImportRegex } from './constants';
import type { Output } from './types';

function matchAllPolyfill(input: string, pattern: string | RegExp): RegExpMatchArray[] {
const regex = new RegExp(pattern, 'g');
const output: RegExpMatchArray[] = [];

const result = input.match(regex);
if (!result) {
return [];
}

for (let i = 0; i < result.length; i++) {
output.push(result[i].match(new RegExp(pattern)) || []);
}
return output;
}

export function matchAll(regex: RegExp, input: string, addition: Record<string, any>) {
const matches = [];
for (const match of matchAllPolyfill(input, regex)) {
matches.push({
...addition,
...match.groups,
code: match[0],
start: match.index,
end: (match.index || 0) + match[0].length
});
}
return matches;
}

export function provideCJSSyntax(code: string): Output | null {
if (code.includes(ESMShim) || !CJSyntaxRegex.test(code)) {
return null;
}

const lastESMImport = matchAll(ESMStaticImportRegex, code, { type: 'static' }).pop();
const indexToAppend = lastESMImport ? lastESMImport.end : 0;
const s = new MagicString(code);
s.appendRight(indexToAppend, ESMShim);

return {
code: s.toString(),
map: s.generateMap()
};
}
1 change: 1 addition & 0 deletions packages/esm-shim/test/fixtures/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar';
3 changes: 3 additions & 0 deletions packages/esm-shim/test/fixtures/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const child = require('child');

export { child };
51 changes: 51 additions & 0 deletions packages/esm-shim/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Snapshot report for `test/test.js`

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://avajs.dev).

## inject cjs shim for esm output

> Snapshot 1
`␊
// -- Shims --␊
import cjsUrl from 'url';␊
import cjsPath from 'path';␊
import cjsModule from 'module';␊
const __filename = cjsUrl.fileURLToPath(import.meta.url);␊
const __dirname = cjsPath.dirname(__filename);␊
const require = cjsModule.createRequire(import.meta.url);␊
const child = require('child');␊
␊
export { child };␊
`

## inject cjs shim for esm output with sourcemap

> Snapshot 1
`␊
// -- Shims --␊
import cjsUrl from 'url';␊
import cjsPath from 'path';␊
import cjsModule from 'module';␊
const __filename = cjsUrl.fileURLToPath(import.meta.url);␊
const __dirname = cjsPath.dirname(__filename);␊
const require = cjsModule.createRequire(import.meta.url);␊
const child = require('child');␊
␊
export { child };␊
//# sourceMappingURL=cjs.js.map␊
`

## not inject cjs shim for cjs output

> Snapshot 1
`'use strict';␊
␊
const child = require('child');␊
␊
exports.child = child;␊
`
Binary file added packages/esm-shim/test/snapshots/test.js.snap
Binary file not shown.
Loading