diff --git a/packages/vite-plugin-vue-i18n/src/index.ts b/packages/vite-plugin-vue-i18n/src/index.ts index 1ea2041..0e5db25 100644 --- a/packages/vite-plugin-vue-i18n/src/index.ts +++ b/packages/vite-plugin-vue-i18n/src/index.ts @@ -78,6 +78,13 @@ function pluginI18n( return { name: 'vite-plugin-vue-i18n', + /** + * NOTE: + * If we have json (including SFC's custom block), + * transform it first because it will be transformed into javascript code by `vite:json` plugin. + */ + enforce: 'pre', + config(config: UserConfig, { command }) { if (command === 'build' && runtimeOnly) { normalizeConfigResolveAlias(config) @@ -142,6 +149,23 @@ function pluginI18n( if (!/\.json$/.test(id)) { return null } + + /** + * NOTE: + * `vite:json` plugin will be handled if the query generated from the result of parse SFC + * with `vite:vue` plugin contains json as follows. + * e.g src/components/HelloI18n.vue?vue&type=i18n&index=1&lang.json + * + * To avoid this, return the result that has already been processed (`enforce: 'pre'`) in the wrapped json plugin. + */ + const { query } = parseVueRequest(id) + if (query.vue) { + return Promise.resolve({ + code, + map: sourceMap ? this.getCombinedSourcemap() : { mappings: '' } + }) + } + if (filter(id)) { const map = this.getCombinedSourcemap() debug('override json plugin', code, map) diff --git a/packages/vite-plugin-vue-i18n/test/__snapshots__/sourcemap.test.ts.snap b/packages/vite-plugin-vue-i18n/test/__snapshots__/sourcemap.test.ts.snap index f7da430..60c3900 100644 --- a/packages/vite-plugin-vue-i18n/test/__snapshots__/sourcemap.test.ts.snap +++ b/packages/vite-plugin-vue-i18n/test/__snapshots__/sourcemap.test.ts.snap @@ -1,15 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`custom blocks json 1`] = `";;;;;;;;;;;;;;;;;;;;;;AAIE;SACO;;;"`; +exports[`custom blocks json 1`] = `";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAI,OAAO,WAAW,aAAa;SAC1B,SAASA;SACT,UAAU;AAAA;"`; -exports[`custom blocks json5 1`] = `";;;;;;;;;;;;;;;;;;;;;;AAIE;SACO;;;"`; +exports[`custom blocks json5 1`] = `";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAI,OAAO,WAAW,aAAa;SAC1B,SAASA;SACT,UAAU;AAAA;"`; -exports[`custom blocks yaml 1`] = `";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIE;SACO;;;"`; +exports[`custom blocks yaml 1`] = `";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAI,OAAO,WAAW,aAAa;SAC1B,SAASA;SACT,UAAU;AAAA;"`; -exports[`custom blocks yml 1`] = `";;;;;;;;;;;;;;;;;;;;AAIE;SACO;;;"`; +exports[`custom blocks yml 1`] = `";;;;;;;;;;;;;;;;;;;;AAGA,IAAI,OAAO,WAAW,aAAa;SAC1B,SAASA;SACT,UAAU;AAAA;"`; -exports[`resource files json 1`] = `";;;2EACYA,gDAAAC;;;;;wBAEAC;;;;;;;;;;;;;;;;;;;;;;ACCV;SACO;;;"`; +exports[`resource files json 1`] = `";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAI,OAAO,WAAW,aAAa;SAC1B,SAAS;SACT,UAAU;AAAA;"`; -exports[`resource files json5 1`] = `";;;;;;;;;;;AAIE;SACO;;;"`; +exports[`resource files json5 1`] = `";;;;;;;;;;;AAGA,IAAI,OAAO,WAAW,aAAa;SAC1B,SAAS;SACT,UAAU;AAAA;"`; -exports[`resource files yaml 1`] = `";;;;;;;;;;;AAIE;SACO;;;"`; +exports[`resource files yaml 1`] = `";;;;;;;;;;;AAGA,IAAI,OAAO,WAAW,aAAa;SAC1B,SAAS;SACT,UAAU;AAAA;"`;