@openmrs/webpack-config 4.5.1-pre.804 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/index.js +32 -19
- package/package.json +8 -9
- package/src/index.ts +41 -22
- package/.babelrc +0 -3
- package/dist/optimize.js +0 -146
- package/src/optimize.ts +0 -173
package/.turbo/turbo-build.log
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
[
|
|
1
|
+
[32m@openmrs/webpack-config:build[0m: cache hit, replaying output [2mc164cc055b2a0549[0m
|
package/dist/index.js
CHANGED
|
@@ -45,12 +45,13 @@ exports.watchConfig = exports.assetRuleConfig = exports.scssRuleConfig = exports
|
|
|
45
45
|
const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
|
|
46
46
|
const path_1 = require("path");
|
|
47
47
|
const clean_webpack_plugin_1 = require("clean-webpack-plugin");
|
|
48
|
+
const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
|
|
48
49
|
const webpack_1 = require("webpack");
|
|
49
50
|
const webpack_bundle_analyzer_1 = require("webpack-bundle-analyzer");
|
|
50
51
|
const webpack_stats_plugin_1 = require("webpack-stats-plugin");
|
|
51
52
|
// eslint-disable-next-line no-restricted-imports
|
|
52
53
|
const lodash_1 = require("lodash");
|
|
53
|
-
const
|
|
54
|
+
const fs_1 = require("fs");
|
|
54
55
|
const semver_1 = require("semver");
|
|
55
56
|
const production = "production";
|
|
56
57
|
const { ModuleFederationPlugin } = webpack_1.container;
|
|
@@ -80,6 +81,9 @@ function mergeFunction(objValue, srcValue) {
|
|
|
80
81
|
function slugify(name) {
|
|
81
82
|
return name.replace(/[\/\-@]/g, "_");
|
|
82
83
|
}
|
|
84
|
+
function fileExistsSync(name) {
|
|
85
|
+
return (0, fs_1.existsSync)(name) && (0, fs_1.statSync)(name).isFile();
|
|
86
|
+
}
|
|
83
87
|
/**
|
|
84
88
|
* This object will be merged into the webpack config.
|
|
85
89
|
* Array values will be concatenated with the existing array.
|
|
@@ -131,6 +135,14 @@ exports.default = (env, argv = {}) => {
|
|
|
131
135
|
const srcFile = (0, path_1.resolve)(root, browser ? main : types);
|
|
132
136
|
const ident = makeIdent(name);
|
|
133
137
|
const frameworkVersion = getFrameworkVersion();
|
|
138
|
+
const routes = (0, path_1.resolve)(root, "src", "routes.json");
|
|
139
|
+
const hasRoutesDefined = fileExistsSync(routes);
|
|
140
|
+
if (!hasRoutesDefined) {
|
|
141
|
+
console.error("This app does not define a routes.json. This file is required for this app to be used by the OpenMRS 3 App Shell.");
|
|
142
|
+
// key-smash error code
|
|
143
|
+
// so this (hopefully) doesn't interfere with Webpack-specific exit codes
|
|
144
|
+
process.exit(9819023573289);
|
|
145
|
+
}
|
|
134
146
|
const cssLoader = {
|
|
135
147
|
loader: "css-loader",
|
|
136
148
|
options: {
|
|
@@ -154,7 +166,7 @@ exports.default = (env, argv = {}) => {
|
|
|
154
166
|
test: /\.m?(js|ts|tsx)$/,
|
|
155
167
|
exclude: /(node_modules|bower_components)/,
|
|
156
168
|
use: {
|
|
157
|
-
loader:
|
|
169
|
+
loader: "@swc-node/loader",
|
|
158
170
|
},
|
|
159
171
|
}, exports.scriptRuleConfig),
|
|
160
172
|
(0, lodash_1.merge)({
|
|
@@ -177,7 +189,7 @@ exports.default = (env, argv = {}) => {
|
|
|
177
189
|
type: "asset/resource",
|
|
178
190
|
}, exports.assetRuleConfig),
|
|
179
191
|
],
|
|
180
|
-
}, mode, devtool: mode === production ? "hidden-nosources-source-map" : "
|
|
192
|
+
}, mode, devtool: mode === production ? "hidden-nosources-source-map" : "source-map", devServer: {
|
|
181
193
|
headers: {
|
|
182
194
|
"Access-Control-Allow-Origin": "*",
|
|
183
195
|
},
|
|
@@ -196,9 +208,6 @@ exports.default = (env, argv = {}) => {
|
|
|
196
208
|
analyzerMode: env && env.analyze ? "server" : "disabled",
|
|
197
209
|
}),
|
|
198
210
|
new webpack_1.DefinePlugin({
|
|
199
|
-
__VERSION__: mode === production
|
|
200
|
-
? JSON.stringify(version)
|
|
201
|
-
: JSON.stringify((0, semver_1.inc)(version, "prerelease", "local")),
|
|
202
211
|
"process.env.FRAMEWORK_VERSION": JSON.stringify(frameworkVersion),
|
|
203
212
|
}),
|
|
204
213
|
new ModuleFederationPlugin({
|
|
@@ -225,6 +234,21 @@ exports.default = (env, argv = {}) => {
|
|
|
225
234
|
return obj;
|
|
226
235
|
}, {}),
|
|
227
236
|
}),
|
|
237
|
+
hasRoutesDefined &&
|
|
238
|
+
new copy_webpack_plugin_1.default({
|
|
239
|
+
patterns: [
|
|
240
|
+
{
|
|
241
|
+
from: routes,
|
|
242
|
+
transform: {
|
|
243
|
+
transformer: (content) => JSON.stringify(Object.assign({}, JSON.parse(content.toString()), {
|
|
244
|
+
version: mode === production
|
|
245
|
+
? version
|
|
246
|
+
: (0, semver_1.inc)(version, "prerelease", "local"),
|
|
247
|
+
})),
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
],
|
|
251
|
+
}),
|
|
228
252
|
new webpack_stats_plugin_1.StatsWriterPlugin({
|
|
229
253
|
filename: `${filename}.buildmanifest.json`,
|
|
230
254
|
stats: {
|
|
@@ -232,19 +256,8 @@ exports.default = (env, argv = {}) => {
|
|
|
232
256
|
chunks: true,
|
|
233
257
|
},
|
|
234
258
|
}),
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
compiler.hooks.afterEmit.tap("PostProcessPlugin", (compilation) => {
|
|
238
|
-
if (mode === "production") {
|
|
239
|
-
// only post-optimize the bundle in production mode
|
|
240
|
-
const fn = (0, path_1.resolve)(root, outDir, filename);
|
|
241
|
-
(0, optimize_1.postProcessFile)(fn);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
},
|
|
245
|
-
},
|
|
246
|
-
], resolve: {
|
|
247
|
-
extensions: [".tsx", ".ts", ".jsx", ".js", ".scss"],
|
|
259
|
+
].filter(Boolean), resolve: {
|
|
260
|
+
extensions: [".tsx", ".ts", ".jsx", ".js", ".scss", ".json"],
|
|
248
261
|
alias: {
|
|
249
262
|
"@openmrs/esm-framework": "@openmrs/esm-framework/src/internal",
|
|
250
263
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/webpack-config",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "jest --passWithNoTests",
|
|
12
12
|
"build": "tsc",
|
|
13
|
+
"build:development": "tsc",
|
|
13
14
|
"lint": "eslint src --ext ts,tsx"
|
|
14
15
|
},
|
|
15
16
|
"repository": {
|
|
@@ -30,20 +31,18 @@
|
|
|
30
31
|
],
|
|
31
32
|
"homepage": "https://github.com/openmrs/openmrs-esm-core#readme",
|
|
32
33
|
"dependencies": {
|
|
33
|
-
"@
|
|
34
|
-
"@
|
|
35
|
-
"@swc/core": "^1.2.165",
|
|
34
|
+
"@swc-node/loader": "^1.3.5",
|
|
35
|
+
"@swc/core": "^1.3.58",
|
|
36
36
|
"babel-preset-minify": "^0.5.1",
|
|
37
37
|
"clean-webpack-plugin": "^4.0.0",
|
|
38
|
+
"copy-webpack-plugin": "^11.0.0",
|
|
38
39
|
"css-loader": "^5.2.4",
|
|
39
40
|
"fork-ts-checker-webpack-plugin": "^6.5.0",
|
|
40
41
|
"lodash": "^4.17.21",
|
|
41
42
|
"sass": "^1.44.0",
|
|
42
43
|
"sass-loader": "^12.3.0",
|
|
43
44
|
"style-loader": "^3.3.1",
|
|
44
|
-
"
|
|
45
|
-
"systemjs-webpack-interop": "^2.3.7",
|
|
46
|
-
"webpack": "^5.75.0",
|
|
45
|
+
"webpack": "^5.86.0",
|
|
47
46
|
"webpack-bundle-analyzer": "^4.5.0",
|
|
48
47
|
"webpack-stats-plugin": "^1.0.3"
|
|
49
48
|
},
|
|
@@ -51,7 +50,7 @@
|
|
|
51
50
|
"webpack": "5.x"
|
|
52
51
|
},
|
|
53
52
|
"devDependencies": {
|
|
54
|
-
"typescript": "
|
|
53
|
+
"typescript": "^4.6.4"
|
|
55
54
|
},
|
|
56
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "2de5bf392a036e12c6d48425ff61881be7e32344"
|
|
57
56
|
}
|
package/src/index.ts
CHANGED
|
@@ -39,12 +39,13 @@
|
|
|
39
39
|
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
|
|
40
40
|
import { resolve, dirname, basename } from "path";
|
|
41
41
|
import { CleanWebpackPlugin } from "clean-webpack-plugin";
|
|
42
|
+
import CopyWebpackPlugin from "copy-webpack-plugin";
|
|
42
43
|
import { DefinePlugin, container } from "webpack";
|
|
43
44
|
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
|
|
44
45
|
import { StatsWriterPlugin } from "webpack-stats-plugin";
|
|
45
46
|
// eslint-disable-next-line no-restricted-imports
|
|
46
47
|
import { merge, mergeWith, isArray } from "lodash";
|
|
47
|
-
import {
|
|
48
|
+
import { existsSync, statSync } from "fs";
|
|
48
49
|
import { inc } from "semver";
|
|
49
50
|
|
|
50
51
|
const production = "production";
|
|
@@ -77,10 +78,14 @@ function mergeFunction(objValue: any, srcValue: any) {
|
|
|
77
78
|
}
|
|
78
79
|
}
|
|
79
80
|
|
|
80
|
-
function slugify(name) {
|
|
81
|
+
function slugify(name: string) {
|
|
81
82
|
return name.replace(/[\/\-@]/g, "_");
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
function fileExistsSync(name: string) {
|
|
86
|
+
return existsSync(name) && statSync(name).isFile();
|
|
87
|
+
}
|
|
88
|
+
|
|
84
89
|
/**
|
|
85
90
|
* This object will be merged into the webpack config.
|
|
86
91
|
* Array values will be concatenated with the existing array.
|
|
@@ -149,6 +154,17 @@ export default (
|
|
|
149
154
|
const srcFile = resolve(root, browser ? main : types);
|
|
150
155
|
const ident = makeIdent(name);
|
|
151
156
|
const frameworkVersion = getFrameworkVersion();
|
|
157
|
+
const routes = resolve(root, "src", "routes.json");
|
|
158
|
+
const hasRoutesDefined = fileExistsSync(routes);
|
|
159
|
+
|
|
160
|
+
if (!hasRoutesDefined) {
|
|
161
|
+
console.error(
|
|
162
|
+
"This app does not define a routes.json. This file is required for this app to be used by the OpenMRS 3 App Shell."
|
|
163
|
+
);
|
|
164
|
+
// key-smash error code
|
|
165
|
+
// so this (hopefully) doesn't interfere with Webpack-specific exit codes
|
|
166
|
+
process.exit(9819023573289);
|
|
167
|
+
}
|
|
152
168
|
|
|
153
169
|
const cssLoader = {
|
|
154
170
|
loader: "css-loader",
|
|
@@ -176,7 +192,7 @@ export default (
|
|
|
176
192
|
test: /\.m?(js|ts|tsx)$/,
|
|
177
193
|
exclude: /(node_modules|bower_components)/,
|
|
178
194
|
use: {
|
|
179
|
-
loader:
|
|
195
|
+
loader: "@swc-node/loader",
|
|
180
196
|
},
|
|
181
197
|
},
|
|
182
198
|
scriptRuleConfig
|
|
@@ -212,8 +228,7 @@ export default (
|
|
|
212
228
|
],
|
|
213
229
|
},
|
|
214
230
|
mode,
|
|
215
|
-
devtool:
|
|
216
|
-
mode === production ? "hidden-nosources-source-map" : "eval-source-map",
|
|
231
|
+
devtool: mode === production ? "hidden-nosources-source-map" : "source-map",
|
|
217
232
|
devServer: {
|
|
218
233
|
headers: {
|
|
219
234
|
"Access-Control-Allow-Origin": "*",
|
|
@@ -239,10 +254,6 @@ export default (
|
|
|
239
254
|
analyzerMode: env && env.analyze ? "server" : "disabled",
|
|
240
255
|
}),
|
|
241
256
|
new DefinePlugin({
|
|
242
|
-
__VERSION__:
|
|
243
|
-
mode === production
|
|
244
|
-
? JSON.stringify(version)
|
|
245
|
-
: JSON.stringify(inc(version, "prerelease", "local")),
|
|
246
257
|
"process.env.FRAMEWORK_VERSION": JSON.stringify(frameworkVersion),
|
|
247
258
|
}),
|
|
248
259
|
new ModuleFederationPlugin({
|
|
@@ -268,6 +279,25 @@ export default (
|
|
|
268
279
|
return obj;
|
|
269
280
|
}, {}),
|
|
270
281
|
}),
|
|
282
|
+
hasRoutesDefined &&
|
|
283
|
+
new CopyWebpackPlugin({
|
|
284
|
+
patterns: [
|
|
285
|
+
{
|
|
286
|
+
from: routes,
|
|
287
|
+
transform: {
|
|
288
|
+
transformer: (content) =>
|
|
289
|
+
JSON.stringify(
|
|
290
|
+
Object.assign({}, JSON.parse(content.toString()), {
|
|
291
|
+
version:
|
|
292
|
+
mode === production
|
|
293
|
+
? version
|
|
294
|
+
: inc(version, "prerelease", "local"),
|
|
295
|
+
})
|
|
296
|
+
),
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
],
|
|
300
|
+
}),
|
|
271
301
|
new StatsWriterPlugin({
|
|
272
302
|
filename: `${filename}.buildmanifest.json`,
|
|
273
303
|
stats: {
|
|
@@ -275,20 +305,9 @@ export default (
|
|
|
275
305
|
chunks: true,
|
|
276
306
|
},
|
|
277
307
|
}),
|
|
278
|
-
|
|
279
|
-
apply(compiler) {
|
|
280
|
-
compiler.hooks.afterEmit.tap("PostProcessPlugin", (compilation) => {
|
|
281
|
-
if (mode === "production") {
|
|
282
|
-
// only post-optimize the bundle in production mode
|
|
283
|
-
const fn = resolve(root, outDir, filename);
|
|
284
|
-
postProcessFile(fn);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
},
|
|
288
|
-
},
|
|
289
|
-
],
|
|
308
|
+
].filter(Boolean),
|
|
290
309
|
resolve: {
|
|
291
|
-
extensions: [".tsx", ".ts", ".jsx", ".js", ".scss"],
|
|
310
|
+
extensions: [".tsx", ".ts", ".jsx", ".js", ".scss", ".json"],
|
|
292
311
|
alias: {
|
|
293
312
|
"@openmrs/esm-framework": "@openmrs/esm-framework/src/internal",
|
|
294
313
|
},
|
package/.babelrc
DELETED
package/dist/optimize.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.postProcessFile = exports.transformFile = void 0;
|
|
23
|
-
const types = __importStar(require("@babel/types"));
|
|
24
|
-
const core_1 = require("@babel/core");
|
|
25
|
-
const fs_1 = require("fs");
|
|
26
|
-
const path_1 = require("path");
|
|
27
|
-
const exportedSetupFunctionName = "setupOpenMRS";
|
|
28
|
-
const assetsPropertyName = "assets";
|
|
29
|
-
const loaderName = "_$load";
|
|
30
|
-
function isUnique(current, index, arr) {
|
|
31
|
-
return arr.indexOf(current) === index;
|
|
32
|
-
}
|
|
33
|
-
function getFiles(manifest) {
|
|
34
|
-
const files = [];
|
|
35
|
-
manifest.chunks.forEach((chunk) => {
|
|
36
|
-
files.push(...chunk.files);
|
|
37
|
-
});
|
|
38
|
-
return files.filter(isUnique);
|
|
39
|
-
}
|
|
40
|
-
function getChunkId(arg) {
|
|
41
|
-
if (types.isNumericLiteral(arg)) {
|
|
42
|
-
// e.g., t.e(307).then(...)
|
|
43
|
-
return arg.value;
|
|
44
|
-
}
|
|
45
|
-
else if (types.isArrayExpression(arg)) {
|
|
46
|
-
// e.g., Promise.all([t.e(983),t.e(307)]).then(...)
|
|
47
|
-
const last = arg.elements[arg.elements.length - 1];
|
|
48
|
-
if (types.isCallExpression(last)) {
|
|
49
|
-
const lastArg = last.arguments[0];
|
|
50
|
-
if (types.isNumericLiteral(lastArg)) {
|
|
51
|
-
return lastArg.value;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return 0;
|
|
56
|
-
}
|
|
57
|
-
function extendEntryFile(root, chunkId, allFiles) {
|
|
58
|
-
var _a, _b;
|
|
59
|
-
const chunkName = `${chunkId}.js`;
|
|
60
|
-
const filename = (0, path_1.resolve)(root, chunkName);
|
|
61
|
-
const code = (0, fs_1.readFileSync)(filename, "utf8");
|
|
62
|
-
const result = (0, core_1.transformSync)(code, {
|
|
63
|
-
ast: true,
|
|
64
|
-
filename,
|
|
65
|
-
sourceType: "script",
|
|
66
|
-
plugins: [
|
|
67
|
-
() => ({
|
|
68
|
-
visitor: {
|
|
69
|
-
Property(path) {
|
|
70
|
-
if (path.node.key.name === exportedSetupFunctionName) {
|
|
71
|
-
path.insertAfter(types.objectProperty(types.identifier(assetsPropertyName), types.arrowFunctionExpression([], types.arrayExpression(allFiles
|
|
72
|
-
.filter((file) => file !== chunkName)
|
|
73
|
-
.map((file) => types.stringLiteral(file))))));
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
}),
|
|
78
|
-
],
|
|
79
|
-
});
|
|
80
|
-
return (_b = (_a = result === null || result === void 0 ? void 0 : result.ast) === null || _a === void 0 ? void 0 : _a.program.body) !== null && _b !== void 0 ? _b : [];
|
|
81
|
-
}
|
|
82
|
-
function transformFile(filename) {
|
|
83
|
-
var _a;
|
|
84
|
-
const root = (0, path_1.dirname)(filename);
|
|
85
|
-
const manifest = require(`${filename}.buildmanifest.json`);
|
|
86
|
-
const files = getFiles(manifest);
|
|
87
|
-
const code = (0, fs_1.readFileSync)(filename, "utf8");
|
|
88
|
-
const result = (0, core_1.transformSync)(code, {
|
|
89
|
-
filename,
|
|
90
|
-
sourceType: "script",
|
|
91
|
-
presets: ["minify"],
|
|
92
|
-
plugins: [
|
|
93
|
-
() => ({
|
|
94
|
-
visitor: {
|
|
95
|
-
Property(path) {
|
|
96
|
-
if (path.node.key.name === "app") {
|
|
97
|
-
const body = path.get("value.body");
|
|
98
|
-
if (types.isExpression(body.node)) {
|
|
99
|
-
const statements = [
|
|
100
|
-
types.expressionStatement(types.assignmentExpression("=", types.identifier(loaderName), body.node)),
|
|
101
|
-
];
|
|
102
|
-
if (types.isMemberExpression(body.node.callee) &&
|
|
103
|
-
types.isCallExpression(body.node.callee.object) &&
|
|
104
|
-
types.isMemberExpression(body.node.callee.object.callee)) {
|
|
105
|
-
const arg = body.node.callee.object.arguments[0];
|
|
106
|
-
const chunkId = getChunkId(arg);
|
|
107
|
-
statements.unshift(...extendEntryFile(root, chunkId, files));
|
|
108
|
-
}
|
|
109
|
-
body.replaceWith(types.blockStatement([
|
|
110
|
-
types.ifStatement(types.unaryExpression("!", types.identifier(loaderName)), types.blockStatement(statements)),
|
|
111
|
-
types.returnStatement(types.identifier(loaderName)),
|
|
112
|
-
]));
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
CallExpression(path) {
|
|
117
|
-
const node = path.node;
|
|
118
|
-
if (types.isMemberExpression(node.callee) &&
|
|
119
|
-
types.isIdentifier(node.callee.object, { name: "System" }) &&
|
|
120
|
-
types.isIdentifier(node.callee.property, {
|
|
121
|
-
name: "register",
|
|
122
|
-
}) &&
|
|
123
|
-
node.arguments.length === 3 &&
|
|
124
|
-
types.isFunctionExpression(node.arguments[2])) {
|
|
125
|
-
path
|
|
126
|
-
.get("arguments.2.body")
|
|
127
|
-
.unshiftContainer("body", types.variableDeclaration("var", [
|
|
128
|
-
types.variableDeclarator(types.identifier(loaderName), types.identifier("undefined")),
|
|
129
|
-
]));
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
}),
|
|
134
|
-
],
|
|
135
|
-
});
|
|
136
|
-
return [(_a = result === null || result === void 0 ? void 0 : result.code) === null || _a === void 0 ? void 0 : _a.split('"use strict";').join(""), code];
|
|
137
|
-
}
|
|
138
|
-
exports.transformFile = transformFile;
|
|
139
|
-
function postProcessFile(fn) {
|
|
140
|
-
const [result, original] = transformFile(fn);
|
|
141
|
-
if (result && original) {
|
|
142
|
-
(0, fs_1.writeFileSync)(fn.replace(".js", ".old"), original, "utf8");
|
|
143
|
-
(0, fs_1.writeFileSync)(fn.replace(".js", ".js"), result, "utf8");
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
exports.postProcessFile = postProcessFile;
|
package/src/optimize.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import * as types from "@babel/types";
|
|
2
|
-
import { transformSync, Node } from "@babel/core";
|
|
3
|
-
import { readFileSync, writeFileSync } from "fs";
|
|
4
|
-
import { resolve, dirname } from "path";
|
|
5
|
-
|
|
6
|
-
const exportedSetupFunctionName = "setupOpenMRS";
|
|
7
|
-
const assetsPropertyName = "assets";
|
|
8
|
-
const loaderName = "_$load";
|
|
9
|
-
|
|
10
|
-
function isUnique(current: string, index: number, arr: Array<string>) {
|
|
11
|
-
return arr.indexOf(current) === index;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function getFiles(manifest: { chunks: Array<{ files: Array<string> }> }) {
|
|
15
|
-
const files: Array<string> = [];
|
|
16
|
-
|
|
17
|
-
manifest.chunks.forEach((chunk) => {
|
|
18
|
-
files.push(...chunk.files);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
return files.filter(isUnique);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function getChunkId(arg: Node): number {
|
|
25
|
-
if (types.isNumericLiteral(arg)) {
|
|
26
|
-
// e.g., t.e(307).then(...)
|
|
27
|
-
return arg.value;
|
|
28
|
-
} else if (types.isArrayExpression(arg)) {
|
|
29
|
-
// e.g., Promise.all([t.e(983),t.e(307)]).then(...)
|
|
30
|
-
const last = arg.elements[arg.elements.length - 1];
|
|
31
|
-
|
|
32
|
-
if (types.isCallExpression(last)) {
|
|
33
|
-
const lastArg = last.arguments[0];
|
|
34
|
-
|
|
35
|
-
if (types.isNumericLiteral(lastArg)) {
|
|
36
|
-
return lastArg.value;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return 0;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function extendEntryFile(
|
|
45
|
-
root: string,
|
|
46
|
-
chunkId: number,
|
|
47
|
-
allFiles: Array<string>
|
|
48
|
-
) {
|
|
49
|
-
const chunkName = `${chunkId}.js`;
|
|
50
|
-
const filename = resolve(root, chunkName);
|
|
51
|
-
const code = readFileSync(filename, "utf8");
|
|
52
|
-
const result = transformSync(code, {
|
|
53
|
-
ast: true,
|
|
54
|
-
filename,
|
|
55
|
-
sourceType: "script",
|
|
56
|
-
plugins: [
|
|
57
|
-
() => ({
|
|
58
|
-
visitor: {
|
|
59
|
-
Property(path) {
|
|
60
|
-
if (path.node.key.name === exportedSetupFunctionName) {
|
|
61
|
-
path.insertAfter(
|
|
62
|
-
types.objectProperty(
|
|
63
|
-
types.identifier(assetsPropertyName),
|
|
64
|
-
types.arrowFunctionExpression(
|
|
65
|
-
[],
|
|
66
|
-
types.arrayExpression(
|
|
67
|
-
allFiles
|
|
68
|
-
.filter((file) => file !== chunkName)
|
|
69
|
-
.map((file) => types.stringLiteral(file))
|
|
70
|
-
)
|
|
71
|
-
)
|
|
72
|
-
)
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
}),
|
|
78
|
-
],
|
|
79
|
-
});
|
|
80
|
-
return result?.ast?.program.body ?? [];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function transformFile(filename: string) {
|
|
84
|
-
const root = dirname(filename);
|
|
85
|
-
const manifest = require(`${filename}.buildmanifest.json`);
|
|
86
|
-
const files = getFiles(manifest);
|
|
87
|
-
const code = readFileSync(filename, "utf8");
|
|
88
|
-
const result = transformSync(code, {
|
|
89
|
-
filename,
|
|
90
|
-
sourceType: "script",
|
|
91
|
-
presets: ["minify"],
|
|
92
|
-
plugins: [
|
|
93
|
-
() => ({
|
|
94
|
-
visitor: {
|
|
95
|
-
Property(path) {
|
|
96
|
-
if (path.node.key.name === "app") {
|
|
97
|
-
const body = path.get("value.body");
|
|
98
|
-
|
|
99
|
-
if (types.isExpression(body.node)) {
|
|
100
|
-
const statements: Array<types.Statement> = [
|
|
101
|
-
types.expressionStatement(
|
|
102
|
-
types.assignmentExpression(
|
|
103
|
-
"=",
|
|
104
|
-
types.identifier(loaderName),
|
|
105
|
-
body.node
|
|
106
|
-
)
|
|
107
|
-
),
|
|
108
|
-
];
|
|
109
|
-
|
|
110
|
-
if (
|
|
111
|
-
types.isMemberExpression(body.node.callee) &&
|
|
112
|
-
types.isCallExpression(body.node.callee.object) &&
|
|
113
|
-
types.isMemberExpression(body.node.callee.object.callee)
|
|
114
|
-
) {
|
|
115
|
-
const arg = body.node.callee.object.arguments[0];
|
|
116
|
-
const chunkId = getChunkId(arg);
|
|
117
|
-
|
|
118
|
-
statements.unshift(...extendEntryFile(root, chunkId, files));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
body.replaceWith(
|
|
122
|
-
types.blockStatement([
|
|
123
|
-
types.ifStatement(
|
|
124
|
-
types.unaryExpression("!", types.identifier(loaderName)),
|
|
125
|
-
types.blockStatement(statements)
|
|
126
|
-
),
|
|
127
|
-
types.returnStatement(types.identifier(loaderName)),
|
|
128
|
-
])
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
CallExpression(path) {
|
|
134
|
-
const node = path.node;
|
|
135
|
-
|
|
136
|
-
if (
|
|
137
|
-
types.isMemberExpression(node.callee) &&
|
|
138
|
-
types.isIdentifier(node.callee.object, { name: "System" }) &&
|
|
139
|
-
types.isIdentifier(node.callee.property, {
|
|
140
|
-
name: "register",
|
|
141
|
-
}) &&
|
|
142
|
-
node.arguments.length === 3 &&
|
|
143
|
-
types.isFunctionExpression(node.arguments[2])
|
|
144
|
-
) {
|
|
145
|
-
path
|
|
146
|
-
.get("arguments.2.body")
|
|
147
|
-
.unshiftContainer(
|
|
148
|
-
"body",
|
|
149
|
-
types.variableDeclaration("var", [
|
|
150
|
-
types.variableDeclarator(
|
|
151
|
-
types.identifier(loaderName),
|
|
152
|
-
types.identifier("undefined")
|
|
153
|
-
),
|
|
154
|
-
])
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
}),
|
|
160
|
-
],
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
return [result?.code?.split('"use strict";').join(""), code];
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function postProcessFile(fn: string) {
|
|
167
|
-
const [result, original] = transformFile(fn);
|
|
168
|
-
|
|
169
|
-
if (result && original) {
|
|
170
|
-
writeFileSync(fn.replace(".js", ".old"), original, "utf8");
|
|
171
|
-
writeFileSync(fn.replace(".js", ".js"), result, "utf8");
|
|
172
|
-
}
|
|
173
|
-
}
|