Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
fix: load custom blocks by index
Browse files Browse the repository at this point in the history
closes #355
  • Loading branch information
znck committed May 28, 2020
1 parent 129a36b commit 0720102
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
13 changes: 13 additions & 0 deletions examples/custom-block/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "custom-block",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "rollup -c"
},
"dependencies": {
"rollup": "^2.10.9",
"rollup-pluginutils": "^2.8.2",
"rollup-plugin-vue": "link:../.."
}
}
33 changes: 33 additions & 0 deletions examples/custom-block/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import VuePlugin from 'rollup-plugin-vue'
import { createFilter } from 'rollup-pluginutils'

export default [
{
input: 'src/App.vue',
output: {
file: 'dist/app.js',
format: 'esm',
sourcemap: 'inline',
},
plugins: [VuePlugin({ customBlocks: ['i18n'] }), VueI18N()],
external: ['vue'],
},
]

function VueI18N() {
const filter = createFilter([/vue&type=i18n/])

return {
transform(code, id) {
if (filter(id)) {
return {
code: `
export default function i18n(component) {
component.i18n = ${JSON.stringify(JSON.parse(code.trim()))}
}`,
map: null,
}
}
},
}
}
17 changes: 17 additions & 0 deletions examples/custom-block/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
export default {
name: 'App',
}
</script>

<template>
<h1>{{ $t('say.hello', { name: 'Rahul' }) }}</h1>
</template>

<i18n lang="json">
{
"say": {
"hello": "Hello :name"
}
}
</i18n>
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
? descriptor.script!
: query.type === 'style'
? descriptor.styles[query.index]
: query.type === 'custom'
: typeof query.index === 'number'
? descriptor.customBlocks[query.index]
: null

Expand Down
16 changes: 16 additions & 0 deletions test/core.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ describe('simple', () => {
})
})

describe('custom-block', () => {
let result!: RollupOutput

beforeAll(async () => {
result = await roll('custom-block')
})

it('should compile <i18n>', () => {
expect(result.output[0].code).toEqual(
expect.stringContaining(
'component.i18n = {"say":{"hello":"Hello :name"}}'
)
)
})
})

describe('css-modules', () => {
let result!: RollupOutput

Expand Down

0 comments on commit 0720102

Please sign in to comment.