forked from rollup/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate rollup-plugin-sucrase (rollup#104)
* 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
1 parent
3fc40e0
commit dd19946
Showing
17 changed files
with
393 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"globals": { | ||
"t": "readonly" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export default (a: number, b: number): number => a * b; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import example from './example'; | ||
|
||
t.snapshot(example.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <div id="foo">hello world</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default (a: number, b: number): number => a * b; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.