sourcegraph/client/web/dev/webpack/get-html-webpack-plugins.ts
Valery Bugakov e31288f052
Attempt #3: upgrade to Webpack v5 (#22580)
- web: upgrade `Webpack` to v5 for the web app
- web: upgrade `Webpack` to v5 for Storybook
- web: upgrade `Webpack` to v5 for the browser extension
- web: enable filesystem cache only in the development environment
- web: disable node.js polyfills
- web: disable `webpack.IgnorePlugin` for the web app
- web: switch to `react-refresh-webpack-plugin` from `react-hot-loader`
- web: use `cache` only in development mode
- web: remove redundant polyfill dependencies
- web: do not use `Webpack` cache on CI
- web: use runtime bundle in development mode by `frontend` server
2021-07-19 16:08:50 +08:00

56 lines
2.3 KiB
TypeScript

import path from 'path'
import HtmlWebpackHarddiskPlugin from 'html-webpack-harddisk-plugin'
import HtmlWebpackPlugin, { TemplateParameter, Options } from 'html-webpack-plugin'
import { WebpackPluginInstance } from 'webpack'
import { createJsContext, environmentConfig, STATIC_ASSETS_PATH } from '../utils'
const { SOURCEGRAPH_HTTPS_PORT, NODE_ENV } = environmentConfig
export const getHTMLWebpackPlugins = (): WebpackPluginInstance[] => {
const jsContext = createJsContext({ sourcegraphBaseUrl: `http://localhost:${SOURCEGRAPH_HTTPS_PORT}` })
/**
* To match `cmd/frontend/internal/app/ui/app.html` template used by our `frontend` server
* script tags are injected after the <body> tag.
*/
const templateContent = ({ htmlWebpackPlugin }: TemplateParameter): string => `
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sourcegraph</title>
${htmlWebpackPlugin.tags.headTags.filter(tag => tag.tagName !== 'script').toString()}
</head>
<body>
<div id="root"></div>
<script>
// Optional value useful for checking if index.html is created by HtmlWebpackPlugin with the right NODE_ENV.
window.webpackBuildEnvironment = '${NODE_ENV}'
// Required mock of the JS context object.
window.context = ${JSON.stringify(jsContext)}
</script>
${htmlWebpackPlugin.tags.headTags.filter(tag => tag.tagName === 'script').toString()}
</body>
</html>
`
const htmlWebpackPlugin = new HtmlWebpackPlugin({
// `TemplateParameter` can be mutated. We need to tell TS that we didn't touch it.
templateContent: templateContent as Options['templateContent'],
meta: {
charset: 'utf-8',
viewport: 'width=device-width, viewport-fit=cover, initial-scale=1',
referrer: 'origin-when-cross-origin',
'color-scheme': 'light dark',
},
filename: path.resolve(STATIC_ASSETS_PATH, 'index.html'),
alwaysWriteToDisk: true,
inject: false,
})
// Write index.html to the disk so it can be served by dev/prod servers.
return [htmlWebpackPlugin, new HtmlWebpackHarddiskPlugin()]
}