Skip to content

Commit

Permalink
feat: support JIT compilation opt-outing (#2250)
Browse files Browse the repository at this point in the history
* feat: support JIT compilation opt-outing

* fix

* updates

* update snapshot
  • Loading branch information
kazupon committed Jul 20, 2023
1 parent c6ffffc commit 0411b2c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 12 deletions.
12 changes: 11 additions & 1 deletion docs/content/3.options/6.misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ Supported properties:
## `compilation`

- type: `object`
- default: `{ strictMessage: true, escapeHtml: false }`
- default: `{ jit: true, strictMessage: true, escapeHtml: false }`

Configure flags that sets the behavior compilation of locale messages.

Supported properties:

- `jit` (default: `true`) Whether to use the JIT compilation of Vue I18n message compiler.

::alert{type="warning"}

Mean that necessary to pre-compile locale messages that are not managed by the nuxt i18n module (e.g. in the case of importing from a specific URL, you will need to precompile them yourself.)

And also, you need to understand that you cannot support use cases where you dynamically compose locale messages from the back-end via an API.

::

- `strictMessage` (default: `true`) Strictly check that the locale message does not contain HTML tags. If HTML tags are included, an error is thrown.
::alert{type="warning"}

Expand Down
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default defineNuxtConfig({
jsTsFormatResource: true
},
compilation: {
// jit: false,
strictMessage: false,
escapeHtml: true
},
Expand Down
12 changes: 7 additions & 5 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function extendBundler(
const webpackPluginOptions: PluginOptions = {
runtimeOnly: true,
allowDynamic: true,
jitCompilation: true,
jitCompilation: nuxtOptions.compilation.jit,
strictMessage: nuxtOptions.compilation.strictMessage,
escapeHtml: nuxtOptions.compilation.escapeHtml
}
Expand All @@ -87,7 +87,9 @@ export async function extendBundler(
extendWebpackConfig(config => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- `config.plugins` is safe, so it's assigned with nuxt!
config.plugins!.push(
new webpack.DefinePlugin(assign(getFeatureFlags(), { __DEBUG__: String(nuxtOptions.debug) }))
new webpack.DefinePlugin(
assign(getFeatureFlags(nuxtOptions.compilation.jit), { __DEBUG__: String(nuxtOptions.debug) })
)
)
})
} catch (e: unknown) {
Expand All @@ -101,7 +103,7 @@ export async function extendBundler(
const vitePluginOptions: PluginOptions = {
runtimeOnly: true,
allowDynamic: true,
jitCompilation: true,
jitCompilation: nuxtOptions.compilation.jit,
strictMessage: nuxtOptions.compilation.strictMessage,
escapeHtml: nuxtOptions.compilation.escapeHtml
}
Expand All @@ -126,11 +128,11 @@ export async function extendBundler(
})
}

export function getFeatureFlags() {
export function getFeatureFlags(jit = true) {
return {
__VUE_I18N_FULL_INSTALL__: 'true',
__VUE_I18N_LEGACY_API__: 'true',
__INTLIFY_PROD_DEVTOOLS__: 'false',
__INTLIFY_JIT_COMPILATION__: 'true'
__INTLIFY_JIT_COMPILATION__: String(jit)
}
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const DEFAULT_OPTIONS = {
jsTsFormatResource: false
},
compilation: {
jit: true,
strictMessage: true,
escapeHtml: false
},
Expand Down
10 changes: 9 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ export default defineNuxtModule<NuxtI18nOptions>({
logger.warn('JS / TS extension format is experimental')
}

if (!options.compilation.jit) {
logger.warn(
'Opt-out JIT compilation. ' +
`It's necessary to pre-compile locale messages that are not managed by the nuxt i18n module (e.g. in the case of importing from a specific URL, you will need to precompile them yourself.) ` +
`And also, you need to understand that you cannot support use cases where you dynamically compose locale messages from the back-end via an API.`
)
}

/**
* Check vertions
*/
Expand Down Expand Up @@ -289,7 +297,7 @@ export default defineNuxtModule<NuxtI18nOptions>({
* setup nitro
*/

await setupNitro(nuxt)
await setupNitro(nuxt, options)

/**
* auto imports
Expand Down
5 changes: 3 additions & 2 deletions src/nitro.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { assign } from '@intlify/shared'
import { getFeatureFlags } from './bundler'

import type { NuxtI18nOptions } from './types'
import type { Nuxt } from '@nuxt/schema'

export async function setupNitro(nuxt: Nuxt) {
export async function setupNitro(nuxt: Nuxt, nuxtOptions: Required<NuxtI18nOptions>) {
if (nuxt.options.ssr) {
if (!nuxt.options.nitro) {
nuxt.options.nitro = {}
}
const nitroConfig = nuxt.options.nitro

// vue-i18n feature flags configuration for server-side (server api, server middleware, etc...)
nitroConfig.replace = assign(nitroConfig.replace || {}, getFeatureFlags())
nitroConfig.replace = assign(nitroConfig.replace || {}, getFeatureFlags(nuxtOptions.compilation.jit))
}
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface ExperimentalFeatures {
}

export interface LocaleMessageCompilationOptions {
jit?: boolean
strictMessage?: boolean
escapeHtml?: boolean
}
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/gen.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const resolveNuxtI18nOptions = async (context) => {
return nuxtI18nOptions
}
export const nuxtI18nOptionsDefault = Object({experimental: Object({\\"jsTsFormatResource\\":false}),compilation: Object({\\"strictMessage\\":true,\\"escapeHtml\\":false}),vueI18n: \\"\\",locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: \\"composition\\",debug: false})
export const nuxtI18nOptionsDefault = Object({experimental: Object({\\"jsTsFormatResource\\":false}),compilation: Object({\\"jit\\":true,\\"strictMessage\\":true,\\"escapeHtml\\":false}),vueI18n: \\"\\",locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: \\"composition\\",debug: false})
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
Expand Down Expand Up @@ -117,7 +117,7 @@ export const resolveNuxtI18nOptions = async (context) => {
return nuxtI18nOptions
}
export const nuxtI18nOptionsDefault = Object({experimental: Object({\\"jsTsFormatResource\\":false}),compilation: Object({\\"strictMessage\\":true,\\"escapeHtml\\":false}),vueI18n: \\"\\",locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: \\"composition\\",debug: false})
export const nuxtI18nOptionsDefault = Object({experimental: Object({\\"jsTsFormatResource\\":false}),compilation: Object({\\"jit\\":true,\\"strictMessage\\":true,\\"escapeHtml\\":false}),vueI18n: \\"\\",locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: \\"composition\\",debug: false})
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
Expand All @@ -143,7 +143,7 @@ export const resolveNuxtI18nOptions = async (context) => {
return nuxtI18nOptions
}
export const nuxtI18nOptionsDefault = Object({experimental: Object({\\"jsTsFormatResource\\":false}),compilation: Object({\\"strictMessage\\":true,\\"escapeHtml\\":false}),vueI18n: \\"\\",locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: \\"composition\\",debug: false})
export const nuxtI18nOptionsDefault = Object({experimental: Object({\\"jsTsFormatResource\\":false}),compilation: Object({\\"jit\\":true,\\"strictMessage\\":true,\\"escapeHtml\\":false}),vueI18n: \\"\\",locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: \\"composition\\",debug: false})
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
Expand Down

0 comments on commit 0411b2c

Please sign in to comment.