1 | const fs = require('fs');
|
2 | const rollup = require('rollup');
|
3 | const rollupPluginNodeResolve = require('@rollup/plugin-node-resolve');
|
4 | const rollupPluginCommonjs = require('@rollup/plugin-commonjs');
|
5 | const babel = require('@babel/core');
|
6 | const cp = require('child_process');
|
7 |
|
8 |
|
9 | let banner = function (pkg) {
|
10 | return `/*!
|
11 | * web Studio Code - `+ pkg.description + `
|
12 | * `+ pkg.repository.url + `
|
13 | *
|
14 | * author `+ pkg.author + `
|
15 | *
|
16 | * version `+ pkg.version + `
|
17 | *
|
18 | * build Fri May 08 2020
|
19 | *
|
20 | * Copyright yelloxing
|
21 | * Released under the `+ pkg.license + ` license
|
22 | *
|
23 | * Date:`+ new Date() + `
|
24 | */\n\n`;
|
25 |
|
26 | };
|
27 |
|
28 | module.exports = {
|
29 |
|
30 |
|
31 | path: __dirname,
|
32 |
|
33 |
|
34 | pkg: '.',
|
35 |
|
36 |
|
37 | task: {
|
38 |
|
39 | init(cuf, pkg) {
|
40 |
|
41 | cuf.print(pkg.name + "@" + pkg.version + " " + pkg.description);
|
42 |
|
43 |
|
44 | if (fs.existsSync('./dist')) cuf.deleteSync('./dist');
|
45 |
|
46 | cuf.log("\n-----------------------\n环境整理完毕,开始打包!\n-----------------------");
|
47 | cuf.print("Date : " + new Date() + "\n");
|
48 |
|
49 | },
|
50 |
|
51 | end(cuf) {
|
52 |
|
53 | cuf.log("\n-----------------------\n打包完毕!\n-----------------------");
|
54 | cuf.print("Date : " + new Date() + "\n\n");
|
55 |
|
56 |
|
57 | const printFileSize = function (index, url) {
|
58 |
|
59 | fs.stat(url, (err, stats) => {
|
60 | if (err) {
|
61 | console.log(err);
|
62 | } else {
|
63 | let size = (bytes => {
|
64 | if (bytes < 1024) return bytes + " Bytes";
|
65 | else if (bytes < 1048576) return (bytes / 1024).toFixed(3) + " KB";
|
66 | else if (bytes < 1073741824) return (bytes / 1048576).toFixed(3) + " MB";
|
67 | else return (bytes / 1073741824).toFixed(3) + " GB";
|
68 | })(stats.size);
|
69 |
|
70 | cuf.log("[" + index + "] " + url + " " + size);
|
71 | }
|
72 |
|
73 | });
|
74 | };
|
75 |
|
76 | printFileSize(1, './dist/wscode.js');
|
77 | printFileSize(2, './dist/wscode.min.js');
|
78 |
|
79 | },
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 | bundle(cuf) {
|
86 | async function build(inputOptions, outputOptions) {
|
87 | const bundle = await rollup.rollup(inputOptions);
|
88 | await bundle.write(outputOptions);
|
89 |
|
90 | cuf.error('模块打包完毕');
|
91 | }
|
92 |
|
93 | cuf.log("\n[1]模块打包:./dist/module.new.js\n");
|
94 |
|
95 | build({
|
96 | input: 'src/index.js',
|
97 | plugins: [
|
98 |
|
99 |
|
100 | rollupPluginNodeResolve({
|
101 | customResolveOptions: {
|
102 | moduleDirectory: 'node_modules'
|
103 | }
|
104 | }),
|
105 |
|
106 |
|
107 | rollupPluginCommonjs({
|
108 | include: "node_modules/**",
|
109 | exclude: []
|
110 | })
|
111 |
|
112 | ]
|
113 | }, {
|
114 | format: 'iife',
|
115 | name: "temp",
|
116 | file: './dist/module.new.js'
|
117 | });
|
118 | },
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 | babel(cuf, pkg) {
|
125 |
|
126 | cuf.log("\n[2]babel转义:./dist/module.new.js → ./dist/wscode.js\n");
|
127 |
|
128 | babel.transformFile("./dist/module.new.js", {}, function (err, result) {
|
129 | if (!err) {
|
130 | fs.writeFileSync("./dist/wscode.js", banner(pkg));
|
131 | fs.appendFileSync("./dist/wscode.js", result.code);
|
132 | cuf.deleteSync("./dist/module.new.js");
|
133 |
|
134 | cuf.error('转义完毕');
|
135 | } else {
|
136 | console.log(err);
|
137 | }
|
138 | });
|
139 | },
|
140 |
|
141 | |
142 |
|
143 |
|
144 |
|
145 | uglifyjs(cuf, pkg) {
|
146 |
|
147 | cuf.log("\n[3]压缩代码:./dist/wscode.js → ./dist/wscode.min.js\n");
|
148 |
|
149 | cp.exec("uglifyjs ./dist/wscode.js -m -o ./dist/uglifyjs.new.js", function (error) {
|
150 | if (error) {
|
151 | console.log(error);
|
152 | } else {
|
153 |
|
154 | fs.writeFileSync("./dist/wscode.min.js", banner(pkg));
|
155 | fs.appendFileSync("./dist/wscode.min.js", fs.readFileSync("./dist/uglifyjs.new.js"));
|
156 |
|
157 | cuf.error('压缩完毕');
|
158 | }
|
159 | cuf.deleteSync("./dist/uglifyjs.new.js");
|
160 | });
|
161 | }
|
162 |
|
163 | }
|
164 |
|
165 | };
|