UNPKG

3.06 kBJavaScriptView Raw
1const path = require('path')
2const fs = require('fs')
3const mockFs = require('./mock-fs')
4const { promisify } = require('util')
5const writeFile = promisify(fs.writeFile)
6const makeDir = require('make-dir')
7const del = require('del')
8
9const runtimeTsConfig = {
10 compilerOptions: {
11 lib: ['esnext', 'esnext.asynciterable'],
12 module: 'commonjs',
13 target: 'es2018',
14 strict: false,
15 esModuleInterop: true,
16 sourceMap: true,
17 noImplicitAny: false,
18 outDir: 'runtime',
19 rootDir: 'src/runtime',
20 declaration: true,
21 },
22 include: ['src/runtime'],
23 exclude: [
24 'archive',
25 'dist',
26 'build',
27 'cli',
28 'examples',
29 'runtime',
30 'src/fixtures',
31 'src/__tests__',
32 ],
33}
34
35mockFs({
36 [path.join(__dirname, '../tsconfig.json')]: JSON.stringify(
37 runtimeTsConfig,
38 null,
39 2,
40 ),
41})
42
43const options = {
44 minify: true,
45 sourceMap: true,
46 sourceMapRegister: false,
47}
48
49let targetDir = path.join(__dirname, '../runtime')
50let sourceFile = path.join(__dirname, '../src/runtime/index.ts')
51
52require('@zeit/ncc')(sourceFile, options)
53 .then(async ({ code, map, assets }) => {
54 // Assets is an object of asset file names to { source, permissions, symlinks }
55 // expected relative to the output code (if any)
56 await saveToDisc(code, map, assets, targetDir)
57 })
58 .catch(console.error)
59
60async function saveToDisc(source, map, assets, outputDir) {
61 await del([
62 outputDir + '/**',
63 '!' + outputDir,
64 `!${path.join(outputDir, 'prisma')}`,
65 ])
66 await makeDir(outputDir)
67 assets['index.js'] = { source: fixCode(source) }
68 if (map) {
69 assets['index.js.map'] = map
70 }
71 // TODO add concurrency when we would have too many files
72 const madeDirs = {}
73 const files = []
74 await Promise.all(
75 Object.entries(assets).map(async ([filePath, file]) => {
76 const targetPath = path.join(outputDir, filePath)
77 const targetDir = path.dirname(targetPath)
78 if (!madeDirs[targetDir]) {
79 await makeDir(targetDir)
80 madeDirs[targetDir] = true
81 }
82 if (!file.source) {
83 files.push({
84 size: Math.round(file.length / 1024),
85 targetPath,
86 })
87 await writeFile(targetPath, file)
88 } else {
89 files.push({
90 size: Math.round(file.source.length / 1024),
91 targetPath,
92 })
93 await writeFile(targetPath, file.source)
94 }
95 }),
96 )
97 files.sort((a, b) => (a.size < b.size ? -1 : 1))
98 const max = files.reduce((max, curr) => Math.max(max, curr.size), 0)
99 const maxLength = `${max}kB`.length
100 const doneStr = files
101 .map(
102 ({ size, targetPath }) =>
103 `${size}kB`.padStart(maxLength) +
104 ' ' +
105 path.relative(process.cwd(), targetPath),
106 )
107 .join('\n')
108 console.log(doneStr)
109}
110
111function fixCode(code) {
112 return code
113 .split('\n')
114 .map((line) => {
115 if (line.startsWith('NodeEngine.defaultPrismaPath = path.join(')) {
116 return `NodeEngine.defaultPrismaPath = path.join(__dirname + '/prisma');`
117 }
118 return line
119 })
120 .join('\n')
121}