Skip to content

Commit

Permalink
fix: load vue-template-compiler with nuxt.resolver.requireModule (#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Oct 24, 2021
1 parent 9c2b290 commit c5a3ca8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
11 changes: 6 additions & 5 deletions src/core/hooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { STRATEGIES } from '../helpers/constants'
import { formatMessage } from '../templates/utils-common'
import { makeRoutes } from '../helpers/routes'

/**
* @this {import('@nuxt/types/config/module').ModuleThis}
Expand All @@ -19,16 +20,16 @@ export function createExtendRoutesHook (options) {
const { trailingSlash } = nuxtOptions.router

return routes => {
// This import (or more specifically 'vue-template-compiler' in helpers/components.js) needs to
// be required only at build time to avoid problems when 'vue-template-compiler' dependency is
// not available (at runtime, when using nuxt-start).
const { makeRoutes } = require('../helpers/routes')
// Load 'vue-template-compiler' module with Resolver dynamically
// See https://github.com/nuxt-community/i18n-module/issues/297#issuecomment-491755323
const { parseComponent } = this.nuxt.resolver.requireModule('vue-template-compiler')

const localizedRoutes = makeRoutes(routes, {
...options,
pagesDir,
includeUprefixedFallback,
trailingSlash
trailingSlash,
parseComponent
})
routes.splice(0, routes.length)
routes.unshift(...localizedRoutes)
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/components.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { readFileSync } from 'fs'
import { parse } from '@babel/parser'
import traverse from '@babel/traverse'
// Must not be an explicit dependency to avoid version mismatch issue.
// See https://github.com/nuxt-community/i18n-module/issues/297
import { parseComponent } from 'vue-template-compiler'
import { formatMessage } from '../templates/utils-common'
import { COMPONENT_OPTIONS_KEY } from './constants'

Expand All @@ -13,9 +10,10 @@ import { COMPONENT_OPTIONS_KEY } from './constants'
* @typedef {Required<Pick<import('../../types/vue').NuxtI18nComponentOptions, 'locales' | 'paths'>>} ComputedPageOptions
*
* @param {import('@nuxt/types/config/router').NuxtRouteConfig['component']} component
* @param {import('vue-template-compiler')['parseComponent']} parseComponent
* @return {ComputedPageOptions | false}
*/
export function extractComponentOptions (component) {
export function extractComponentOptions (component, parseComponent) {
if (typeof (component) !== 'string') {
return false
}
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { adjustRouteDefinitionForTrailingSlash, getPageOptions } from './utils'
* pagesDir: string
* includeUprefixedFallback: boolean
* trailingSlash: import('@nuxt/types/config/router').NuxtOptionsRouter['trailingSlash']
* parseComponent: import('vue-template-compiler')['parseComponent']
* }} MakeRouteOptions
*
* @param {NuxtRouteConfig[]} baseRoutes
Expand All @@ -26,7 +27,8 @@ export function makeRoutes (baseRoutes, {
routesNameSeparator,
sortRoutes,
strategy,
trailingSlash
trailingSlash,
parseComponent
}) {
/** @type {NuxtRouteConfig[]} */
let localizedRoutes = []
Expand All @@ -48,7 +50,7 @@ export function makeRoutes (baseRoutes, {
}

const pageOptions = parsePages
? extractComponentOptions(route.component)
? extractComponentOptions(route.component, parseComponent)
: getPageOptions(route, pages, allowedLocaleCodes, pagesDir, defaultLocale)

// Skip route if i18n is disabled on page
Expand Down
5 changes: 3 additions & 2 deletions test/unit.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from 'path'
import { parseComponent } from 'vue-template-compiler'
import { matchBrowserLocale, parseAcceptLanguage } from '../src/templates/utils-common'

describe('parsePages', () => {
test('parses in-component options', async () => {
const { extractComponentOptions } = await import('../src/helpers/components')
const options = extractComponentOptions(path.join(__dirname, './fixture/typescript/pages/index.vue'))
const options = extractComponentOptions(path.join(__dirname, './fixture/typescript/pages/index.vue'), parseComponent)
expect(options).toHaveProperty('paths')
if (options) {
expect(options.paths).toHaveProperty('pl')
Expand All @@ -16,7 +17,7 @@ describe('parsePages', () => {
const { extractComponentOptions } = await import('../src/helpers/components')

const spy = jest.spyOn(console, 'warn').mockImplementation(() => { })
const options = extractComponentOptions(path.join(__dirname, './fixture/typescript/pages/invalidOptions.vue'))
const options = extractComponentOptions(path.join(__dirname, './fixture/typescript/pages/invalidOptions.vue'), parseComponent)
expect(spy.mock.calls[0][0]).toContain('Error parsing')
spy.mockRestore()

Expand Down

0 comments on commit c5a3ca8

Please sign in to comment.