This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.js
161 lines (146 loc) · 5.12 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const middleware = require('webpack-dev-middleware');
const WriteFilePlugin = require('write-file-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const puppeteer = require('puppeteer');
const fs = require('fs');
const { spawn } = require('child_process');
const SERVER_PORT = 3123;
const PUBLIC_PATH = `http://127.0.0.1:${SERVER_PORT}`;
const runTests = (projectName) => {
return new Promise((resolve, reject) => {
const opts = [
'--testEnvironment=node',
"--transform='" + JSON.stringify({
'\\.tsx?$': 'ts-jest',
}) + "'",
`--moduleFileExtensions=js`,
`--moduleFileExtensions=ts`,
`--testRegex='providers/${projectName}/tests/.+?spec\\.ts$'`,
];
console.log(`Jest opts: ${opts.join(' ')}`);
const jest = spawn('yarn jest', opts, { shell: true, stdio: 'inherit' });
jest.on('close', (code) => {
if(code === 0){
resolve();
} else {
reject('Failed to pass tests');
}
});
});
}
const runBuild = async (projectName) => {
console.log('Running tests for: ' + projectName);
await runTests(projectName);
console.log('Running build for: ' + projectName);
const outputDir = path.resolve(__dirname, './builds/' + projectName);
const compiler = webpack({
mode: 'production',
entry: {
main: './providers/' + projectName + '/src/Main.ts',
},
output: {
path: outputDir,
filename: '[name].js',
chunkFilename: '[name].chunk.js',
publicPath: PUBLIC_PATH,
jsonpFunction: '_s8_' + projectName.toLowerCase() + '_loader',
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json']
},
devtool: 'source-map',
watch: false,
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env',{
useBuiltIns: "usage",
targets: "> 0.25%, last 2 versions, not dead, ie 11",
corejs: "3.15.1",
debug: true
}]
,'@babel/preset-typescript'],
plugins: [
'@babel/proposal-class-properties',
'@babel/proposal-object-rest-spread'
]
}
},
]
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: 'ts-loader',
},
],
},
plugins: [
new webpack.DefinePlugin({
__S8_MODE: JSON.stringify('build'),
__S8_IS_CORE: JSON.stringify(projectName === 'Scale8'),
}),
new WriteFilePlugin(),
new HtmlWebpackPlugin(),
],
optimization: {
minimizer: [
new UglifyJsPlugin({
test: /\.js(\?.*)?$/i,
uglifyOptions: {
comments: false,
keep_fnames: true,
}
})
],
},
});
const buildConfigFromSpec = (spec) => JSON.stringify(
spec,
(k, v) => {
return typeof v === 'object' && Object.keys(v).length === 0? undefined : v;
},
2
);
const instance = middleware(compiler);
const app = express();
app.use(instance);
return new Promise(resolve => {
const server = app.listen(SERVER_PORT, () => {
instance.waitUntilValid(() => {
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(PUBLIC_PATH);
const spec = await page.evaluate(() => __S8_BUILD_SPEC);
fs.writeFileSync(outputDir + "/config.json", buildConfigFromSpec(spec));
await browser.close();
await server.close(() => resolve());
})();
});
});
});
}
const directories = source => fs.readdirSync(source, {
withFileTypes: true
}).reduce((a, c) => {
c.isDirectory() && a.push(c.name)
return a
}, []);
(async () => {
for(const provider of directories(path.resolve(__dirname, './providers'))){
await runBuild(provider);
}
process.exit(0);
})();