|
1 | 1 | // @ts-check
|
| 2 | +const { cpus } = require('os') |
| 3 | + |
2 | 4 | const { existsSync, readJson, move, cpSync, copy, writeJson } = require('fs-extra')
|
3 | 5 | const pLimit = require('p-limit')
|
4 | 6 | const { join } = require('pathe')
|
| 7 | +const glob = require('tiny-glob') |
5 | 8 |
|
6 |
| -const TEST_ROUTE = /\/\[[^/]+?](?=\/|$)/ |
| 9 | +const TEST_ROUTE = /(|\/)\[[^/]+?](\/|\.html|$)/ |
7 | 10 |
|
8 | 11 | const isDynamicRoute = (route) => TEST_ROUTE.test(route)
|
9 | 12 |
|
10 | 13 | exports.moveStaticPages = async ({ netlifyConfig, target, i18n, failBuild }) => {
|
11 |
| - const root = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless') |
12 |
| - const pagesManifestPath = join(root, 'pages-manifest.json') |
13 |
| - if (!existsSync(pagesManifestPath)) { |
14 |
| - failBuild(`Could not find pages manifest at ${pagesManifestPath}`) |
15 |
| - } |
| 14 | + console.log('Moving static page files to serve from CDN...') |
| 15 | + const root = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless', 'pages') |
| 16 | + |
16 | 17 | const files = []
|
17 | 18 |
|
18 | 19 | const moveFile = async (file) => {
|
19 | 20 | const source = join(root, file)
|
20 |
| - // Trim the initial "pages" |
21 |
| - const filePath = file.slice(6) |
22 |
| - files.push(filePath) |
23 |
| - const dest = join(netlifyConfig.build.publish, filePath) |
| 21 | + files.push(file) |
| 22 | + const dest = join(netlifyConfig.build.publish, source) |
24 | 23 | await move(source, dest)
|
25 | 24 | }
|
| 25 | + // Move all static files, except error documents and nft manifests |
| 26 | + const pages = await glob('**/!(500|404|*.nft).{html,json}', { |
| 27 | + cwd: root, |
| 28 | + dot: true, |
| 29 | + }) |
26 | 30 |
|
27 |
| - const pagesManifest = await readJson(pagesManifestPath) |
28 |
| - // Arbitrary limit of 10 concurrent file moves |
29 |
| - const limit = pLimit(10) |
30 |
| - const promises = Object.entries(pagesManifest).map(async ([route, filePath]) => { |
31 |
| - if ( |
32 |
| - isDynamicRoute(route) || |
33 |
| - !(filePath.endsWith('.html') || filePath.endsWith('.json')) || |
34 |
| - filePath.endsWith('/404.html') || |
35 |
| - filePath.endsWith('/500.html') |
36 |
| - ) { |
| 31 | + // Limit concurrent file moves to number of cpus or 2 if there is only 1 |
| 32 | + const limit = pLimit(Math.max(2, cpus().length)) |
| 33 | + const promises = pages.map(async (filePath) => { |
| 34 | + if (isDynamicRoute(filePath)) { |
37 | 35 | return
|
38 | 36 | }
|
39 | 37 | return limit(moveFile, filePath)
|
40 | 38 | })
|
41 | 39 | await Promise.all(promises)
|
42 |
| - console.log(`Moved ${files.length} page files`) |
| 40 | + console.log(`Moved ${files.length} files`) |
43 | 41 |
|
44 | 42 | // Write the manifest for use in the serverless functions
|
45 | 43 | await writeJson(join(netlifyConfig.build.publish, 'static-manifest.json'), files)
|
|
0 commit comments