Skip to content

Commit

Permalink
fix: sourcemap warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Jul 5, 2023
1 parent ed975fb commit 06e6d19
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 19 deletions.
2 changes: 0 additions & 2 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ export async function extendBundler(
}

const dynamicOptions: ResourceDynamicPluginOptions = {
ssr: nuxt.options.ssr,
dev: nuxt.options.dev,
sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client
}

Expand Down
13 changes: 2 additions & 11 deletions src/transform/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import MagicString from 'magic-string'
import { VIRTUAL_PREFIX_HEX } from './utils'
import { NUXT_I18N_COMPOSABLE_DEFINE_LOCALE, NUXT_I18N_COMPOSABLE_DEFINE_CONFIG } from '../constants'

// TODO: remove
export interface ResourceDynamicPluginOptions {
ssr: boolean
dev?: boolean
sourcemap?: boolean
}

Expand All @@ -18,8 +15,6 @@ const debug = createDebug('@nuxtjs/i18n:transform:dynamic')
export const ResourceDynamicPlugin = createUnplugin((options: ResourceDynamicPluginOptions) => {
debug('options', options)

// const relativeToSrcDir = (path: string) => relative(options.srcDir, path)

return {
name: 'nuxtjs:i18n-resource-dynamic',
enforce: 'post',
Expand All @@ -33,26 +28,22 @@ export const ResourceDynamicPlugin = createUnplugin((options: ResourceDynamicPlu

const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
const query = parseQuery(search)
return /\.([c|m]?[j|t]s)$/.test(pathname) && !!query.hash && (!!query.locale || !!query.config)
return /\.([c|m]?[j|t]s)$/.test(pathname) && (!!query.locale || !!query.config)
},

transform(code, id) {
debug('transform', id)

const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
const query = parseQuery(search)
const hash = query.hash as string

const s = new MagicString(code)

function result() {
if (s.hasChanged()) {
return {
code: s.toString(),
map:
options.sourcemap && !/\.([c|m]?ts)$/.test(pathname)
? s.generateMap({ source: id, includeContent: true })
: undefined
map: options.sourcemap && !/\.([c|m]?ts)$/.test(pathname) ? s.generateMap({ hires: true }) : null
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/transform/macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
debug('transformed: code -> ', s.toString())
return {
code: s.toString(),
map: options.sourcemap ? s.generateMap({ source: id, includeContent: true }) : undefined
map: options.sourcemap ? s.generateMap({ hires: true }) : undefined
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/transform/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export const ResourceProxyPlugin = createUnplugin((options: ResourceProxyPluginO
// prettier-ignore
const code = `export default async function(locale) {
__DEBUG__ && console.log('loadResource', locale)
const loader = await import(${toCode(withQuery(resolve(baseDir, query.target), { hash: query.hash, locale: query.locale }))}).then(m => m.default || m)
const loader = await import(${toCode(withQuery(resolve(baseDir, query.target), { locale: query.locale }))}).then(m => m.default || m)
const message = await loader(locale)
__DEBUG__ && console.log('loaded on loadResource', message)
return message
}`
const s = new MagicString(code)
return {
code: s.toString(),
map: options.sourcemap ? s.generateMap({ source: id, includeContent: true }) : undefined
map: options.sourcemap ? s.generateMap({ hires: true }) : undefined
}
}
} else if (pathname === NUXT_I18N_CONFIG_PROXY_ID) {
Expand All @@ -86,7 +86,7 @@ export const ResourceProxyPlugin = createUnplugin((options: ResourceProxyPluginO
// prettier-ignore
const code = `import { isObject, isFunction } from '@intlify/shared'
export default async function() {
const loader = await import(${toCode(withQuery(resolve(baseDir, query.target), { hash: query.hash, config: 'true' }))}).then(m => m.default || m)
const loader = await import(${toCode(withQuery(resolve(baseDir, query.target), { config: 'true' }))}).then(m => m.default || m)
const config = isFunction(loader)
? await loader()
: isObject(loader)
Expand All @@ -98,7 +98,7 @@ export default async function() {
const s = new MagicString(code)
return {
code: s.toString(),
map: options.sourcemap ? s.generateMap({ source: id, includeContent: true }) : undefined
map: options.sourcemap ? s.generateMap({ hires: true }) : undefined
}
}
}
Expand All @@ -125,7 +125,7 @@ export default async function() {

return {
code: s.toString(),
map: options.sourcemap ? s.generateMap({ source: id, includeContent: true }) : undefined
map: options.sourcemap ? s.generateMap({ hires: true }) : undefined
}
}
}
Expand Down

0 comments on commit 06e6d19

Please sign in to comment.