Skip to content

Commit

Permalink
feat(pack): remove replace option
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Jun 22, 2021
1 parent 69789db commit 88a6338
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 36 deletions.
5 changes: 0 additions & 5 deletions packages/pack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,6 @@ type Options = {
* A callback for when creating side effects.
*/
onCreateSideEffects: (path: string) => string[] | boolean | undefined
/**
* A map whose keys must be replaced for values.
*/
replace?: Record<string, string>
}
function useTypeScriptPlugin(options: Options): TypeScriptPlugin
Expand Down
35 changes: 4 additions & 31 deletions packages/pack/src/plugins/TypeScriptPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exec } from 'child_process'
import { promisify } from 'util'
import { resolve, join, dirname, relative } from 'path'
import { existsSync, writeJson, readFile, writeFile } from 'fs-extra'
import { existsSync, writeJson } from 'fs-extra'
import glob from 'fast-glob'

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

class TypeScriptPlugin implements Plugin {
name = 'TypeScriptPlugin'
private typescriptResult: { stdout: string }[] = []

constructor(public options: Options = {} as Options) {
mark('TypeScriptPlugin::constructor')
Expand All @@ -38,11 +37,11 @@ class TypeScriptPlugin implements Plugin {
mark('TypeScriptPlugin::onRun(start)')
const configPath = this.getConfigPath(context)
try {
this.typescriptResult = await Promise.all([
await Promise.all([
// prettier-ignore
execAsync(`npx tsc -p ${configPath} --listEmittedFiles --module commonjs --outDir ${output}`),
execAsync(`npx tsc -p ${configPath} --module commonjs --outDir ${output}`),
// prettier-ignore
execAsync(`npx tsc -p ${configPath} --listEmittedFiles --module esnext --outDir ${resolve(output, 'esm')}`),
execAsync(`npx tsc -p ${configPath} --module esnext --outDir ${resolve(output, 'esm')}`),
])
} catch (error) {
throw new Error(error.stdout)
Expand All @@ -52,32 +51,6 @@ class TypeScriptPlugin implements Plugin {
done()
}

async onAfterRun(done: OnDone) {
if (this.options.replace !== undefined) {
const [cjs, esm] = this.typescriptResult
this.replace(cjs.stdout)
this.replace(esm.stdout)
}
done()
}

private async replace(rawFiles: string): Promise<void> {
// Remove artifacts from tsc stdout.
const files = rawFiles
.split(/\n/)
.map((x) => x.replace(/^TSFILE:\s/, ''))
.filter(Boolean)
for (const file of files) {
let content = await readFile(file, 'utf-8')
for (const key in this.options.replace) {
const needleRe = new RegExp(key, 'g')
content = content.replace(needleRe, this.options.replace[key])
}
// TODO: Maybe optimized — don't rewrite file if nothing replaced.
await writeFile(file, content)
}
}

private getConfigPath(context: string): string {
const configPath = resolve(context, this.options.configPath || 'tsconfig.json')
if (!existsSync(configPath)) {
Expand Down

0 comments on commit 88a6338

Please sign in to comment.