web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.4 KiB

  1. # rollup-plugin-terser [![Travis Build Status][travis-img]][travis]
  2. [travis-img]: https://travis-ci.org/TrySound/rollup-plugin-terser.svg
  3. [travis]: https://travis-ci.org/TrySound/rollup-plugin-terser
  4. [Rollup](https://github.com/rollup/rollup) plugin to minify generated es bundle. Uses [terser](https://github.com/fabiosantoscode/terser) under the hood.
  5. ## Install
  6. ```sh
  7. yarn add rollup-plugin-terser --dev
  8. ```
  9. _Note: this package requires rollup@0.66 and higher (including rollup@1.0.0)_
  10. ## Usage
  11. ```js
  12. import { rollup } from "rollup";
  13. import { terser } from "rollup-plugin-terser";
  14. rollup({
  15. input: "main.js",
  16. plugins: [terser()]
  17. });
  18. ```
  19. ## Why named export?
  20. 1. Module is a namespace. Default export often leads to function/component per file dogma and makes code less maintainable.
  21. 2. Interop with commonjs is broken in many cases or hard to maintain.
  22. 3. Show me any good language with default exports. It's historical javascriptism.
  23. ## Options
  24. > ⚠️ **Caveat:** any function used in options object cannot rely on its surrounding scope, since it is executed in an isolated context.
  25. ```js
  26. terser(options);
  27. ```
  28. `options` - [terser API options](https://github.com/fabiosantoscode/terser#minify-options)
  29. Note: some terser options are set by the plugin automatically:
  30. - `module: true` is set when `format` is `esm` or `es`
  31. - `toplevel: true` is set when `format` is `cjs`
  32. `options.sourcemap: boolean`
  33. Generates source maps and passes them to rollup. Defaults to `true`.
  34. `options.numWorkers: number`
  35. Amount of workers to spawn. Defaults to the number of CPUs minus 1.
  36. `options.include: Array<string | RegExp> | string | RegExp`
  37. `options.exclude: Array<string | RegExp> | string | RegExp`
  38. Specifically include/exclude chunk files names (minimatch pattern, or array of minimatch patterns), By default all chunk files will be minify.
  39. ## Examples
  40. ### Using as output plugin
  41. ```js
  42. // rollup.config.js
  43. import { terser } from "rollup-plugin-terser";
  44. export default {
  45. input: "index.js",
  46. output: [
  47. { file: "lib.js", format: "cjs" },
  48. { file: "lib.min.js", format: "cjs", plugins: [terser()] },
  49. { file: "lib.esm.js", format: "esm" }
  50. ]
  51. };
  52. ```
  53. ### include/exclude
  54. If you'd like that only some of the files will be minify, then you can filter by `include` and `exclude` to do this like so:
  55. ```js
  56. // rollup.config.js
  57. import { terser } from "rollup-plugin-terser";
  58. export default {
  59. input: "index.js",
  60. output: [
  61. { file: "lib.js", format: "cjs" },
  62. { file: "lib.min.js", format: "cjs" },
  63. { file: "lib.esm.js", format: "esm" },
  64. { dir: ".", entryFileNames: "lib-[format].js", format: "iife" }
  65. ],
  66. plugins: [
  67. terser({
  68. include: [/^.+\.min\.js$/, "*esm*"],
  69. exclude: ["some*"]
  70. })
  71. ]
  72. };
  73. ```
  74. ### Comments
  75. If you'd like to preserve comments (for licensing for example), then you can specify a function to do this like so:
  76. ```js
  77. terser({
  78. output: {
  79. comments: function(node, comment) {
  80. var text = comment.value;
  81. var type = comment.type;
  82. if (type == "comment2") {
  83. // multiline comment
  84. return /@preserve|@license|@cc_on/i.test(text);
  85. }
  86. }
  87. }
  88. });
  89. ```
  90. Alternatively, you can also choose to keep all comments (e.g. if a licensing header has already been prepended by a previous rollup plugin):
  91. ```js
  92. terser({
  93. output: {
  94. comments: "all"
  95. }
  96. });
  97. ```
  98. See [Terser documentation](https://github.com/fabiosantoscode/terser#terser) for further reference.
  99. # License
  100. MIT © [Bogdan Chadkin](mailto:trysound@yandex.ru)