Skip to content

Commit

Permalink
fix: add plugins from the main context to have consistent loading order
Browse files Browse the repository at this point in the history
Resolves #874
  • Loading branch information
rchl committed Sep 11, 2020
1 parent 7518bd2 commit bce6bbb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
46 changes: 1 addition & 45 deletions src/core/hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { resolve, join } from 'path'
import { readdirSync } from 'fs'
import { getLocaleCodes } from '../helpers/utils'
import { MODULE_NAME, ROOT_DIR, LOCALE_CODE_KEY, LOCALE_ISO_KEY, LOCALE_DOMAIN_KEY, LOCALE_FILE_KEY, STRATEGIES, COMPONENT_OPTIONS_KEY } from '../helpers/constants'
import { MODULE_NAME, STRATEGIES } from '../helpers/constants'

export function createExtendRoutesHook (moduleContainer, options) {
const nuxtOptions = moduleContainer.options
Expand Down Expand Up @@ -37,30 +34,12 @@ export function buildHook (moduleContainer, options) {
// Transpile deepcopy dependency that is not sufficiently transpiled for IE11.
nuxtOptions.build.transpile.push(({ isServer, isModern }) => isServer || isModern ? false : 'deepcopy')

// Copy lang files to the build directory.
if (options.langDir) {
if (!options.locales.length || typeof options.locales[0] === 'string') {
console.error('[' + MODULE_NAME + '] When using "langDir" option, the "locales" option must be a list of objects')
}
}

const localeCodes = getLocaleCodes(options.locales)
const { trailingSlash } = nuxtOptions.router

const templatesOptions = {
...options,
IS_UNIVERSAL_MODE: nuxtOptions.mode === 'universal',
MODULE_NAME,
LOCALE_CODE_KEY,
LOCALE_ISO_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY,
localeCodes,
trailingSlash
}

if (options.strategy === STRATEGIES.NO_PREFIX && options.differentDomains) {
// eslint-disable-next-line no-console
console.warn('[' + MODULE_NAME + '] The `differentDomains` option and `no_prefix` strategy are not compatible. Change strategy or disable `differentDomains` option.')
Expand All @@ -71,29 +50,6 @@ export function buildHook (moduleContainer, options) {
console.warn('[' + MODULE_NAME + '] The `forwardedHost` option is deprecated. You can safely remove it. See: https://github.com/nuxt-community/i18n-module/pull/630.')
}

const templatesPath = join(__dirname, '..', '/templates')

// Templates (including plugins)
for (const file of readdirSync(templatesPath)) {
if (file.startsWith('plugin.')) {
if (file === 'plugin.seo.js' && !options.seo) {
continue
}

moduleContainer.addPlugin({
src: resolve(templatesPath, file),
fileName: join(ROOT_DIR, file),
options: templatesOptions
})
} else {
moduleContainer.addTemplate({
src: resolve(templatesPath, file),
fileName: join(ROOT_DIR, file),
options: templatesOptions
})
}
}

// Add vue-i18n-loader if applicable
if (options.vueI18nLoader) {
moduleContainer.extendBuild(config => {
Expand Down
45 changes: 44 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { resolve, join } from 'path'
import { readdirSync } from 'fs'
import { directive as i18nExtensionsDirective } from '@intlify/vue-i18n-extensions'
import { MODULE_NAME, DEFAULT_OPTIONS, NESTED_OPTIONS, STRATEGIES } from './helpers/constants'
import { MODULE_NAME, COMPONENT_OPTIONS_KEY, DEFAULT_OPTIONS, LOCALE_CODE_KEY, LOCALE_ISO_KEY, LOCALE_DOMAIN_KEY, LOCALE_FILE_KEY, NESTED_OPTIONS, ROOT_DIR, STRATEGIES } from './helpers/constants'
import { getLocaleCodes } from './helpers/utils'
import { buildHook, createExtendRoutesHook } from './core/hooks'

Expand All @@ -19,7 +21,48 @@ export default function (userOptions) {
return
}

// Templates (including plugins).
// This is done here rather than in the build hook to ensure the order the plugins are added
// is predictable between different modules.
const localeCodes = getLocaleCodes(options.locales)
const nuxtOptions = this.options
const { trailingSlash } = nuxtOptions.router

const templatesOptions = {
...options,
IS_UNIVERSAL_MODE: nuxtOptions.mode === 'universal',
MODULE_NAME,
LOCALE_CODE_KEY,
LOCALE_ISO_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY,
localeCodes,
trailingSlash
}

const templatesPath = join(__dirname, '/templates')
for (const file of readdirSync(templatesPath)) {
if (file.startsWith('plugin.')) {
if (file === 'plugin.seo.js' && !options.seo) {
continue
}

this.addPlugin({
src: resolve(templatesPath, file),
fileName: join(ROOT_DIR, file),
options: templatesOptions
})
} else {
this.addTemplate({
src: resolve(templatesPath, file),
fileName: join(ROOT_DIR, file),
options: templatesOptions
})
}
}

if (options.strategy !== STRATEGIES.NO_PREFIX && localeCodes.length) {
this.extendRoutes(createExtendRoutesHook(this, options))
}
Expand Down

0 comments on commit bce6bbb

Please sign in to comment.