|
1 | 1 | const { handleRequest } = require('@bolt/api');
|
2 |
| -const { |
3 |
| - devMiddleware, |
4 |
| - webpack, |
5 |
| - express, |
6 |
| -} = require('@bolt/build-tools/webpack-dev-server'); |
7 |
| -const { join } = require('path'); |
8 |
| -const globby = require('globby'); |
| 2 | +const { express } = require('@bolt/build-tools/webpack-dev-server'); |
9 | 3 | const app = express();
|
10 | 4 | const path = require('path');
|
11 | 5 |
|
12 | 6 | const port = process.env.PORT || 4444;
|
13 |
| -const createWebpackConfig = require('@bolt/build-tools/create-webpack-config'); |
14 | 7 | const { getConfig } = require('@bolt/build-tools/utils/config-store');
|
| 8 | +const webpackTasks = require('@bolt/build-tools/tasks/webpack-tasks'); |
15 | 9 |
|
16 | 10 | let server;
|
17 | 11 |
|
18 |
| -// @todo: re-evaluate if this is worthwhile to re-enable |
19 |
| -// const allComponentsWithTests = globby |
20 |
| -// .sync( |
21 |
| -// path.join(__dirname, '../../../', '/packages/components/**/__tests__'), |
22 |
| -// { |
23 |
| -// onlyDirectories: true, |
24 |
| -// }, |
25 |
| -// ) |
26 |
| -// .map(testsDirPath => |
27 |
| -// require(path.join(path.resolve(testsDirPath, '..'), 'package.json')), |
28 |
| -// ) |
29 |
| -// .map(pkg => pkg.name); |
30 |
| - |
31 | 12 | getConfig().then(async boltConfig => {
|
32 | 13 | let config = boltConfig;
|
33 |
| - |
34 |
| - // don't compile anything in Webpack except for the exported JSON data from Bolt's Design Tokens + all packages with tests |
35 |
| - // config.components.global = [ |
36 |
| - // './packages/core/styles/index.scss', |
37 |
| - // '@bolt/global', |
38 |
| - // ...allComponentsWithTests, |
39 |
| - // ]; |
40 |
| - |
41 |
| - config.mode = 'client'; |
42 |
| - config.components.individual = []; |
43 |
| - config.prod = true; |
44 |
| - config.sourceMaps = false; |
45 |
| - |
46 |
| - const webpackConfig = await createWebpackConfig(config); |
47 |
| - const compiler = webpack(webpackConfig); |
48 |
| - |
49 |
| - // This function makes server rendering of asset references consistent with different webpack chunk/entry configurations |
50 |
| - function normalizeAssets(assets) { |
51 |
| - return Array.isArray(assets) ? assets : [assets]; |
52 |
| - } |
53 |
| - |
54 |
| - app.use( |
55 |
| - devMiddleware(compiler, { |
56 |
| - serverSideRender: true, |
57 |
| - stats: webpackConfig[0].devServer.stats, |
58 |
| - }), |
59 |
| - ); |
60 |
| - |
| 14 | + await webpackTasks.compile(); |
61 | 15 | app.use(express.static(path.relative(process.cwd(), config.wwwDir)));
|
62 | 16 |
|
63 |
| - // The following middleware would not be invoked until the latest build is finished. |
64 | 17 | app.use((req, res) => {
|
65 |
| - const assetsByChunkName = res.locals.webpackStats.toJson().children[0] |
66 |
| - .assetsByChunkName; |
67 |
| - const fs = res.locals.fs; |
68 |
| - const outputPath = res.locals.webpackStats.toJson().children[0].outputPath; |
69 |
| - |
70 |
| - // then use `assetsByChunkName` for server-sider rendering |
71 |
| - // For example, if you have only one main chunk: |
72 | 18 | res.send(
|
73 | 19 | `<html class="js-fonts-loaded">
|
74 | 20 | <head>
|
75 | 21 | <title>Test</title>
|
76 |
| - <style>${normalizeAssets(assetsByChunkName['bolt-global']) |
77 |
| - .filter(path => path.endsWith('.css')) |
78 |
| - .map(path => fs.readFileSync(outputPath + '/' + path)) |
79 |
| - .join('\n')}</style> |
80 |
| -
|
81 |
| - ${normalizeAssets(assetsByChunkName['bolt-global']) |
82 |
| - .filter(path => path.endsWith('.js')) |
83 |
| - .map(path => `<script src="${path}"></script>`) |
84 |
| - .join('\n')} |
| 22 | + <link rel="stylesheet" href="/build/bolt-global.css" /> |
| 23 | + <script src="/build/bolt-global.js"></script> |
85 | 24 | </head>
|
86 | 25 | <body>
|
87 | 26 | <!-- set #root to inline-block so VRT screenshots are only as wide as the component vs are always full width -->
|
@@ -110,7 +49,6 @@ getConfig().then(async boltConfig => {
|
110 | 49 | });
|
111 | 50 |
|
112 | 51 | app.redirect;
|
113 |
| - |
114 | 52 | // handle cleaning up + shutting down the server instance
|
115 | 53 | process.on('SIGTERM', shutDownSSRServer);
|
116 | 54 | process.on('SIGINT', shutDownSSRServer);
|
|
0 commit comments