Minify Node.js projects with Rollup
My last Node project is refactoring sophisticated multithreading Telegram bot with 3,000 code lines.
This is result of minification:
As you can see I stored .ENV file with setting as clear plain text. And this is rollup.config.js of this my project.
1:
2: import resolve from '@rollup/plugin-node-resolve';
3: import commonjs from '@rollup/plugin-commonjs';
4: import json from '@rollup/plugin-json';
5: import terser from '@rollup/plugin-terser';
6: import { builtinModules } from 'node:module';
7: import copy from 'rollup-plugin-copy';
8:
9: const external = [
10: ...builtinModules,
11: 'dotenv',
12: 'node:path',
13: 'node:url',
14: 'node:fs',
15: 'mysql2',
16: 'redis',
17: 'telegraf',
18: "i18n",
19: "node-fetch",
20: "node-geometry-library",
21: "winston",
22: "wkx",
23: "ws"
24: ];
25:
26: export default {
27: input: 'src/index.js',
28: output: {
29: file: 'dist/index.js',
30: format: 'esm',
31: sourcemap: false,
32: inlineDynamicImports: true,
33: },
34: external,
35: plugins: [
36: resolve({
37: preferBuiltins: true,
38: exportConditions: ['node', 'import'],
39: }),
40: commonjs({
41: ignoreDynamicRequires: true,
42: esmExternals: true,
43: }),
44: json(),
45: terser({
46: ecma: 2020,
47: module: true,
48: }),
49: copy({
50: targets: [{ src: '.env', dest: 'dist' }]
51: })
52: ],
53: };
54:
And in project config you can see what packages need to install for this awesome result.
1:
2: {
3: "name": "telegram-bot-notifications",
4: "version": "1.0.0",
5: "main": "index.js",
6: "license": "MIT",
7: "private": true,
8: "type": "module",
9: "scripts": {
10: "dev": "node src/index.js",
11: "build": "rollup -c"
12: },
13: "dependencies": {
14: "dotenv": "^16.4.5",
15: "i18n": "^0.15.1",
16: "mysql2": "^3.11.3",
17: "node-fetch": "^3.3.2",
18: "node-geometry-library": "^1.2.6",
19: "redis": "^4.7.0",
20: "telegraf": "^4.16.3",
21: "winston": "^3.15.0",
22: "wkx": "^0.5.0",
23: "ws": "^8.18.0"
24: },
25: "devDependencies": {
26: "@rollup/plugin-commonjs": "^28.0.3",
27: "@rollup/plugin-json": "^6.1.0",
28: "@rollup/plugin-node-resolve": "^16.0.1",
29: "@rollup/plugin-terser": "^0.4.4",
30: "rollup": "^4.41.0",
31: "rollup-plugin-copy": "^3.5.0"
32: }
33: }
34:
35:
Output size of dist/index.js in my case 807 KB, size of scripts in src folder is 107 KB. Pay attention that this in not webpack, you still need make "npm i" on remote server.
JavascriptProjects context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/MinifyJs/Index.htm
|
|