| 37 | } |
| 38 | |
| 39 | async function main() { |
| 40 | console.log('Compiling sequelize...'); |
| 41 | const [sourceFiles] = await Promise.all([ |
| 42 | // Find all .js and .ts files from /src |
| 43 | glob('./src/**/*.{mjs,cjs,js,mts,cts,ts}', { onlyFiles: true, absolute: false }), |
| 44 | // Delete /lib for a full rebuild. |
| 45 | rmDir(outdir), |
| 46 | // Delete /types for a full rebuild. |
| 47 | rmDir(typesDir) |
| 48 | ]); |
| 49 | |
| 50 | const filesToCompile = []; |
| 51 | const filesToCopyToLib = []; |
| 52 | const declarationFiles = []; |
| 53 | |
| 54 | for (const file of sourceFiles) { |
| 55 | // mjs files cannot be built as they would be compiled to commonjs |
| 56 | if (file.endsWith('.mjs')) { |
| 57 | filesToCopyToLib.push(file); |
| 58 | } else if (file.endsWith('.d.ts')) { |
| 59 | declarationFiles.push(file); |
| 60 | } else { |
| 61 | filesToCompile.push(file); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // copy .d.ts files prior to generating them from the .ts files |
| 66 | // so the .ts files in lib/ will take priority.. |
| 67 | await copyFiles( |
| 68 | // The last path in the list is the output directory |
| 69 | declarationFiles.concat(typesDir), |
| 70 | { up: 1 } |
| 71 | ); |
| 72 | |
| 73 | if (filesToCopyToLib.length > 0) { |
| 74 | await copyFiles( |
| 75 | // The last path in the list is the output directory |
| 76 | filesToCopyToLib.concat(outdir), |
| 77 | { up: 1 } |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | await Promise.all([ |
| 82 | build({ |
| 83 | // Adds source mapping |
| 84 | sourcemap: true, |
| 85 | // The compiled code should be usable in node v10 |
| 86 | target: 'node10', |
| 87 | // The source code's format is commonjs. |
| 88 | format: 'cjs', |
| 89 | |
| 90 | outdir, |
| 91 | entryPoints: filesToCompile |
| 92 | .map(file => path.resolve(file)) |
| 93 | }), |
| 94 | |
| 95 | exec('tsc', { |
| 96 | env: { |