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

Commit 0720102

Browse files
committed
fix: load custom blocks by index
closes #355
1 parent 129a36b commit 0720102

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

Diff for: examples/custom-block/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "custom-block",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"build": "rollup -c"
7+
},
8+
"dependencies": {
9+
"rollup": "^2.10.9",
10+
"rollup-pluginutils": "^2.8.2",
11+
"rollup-plugin-vue": "link:../.."
12+
}
13+
}

Diff for: examples/custom-block/rollup.config.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import VuePlugin from 'rollup-plugin-vue'
2+
import { createFilter } from 'rollup-pluginutils'
3+
4+
export default [
5+
{
6+
input: 'src/App.vue',
7+
output: {
8+
file: 'dist/app.js',
9+
format: 'esm',
10+
sourcemap: 'inline',
11+
},
12+
plugins: [VuePlugin({ customBlocks: ['i18n'] }), VueI18N()],
13+
external: ['vue'],
14+
},
15+
]
16+
17+
function VueI18N() {
18+
const filter = createFilter([/vue&type=i18n/])
19+
20+
return {
21+
transform(code, id) {
22+
if (filter(id)) {
23+
return {
24+
code: `
25+
export default function i18n(component) {
26+
component.i18n = ${JSON.stringify(JSON.parse(code.trim()))}
27+
}`,
28+
map: null,
29+
}
30+
}
31+
},
32+
}
33+
}

Diff for: examples/custom-block/src/App.vue

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
export default {
3+
name: 'App',
4+
}
5+
</script>
6+
7+
<template>
8+
<h1>{{ $t('say.hello', { name: 'Rahul' }) }}</h1>
9+
</template>
10+
11+
<i18n lang="json">
12+
{
13+
"say": {
14+
"hello": "Hello :name"
15+
}
16+
}
17+
</i18n>

Diff for: src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
112112
? descriptor.script!
113113
: query.type === 'style'
114114
? descriptor.styles[query.index]
115-
: query.type === 'custom'
115+
: typeof query.index === 'number'
116116
? descriptor.customBlocks[query.index]
117117
: null
118118

Diff for: test/core.e2e.ts

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ describe('simple', () => {
1212
})
1313
})
1414

15+
describe('custom-block', () => {
16+
let result!: RollupOutput
17+
18+
beforeAll(async () => {
19+
result = await roll('custom-block')
20+
})
21+
22+
it('should compile <i18n>', () => {
23+
expect(result.output[0].code).toEqual(
24+
expect.stringContaining(
25+
'component.i18n = {"say":{"hello":"Hello :name"}}'
26+
)
27+
)
28+
})
29+
})
30+
1531
describe('css-modules', () => {
1632
let result!: RollupOutput
1733

0 commit comments

Comments
 (0)