Skip to content

Commit

Permalink
update arc write full path on output
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstyles committed May 17, 2024
1 parent 39d0f06 commit 0ca2966
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
11 changes: 2 additions & 9 deletions scripts/arc/src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parse } from 'tsconfck'
import ts from 'typescript'

import { createLogger } from '@urban-ui/arc-log'
import { readFile, writeFile } from './file.ts'
import { log } from './log'
import { Pipeline } from './transform/pipeline.ts'
import { createTask } from './transform/task.ts'
Expand Down Expand Up @@ -139,6 +140,7 @@ const write = createTask(
await Promise.all(
Object.entries(files).map(async ([filepath, content]) => {
const report = await writeFile(filepath, content)
debug('Writing file:', report)
// ctx.ftrace.getSizes(report.filepath).dts = report.size;
// return writeFile(filepath, content);
}),
Expand All @@ -148,15 +150,6 @@ const write = createTask(
},
)

async function writeFile(filepath: string, content: string) {
const bytes = await Bun.write(filepath, content)
debug('Writing file:', filepath, bytes)
return {
filepath: filepath,
size: bytes,
}
}

/**
* Using ts method to get the full ts config, including extends from a monorepo/workspace.
* ~3x slower than tsconfck with parsing
Expand Down
25 changes: 25 additions & 0 deletions scripts/arc/src/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'node:fs/promises'
import path from 'node:path'

export async function writeFile(filepath: string, content: string) {
const dirPath = path.dirname(filepath)
if (!(await fs.exists(dirPath))) {
await fs.mkdir(dirPath, { recursive: true })
}

const bytes = await Bun.write(filepath, content)

return {
filepath: filepath,
size: bytes,
}
}

export async function readFile(filepath: string) {
const file = Bun.file(filepath)
const content = await file.text()
return {
file: content,
filepath: filepath,
}
}
39 changes: 11 additions & 28 deletions scripts/arc/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createLogger } from '@urban-ui/arc-log'
import { jscOps } from './configs/jsc.ts'
import { minify } from './configs/minify.ts'
import { transformImports } from './configs/plugins.ts'
import { readFile, writeFile } from './file.ts'
import { log } from './log'
import { traceFn } from './trace.ts'
import { fileEvents, measure } from './transform/analytics.ts'
Expand Down Expand Up @@ -181,32 +182,36 @@ const write = createTask(
await Promise.all([
pipe(
async () => await writeFile(esmFilepath, files.esm.code),
async (opts) => {
ctx.ftrace.getSizes(filepath).esm = opts.size
return opts
async (file) => {
debug('Write file:', file)
ctx.ftrace.getSizes(filepath).esm = file.size
return file
},
),
files.esm.map &&
pipe(
async () =>
await writeFile(`${esmFilepath}.map`, files.esm.map as string),
async (file) => {
debug('Write file:', file)
ctx.ftrace.getSizes(filepath)['esm::map'] = file.size
return file
},
),
pipe(
async () => await writeFile(cjsFilepath, files.cjs.code),
async (opts) => {
ctx.ftrace.getSizes(filepath).cjs = opts.size
return opts
async (file) => {
debug('Write file:', file)
ctx.ftrace.getSizes(filepath).cjs = file.size
return file
},
),
files.cjs.map &&
pipe(
async () =>
await writeFile(`${cjsFilepath}.map`, files.cjs.map as string),
async (file) => {
debug('Write file:', file)
ctx.ftrace.getSizes(filepath)['cjs::map'] = file.size
return file
},
Expand All @@ -228,28 +233,6 @@ async function pipe<A, B>(
return await fn2(value)
}

async function readFile(filepath: string) {
const file = Bun.file(filepath)
const content = await file.text()
return {
file: content,
filepath: filepath,
}
}

async function writeFile(filepath: string, content: string) {
const bytes = await Bun.write(filepath, content)
debug('Writing file:', filepath, bytes)

// gzip is not free, increases time by ~50%.
// @TODO add option to output gzipped weight.
// const l = await gzip(Buffer.from(content))
return {
filepath: filepath,
size: bytes,
}
}

function generateOutputPath(
filepath: string,
ext: string,
Expand Down

0 comments on commit 0ca2966

Please sign in to comment.