Unverified Commit dfe6d148 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

[Experimental] Add support for production browser source maps (#13018)

parent 9f8076be
...@@ -136,6 +136,8 @@ export default async function getBaseWebpackConfig( ...@@ -136,6 +136,8 @@ export default async function getBaseWebpackConfig(
entrypoints: WebpackEntrypoints entrypoints: WebpackEntrypoints
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
const productionBrowserSourceMaps =
config.experimental.productionBrowserSourceMaps && !isServer
let plugins: PluginMetaData[] = [] let plugins: PluginMetaData[] = []
let babelPresetPlugins: { dir: string; config: any }[] = [] let babelPresetPlugins: { dir: string; config: any }[] = []
...@@ -1032,6 +1034,7 @@ export default async function getBaseWebpackConfig( ...@@ -1032,6 +1034,7 @@ export default async function getBaseWebpackConfig(
isServer, isServer,
assetPrefix: config.assetPrefix || '', assetPrefix: config.assetPrefix || '',
sassOptions: config.sassOptions, sassOptions: config.sassOptions,
productionBrowserSourceMaps,
}) })
if (typeof config.webpack === 'function') { if (typeof config.webpack === 'function') {
......
...@@ -25,7 +25,12 @@ export const base = curry(function base( ...@@ -25,7 +25,12 @@ export const base = curry(function base(
config.devtool = 'eval-source-map' config.devtool = 'eval-source-map'
} }
} else { } else {
config.devtool = false // Enable browser sourcemaps
if (ctx.productionBrowserSourceMaps) {
config.devtool = 'source-map'
} else {
config.devtool = false
}
} }
if (!config.module) { if (!config.module) {
......
...@@ -12,6 +12,7 @@ export async function build( ...@@ -12,6 +12,7 @@ export async function build(
isServer, isServer,
assetPrefix, assetPrefix,
sassOptions, sassOptions,
productionBrowserSourceMaps,
}: { }: {
rootDirectory: string rootDirectory: string
customAppFile: string | null customAppFile: string | null
...@@ -19,6 +20,7 @@ export async function build( ...@@ -19,6 +20,7 @@ export async function build(
isServer: boolean isServer: boolean
assetPrefix: string assetPrefix: string
sassOptions: any sassOptions: any
productionBrowserSourceMaps: boolean
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
const ctx: ConfigurationContext = { const ctx: ConfigurationContext = {
...@@ -34,6 +36,7 @@ export async function build( ...@@ -34,6 +36,7 @@ export async function build(
: assetPrefix : assetPrefix
: '', : '',
sassOptions, sassOptions,
productionBrowserSourceMaps,
} }
const fn = pipe(base(ctx), css(ctx)) const fn = pipe(base(ctx), css(ctx))
......
...@@ -13,6 +13,7 @@ export type ConfigurationContext = { ...@@ -13,6 +13,7 @@ export type ConfigurationContext = {
assetPrefix: string assetPrefix: string
sassOptions: any sassOptions: any
productionBrowserSourceMaps: boolean
} }
export type ConfigurationFn = ( export type ConfigurationFn = (
......
...@@ -52,6 +52,7 @@ const defaultConfig: { [key: string]: any } = { ...@@ -52,6 +52,7 @@ const defaultConfig: { [key: string]: any } = {
workerThreads: false, workerThreads: false,
basePath: '', basePath: '',
pageEnv: false, pageEnv: false,
productionBrowserSourceMaps: false,
}, },
future: { future: {
excludeDefaultMomentLocales: false, excludeDefaultMomentLocales: false,
......
module.exports = {
experimental: {
productionBrowserSourceMaps: true,
},
}
export default function SSRPage({ data }) {
return <div>{data.foo}</div>
}
export async function getServerSideProps() {
return {
props: {
data: {
foo: 'bar',
},
},
}
}
export default function StaticPage({ data }) {
return <div>{data.foo}</div>
}
export async function getStaticProps() {
return {
props: {
data: {
foo: 'bar',
},
},
}
}
// This is not the correct way to implement the Next.js serverless target for production traffic
// It is only used for testing cases of rendering specific pages in the integration test suite
const path = require('path')
const http = require('http')
const send = require('send')
const server = http.createServer((req, res) => {
if (req.url === '/ssr') {
return require('./.next/serverless/pages/ssr.js').render(req, res)
}
if (req.url === '/getinitialprops') {
return require('./.next/serverless/pages/getinitialprops.js').render(
req,
res
)
}
if (req.url === '/api/api-route') {
return require('./.next/serverless/pages/api/api-route.js').default(
req,
res
)
}
if (req.url === '/user/a') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/a.html')
).pipe(res)
}
if (req.url === '/user/b') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/b.html')
).pipe(res)
}
if (req.url === '/static') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/static.html')
).pipe(res)
}
if (req.url.startsWith('/_next')) {
send(
req,
path.join(__dirname, '.next', req.url.split('/_next').pop())
).pipe(res)
}
})
server.listen(process.env.PORT, () => {
console.log('ready on', process.env.PORT)
})
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let nextConfigContent
function runTests() {
it('includes sourcemaps for all browser files', async () => {
const browserFiles = await recursiveReadDir(
join(appDir, '.next', 'static'),
/.*/
)
const jsFiles = browserFiles.filter(
file => file.endsWith('.js') && file.includes('/pages/')
)
jsFiles.forEach(file => {
expect(browserFiles.includes(`${file}.map`)).toBe(true)
})
})
}
describe('Production browser sourcemaps', () => {
describe('Server support', () => {
beforeAll(async () => {
await nextBuild(appDir, [], {})
})
runTests()
})
describe('Serverless support', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
module.exports = {
target: 'serverless',
experimental: {
productionBrowserSourceMaps: true
}
}
`
)
await nextBuild(appDir, [], {})
})
afterAll(async () => {
await fs.writeFile(nextConfig, nextConfigContent)
})
runTests()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment