Skip to content

Commit 88a6338

Browse files
committed
feat(pack): remove replace option
1 parent 69789db commit 88a6338

2 files changed

Lines changed: 4 additions & 36 deletions

File tree

packages/pack/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ type Options = {
238238
* A callback for when creating side effects.
239239
*/
240240
onCreateSideEffects: (path: string) => string[] | boolean | undefined
241-
242-
/**
243-
* A map whose keys must be replaced for values.
244-
*/
245-
replace?: Record<string, string>
246241
}
247242
248243
function useTypeScriptPlugin(options: Options): TypeScriptPlugin

packages/pack/src/plugins/TypeScriptPlugin.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { exec } from 'child_process'
22
import { promisify } from 'util'
33
import { resolve, join, dirname, relative } from 'path'
4-
import { existsSync, writeJson, readFile, writeFile } from 'fs-extra'
4+
import { existsSync, writeJson } from 'fs-extra'
55
import glob from 'fast-glob'
66

77
import { Plugin, OnDone, HookOptions } from '../interfaces'
@@ -28,7 +28,6 @@ type Options = {
2828

2929
class TypeScriptPlugin implements Plugin {
3030
name = 'TypeScriptPlugin'
31-
private typescriptResult: { stdout: string }[] = []
3231

3332
constructor(public options: Options = {} as Options) {
3433
mark('TypeScriptPlugin::constructor')
@@ -38,11 +37,11 @@ class TypeScriptPlugin implements Plugin {
3837
mark('TypeScriptPlugin::onRun(start)')
3938
const configPath = this.getConfigPath(context)
4039
try {
41-
this.typescriptResult = await Promise.all([
40+
await Promise.all([
4241
// prettier-ignore
43-
execAsync(`npx tsc -p ${configPath} --listEmittedFiles --module commonjs --outDir ${output}`),
42+
execAsync(`npx tsc -p ${configPath} --module commonjs --outDir ${output}`),
4443
// prettier-ignore
45-
execAsync(`npx tsc -p ${configPath} --listEmittedFiles --module esnext --outDir ${resolve(output, 'esm')}`),
44+
execAsync(`npx tsc -p ${configPath} --module esnext --outDir ${resolve(output, 'esm')}`),
4645
])
4746
} catch (error) {
4847
throw new Error(error.stdout)
@@ -52,32 +51,6 @@ class TypeScriptPlugin implements Plugin {
5251
done()
5352
}
5453

55-
async onAfterRun(done: OnDone) {
56-
if (this.options.replace !== undefined) {
57-
const [cjs, esm] = this.typescriptResult
58-
this.replace(cjs.stdout)
59-
this.replace(esm.stdout)
60-
}
61-
done()
62-
}
63-
64-
private async replace(rawFiles: string): Promise<void> {
65-
// Remove artifacts from tsc stdout.
66-
const files = rawFiles
67-
.split(/\n/)
68-
.map((x) => x.replace(/^TSFILE:\s/, ''))
69-
.filter(Boolean)
70-
for (const file of files) {
71-
let content = await readFile(file, 'utf-8')
72-
for (const key in this.options.replace) {
73-
const needleRe = new RegExp(key, 'g')
74-
content = content.replace(needleRe, this.options.replace[key])
75-
}
76-
// TODO: Maybe optimized — don't rewrite file if nothing replaced.
77-
await writeFile(file, content)
78-
}
79-
}
80-
8154
private getConfigPath(context: string): string {
8255
const configPath = resolve(context, this.options.configPath || 'tsconfig.json')
8356
if (!existsSync(configPath)) {

0 commit comments

Comments
 (0)