rsnext/packages/next/build/webpack/plugins/next-drop-client-page-plugin.ts
Guy Bedford e85a517e1a ncc Webpack build redux (#7628)
* Reimplement ncc webpack build

This reverts commit 6feca310d7.

* Shared webpack build

* ncc workaround pending @zeit/ncc#437

* update ncc

* build tweaks, fixup autodll-import

* possible Node 8 fix

* second possible Node 8 fix

* and update taskfile
2019-06-21 12:28:41 -04:00

28 lines
895 B
TypeScript

import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
import { extname } from 'path'
// Prevents outputting client pages when they are not needed
export class DropClientPage implements Plugin {
ampPages = new Set()
apply(compiler: Compiler) {
compiler.hooks.emit.tap('DropClientPage', compilation => {
Object.keys(compilation.assets).forEach(assetKey => {
const asset = compilation.assets[assetKey]
if (
asset &&
asset._value &&
asset._value.includes('__NEXT_DROP_CLIENT_FILE__')
) {
const cleanAssetKey = assetKey.replace(/\\/g, '/')
const page = '/' + cleanAssetKey.split('pages/')[1]
const pageNoExt = page.split(extname(page))[0]
this.ampPages.add(pageNoExt.replace(/\/index$/, '') || '/')
delete compilation.assets[assetKey]
}
})
})
}
}