-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the compile task and use swc for non-watchmode builds
- Loading branch information
Showing
14 changed files
with
126 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Task} from './task/task.js'; | ||
import {compile} from './compile/compile.js'; | ||
import {cleanBuild} from './project/clean.js'; | ||
|
||
export const task: Task = { | ||
description: 'compiles all files to the build directory', | ||
run: async ({log}) => { | ||
await cleanBuild(log); | ||
await compile(log); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import swc from '@swc/core'; | ||
import {join} from 'path'; | ||
|
||
import {loadTsconfig} from './tsHelpers.js'; | ||
import {toSwcCompilerTarget, mergeSwcOptions, getDefaultSwcOptions} from './swcHelpers.js'; | ||
import {spawnProcess} from '../utils/process.js'; | ||
import {printMs, printPath, printSubTiming} from '../utils/print.js'; | ||
import {Logger} from '../utils/log.js'; | ||
import {Timings} from '../utils/time.js'; | ||
import {findFiles, outputFile, readFile} from '../fs/nodeFs.js'; | ||
import {paths, toBuildId} from '../paths.js'; | ||
import {red} from '../colors/terminal.js'; | ||
|
||
export const compile = async (log: Logger): Promise<void> => { | ||
log.info('compiling...'); | ||
|
||
const timings = new Timings<'total'>(); | ||
timings.start('total'); | ||
const subTimings = new Timings(); | ||
const logTimings = () => { | ||
for (const [key, timing] of subTimings.getAll()) { | ||
log.trace(printSubTiming(key, timing)); | ||
} | ||
log.info(`🕒 compiled in ${printMs(timings.stop('total'))}`); | ||
}; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
await spawnProcess('node_modules/.bin/tsc'); // ignore compiler errors | ||
logTimings(); | ||
return; | ||
} | ||
|
||
// load all files into memory | ||
subTimings.start('find files'); | ||
const statsByPath = await findFiles(paths.source, ({path}) => path.endsWith('.ts'), null); | ||
subTimings.stop('find files'); | ||
subTimings.start('read files'); | ||
const codeByPath = new Map<string, string>(); | ||
await Promise.all( | ||
Array.from(statsByPath.entries()).map(async ([path, stats]) => { | ||
if (stats.isDirectory()) return; | ||
const contents = await readFile(join(paths.source, path), 'utf8'); | ||
// console.log('file', path, contents.length); | ||
codeByPath.set(path, contents); | ||
}), | ||
); | ||
subTimings.stop('read files'); | ||
|
||
// load the options | ||
const tsconfigPath = undefined; // TODO parameterized options? | ||
const basePath = undefined; // TODO parameterized options? | ||
subTimings.start('load tsconfig'); | ||
const tsconfig = loadTsconfig(log, tsconfigPath, basePath); | ||
subTimings.stop('load tsconfig'); | ||
const {compilerOptions} = tsconfig; | ||
const target = toSwcCompilerTarget(compilerOptions && compilerOptions.target); | ||
const swcOptions = getDefaultSwcOptions(); // TODO parameterized options? | ||
|
||
const results = new Map<string, string>(); | ||
|
||
// compile everything | ||
subTimings.start('compile'); | ||
await Promise.all( | ||
Array.from(codeByPath.entries()).map(async ([path, code]) => { | ||
const finalSwcOptions = mergeSwcOptions(swcOptions, target, path); | ||
|
||
let output: swc.Output; | ||
try { | ||
// TODO maybe use the async version so we can preprocess in parallel? | ||
output = await swc.transform(code, finalSwcOptions); | ||
} catch (err) { | ||
log.error(red('Failed to transpile TypeScript'), printPath(path)); | ||
throw err; | ||
} | ||
|
||
results.set(path, output.code); | ||
results.set(`${path}.map`, output.map!); | ||
}), | ||
); | ||
subTimings.stop('compile'); | ||
|
||
// output the compiled files | ||
subTimings.start('write to disk'); | ||
await Promise.all( | ||
Array.from(results.entries()).map(([path, contents]) => outputFile(toBuildId(path), contents)), | ||
); | ||
subTimings.stop('write to disk'); | ||
|
||
logTimings(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,9 @@ | ||
import {Task} from '../task/task.js'; | ||
import {spawnProcess} from '../utils/process.js'; | ||
import {cleanBuild} from './clean.js'; | ||
|
||
// TODO `process.env.NODE_ENV = 'production'`? | ||
// set it where? what will it be used for? | ||
|
||
export const task: Task = { | ||
description: 'build, create, and link the distribution', | ||
run: async ({log, invokeTask}) => { | ||
await cleanBuild(log); | ||
|
||
log.info('compiling typescript'); | ||
await spawnProcess('node_modules/.bin/tsc'); | ||
|
||
run: async ({invokeTask}) => { | ||
await invokeTask('compile'); | ||
await invokeTask('project/dist'); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters