mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
feat: allow disabling sourcemap generation (#4509)
This commit is contained in:
parent
4c6c027014
commit
650d12fa9c
@ -74,8 +74,8 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
|
|||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// Transform to inject imports in production mode
|
// Transform to inject imports in production mode
|
||||||
addVitePlugin(TransformPlugin.vite({ ctx, options }))
|
addVitePlugin(TransformPlugin.vite({ ctx, options, sourcemap: nuxt.options.sourcemap }))
|
||||||
addWebpackPlugin(TransformPlugin.webpack({ ctx, options }))
|
addWebpackPlugin(TransformPlugin.webpack({ ctx, options, sourcemap: nuxt.options.sourcemap }))
|
||||||
}
|
}
|
||||||
|
|
||||||
const regenerateAutoImports = async () => {
|
const regenerateAutoImports = async () => {
|
||||||
|
@ -4,7 +4,7 @@ import { parseQuery, parseURL } from 'ufo'
|
|||||||
import { Unimport } from 'unimport'
|
import { Unimport } from 'unimport'
|
||||||
import { AutoImportsOptions } from '@nuxt/schema'
|
import { AutoImportsOptions } from '@nuxt/schema'
|
||||||
|
|
||||||
export const TransformPlugin = createUnplugin(({ ctx, options }: {ctx: Unimport, options: Partial<AutoImportsOptions> }) => {
|
export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx: Unimport, options: Partial<AutoImportsOptions>, sourcemap?: boolean }) => {
|
||||||
return {
|
return {
|
||||||
name: 'nuxt:auto-imports-transform',
|
name: 'nuxt:auto-imports-transform',
|
||||||
enforce: 'post',
|
enforce: 'post',
|
||||||
@ -39,7 +39,7 @@ export const TransformPlugin = createUnplugin(({ ctx, options }: {ctx: Unimport,
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
code,
|
code,
|
||||||
map: s.generateMap({ source: id, includeContent: true })
|
map: sourcemap && s.generateMap({ source: id, includeContent: true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import { pascalCase } from 'scule'
|
|||||||
interface LoaderOptions {
|
interface LoaderOptions {
|
||||||
getComponents(): Component[]
|
getComponents(): Component[]
|
||||||
mode: 'server' | 'client'
|
mode: 'server' | 'client'
|
||||||
|
sourcemap?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const loaderPlugin = createUnplugin((options: LoaderOptions) => ({
|
export const loaderPlugin = createUnplugin((options: LoaderOptions) => ({
|
||||||
@ -22,7 +23,46 @@ export const loaderPlugin = createUnplugin((options: LoaderOptions) => ({
|
|||||||
return pathname.endsWith('.vue') && (query.type === 'template' || !!query.macro || !search)
|
return pathname.endsWith('.vue') && (query.type === 'template' || !!query.macro || !search)
|
||||||
},
|
},
|
||||||
transform (code, id) {
|
transform (code, id) {
|
||||||
return transform(code, id, options.getComponents(), options.mode)
|
const components = options.getComponents()
|
||||||
|
|
||||||
|
let num = 0
|
||||||
|
const imports = new Set<string>()
|
||||||
|
const map = new Map<Component, string>()
|
||||||
|
const s = new MagicString(code)
|
||||||
|
|
||||||
|
// replace `_resolveComponent("...")` to direct import
|
||||||
|
s.replace(/(?<=[ (])_?resolveComponent\(["'](lazy-|Lazy)?([^'"]*?)["']\)/g, (full, lazy, name) => {
|
||||||
|
const component = findComponent(components, name, options.mode)
|
||||||
|
if (component) {
|
||||||
|
const identifier = map.get(component) || `__nuxt_component_${num++}`
|
||||||
|
map.set(component, identifier)
|
||||||
|
const isClientOnly = component.mode === 'client'
|
||||||
|
if (isClientOnly) {
|
||||||
|
imports.add(genImport('#app/components/client-only', [{ name: 'createClientOnly' }]))
|
||||||
|
}
|
||||||
|
if (lazy) {
|
||||||
|
imports.add(genImport('vue', [{ name: 'defineAsyncComponent', as: '__defineAsyncComponent' }]))
|
||||||
|
imports.add(`const ${identifier}_lazy = __defineAsyncComponent(${genDynamicImport(component.filePath)})`)
|
||||||
|
return isClientOnly ? `createClientOnly(${identifier}_lazy)` : `${identifier}_lazy`
|
||||||
|
} else {
|
||||||
|
imports.add(genImport(component.filePath, [{ name: component.export, as: identifier }]))
|
||||||
|
return isClientOnly ? `createClientOnly(${identifier})` : identifier
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// no matched
|
||||||
|
return full
|
||||||
|
})
|
||||||
|
|
||||||
|
if (imports.size) {
|
||||||
|
s.prepend([...imports, ''].join('\n'))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s.hasChanged()) {
|
||||||
|
return {
|
||||||
|
code: s.toString(),
|
||||||
|
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@ -34,44 +74,3 @@ function findComponent (components: Component[], name: string, mode: LoaderOptio
|
|||||||
}
|
}
|
||||||
return component
|
return component
|
||||||
}
|
}
|
||||||
|
|
||||||
function transform (code: string, id: string, components: Component[], mode: LoaderOptions['mode']) {
|
|
||||||
let num = 0
|
|
||||||
const imports = new Set<string>()
|
|
||||||
const map = new Map<Component, string>()
|
|
||||||
const s = new MagicString(code)
|
|
||||||
|
|
||||||
// replace `_resolveComponent("...")` to direct import
|
|
||||||
s.replace(/(?<=[ (])_?resolveComponent\(["'](lazy-|Lazy)?([^'"]*?)["']\)/g, (full, lazy, name) => {
|
|
||||||
const component = findComponent(components, name, mode)
|
|
||||||
if (component) {
|
|
||||||
const identifier = map.get(component) || `__nuxt_component_${num++}`
|
|
||||||
map.set(component, identifier)
|
|
||||||
const isClientOnly = component.mode === 'client'
|
|
||||||
if (isClientOnly) {
|
|
||||||
imports.add(genImport('#app/components/client-only', [{ name: 'createClientOnly' }]))
|
|
||||||
}
|
|
||||||
if (lazy) {
|
|
||||||
imports.add(genImport('vue', [{ name: 'defineAsyncComponent', as: '__defineAsyncComponent' }]))
|
|
||||||
imports.add(`const ${identifier}_lazy = __defineAsyncComponent(${genDynamicImport(component.filePath)})`)
|
|
||||||
return isClientOnly ? `createClientOnly(${identifier}_lazy)` : `${identifier}_lazy`
|
|
||||||
} else {
|
|
||||||
imports.add(genImport(component.filePath, [{ name: component.export, as: identifier }]))
|
|
||||||
return isClientOnly ? `createClientOnly(${identifier})` : identifier
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// no matched
|
|
||||||
return full
|
|
||||||
})
|
|
||||||
|
|
||||||
if (imports.size) {
|
|
||||||
s.prepend([...imports, ''].join('\n'))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s.hasChanged()) {
|
|
||||||
return {
|
|
||||||
code: s.toString(),
|
|
||||||
map: s.generateMap({ source: id, includeContent: true })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -132,6 +132,7 @@ export default defineNuxtModule<ComponentsOptions>({
|
|||||||
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
|
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
|
||||||
config.plugins = config.plugins || []
|
config.plugins = config.plugins || []
|
||||||
config.plugins.push(loaderPlugin.vite({
|
config.plugins.push(loaderPlugin.vite({
|
||||||
|
sourcemap: nuxt.options.sourcemap,
|
||||||
getComponents,
|
getComponents,
|
||||||
mode: isClient ? 'client' : 'server'
|
mode: isClient ? 'client' : 'server'
|
||||||
}))
|
}))
|
||||||
@ -140,6 +141,7 @@ export default defineNuxtModule<ComponentsOptions>({
|
|||||||
configs.forEach((config) => {
|
configs.forEach((config) => {
|
||||||
config.plugins = config.plugins || []
|
config.plugins = config.plugins || []
|
||||||
config.plugins.push(loaderPlugin.webpack({
|
config.plugins.push(loaderPlugin.webpack({
|
||||||
|
sourcemap: nuxt.options.sourcemap,
|
||||||
getComponents,
|
getComponents,
|
||||||
mode: config.name === 'client' ? 'client' : 'server'
|
mode: config.name === 'client' ? 'client' : 'server'
|
||||||
}))
|
}))
|
||||||
|
@ -55,6 +55,7 @@ export async function initNitro (nuxt: Nuxt) {
|
|||||||
.concat(nuxt.options._generate ? ['/', ...nuxt.options.generate.routes] : [])
|
.concat(nuxt.options._generate ? ['/', ...nuxt.options.generate.routes] : [])
|
||||||
.concat(nuxt.options.ssr === false ? ['/', '/200', '/404'] : [])
|
.concat(nuxt.options.ssr === false ? ['/', '/200', '/404'] : [])
|
||||||
},
|
},
|
||||||
|
sourcemap: nuxt.options.sourcemap,
|
||||||
externals: {
|
externals: {
|
||||||
inline: [
|
inline: [
|
||||||
...(nuxt.options.dev ? [] : ['vue', '@vue/', '@nuxt/', nuxt.options.buildDir]),
|
...(nuxt.options.dev ? [] : ['vue', '@vue/', '@nuxt/', nuxt.options.buildDir]),
|
||||||
|
@ -63,8 +63,8 @@ async function initNuxt (nuxt: Nuxt) {
|
|||||||
addWebpackPlugin(ImportProtectionPlugin.webpack(config))
|
addWebpackPlugin(ImportProtectionPlugin.webpack(config))
|
||||||
|
|
||||||
// Add unctx transform
|
// Add unctx transform
|
||||||
addVitePlugin(UnctxTransformPlugin(nuxt).vite())
|
addVitePlugin(UnctxTransformPlugin(nuxt).vite({ sourcemap: nuxt.options.sourcemap }))
|
||||||
addWebpackPlugin(UnctxTransformPlugin(nuxt).webpack())
|
addWebpackPlugin(UnctxTransformPlugin(nuxt).webpack({ sourcemap: nuxt.options.sourcemap }))
|
||||||
|
|
||||||
// Init user modules
|
// Init user modules
|
||||||
await nuxt.callHook('modules:before', { nuxt } as ModuleContainer)
|
await nuxt.callHook('modules:before', { nuxt } as ModuleContainer)
|
||||||
|
@ -12,7 +12,7 @@ export const UnctxTransformPlugin = (nuxt: Nuxt) => {
|
|||||||
nuxt.hook('app:resolve', (_app) => { app = _app })
|
nuxt.hook('app:resolve', (_app) => { app = _app })
|
||||||
nuxt.hook('pages:middleware:extend', (_middlewares) => { middleware = _middlewares })
|
nuxt.hook('pages:middleware:extend', (_middlewares) => { middleware = _middlewares })
|
||||||
|
|
||||||
return createUnplugin(() => ({
|
return createUnplugin((options: { sourcemap?: boolean } = {}) => ({
|
||||||
name: 'unctx:transfrom',
|
name: 'unctx:transfrom',
|
||||||
enforce: 'post',
|
enforce: 'post',
|
||||||
transformInclude (id) {
|
transformInclude (id) {
|
||||||
@ -23,7 +23,7 @@ export const UnctxTransformPlugin = (nuxt: Nuxt) => {
|
|||||||
if (result) {
|
if (result) {
|
||||||
return {
|
return {
|
||||||
code: result.code,
|
code: result.code,
|
||||||
map: result.magicString.generateMap({ source: id, includeContent: true })
|
map: options.sourcemap && result.magicString.generateMap({ source: id, includeContent: true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import MagicString from 'magic-string'
|
|||||||
export interface TransformMacroPluginOptions {
|
export interface TransformMacroPluginOptions {
|
||||||
macros: Record<string, string>
|
macros: Record<string, string>
|
||||||
dev?: boolean
|
dev?: boolean
|
||||||
|
sourcemap?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TransformMacroPlugin = createUnplugin((options: TransformMacroPluginOptions) => {
|
export const TransformMacroPlugin = createUnplugin((options: TransformMacroPluginOptions) => {
|
||||||
@ -24,7 +25,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
|
|||||||
|
|
||||||
function result () {
|
function result () {
|
||||||
if (s.hasChanged()) {
|
if (s.hasChanged()) {
|
||||||
return { code: s.toString(), map: s.generateMap({ source: id, includeContent: true }) }
|
return { code: s.toString(), map: options.sourcemap && s.generateMap({ source: id, includeContent: true }) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +57,7 @@ export default defineNuxtModule({
|
|||||||
// Extract macros from pages
|
// Extract macros from pages
|
||||||
const macroOptions: TransformMacroPluginOptions = {
|
const macroOptions: TransformMacroPluginOptions = {
|
||||||
dev: nuxt.options.dev,
|
dev: nuxt.options.dev,
|
||||||
|
sourcemap: nuxt.options.sourcemap,
|
||||||
macros: {
|
macros: {
|
||||||
definePageMeta: 'meta'
|
definePageMeta: 'meta'
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,12 @@ export default {
|
|||||||
return map[val] || (get('vite') === false ? map.webpack : map.vite)
|
return map[val] || (get('vite') === false ? map.webpack : map.vite)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Whether to generate sourcemaps.
|
||||||
|
*
|
||||||
|
* @version 3
|
||||||
|
*/
|
||||||
|
sourcemap: true,
|
||||||
/**
|
/**
|
||||||
* Shared build configuration.
|
* Shared build configuration.
|
||||||
* @version 2
|
* @version 2
|
||||||
@ -132,7 +138,7 @@ export default {
|
|||||||
* @version 2
|
* @version 2
|
||||||
*/
|
*/
|
||||||
cssSourceMap: {
|
cssSourceMap: {
|
||||||
$resolve: (val, get) => val ?? get('dev')
|
$resolve: (val, get) => val ?? get('sourcemap') ?? get('dev')
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -242,8 +248,8 @@ export default {
|
|||||||
]
|
]
|
||||||
for (const name of styleLoaders) {
|
for (const name of styleLoaders) {
|
||||||
const loader = val[name]
|
const loader = val[name]
|
||||||
if (loader && loader.sourceMap === undefined) {
|
if (loader && loader.sourcemap === undefined) {
|
||||||
loader.sourceMap = Boolean(get('build.cssSourceMap'))
|
loader.sourcemap = Boolean(get('build.cssSourceMap'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
@ -315,7 +321,7 @@ export default {
|
|||||||
*
|
*
|
||||||
* @see [terser-webpack-plugin documentation](https://github.com/webpack-contrib/terser-webpack-plugin)
|
* @see [terser-webpack-plugin documentation](https://github.com/webpack-contrib/terser-webpack-plugin)
|
||||||
*
|
*
|
||||||
* @note Enabling sourceMap will leave `//# sourceMappingURL` linking comment at
|
* @note Enabling sourcemap will leave `//# sourcemappingURL` linking comment at
|
||||||
* the end of each output file if webpack `config.devtool` is set to `source-map`.
|
* the end of each output file if webpack `config.devtool` is set to `source-map`.
|
||||||
* @version 2
|
* @version 2
|
||||||
*/
|
*/
|
||||||
@ -493,7 +499,7 @@ export default {
|
|||||||
return postcssOptions
|
return postcssOptions
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
sourceMap: undefined,
|
sourcemap: undefined,
|
||||||
implementation: undefined,
|
implementation: undefined,
|
||||||
order: ''
|
order: ''
|
||||||
},
|
},
|
||||||
|
@ -5,6 +5,7 @@ import MagicString from 'magic-string'
|
|||||||
|
|
||||||
interface DynamicBasePluginOptions {
|
interface DynamicBasePluginOptions {
|
||||||
globalPublicPath?: string
|
globalPublicPath?: string
|
||||||
|
sourcemap?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RelativeAssetPlugin = function (): Plugin {
|
export const RelativeAssetPlugin = function (): Plugin {
|
||||||
@ -95,7 +96,7 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl
|
|||||||
if (s.hasChanged()) {
|
if (s.hasChanged()) {
|
||||||
return {
|
return {
|
||||||
code: s.toString(),
|
code: s.toString(),
|
||||||
map: s.generateMap({ source: id, includeContent: true })
|
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ export async function bundle (nuxt: Nuxt) {
|
|||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
virtual(nuxt.vfs),
|
virtual(nuxt.vfs),
|
||||||
DynamicBasePlugin.vite()
|
DynamicBasePlugin.vite({ sourcemap: nuxt.options.sourcemap })
|
||||||
],
|
],
|
||||||
vue: {
|
vue: {
|
||||||
reactivityTransform: nuxt.options.experimental.reactivityTransform
|
reactivityTransform: nuxt.options.experimental.reactivityTransform
|
||||||
|
@ -34,6 +34,7 @@ export async function bundle (nuxt: Nuxt) {
|
|||||||
// Configure compilers
|
// Configure compilers
|
||||||
const compilers = webpackConfigs.map((config) => {
|
const compilers = webpackConfigs.map((config) => {
|
||||||
config.plugins.push(DynamicBasePlugin.webpack({
|
config.plugins.push(DynamicBasePlugin.webpack({
|
||||||
|
sourcemap: nuxt.options.sourcemap,
|
||||||
globalPublicPath: '__webpack_public_path__'
|
globalPublicPath: '__webpack_public_path__'
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user