|
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 slash = require('slash') |
| 8 | +const glob = require('tiny-glob') |
5 | 9 |
|
6 |
| -const TEST_ROUTE = /\/\[[^/]+?](?=\/|$)/ |
| 10 | +const TEST_ROUTE = /(|\/)\[[^/]+?](\/|\.html|$)/ |
7 | 11 |
|
8 | 12 | const isDynamicRoute = (route) => TEST_ROUTE.test(route)
|
9 | 13 |
|
10 | 14 | 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 |
| - } |
| 15 | + console.log('Moving static page files to serve from CDN...') |
| 16 | + const root = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless', 'pages') |
| 17 | + |
16 | 18 | const files = []
|
17 | 19 |
|
18 | 20 | const moveFile = async (file) => {
|
19 | 21 | 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) |
| 22 | + files.push(file) |
| 23 | + const dest = join(netlifyConfig.build.publish, file) |
24 | 24 | await move(source, dest)
|
25 | 25 | }
|
| 26 | + // Move all static files, except error documents and nft manifests |
| 27 | + const pages = await glob('**/!(500|404|*.js.nft).{html,json}', { |
| 28 | + cwd: root, |
| 29 | + dot: true, |
| 30 | + }) |
26 | 31 |
|
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 |
| - ) { |
| 32 | + // Limit concurrent file moves to number of cpus or 2 if there is only 1 |
| 33 | + const limit = pLimit(Math.max(2, cpus().length)) |
| 34 | + const promises = pages.map(async (rawPath) => { |
| 35 | + const filePath = slash(rawPath) |
| 36 | + if (isDynamicRoute(filePath)) { |
37 | 37 | return
|
38 | 38 | }
|
39 | 39 | return limit(moveFile, filePath)
|
40 | 40 | })
|
41 | 41 | await Promise.all(promises)
|
42 |
| - console.log(`Moved ${files.length} page files`) |
| 42 | + console.log(`Moved ${files.length} files`) |
43 | 43 |
|
44 | 44 | // Write the manifest for use in the serverless functions
|
45 | 45 | await writeJson(join(netlifyConfig.build.publish, 'static-manifest.json'), files)
|
46 | 46 |
|
47 | 47 | if (i18n?.defaultLocale) {
|
48 | 48 | // Copy the default locale into the root
|
49 |
| - await copy(join(netlifyConfig.build.publish, i18n.defaultLocale), `${netlifyConfig.build.publish}/`) |
| 49 | + const defaultLocaleDir = join(netlifyConfig.build.publish, i18n.defaultLocale) |
| 50 | + if (existsSync(defaultLocaleDir)) { |
| 51 | + await copy(defaultLocaleDir, `${netlifyConfig.build.publish}/`) |
| 52 | + } |
50 | 53 | }
|
51 | 54 | }
|
52 | 55 |
|
|
0 commit comments