Repository URL to install this package:
|
Version:
1.2.10 ▾
|
import log from 'fliplog'
import { BannerPlugin } from 'webpack'
import { Configuration, Options } from '../../typings'
const BANNER_CONFIG = {
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false,
}
const loader = {
test: /\.js$/,
use: [require.resolve('source-map-loader')],
enforce: 'pre',
}
/**
* @see https://webpack.js.org/plugins/source-map-dev-tool-plugin/
* @see https://webpack.js.org/plugins/banner-plugin/
* @see https://reactjs.org/docs/cross-origin-errors.html
* @see https://github.com/webpack-contrib/source-map-loader
*
* @name client
* @type {dev} inline or source-map-file for client
* @type {prod} hidden sourcemaps
*
* @name server
* @type {dev | prod | *} add source-map lib using BannerPlugin
* (so it's not part of webpack bundle & all chunks/output-files get it for node)
* @see https://github.com/mozilla/source-map
*
*/
export default function sourceMapMiddleware(
config: Configuration,
options: Options
) {
const { isServer, isDev } = options
const FORCE_SOURCE_MAPS = process.env.FORCE_SOURCE_MAPS
if (isServer) {
const plugin = new BannerPlugin(BANNER_CONFIG)
config.plugins.push(plugin)
} else {
// https://www.snip2code.com/Snippet/568096/hot-reload-webpackHotUpdate-is-not-defin
// https://github.com/gaearon/react-hot-loader/issues/834
// https://github.com/webpack/webpack/issues/5776
// const HMR_BANNER_CONFIG = {
// banner: `window.webpackHotUpdate = window.webpackHotUpdate || function() { console.log('HMR BUG') }`,
// raw: true,
// entryOnly: false,
// }
// const plugin = new BannerPlugin(HMR_BANNER_CONFIG)
// config.plugins.push(plugin)
}
// Include source maps for ANY node bundle so that we can support
// nice stack traces for errors (the source maps get consumed by
// the `node-source-map-support` module to allow for this).
//
// Always include source maps for any development build.
// Allow for the following flag to force source maps even for production
// builds.
//
// >>>>>>>>>>>>>>>>>>
// !!!!!!!! WAS ISNODE
// <<<<<<<<<<<<<<<<<<<<
if (isServer || FORCE_SOURCE_MAPS) {
// Produces an external source map (lives next to bundle output files).
// config.devtool = 'source-map'
config.devtool = 'cheap-module-source-map'
// config.devtool = 'inline-source-map'
} else {
// const IS_STAGING = ('STAGING')
if (isDev) {
// Produces source map,
// but no link to the source map is added to the source code
// which allows re-tracing errors in production behind the scenes without exposing sourcemap
// config.devtool = 'hidden-source-map'
// @NOTE enabled for EASY sourcemap in prod
// config.devtool = 'inline-source-map'
// https://reactjs.org/docs/cross-origin-errors.html
config.devtool = 'cheap-module-source-map'
} else if (process.env.OPTIMIZE) {
log.bold('IS_STAGING_EH').echo()
config.devtool = 'hidden'
// @note - can enable this when debugging uglified
// ^ debugging it now
console.warn('ENABLED_SOURCEMAPS')
}
// @todo ! - currently has issues with the worker loader...
// config.module.rules.unshift(loader)
if (process.env.FORCE_SOURCE_MAPS) {
config.devtool = 'cheap-module-source-map'
}
}
}