Skip to content

Commit

Permalink
chore: bundle doc assets
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Mar 31, 2021
1 parent 4c40483 commit 1fc24e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import { UserConfig } from 'vite'
import prefresh from '@prefresh/vite'
import windicss from 'vite-plugin-windicss'
import { bundleDocsPlugin } from '../scripts/bundle-docs'

const pkg = require('../package.json')

Expand Down Expand Up @@ -42,6 +43,7 @@ const config: UserConfig = {
dirs: [path.resolve('src')],
},
}),
bundleDocsPlugin,
],
}

Expand Down
31 changes: 31 additions & 0 deletions scripts/bundle-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ensure everything results in a single js file
import fs from 'fs'
import { build } from 'esbuild'
import { Plugin } from 'vite'

export const bundleDocsPlugin: Plugin = {
name: 'bundle-docs',

async writeBundle() {
const file = fs
.readdirSync('./docs/dist/assets')
.filter((file) => file.endsWith('.js') && /^index\./.test(file))
await build({
entryPoints: [`./docs/dist/assets/${file}`],
bundle: true,
format: 'esm',
outdir: './docs/dist',
minify: true,
platform: 'browser',
})

const html = fs
.readFileSync('./docs/dist/index.html', 'utf8')
.replace(
/<script type=\"module\" crossorigin src="[^"]+"><\/script\>/,
`<script type="module" crossorigin src="/${file}"></script>`
)
.replace(/<link rel="modulepreload" href="[^"]+">/, '')
fs.writeFileSync('./docs/dist/index.html', html, 'utf8')
},
}

1 comment on commit 1fc24e2

@vercel
Copy link

@vercel vercel bot commented on 1fc24e2 Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.