@elliemae/pui-cli 6.13.0-beta.3 → 7.0.0-beta.3
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/lib/cli-commands/build.js +7 -3
- package/lib/cli-commands/utils.js +48 -2
- package/lib/server/csp.js +2 -2
- package/lib/server/index.js +8 -40
- package/lib/server/middlewares/addProdMiddlewares.js +9 -10
- package/lib/server/middlewares/index.js +37 -0
- package/lib/server/util/index.js +10 -5
- package/lib/testing/jest.config.js +4 -2
- package/lib/webpack/helpers.js +2 -4
- package/lib/webpack/webpack.prod.babel.js +5 -19
- package/package.json +1 -1
- package/lib/server/argv.js +0 -1
- package/lib/server/middlewares/frontendMiddleware.js +0 -16
- package/lib/server/port.js +0 -6
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
const { exit } = require('yargs');
|
|
2
|
+
const path = require('path');
|
|
2
3
|
const {
|
|
3
4
|
exec,
|
|
4
5
|
logInfo,
|
|
5
6
|
logError,
|
|
6
7
|
logSuccess,
|
|
7
8
|
writeAppInfo,
|
|
9
|
+
copyBuildAssetsToVersionedFolder,
|
|
8
10
|
} = require('./utils');
|
|
9
11
|
const { esBuild, TARGETS } = require('../transpile/esbuild');
|
|
10
12
|
|
|
11
|
-
const { name } = require('../../package.json');
|
|
12
|
-
|
|
13
13
|
async function buildWebApp() {
|
|
14
14
|
await exec(`rimraf ./build`);
|
|
15
15
|
await exec(
|
|
16
|
-
`cross-env NODE_ENV=production webpack --config
|
|
16
|
+
`cross-env NODE_ENV=production webpack --config ${path.resolve(
|
|
17
|
+
__dirname,
|
|
18
|
+
'../webpack/webpack.prod.babel.js',
|
|
19
|
+
)} --color`,
|
|
17
20
|
);
|
|
21
|
+
await copyBuildAssetsToVersionedFolder();
|
|
18
22
|
await writeAppInfo();
|
|
19
23
|
}
|
|
20
24
|
|
|
@@ -1,9 +1,21 @@
|
|
|
1
|
+
/* eslint-disable max-lines */
|
|
1
2
|
/* eslint-disable no-console */
|
|
2
3
|
const execa = require('execa');
|
|
3
4
|
const chalk = require('chalk');
|
|
4
5
|
const path = require('path');
|
|
5
|
-
const {
|
|
6
|
-
|
|
6
|
+
const {
|
|
7
|
+
readFile,
|
|
8
|
+
writeFile,
|
|
9
|
+
mkdir,
|
|
10
|
+
readdir,
|
|
11
|
+
copyFile,
|
|
12
|
+
} = require('fs/promises');
|
|
13
|
+
const {
|
|
14
|
+
getPaths,
|
|
15
|
+
isAppLoaderEnabled,
|
|
16
|
+
getAppVersion,
|
|
17
|
+
LATEST_VERSION,
|
|
18
|
+
} = require('../webpack/helpers');
|
|
7
19
|
|
|
8
20
|
const browsersMapping = {
|
|
9
21
|
and_chr: 'Chrome for Android',
|
|
@@ -87,3 +99,37 @@ exports.writeAppInfo = async () => {
|
|
|
87
99
|
path.join(process.cwd(), 'build', 'public', 'info.json'),
|
|
88
100
|
].forEach(async (infoPath) => writeFile(infoPath, infoJSON));
|
|
89
101
|
};
|
|
102
|
+
|
|
103
|
+
const copyDir = async (src, dest) => {
|
|
104
|
+
const entries = await readdir(src, {
|
|
105
|
+
withFileTypes: true,
|
|
106
|
+
});
|
|
107
|
+
await mkdir(dest);
|
|
108
|
+
return Promise.all(
|
|
109
|
+
entries.map((entry) => {
|
|
110
|
+
const srcPath = path.join(src, entry.name);
|
|
111
|
+
const destPath = path.join(dest, entry.name);
|
|
112
|
+
if (entry.isDirectory()) {
|
|
113
|
+
return copyDir(srcPath, destPath);
|
|
114
|
+
}
|
|
115
|
+
return copyFile(srcPath, destPath);
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const updateManifestWithVersionInfo = async (dest) => {
|
|
121
|
+
const manifestFile = path.join(dest, 'manifest.json');
|
|
122
|
+
let manifestData = await readFile(manifestFile, 'utf8');
|
|
123
|
+
manifestData = manifestData.replace(/latest\//g, `${getAppVersion()}/`);
|
|
124
|
+
await writeFile(manifestFile, manifestData);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
exports.copyBuildAssetsToVersionedFolder = async () => {
|
|
128
|
+
const appVersion = getAppVersion();
|
|
129
|
+
const isVersionedApp = isAppLoaderEnabled() && appVersion !== LATEST_VERSION;
|
|
130
|
+
if (!isVersionedApp) return;
|
|
131
|
+
const src = path.resolve(process.cwd(), 'build/public/latest');
|
|
132
|
+
const dest = path.resolve(process.cwd(), `build/public/${appVersion}`);
|
|
133
|
+
await copyDir(src, dest);
|
|
134
|
+
await updateManifestWithVersionInfo(dest);
|
|
135
|
+
};
|
package/lib/server/csp.js
CHANGED
|
@@ -17,13 +17,13 @@ const sources = [
|
|
|
17
17
|
];
|
|
18
18
|
|
|
19
19
|
const sendFileWithCSPNonce = ({
|
|
20
|
-
|
|
20
|
+
buildPath,
|
|
21
21
|
page = 'index.html',
|
|
22
22
|
nonceRegex = /__CSP_NONCE__/g,
|
|
23
23
|
res,
|
|
24
24
|
fileSystem = fs,
|
|
25
25
|
}) => {
|
|
26
|
-
fileSystem.readFile(path.resolve(
|
|
26
|
+
fileSystem.readFile(path.resolve(buildPath, page), 'utf8', (err, html) => {
|
|
27
27
|
if (err) {
|
|
28
28
|
res.sendStatus(404);
|
|
29
29
|
} else {
|
package/lib/server/index.js
CHANGED
|
@@ -1,26 +1,11 @@
|
|
|
1
1
|
/* eslint consistent-return:0 import/order:0 */
|
|
2
|
-
|
|
3
2
|
const express = require('express');
|
|
4
|
-
const cors = require('cors');
|
|
5
|
-
const { resolve } = require('path');
|
|
6
|
-
const expressPinoLogger = require('express-pino-logger');
|
|
7
|
-
const { csp } = require('./csp');
|
|
8
3
|
const logger = require('./logger');
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
|
|
15
|
-
const pino = expressPinoLogger({
|
|
16
|
-
transport: {
|
|
17
|
-
target: 'pino-pretty',
|
|
18
|
-
options: {
|
|
19
|
-
colorize: true,
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
});
|
|
23
|
-
pino.logger.level = 'warn';
|
|
4
|
+
const {
|
|
5
|
+
setupDefaultMiddlewares,
|
|
6
|
+
setupAdditionalMiddlewars,
|
|
7
|
+
} = require('./middlewares');
|
|
8
|
+
const { loadRoutes, port, host } = require('./util');
|
|
24
9
|
|
|
25
10
|
// const corsOptions = {
|
|
26
11
|
// origin: '*',
|
|
@@ -37,33 +22,16 @@ pino.logger.level = 'warn';
|
|
|
37
22
|
// maxAge: 3600,
|
|
38
23
|
// };
|
|
39
24
|
const app = express();
|
|
40
|
-
app
|
|
41
|
-
app.use(cors());
|
|
42
|
-
app.options('*', cors());
|
|
43
|
-
csp(app);
|
|
44
|
-
app.use(express.urlencoded({ extended: false }));
|
|
45
|
-
app.use(express.text({ type: 'text/plain' }));
|
|
46
|
-
app.use(express.json({ type: 'application/json' }));
|
|
47
|
-
app.use(express.json({ type: 'application/csp-report' }));
|
|
48
|
-
|
|
25
|
+
setupDefaultMiddlewares(app);
|
|
49
26
|
// load all custom routes from the application
|
|
50
27
|
loadRoutes(app);
|
|
51
|
-
|
|
52
28
|
// In production we need to pass these values in instead of relying on webpack
|
|
53
|
-
|
|
54
|
-
outputPath: resolve(process.cwd(), 'build/public'),
|
|
55
|
-
publicPath: getAssetPath(),
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// get the intended host and port number, use localhost and port 3000 if not provided
|
|
59
|
-
const customHost = argv.host || process.env.HOST;
|
|
60
|
-
const host = customHost || null; // Let http.Server use its default IPv6/4 host
|
|
61
|
-
const prettyHost = customHost || 'localhost';
|
|
29
|
+
setupAdditionalMiddlewars(app);
|
|
62
30
|
|
|
63
31
|
// Start your app.
|
|
64
32
|
app.listen(port, host, async (err) => {
|
|
65
33
|
if (err) {
|
|
66
34
|
return logger.error(err.message);
|
|
67
35
|
}
|
|
68
|
-
logger.appStarted(port,
|
|
36
|
+
logger.appStarted(port, host || 'localhost');
|
|
69
37
|
});
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
1
|
const compression = require('compression');
|
|
3
2
|
const expressStaticGzip = require('express-static-gzip');
|
|
4
3
|
const { sendFileWithCSPNonce } = require('../csp');
|
|
4
|
+
const { getPaths } = require('../../webpack/helpers');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const publicPath = options.publicPath || '/';
|
|
8
|
-
const outputPath =
|
|
9
|
-
options.outputPath || path.resolve(process.cwd(), 'build/public');
|
|
6
|
+
const paths = getPaths();
|
|
10
7
|
|
|
8
|
+
module.exports = function addProdMiddlewares(app, options = {}) {
|
|
9
|
+
const { buildPath = paths.buildPath, mountPath = paths.mountPath } = options;
|
|
11
10
|
// compression middleware compresses your server responses which makes them
|
|
12
11
|
// smaller (applies also to assets). You can read more about that technique
|
|
13
12
|
// and other good practices on official Express.js docs http://mxs.is/googmy
|
|
14
13
|
app.use(compression());
|
|
15
14
|
|
|
16
|
-
app.get(
|
|
17
|
-
sendFileWithCSPNonce({
|
|
15
|
+
app.get(mountPath, (req, res) => {
|
|
16
|
+
sendFileWithCSPNonce({ buildPath, res });
|
|
18
17
|
});
|
|
19
18
|
|
|
20
19
|
app.use(
|
|
21
|
-
|
|
22
|
-
expressStaticGzip(
|
|
20
|
+
mountPath,
|
|
21
|
+
expressStaticGzip(buildPath, {
|
|
23
22
|
index: false,
|
|
24
23
|
enableBrotli: true,
|
|
25
24
|
orderPreference: ['br'],
|
|
@@ -27,5 +26,5 @@ module.exports = function addProdMiddlewares(app, options) {
|
|
|
27
26
|
);
|
|
28
27
|
app.use(expressStaticGzip('cdn'));
|
|
29
28
|
|
|
30
|
-
app.get('*', (req, res) => sendFileWithCSPNonce({
|
|
29
|
+
app.get('*', (req, res) => sendFileWithCSPNonce({ buildPath, res }));
|
|
31
30
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const cors = require('cors');
|
|
3
|
+
const expressPinoLogger = require('express-pino-logger');
|
|
4
|
+
const { csp } = require('../csp');
|
|
5
|
+
const addProdMiddlewares = require('./addProdMiddlewares');
|
|
6
|
+
const addDevMiddlewares = require('./addDevMiddlewares');
|
|
7
|
+
const webpackConfig = require('../../webpack/webpack.dev.babel');
|
|
8
|
+
|
|
9
|
+
exports.setupDefaultMiddlewares = (app) => {
|
|
10
|
+
const pino = expressPinoLogger({
|
|
11
|
+
transport: {
|
|
12
|
+
target: 'pino-pretty',
|
|
13
|
+
options: {
|
|
14
|
+
colorize: true,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
pino.logger.level = 'warn';
|
|
19
|
+
app.use(pino);
|
|
20
|
+
app.use(cors());
|
|
21
|
+
app.options('*', cors());
|
|
22
|
+
csp(app);
|
|
23
|
+
app.use(express.urlencoded({ extended: false }));
|
|
24
|
+
app.use(express.text({ type: 'text/plain' }));
|
|
25
|
+
app.use(express.json({ type: 'application/json' }));
|
|
26
|
+
app.use(express.json({ type: 'application/csp-report' }));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
exports.setupAdditionalMiddlewars = (app, options) => {
|
|
30
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
31
|
+
if (isProd) {
|
|
32
|
+
addProdMiddlewares(app, options);
|
|
33
|
+
} else {
|
|
34
|
+
addDevMiddlewares(app, webpackConfig);
|
|
35
|
+
}
|
|
36
|
+
return app;
|
|
37
|
+
};
|
package/lib/server/util/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
|
+
const argv = require('minimist')(process.argv.slice(2));
|
|
3
4
|
|
|
4
5
|
const getCWD = () => process.cwd();
|
|
5
6
|
|
|
@@ -25,7 +26,7 @@ const getFilesMatching = (filePattern) => {
|
|
|
25
26
|
const getServerRouteFiles = getFilesMatching(allJS);
|
|
26
27
|
const getServiceEndpoints = getFilesMatching(serviceEndpoints);
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
exports.loadRoutes = (app) => {
|
|
29
30
|
const routeFiles = getServerRouteFiles(path.join(getCWD(), 'server/routes'));
|
|
30
31
|
routeFiles.push(...getServiceEndpoints(path.join(getCWD(), 'app')));
|
|
31
32
|
routeFiles.push(...getServiceEndpoints(path.join(getCWD(), 'lib')));
|
|
@@ -41,7 +42,11 @@ const loadRoutes = (app) => {
|
|
|
41
42
|
});
|
|
42
43
|
};
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
exports.port = parseInt(
|
|
46
|
+
argv.port || process.env.port || process.env.PORT || '3000',
|
|
47
|
+
10,
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
exports.host = argv.host || process.env.HOST;
|
|
51
|
+
|
|
52
|
+
exports.getCWD = getCWD;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const normalizePath = require('normalize-path');
|
|
3
|
-
const { getAppConfig,
|
|
3
|
+
const { getAppConfig, getPaths } = require('../webpack/helpers');
|
|
4
4
|
const swcrcConfig = require('../transpile/swcrc.config.js');
|
|
5
5
|
const { findMonoRepoRoot } = require('../monorepo/utils');
|
|
6
6
|
|
|
7
|
+
const { mountPath } = getPaths();
|
|
8
|
+
|
|
7
9
|
let isReactModule = true;
|
|
8
10
|
try {
|
|
9
11
|
/* eslint-disable global-require, import/no-unresolved */
|
|
@@ -89,7 +91,7 @@ const jestConfig = {
|
|
|
89
91
|
APP_CONFIG: getAppConfig(),
|
|
90
92
|
__webpack_public_path__: '/',
|
|
91
93
|
},
|
|
92
|
-
testURL: `http://localhost:3111${
|
|
94
|
+
testURL: `http://localhost:3111${mountPath}`,
|
|
93
95
|
testEnvironment: 'jsdom',
|
|
94
96
|
};
|
|
95
97
|
|
package/lib/webpack/helpers.js
CHANGED
|
@@ -150,8 +150,6 @@ const getENCWLoaderFileName = () => {
|
|
|
150
150
|
)[0];
|
|
151
151
|
};
|
|
152
152
|
|
|
153
|
-
const getAssetPath = () => (process.env.ASSET_PATH || '/').replace(/\/?$/, '/');
|
|
154
|
-
|
|
155
153
|
const getAppVersion = () => {
|
|
156
154
|
if (!process.env.APP_VERSION) return LATEST_VERSION;
|
|
157
155
|
const match = process.env.APP_VERSION.match(/^v?(\d+\.\d+)\..*$/);
|
|
@@ -166,7 +164,7 @@ const getPaths = (latestVersion = true) => {
|
|
|
166
164
|
return {
|
|
167
165
|
appVersion: version,
|
|
168
166
|
buildPath: path.resolve(process.cwd(), `build/public/`),
|
|
169
|
-
|
|
167
|
+
mountPath: (process.env.MOUNT_PATH || '/').replace(/\/?$/, '/'),
|
|
170
168
|
publicPath,
|
|
171
169
|
userMonScriptPath: `latest/js/${getUserMonitoringFileName()}`,
|
|
172
170
|
appLoaderScriptPath: `latest/js/${getAppLoaderFileName()}`,
|
|
@@ -226,11 +224,11 @@ const filterByFilePresence = (patterns) =>
|
|
|
226
224
|
!noErrorOnMissing || fs.existsSync(path.resolve(process.cwd(), from)),
|
|
227
225
|
);
|
|
228
226
|
|
|
227
|
+
exports.LATEST_VERSION = LATEST_VERSION;
|
|
229
228
|
exports.excludeNodeModulesExcept = excludeNodeModulesExcept;
|
|
230
229
|
exports.getLibraryName = getLibraryName;
|
|
231
230
|
exports.getAppConfig = getAppConfig;
|
|
232
231
|
exports.mapToFolder = mapToFolder;
|
|
233
|
-
exports.getAssetPath = getAssetPath;
|
|
234
232
|
exports.isApp = isApp;
|
|
235
233
|
exports.getAlias = getAlias;
|
|
236
234
|
exports.getAppVersion = getAppVersion;
|
|
@@ -11,9 +11,7 @@ const browserslistToEsbuild = require('browserslist-to-esbuild');
|
|
|
11
11
|
const baseConfigFactory = require('./webpack.base.babel');
|
|
12
12
|
const {
|
|
13
13
|
isAppLoaderEnabled,
|
|
14
|
-
LATEST_VERSION,
|
|
15
14
|
getPaths,
|
|
16
|
-
getAppVersion,
|
|
17
15
|
isGoogleTagManagerEnabled,
|
|
18
16
|
getCompressionPlugins,
|
|
19
17
|
} = require('./helpers');
|
|
@@ -122,21 +120,13 @@ const htmlWebpackPlugin = new HtmlWebpackPlugin({
|
|
|
122
120
|
},
|
|
123
121
|
});
|
|
124
122
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
isAppLoaderEnabled() && getAppVersion() !== LATEST_VERSION;
|
|
123
|
+
const config = baseConfigFactory(getProdConfig());
|
|
124
|
+
config.plugins.push(htmlWebpackPlugin);
|
|
128
125
|
|
|
129
|
-
const
|
|
130
|
-
latestVersionConfig.plugins.push(htmlWebpackPlugin);
|
|
131
|
-
|
|
132
|
-
const appVersionConfig = baseConfigFactory(
|
|
133
|
-
getProdConfig({ latestVersion: false }),
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
const addSMPPlugin = (config) => {
|
|
126
|
+
const addSMPPlugin = (webpackConfig) => {
|
|
137
127
|
const smpConfig = new SpeedMeasurePlugin({
|
|
138
128
|
disable: !process.env.MEASURE,
|
|
139
|
-
}).wrap(
|
|
129
|
+
}).wrap(webpackConfig);
|
|
140
130
|
// mini css extract plugin is not working fine with smp
|
|
141
131
|
smpConfig.plugins.push(
|
|
142
132
|
new MiniCssExtractPlugin({
|
|
@@ -147,8 +137,4 @@ const addSMPPlugin = (config) => {
|
|
|
147
137
|
return smpConfig;
|
|
148
138
|
};
|
|
149
139
|
|
|
150
|
-
|
|
151
|
-
? [latestVersionConfig, appVersionConfig].map(addSMPPlugin)
|
|
152
|
-
: addSMPPlugin(latestVersionConfig);
|
|
153
|
-
|
|
154
|
-
module.exports = config;
|
|
140
|
+
module.exports = addSMPPlugin(config);
|
package/package.json
CHANGED
package/lib/server/argv.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('minimist')(process.argv.slice(2));
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/* eslint-disable global-require */
|
|
2
|
-
|
|
3
|
-
module.exports = (app, options) => {
|
|
4
|
-
const isProd = process.env.NODE_ENV === 'production';
|
|
5
|
-
|
|
6
|
-
if (isProd) {
|
|
7
|
-
const addProdMiddlewares = require('./addProdMiddlewares');
|
|
8
|
-
addProdMiddlewares(app, options);
|
|
9
|
-
} else {
|
|
10
|
-
const webpackConfig = require('../../webpack/webpack.dev.babel');
|
|
11
|
-
const addDevMiddlewares = require('./addDevMiddlewares');
|
|
12
|
-
addDevMiddlewares(app, webpackConfig);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return app;
|
|
16
|
-
};
|