const path = require('path') const TerserPlugin = require('terser-webpack-plugin') const HtmlWebpackPlugin = require('html-webpack-plugin') const PrerenderSPAPlugin = require('prerender-spa-plugin') const Renderer = PrerenderSPAPlugin.PuppeteerRenderer const CopyPlugin = require('copy-webpack-plugin') const { InjectManifest } = require('workbox-webpack-plugin') const { JSDOM } = require('jsdom') const tempy = require('tempy') const fs = require('fs') const magicFile = (text) => { const p = tempy.file() fs.writeFileSync(p, text) return p } // const magicAppend = (orig, text, sep = '\n') => magicFile(fs.readFileSync(orig) + sep + text) const jsMin = { pure_funcs: ["F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"], pure_getters: true, keep_fargs: false, unsafe_comps: true, unsafe: true, compress: true, mangle: true, } module.exports = { module: { rules: [{ test: /\.elm$/, exclude: [/elm-stuff/, /node_modules/], loader: 'elm-webpack-loader' }], }, plugins: [ new TerserPlugin({ terserOptions: jsMin, }), new HtmlWebpackPlugin({ hash: true, meta: { viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no' } }), new CopyPlugin([{ from: 'data', to: 'data' }, { from: 'wand.js', to: 'wand.js' }]), new PrerenderSPAPlugin({ staticDir: path.join(__dirname, 'dist'), routes: ROUTES, minify: { minifyCSS: { compatibility: 'ie9', level: 2 }, minifyJS: jsMin, removeComments: true, collapseBooleanAttributes: true, collapseWhitespace: true, collapseInlineTagWhitespace: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, sortAttributes: true, sortClassName: true, }, renderer: new Renderer(), postProcess(renderedRoute) { const dom = new JSDOM(renderedRoute.html, { runScripts: 'outside-only' }) dom.window.eval(` const wandTarget = document.createElement('script') wandTarget.src = '/wand.js' document.body.appendChild(wandTarget) `) return { ...renderedRoute, html: dom.serialize(), } }, }), new InjectManifest({ importWorkboxFrom: 'local', swSrc: magicFile(` workbox.routing.registerNavigationRoute('/index.html'); workbox.precaching.precacheAndRoute(self.__precacheManifest); `), swDest: 'sw.js', }), ] }