Skip to content

Commit

Permalink
feat(terser): initial release (#1323)
Browse files Browse the repository at this point in the history
* build(terser): add terser plugin to collection

* build(terser): add README.md notice

* fix(terser): test-suite + rollup plugin definition

* style(terser): applied prettier rules

* style(terser): apply prettier on root README.md + minor eslint fixes

* test(terser): removed snapshot test due incorrect path with ava
  • Loading branch information
tada5hi committed Oct 27, 2022
1 parent 45503a6 commit 2d67e34
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 8 deletions.
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/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 |
| [virtual](packages/virtual) | Load virtual modules from memory |
Expand Down
59 changes: 59 additions & 0 deletions packages/terser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[npm]: https://img.shields.io/npm/v/@rollup/plugin-terser
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-terser
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-terser
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-terser

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

# @rollup/plugin-terser

🍣 A Rollup plugin to generate a minified output bundle.

## Requirements

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

## Install

Using npm:

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

## Usage

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

```js
import terser from '@rollup/plugin-terser';

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

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 terser [Options](https://github.com/terser/terser#minify-options) object as input parameter,
to modify the default behaviour.

## Meta

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

[LICENSE (MIT)](/LICENSE)

## Credits

This package was originally developed by [https://github.com/TrySound](TrySound) but is not
maintained anymore.
71 changes: 71 additions & 0 deletions packages/terser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@rollup/plugin-terser",
"version": "0.0.0",
"publishConfig": {
"access": "public"
},
"description": "Generate minified bundle",
"license": "MIT",
"repository": {
"url": "rollup/plugins",
"directory": "packages/terser"
},
"author": "Peter Placzek <peter.placzek1996@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/terser#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:terser --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",
"terser",
"minify",
"npm",
"modules"
],
"peerDependencies": {
"rollup": "^2.x || ^3.x"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"terser": "^5.15.1"
},
"devDependencies": {
"rollup": "^3.0.0-7",
"typescript": "^4.8.3"
},
"types": "./types/index.d.ts"
}
10 changes: 10 additions & 0 deletions packages/terser/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'
};
24 changes: 24 additions & 0 deletions packages/terser/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NormalizedOutputOptions, RenderedChunk } from 'rollup';
import { minify, MinifyOptions } from 'terser';

export default function terser(options?: MinifyOptions) {
return {
name: 'terser',

async renderChunk(code: string, chunk: RenderedChunk, outputOptions: NormalizedOutputOptions) {
const defaultOptions: MinifyOptions = {
sourceMap: outputOptions.sourcemap === true || typeof outputOptions.sourcemap === 'string'
};

if (outputOptions.format === 'es') {
defaultOptions.module = true;
}

if (outputOptions.format === 'cjs') {
defaultOptions.toplevel = true;
}

return minify(code, { ...defaultOptions, ...(options || {}) });
}
};
}
2 changes: 2 additions & 0 deletions packages/terser/test/fixtures/chunk-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const chunk1 = 'chunk-1';
console.log(chunk1)
2 changes: 2 additions & 0 deletions packages/terser/test/fixtures/chunk-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const chunk2 = 'chunk-2';
console.log(chunk2)
13 changes: 13 additions & 0 deletions packages/terser/test/fixtures/commented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// eslint-disable-next-line no-undef
window.a = 5;

/* @preserve this comment */

/* delete this comment */

// and this one too

// eslint-disable-next-line no-undef
if (window.a < 3) {
console.log(4);
}
Empty file.
1 change: 1 addition & 0 deletions packages/terser/test/fixtures/export-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 5;
2 changes: 2 additions & 0 deletions packages/terser/test/fixtures/failed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const a = 1;
4 changes: 4 additions & 0 deletions packages/terser/test/fixtures/plain-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


const foo = 'bar';
console.log(foo);
9 changes: 9 additions & 0 deletions packages/terser/test/fixtures/properties-and-locals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function recurse(count) {
if (count > 0) return recurse(count - 1);
return count;
}
const obj = {
foo: 1,
_priv: 2
};
console.log(obj, recurse(10));
5 changes: 5 additions & 0 deletions packages/terser/test/fixtures/properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const obj = {
foo: 1,
_priv: 2
};
console.log(obj);
3 changes: 3 additions & 0 deletions packages/terser/test/fixtures/sourcemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import result from './export-number.js';

console.log(result);
7 changes: 7 additions & 0 deletions packages/terser/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);
}

0 comments on commit 2d67e34

Please sign in to comment.