rsnext/packages/next/build/webpack/plugins/next-drop-client-page-plugin.ts
Janicklas Ralph f4e6149d1c Experimental module/nomodule support (#7704)
* Module/nomodule implementation based on RFC 7563

* Remove comment

* Fixing issue with building amp pages

* Fixing test cases for serverless mode

* Adding safari 10 nomodule fix. Preloading modern js by default

* Fixing size-limit integration test

* Bug fix

* Adding testcase for modern build

* Trigger rebuild

* Setting default crossOrigin value

* Moving modern config option inside experimental flag

* Adding nomodule attribute to safari-fix script

* Changing safari10NomoduleFix default value to true

* Removing safari-fix flag

* Changing .es6 to .module

* Disable modern default

* Removing default crossOrigin value. Setting modern flag to false by default. Fixed test cases

* Remove confusing defaults and mark required instead

* Adjust blacklist

* Move behavior of page marking

* Fixing childCompiler errors not being captured

* Tweak names

* Revert

* whoops

* Fixing bug with page-loader.js

* Changing modern babel cache name

* Rename helper

* Iterate over both bundles

* Correctly clamp bundle sizes

* Revert test

* Add modern mode tests

* Fix test

* test

* test2
2019-07-24 22:16:32 -04:00

33 lines
1 KiB
TypeScript

import { Compiler, Plugin } from 'webpack'
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]
delete compilation.assets[assetKey]
// Detect being re-ran through a child compiler and don't re-mark the
// page as AMP
if (!pageNoExt.endsWith('.module')) {
this.ampPages.add(pageNoExt.replace(/\/index$/, '') || '/')
}
}
})
})
}
}