Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(terser): emit worker error message (+stack) to parent instance #1394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/packages/babel/ @Andarist
/packages/image/ @tjenkinson
/packages/node-resolve/ @tjenkinson
/packages/swc/ @tada5hi
/packages/terser/ @tada5hi
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 |
| [terser](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 |
Expand Down
57 changes: 57 additions & 0 deletions packages/swc/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-swc
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-swc
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-swc
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-swc

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

# @rollup/plugin-swc

🍣 A Rollup plugin to transpile TypeScript/JavaScript with the speedy-web-compiler (swc).

The plugin makes it possible to avoid the usage of `@rollup/plugin-babel` and `@rollup/plugin-typescript`.
It is also blazingly fast 🔥 ( between 20 - 70 times faster than babel depending on how many cpu cores are used).

## Requirements

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

## Install

Using npm:

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

## Usage

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

```typescript
import swc from '@rollup/plugin-swc';

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

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 plugin accepts a swc [Options](https://swc.rs/docs/configuration/swcrc) object as input parameter,
to modify the default behaviour.

## Meta

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

[LICENSE (MIT)](/LICENSE)
73 changes: 73 additions & 0 deletions packages/swc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "@rollup/plugin-swc",
"version": "0.0.0",
"publishConfig": {
"access": "public"
},
"description": "Transpile JavaScript/TypeScript code with swc.",
"license": "MIT",
"repository": {
"url": "rollup/plugins",
"directory": "packages/swc"
},
"author": "Peter Placzek <peter.placzek1996@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/swc#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": ">=10.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",
"swc",
"speedy-web-compiler",
"npm",
"modules"
],
"peerDependencies": {
"@swc/core": "^1.x",
"rollup": "^2.x || ^3.x"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"smob": "^0.0.6"
},
"devDependencies": {
"@swc/core": "^1.3.27",
"rollup": "^3.0.0-7",
"typescript": "^4.8.3"
},
"types": "./types/index.d.ts"
}
10 changes: 10 additions & 0 deletions packages/swc/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { readFileSync } from 'fs';

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

export default {
...createConfig({
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
}),
input: 'src/index.ts'
};
4 changes: 4 additions & 0 deletions packages/swc/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { swc } from './module';
export * from './type';
export * from './module';
export default swc;
40 changes: 40 additions & 0 deletions packages/swc/src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { OutputOptions, Plugin } from 'rollup';
import { transform } from '@swc/core';
import { merge } from 'smob';
import { Options } from './type';

export function swc(input: Options = {}): Plugin {
const options: Options = merge(
{
jsc: {
target: 'es2020',
parser: {
syntax: 'typescript',
decorators: true
},
transform: {
decoratorMetadata: true,
legacyDecorator: true
},
loose: true
}
},
input
);

return {
name: 'swc',
renderChunk(code: string, chunk, outputOptions: OutputOptions) {
const chunkOptions = merge({}, options);

if (outputOptions.sourcemap === 'inline') {
chunkOptions.sourceMaps = 'inline';
}
if (typeof outputOptions.sourcemap === 'boolean') {
chunkOptions.sourceMaps = outputOptions.sourcemap;
}

return transform(code, chunkOptions);
}
};
}
2 changes: 2 additions & 0 deletions packages/swc/src/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Options as SWCOptions } from '@swc/core';
export type Options = SWCOptions & {};
1 change: 1 addition & 0 deletions packages/swc/test/fixtures/export-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 5;
1 change: 1 addition & 0 deletions packages/swc/test/fixtures/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar';
7 changes: 7 additions & 0 deletions packages/swc/test/fixtures/unminified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line no-undef
window.a = 5;

// eslint-disable-next-line no-undef
if (window.a < 3) {
console.log(4);
}
71 changes: 71 additions & 0 deletions packages/swc/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const test = require('ava');
const { rollup } = require('rollup');

const swc = require('../');

test.serial('cjs output for default export', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/export-default.js',
plugins: [swc()]
});
const result = await bundle.generate({ format: 'cjs' });
t.is(result.output.length, 1);
const [output] = result.output;
t.is(output.code, "'use strict';\nvar exportDefault = 5;\nmodule.exports = exportDefault;\n");
});

test.serial('esm output for default export', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/export-default.js',
plugins: [swc()]
});
const result = await bundle.generate({ format: 'esm' });
t.is(result.output.length, 1);
const [output] = result.output;
t.is(output.code, 'var exportDefault = 5;\nexport { exportDefault as default };\n');
});

test.serial('cjs output for export', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/export.js',
plugins: [swc()]
});
const result = await bundle.generate({ format: 'cjs' });
t.is(result.output.length, 1);
const [output] = result.output;
t.is(output.code, "'use strict';\nconst foo = 'bar';\nexports.foo = foo;\n");
});

test.serial('esm output for export', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/export.js',
plugins: [swc()]
});
const result = await bundle.generate({ format: 'esm' });
t.is(result.output.length, 1);
const [output] = result.output;
t.is(output.code, "const foo = 'bar';\nexport { foo };\n");
});

test.serial('work with source map', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/unminified.js',
plugins: [swc()]
});
const result = await bundle.generate({ format: 'cjs', sourcemap: true });

t.is(result.output.length, 2);

const [file, sourceMap] = result.output;

t.is(file.fileName, 'unminified.js');
t.is(file.type, 'chunk');
t.is(file.name, 'unminified');
t.is(file.type, 'chunk');
t.is(file.map.version, 3);
t.is(file.map.file, 'unminified.js');
t.deepEqual(file.map.names, ['window', 'a', 'console', 'log']);

t.is(sourceMap.fileName, 'unminified.js.map');
t.is(sourceMap.type, 'asset');
});
8 changes: 8 additions & 0 deletions packages/swc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*", "types/**/*"],
"compilerOptions": {
"noUnusedParameters": false,
"module": "ESNext"
}
}
Loading