Skip to content

Commit

Permalink
chore: migrate rollup-plugin-sucrase (rollup#104)
Browse files Browse the repository at this point in the history
* Add package for sucrase with dependencies

* Add existing index.js

* Move index.js to known location, include lint auto-fix changes

* Add updated CHANGELOG

* Fix: correctly pass  option to Sucrase

* Add fix to changelog, update package description, add wip readme

* Document options, add usage example from old repo README

* Update to new package for pluginutils

* Add rollup config for plugin-sucrase builds

* Update description and add to root README

* Add tests for plugin-sucrase

* Updates from linting

* Update options in readme

* Updates from PR feedback

* chore: change description in README

Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com>
  • Loading branch information
2 people authored and LarsDenBakker committed Sep 12, 2020
1 parent 3fc40e0 commit dd19946
Show file tree
Hide file tree
Showing 17 changed files with 393 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This repository houses plugins that Rollup considers critical to every day use o
| [node-resolve](packages/node-resolve) | Locate and bundle third-party dependencies in node_modules |
| [replace](packages/replace) | Replace strings in files while bundling |
| [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 |
| [typescript](packages/typescript) | Integration between Rollup and Typescript |
| [url](packages/url) | Import files as data-URIs or ES Modules |
| [virtual](packages/virtual) | Load virtual modules from memory |
Expand Down
24 changes: 24 additions & 0 deletions packages/sucrase/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# @rollup/plugin-sucrase Change Log

## 3.0.0

_2019-12-??_

- **Breaking:** Minimum compatible Rollup version is 1.2.0
- **Breaking:** Minimum supported Node version is 8.0.0
- Published as @rollup/plugin-sucrase
- Fix: correctly pass `enableLegacyBabel5ModuleInterop` option to Sucrase

## 2.1.0

- Add `filter` option ([#4](https://github.com/rollup/rollup-plugin-sucrase/pull/4))
- Remove lockfile so we always get most recent version of Sucrase
- Resolve extensionless imports ([#3](https://github.com/rollup/rollup-plugin-sucrase/issues/3))

## 2.0.0

- Update to Sucrase 3.x ([#2](https://github.com/rollup/rollup-plugin-sucrase/pull/2))

## 1.0.0

- First release
83 changes: 83 additions & 0 deletions packages/sucrase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[npm]: https://img.shields.io/npm/v/@rollup/plugin-sucrase
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-sucrase
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-sucrase
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-sucrase

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

# @rollup/plugin-sucrase

🍣 A Rollup plugin which compiles TypeScript, Flow, JSX, etc with Sucrase.

## Requirements

This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.

## Install

Using npm:

```console
npm install @rollup/plugin-sucrase --save-dev
```

## Usage

Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin. An example of compiling TypeScript (the node-resolve plugin is added to automatically add file extensions, since TypeScript expects not to find them):

```js
import sucrase from '@rollup/plugin-sucrase';
import resolve from '@rollup/plugin-node-resolve';

export default {
input: 'src/index.ts',
output: {
file: 'dist/bundle.js',
format: 'cjs'
},
plugins: [
resolve({
extensions: ['.js', '.ts']
}),
sucrase({
exclude: ['node_modules/**'],
transforms: ['typescript']
})
]
};
```

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).

## Options

The following [Sucrase options](https://github.com/alangpierce/sucrase#transforms) may be passed as options for this plugin:

- `enableLegacyBabel5ModuleInterop`
- `enableLegacyTypeScriptModuleInterop`
- `jsxFragmentPragma`
- `jsxPragma`
- `production`
- `transforms`

### `exclude`

Type: `String` | `Array[...String]`
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.

### `include`

Type: `String` | `Array(String)`
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

## Meta

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

[LICENSE (MIT)](/LICENSE)
66 changes: 66 additions & 0 deletions packages/sucrase/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@rollup/plugin-sucrase",
"version": "3.0.0",
"publishConfig": {
"access": "public"
},
"description": "Compile TypeScript, Flow, JSX, etc with Sucrase",
"license": "MIT",
"repository": "rollup/plugins",
"author": "Rich Harris",
"homepage": "https://github.com/rollup/plugins/packages/sucrase/#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:lint": "pnpm run build && pnpm run lint",
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm run test -- --verbose",
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
"lint:docs": "prettier --single-quote --write README.md",
"lint:js": "eslint --fix --cache src test",
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
"prebuild": "del-cli dist",
"prepare": "pnpm run build",
"prepublishOnly": "pnpm run lint",
"pretest": "pnpm run build",
"test": "ava"
},
"files": [
"dist",
"README.md",
"LICENSE"
],
"keywords": [
"rollup",
"plugin",
"sucrase",
"typescript",
"flow",
"jsx"
],
"peerDependencies": {
"rollup": "^1.20.0"
},
"dependencies": {
"@rollup/pluginutils": "^3.0.1",
"sucrase": "^3.10.1"
},
"devDependencies": {
"rollup": "^1.27.13"
},
"ava": {
"files": [
"!**/fixtures/**",
"!**/output/**",
"!**/helpers/**",
"!**/recipes/**",
"!**/types.ts"
]
},
"module": "dist/index.es.js"
}
12 changes: 12 additions & 0 deletions packages/sucrase/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pkg from './package.json';

const external = Object.keys(pkg.dependencies).concat(['path', 'fs']);

export default {
input: 'src/index.js',
external,
output: [
{ format: 'cjs', file: pkg.main },
{ format: 'esm', file: pkg.module }
]
};
45 changes: 45 additions & 0 deletions packages/sucrase/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const fs = require('fs');
const path = require('path');

const { transform } = require('sucrase');
const { createFilter } = require('@rollup/pluginutils');

module.exports = function sucrase(opts = {}) {
const filter = createFilter(opts.include, opts.exclude);

return {
name: 'sucrase',

// eslint-disable-next-line consistent-return
resolveId(importee, importer) {
if (importer && importee[0] === '.') {
const resolved = path.resolve(importer ? path.dirname(importer) : process.cwd(), importee);

if (!fs.existsSync(resolved) && fs.existsSync(`${resolved}.ts`)) {
return `${resolved}.ts`;
}
}
},

transform(code, id) {
if (!filter(id)) return null;

const result = transform(code, {
transforms: opts.transforms,
jsxPragma: opts.jsxPragma,
jsxFragmentPragma: opts.jsxFragmentPragma,
enableLegacyTypeScriptModuleInterop: opts.enableLegacyTypeScriptModuleInterop,
enableLegacyBabel5ModuleInterop: opts.enableLegacyBabel5ModuleInterop,
production: opts.production,
filePath: id,
sourceMapOptions: {
compiledFilename: id
}
});
return {
code: result.code,
map: result.sourceMap
};
}
};
};
5 changes: 5 additions & 0 deletions packages/sucrase/test/fixtures/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"globals": {
"t": "readonly"
}
}
2 changes: 2 additions & 0 deletions packages/sucrase/test/fixtures/flow/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @flow
export default (a: number, b: number): number => a * b;
3 changes: 3 additions & 0 deletions packages/sucrase/test/fixtures/flow/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import example from './example';

t.snapshot(example.toString());
1 change: 1 addition & 0 deletions packages/sucrase/test/fixtures/jsx/example.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <div id="foo">hello world</div>
4 changes: 4 additions & 0 deletions packages/sucrase/test/fixtures/jsx/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable import/extensions */
import example from './example.jsx';

t.snapshot(example.toString());
1 change: 1 addition & 0 deletions packages/sucrase/test/fixtures/typescript/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (a: number, b: number): number => a * b;
4 changes: 4 additions & 0 deletions packages/sucrase/test/fixtures/typescript/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable import/extensions */
import example from './example.ts';

t.snapshot(example.toString());
29 changes: 29 additions & 0 deletions packages/sucrase/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Snapshot report for `test/test.js`

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

Generated by [AVA](https://ava.li).

## converts jsx

> Snapshot 1
'() => React.createElement(\'div\', { id: "foo", __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 1}}, "hello world" )'

## converts flow

> Snapshot 1
'(a, b) => a * b'

## converts typescript

> Snapshot 1
'(a, b) => a * b'

## converts jsx with custom jsxPragma

> Snapshot 1
'() => FakeReactCreateElement(\'div\', { id: "foo", __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 1}}, "hello world" )'
Binary file added packages/sucrase/test/snapshots/test.js.snap
Binary file not shown.
50 changes: 50 additions & 0 deletions packages/sucrase/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const test = require('ava');
const { rollup } = require('rollup');

const { testBundle } = require('../../../util/test');

const sucrase = require('..');

require('source-map-support').install();

process.chdir(__dirname);

test('converts jsx', async (t) => {
const bundle = await rollup({
input: 'fixtures/jsx/main.js',
plugins: [
sucrase({
transforms: ['jsx']
})
]
});
t.plan(1);
return testBundle(t, bundle);
});

test('converts jsx with custom jsxPragma', async (t) => {
const bundle = await rollup({
input: 'fixtures/jsx/main.js',
plugins: [
sucrase({
transforms: ['jsx'],
jsxPragma: 'FakeReactCreateElement'
})
]
});
t.plan(1);
return testBundle(t, bundle);
});

test('converts typescript', async (t) => {
const bundle = await rollup({
input: 'fixtures/typescript/main.js',
plugins: [
sucrase({
transforms: ['typescript']
})
]
});
t.plan(1);
return testBundle(t, bundle);
});
Loading

0 comments on commit dd19946

Please sign in to comment.