Skip to content

Commit

Permalink
fix: json resource handling (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Nov 21, 2021
1 parent 32fca8d commit 0320530
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
24 changes: 24 additions & 0 deletions packages/vite-plugin-vue-i18n/src/index.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
@@ -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;"`;

0 comments on commit 0320530

Please sign in to comment.