forked from slimjs/slim.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
110 lines (92 loc) · 3.1 KB
/
build.js
File metadata and controls
110 lines (92 loc) · 3.1 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const fs = require('fs')
const path = require('path')
const UglifyJS = require('uglify-es')
const readline = require('readline')
const ROOT = process.cwd()
const PATH = {
ROOT,
DIRECTIVES: path.resolve(ROOT, 'src/directives'),
CORE: path.resolve(ROOT, 'src/Slim.js'),
DEF: path.resolve(ROOT, 'src/Slim.d.ts'),
DEF_DIST: path.resolve(ROOT, 'Slim.d.ts'),
DECORATORS: path.resolve(ROOT, 'src/Decorators.js'),
DECORATORS_DIST: path.resolve(ROOT, 'Decorators.js'),
DECORATORS_DEF: path.resolve(ROOT, 'src/Decorators.d.ts'),
DECORATORS_DEF_DIST: path.resolve(ROOT, 'Decorators.d.ts'),
DIRECTIVES_DIST: path.resolve(ROOT, 'directives')
}
if (!fs.existsSync(PATH.DIRECTIVES_DIST)) {
fs.mkdirSync(PATH.DIRECTIVES_DIST)
}
function findAllJavascriptFiles(folder) {
return fs
.readdirSync(folder)
.filter(filename => path.extname(filename) === '.js')
}
function stripImportExport (path) {
const streamIn = fs.createReadStream(path)
let streamOut = ''
const rl = readline.createInterface(streamIn)
return new Promise((resolve) => {
rl.on('line', (line) => {
if (!~line.indexOf('import')) {
streamOut = streamOut + line.split('export').join(' ') + '\n'
}
})
rl.on('close', () => {
resolve(streamOut)
})
})
}
async function combineDirectivesNoModule (directives) {
const result = directives.map(async filename => {
return await uglifyFile(path.resolve(PATH.DIRECTIVES, filename), true)
})
const [...combined] = await Promise.all(result)
const targetFile = path.resolve(PATH.DIRECTIVES_DIST, 'all.nomodule.js')
const output = combined.join('\n')
fs.writeFileSync(targetFile, output)
}
async function uglifyFile (path, removeExportStatements = false) {
let content = fs.readFileSync(path, 'utf8').toString()
if (removeExportStatements) {
content = await stripImportExport(path)
}
content = UglifyJS.minify(content).code
return content
}
function copyFile (path, target) {
fs.createReadStream(path)
.pipe(fs.createWriteStream(target))
}
async function build () {
// minify core
fs.writeFileSync(
path.resolve(PATH.ROOT, 'Slim.js'),
await uglifyFile(PATH.CORE))
fs.writeFileSync(
path.resolve(PATH.ROOT, 'Slim.nomodule.js'),
await uglifyFile(PATH.CORE, true)
)
// minify decorators
fs.writeFileSync(
path.resolve(PATH.DECORATORS_DIST),
await uglifyFile(PATH.DECORATORS)
)
// copy typings/definition files
copyFile(PATH.DEF, PATH.DEF_DIST)
copyFile(PATH.DECORATORS_DEF, PATH.DECORATORS_DEF_DIST)
// minify directives
const directives = findAllJavascriptFiles(PATH.DIRECTIVES)
const promises = directives.map(async filename => {
const filepath = path.resolve(PATH.DIRECTIVES, filename)
const result = await uglifyFile(filepath)
const resultNoModule = await uglifyFile(filepath, true)
fs.writeFileSync(path.resolve(PATH.DIRECTIVES_DIST, filename), result)
fs.writeFileSync(path.resolve(PATH.DIRECTIVES_DIST, filename.split('.js').join('.nomodule.js')), resultNoModule)
})
await Promise.all(promises)
// combine directives-nomodule
await combineDirectivesNoModule(directives)
}
build()