Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json resource handling #71

Merged
merged 1 commit into from Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;"`;