-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
125 lines (109 loc) · 3.6 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import path from 'path';
import fs from 'fs';
import fse from 'fs-extra';
import {rollup} from 'rollup';
import alias from '@rollup/plugin-alias';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import commonjs from '@rollup/plugin-commonjs';
import {babel} from '@rollup/plugin-babel';
import {terser} from 'rollup-plugin-terser';
import dotenv from 'dotenv';
import installer from '@amopro/widget-installer';
const {makeZipArchive} = installer;
const __dirname = path.resolve();
dotenv.config();
const PRODUCTION = process.env.APP_ENV === 'production';
const DIST_PATH = path.resolve(__dirname, './dist');
function makeWidgetDir() {
fse.removeSync(DIST_PATH);
fse.copySync(path.resolve(__dirname, './static'), DIST_PATH);
// update path version
{
const manifestPath = path.resolve(__dirname, './dist/manifest.json');
let manifest = fse.readJsonSync(manifestPath);
manifest.widget.version = (ver => {
let [major, minor, patch] = ver.split('.');
const versionPath = path.resolve(__dirname, './.version');
let newPatch = 1;
try {
newPatch = fs.readFileSync(versionPath);
} catch (e) {
// nothing
}
patch = +newPatch + 1;
fs.writeFileSync(versionPath, String(patch));
return [major, minor, patch].join('.');
})(manifest.widget.version);
// manifest.dp.webhook_url = String(manifest.dp.webhook_url)
// .replace('${APP_URL}', process.env.APP_URL.replace(/\/*$/, ''));
fse.writeFileSync(manifestPath, JSON.stringify(manifest, null, ' '));
}
}
async function build() {
console.log('Building widget...');
// see below for details on the options
const inputOptions = {
input: path.resolve(__dirname, './src/script.js'),
plugins: [
alias({
entries: {
'~': path.resolve(__dirname, './src')
}
}),
resolve({
browser: true,
dedupe: [],
extensions: ['.js', '.json']
}),
replace({
preventAssignment: true,
'process.env.APP_URL': JSON.stringify(process.env.APP_URL),
}),
commonjs({
include: ['jquery', 'node_modules/**'],
sourceMap: !PRODUCTION,
}),
babel({
babelHelpers: 'bundled',
babelrc: false,
exclude: 'node_modules/**', // only transpile our source code
presets: [
[
'@babel/env',
{
modules: false,
}
],
],
plugins: [
'@babel/plugin-proposal-class-properties',
]
}),
PRODUCTION && terser()
]
};
// create a bundle
const bundle = await rollup(inputOptions);
// or write the bundle to disk
await bundle.write({
file: path.resolve(__dirname, './dist/script.js'),
format: 'iife',
sourcemap: false
});
}
const makeZip = async () => {
const zipPath = path.resolve(__dirname, './widget.zip');
fse.removeSync(zipPath);
await makeZipArchive(DIST_PATH, zipPath);
}
makeWidgetDir();
try {
build().then(makeZip).then(() => {
console.log('Done!');
}).catch(e => {
console.error(e);
});
} catch (e) {
console.error(e);
}