-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
153 lines (125 loc) · 4.22 KB
/
build.js
File metadata and controls
153 lines (125 loc) · 4.22 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
/**
* Build script for prefetch.ru
*
* Generates:
* - prefetch.js (IIFE, unminified)
* - prefetch.esm.js (ESM, unminified)
* - dist/prefetch.min.js (IIFE, minified)
* - dist/prefetch.esm.min.js (ESM, minified)
*/
const fs = require('fs')
const path = require('path')
const { rollup } = require('rollup')
const { minify } = require('terser')
const pkg = require('./package.json')
const VERSION = pkg.version
// Banner для файлов
const BANNER_IIFE = `/*!
* prefetch.ru v${VERSION} - Мгновенная загрузка страниц
* © 2026 Сергей Макаров | MIT License
* https://prefetch.ru | https://github.com/prefetch-ru
*/`
const BANNER_ESM = `/*!
* prefetch.ru v${VERSION} (ESM) - Мгновенная загрузка страниц
* © 2026 Сергей Макаров | MIT License
* https://prefetch.ru | https://github.com/prefetch-ru
*/`
// UTF-8 BOM для гарантированного распознавания кодировки
const BOM = '\uFEFF'
// Rollup plugin для замены __VERSION__
function replaceVersion() {
return {
name: 'replace-version',
transform(code) {
return {
code: code.replace(/__VERSION__/g, VERSION),
map: null
}
}
}
}
async function build() {
const distDir = path.join(__dirname, 'dist')
// Создаём папку dist если не существует
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir, { recursive: true })
}
console.log(`📦 Building prefetch.ru v${VERSION}...\n`)
// ============================================
// 1. IIFE версия: src/entry-iife.js → prefetch.js
// ============================================
console.log('🔨 Building IIFE version...')
const iifeBundle = await rollup({
input: path.join(__dirname, 'src/entry-iife.js'),
plugins: [replaceVersion()]
})
const iifeOutput = await iifeBundle.generate({
format: 'iife', // Rollup добавит IIFE обёртку
banner: BANNER_IIFE,
compact: false
})
let iifeCode = iifeOutput.output[0].code
fs.writeFileSync(path.join(__dirname, 'prefetch.js'), BOM + iifeCode, 'utf8')
console.log(' ✓ prefetch.js')
// Minified IIFE
const iifeMinified = await minify(iifeCode, {
compress: true,
mangle: true,
format: {
comments: /^!/ // Сохраняем только комментарии с ! (лицензионные)
}
})
fs.writeFileSync(path.join(distDir, 'prefetch.min.js'), BOM + iifeMinified.code, 'utf8')
console.log(' ✓ dist/prefetch.min.js')
await iifeBundle.close()
// ============================================
// 2. ESM версия: src/entry-esm.js → prefetch.esm.js
// ============================================
console.log('🔨 Building ESM version...')
const esmBundle = await rollup({
input: path.join(__dirname, 'src/entry-esm.js'),
plugins: [replaceVersion()]
})
const esmOutput = await esmBundle.generate({
format: 'es',
banner: BANNER_ESM,
compact: false
})
let esmCode = esmOutput.output[0].code
fs.writeFileSync(path.join(__dirname, 'prefetch.esm.js'), BOM + esmCode, 'utf8')
console.log(' ✓ prefetch.esm.js')
// Minified ESM
const esmMinified = await minify(esmCode, {
module: true,
compress: true,
mangle: true,
format: {
comments: /^!/ // Сохраняем только комментарии с ! (лицензионные)
}
})
fs.writeFileSync(path.join(distDir, 'prefetch.esm.min.js'), BOM + esmMinified.code, 'utf8')
console.log(' ✓ dist/prefetch.esm.min.js')
await esmBundle.close()
// ============================================
// Summary
// ============================================
console.log('\n✅ Build complete!\n')
const files = [
'prefetch.js',
'prefetch.esm.js',
'dist/prefetch.min.js',
'dist/prefetch.esm.min.js'
]
console.log('📄 Generated files:')
for (const file of files) {
const filePath = path.join(__dirname, file)
const size = fs.statSync(filePath).size
const sizeKb = (size / 1024).toFixed(2)
console.log(` ${file}: ${sizeKb} KB`)
}
}
build().catch(err => {
console.error('❌ Build failed:', err)
process.exit(1)
})