-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepublish.ts
95 lines (65 loc) · 2.93 KB
/
prepublish.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict'
import fs from 'fs'
import path from 'path'
import { execSync as shell } from 'child_process'
import esbuild from 'esbuild'
import { PATHS, LOC_NAMES, DEFAULT_CONFIG } from './core/constants.js'
function iterateFiles(dirPath: string, cb: (nextDir: string, curDir: string) => void) {
fs.readdirSync(dirPath, { withFileTypes: true })
.forEach(dirent => {
const { name } = dirent
const nextDirPath = `${dirPath}/${name}`
dirent.isDirectory()
? iterateFiles(nextDirPath, cb)
: cb(nextDirPath, dirPath)
})
}
function copyTypes(iterateOverDirPath: string) {
const dirName = iterateOverDirPath.slice( iterateOverDirPath.lastIndexOf('/') + 1 )
iterateFiles(iterateOverDirPath, fileNamePath => {
if (fileNamePath.endsWith('.d.ts') || fileNamePath.endsWith('.sass')) {
const destinationFileName = fileNamePath.replace(
dirName,
`${LOC_NAMES.LIB_OUTPUT_DIRNAME}/${dirName}`
)
fs.createReadStream(fileNamePath)
.pipe(fs.createWriteStream(destinationFileName))
}
})
}
const addExtensionToImportRegExp = /((import|export) .* from\s+['"])(?!@)((.*\/.*)(?<![.]\w*))(?=['"])/g
function normalizeImportPathsAndMinify(iterateOverDirPath: string, isMinify = true) {
iterateFiles(iterateOverDirPath, (fileNamePath, dirPath) => {
if (fileNamePath.endsWith('.js')) {
let notMinifiedJSFile = fs.readFileSync(fileNamePath, 'utf8')
const matchIterator = notMinifiedJSFile.matchAll(addExtensionToImportRegExp)
for (const matchedGroups of matchIterator) {
const [ , _import, , importPath ] = matchedGroups
const importPathResolved = path.join(dirPath, importPath)
const isDirectory = fs.existsSync(importPathResolved) && fs.lstatSync(importPathResolved).isDirectory()
const replace = _import + importPath
const replaceWith = replace + (
isDirectory
? `${replace.endsWith('/') ? '' : '/'}index.js`
: '.js'
)
notMinifiedJSFile = notMinifiedJSFile.replace(replace, replaceWith)
}
const resultCode = isMinify
? esbuild.transformSync(notMinifiedJSFile, {
minify: true,
target: DEFAULT_CONFIG.build.output.target
}).code
: notMinifiedJSFile
fs.writeFileSync(fileNamePath, resultCode)
}
})
}
shell('npx tsc -p .')
copyTypes(PATHS.clientCore)
normalizeImportPathsAndMinify(PATHS.clientCoreOutput)
copyTypes(PATHS.sharedUtils)
normalizeImportPathsAndMinify(PATHS.sharedUtilsOutput)
copyTypes(PATHS.src)
normalizeImportPathsAndMinify(PATHS.srcOutput, false)
normalizeImportPathsAndMinify(PATHS.binOutput, false)