2018-12-18 01:21:29 +00:00
|
|
|
import chokidar from 'chokidar'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
2019-01-19 23:06:25 +00:00
|
|
|
import { promisify } from 'util'
|
2021-07-05 03:19:04 +00:00
|
|
|
import { buildSass } from './build-sass.js'
|
|
|
|
import { buildInlineScript } from './build-inline-script.js'
|
|
|
|
import { buildSvg } from './build-svg.js'
|
2021-07-04 23:39:48 +00:00
|
|
|
import { performance } from 'perf_hooks'
|
2021-07-05 03:19:04 +00:00
|
|
|
import { debounce } from '../src/routes/_thirdparty/lodash/timers.js'
|
|
|
|
import applyIntl from '../webpack/svelte-intl-loader.js'
|
|
|
|
import { LOCALE } from '../src/routes/_static/intl.js'
|
|
|
|
import rtlDetectPackage from 'rtl-detect'
|
2018-12-18 01:21:29 +00:00
|
|
|
|
2021-07-05 03:19:04 +00:00
|
|
|
const { getLangDir } = rtlDetectPackage
|
|
|
|
|
|
|
|
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
2019-01-19 23:06:25 +00:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
2020-11-29 22:13:27 +00:00
|
|
|
const LOCALE_DIRECTION = getLangDir(LOCALE)
|
2018-12-18 01:21:29 +00:00
|
|
|
const DEBOUNCE = 500
|
|
|
|
|
|
|
|
const builders = [
|
|
|
|
{
|
2019-01-26 17:34:05 +00:00
|
|
|
watch: 'src/scss',
|
2018-12-18 01:21:29 +00:00
|
|
|
comment: '<!-- inline CSS -->',
|
|
|
|
rebuild: buildSass
|
|
|
|
},
|
|
|
|
{
|
2019-01-26 18:14:15 +00:00
|
|
|
watch: 'src/inline-script/inline-script.js',
|
2018-12-18 01:21:29 +00:00
|
|
|
comment: '<!-- inline JS -->',
|
|
|
|
rebuild: buildInlineScript
|
|
|
|
},
|
|
|
|
{
|
2019-02-03 07:03:40 +00:00
|
|
|
watch: 'bin/svgs.js',
|
2018-12-18 01:21:29 +00:00
|
|
|
comment: '<!-- inline SVG -->',
|
|
|
|
rebuild: buildSvg
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// array of strings and builder functions, we build this on-the-fly
|
|
|
|
const partials = buildPartials()
|
|
|
|
|
|
|
|
function buildPartials () {
|
2019-08-03 20:49:37 +00:00
|
|
|
const rawTemplate = fs.readFileSync(path.resolve(__dirname, '../src/build/template.html'), 'utf8')
|
2018-12-18 01:21:29 +00:00
|
|
|
|
2019-08-03 20:49:37 +00:00
|
|
|
const partials = [rawTemplate]
|
2018-12-18 01:21:29 +00:00
|
|
|
|
|
|
|
builders.forEach(builder => {
|
|
|
|
for (let i = 0; i < partials.length; i++) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const partial = partials[i]
|
2018-12-18 01:21:29 +00:00
|
|
|
if (typeof partial !== 'string') {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-03 20:49:37 +00:00
|
|
|
const idx = partial.indexOf(builder.comment)
|
2018-12-18 01:21:29 +00:00
|
|
|
if (idx !== -1) {
|
|
|
|
partials.splice(
|
|
|
|
i,
|
|
|
|
1,
|
|
|
|
partial.substring(0, idx),
|
|
|
|
builder,
|
|
|
|
partial.substring(idx + builder.comment.length)
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return partials
|
|
|
|
}
|
|
|
|
|
|
|
|
function doWatch () {
|
|
|
|
// rebuild each of the partials on-the-fly if something changes
|
|
|
|
partials.forEach(partial => {
|
|
|
|
if (typeof partial === 'string') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
chokidar.watch(partial.watch).on('change', debounce(path => {
|
|
|
|
console.log(`Detected change in ${path}...`)
|
|
|
|
delete partial.result
|
|
|
|
buildAll()
|
|
|
|
}), DEBOUNCE)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildAll () {
|
2021-07-04 23:39:48 +00:00
|
|
|
const start = performance.now()
|
2020-11-29 22:13:27 +00:00
|
|
|
let html = (await Promise.all(partials.map(async partial => {
|
2018-12-18 01:21:29 +00:00
|
|
|
if (typeof partial === 'string') {
|
|
|
|
return partial
|
|
|
|
}
|
|
|
|
if (!partial.result) {
|
|
|
|
partial.result = partial.comment + '\n' + (await partial.rebuild())
|
|
|
|
}
|
|
|
|
return partial.result
|
|
|
|
}))).join('')
|
|
|
|
|
2020-11-29 22:13:27 +00:00
|
|
|
html = applyIntl(html)
|
|
|
|
.replace('{process.env.LOCALE}', LOCALE)
|
|
|
|
.replace('{process.env.LOCALE_DIRECTION}', LOCALE_DIRECTION)
|
2018-12-18 01:21:29 +00:00
|
|
|
await writeFile(path.resolve(__dirname, '../src/template.html'), html, 'utf8')
|
2021-07-04 23:39:48 +00:00
|
|
|
const end = performance.now()
|
2018-12-18 01:21:29 +00:00
|
|
|
console.log(`Built template.html in ${(end - start).toFixed(2)}ms`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
if (process.argv.includes('--watch')) {
|
|
|
|
doWatch()
|
2018-12-18 06:42:39 +00:00
|
|
|
} else {
|
|
|
|
await buildAll()
|
2018-12-18 01:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|