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

Commit c82e82d

Browse files
committed
Fix parsing vue components
1 parent 3a1ef1a commit c82e82d

File tree

3 files changed

+150
-23
lines changed

3 files changed

+150
-23
lines changed

__tests__/__snapshots__/external.spec.ts.snap

+76-10
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ function __vue2_injectStyles (context) {
2626
}
2727
}
2828
29-
__component__.options.fluent = __component__.options.fluent || {};
30-
__component__.options.fluent['en'] = en_ftl
31-
__component__.options.fluent['da'] = da_ftl
3229
__component__.options.__file = \\"fixtures/components/external.vue\\"
3330
/* hot reload */
3431
import __VUE_HMR_RUNTIME__ from \\"/@id/__x00__/vite/vueHotReload\\"
@@ -45,6 +42,41 @@ if(!import.meta.env.SSR && __VUE_HMR_RUNTIME__.compatible){
4542
__VUE_HMR_RUNTIME__.rerender(\\"4c0553ad\\", update)
4643
})
4744
}
45+
__component__.fluent = __component__.fluent || {};
46+
__component__.fluent['en'] = en_ftl
47+
__component__.fluent['da'] = da_ftl
48+
export default /*#__PURE__*/(function () { return __component__.exports })()"
49+
`;
50+
51+
exports[`external ftl file support > works with vue 2 in production 1`] = `
52+
"import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
53+
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
54+
const __vue2_script = {}
55+
import { render as __vue2_render, staticRenderFns as __vue2_staticRenderFns } from \\"/fixtures/components/external.vue?vue&type=template&lang.js\\"
56+
const __cssModules = {}
57+
58+
/* normalize component */
59+
import __vue2_normalizer from \\"/@id/__x00__/vite/vueComponentNormalizer\\"
60+
var __component__ = /*#__PURE__*/__vue2_normalizer(
61+
__vue2_script,
62+
__vue2_render,
63+
__vue2_staticRenderFns,
64+
false,
65+
__vue2_injectStyles,
66+
null,
67+
null,
68+
null
69+
)
70+
71+
function __vue2_injectStyles (context) {
72+
for(let o in __cssModules){
73+
this[o] = __cssModules[o]
74+
}
75+
}
76+
77+
__component__.fluent = __component__.fluent || {};
78+
__component__.fluent['en'] = en_ftl
79+
__component__.fluent['da'] = da_ftl
4880
export default /*#__PURE__*/(function () { return __component__.exports })()"
4981
`;
5082

@@ -62,9 +94,6 @@ function _sfc_render(_ctx, _cache) {
6294
}
6395
6496
65-
_sfc_main.fluent = _sfc_main.fluent || {};
66-
_sfc_main.fluent['en'] = en_ftl
67-
_sfc_main.fluent['da'] = da_ftl
6897
_sfc_main.__hmrId = \\"4c0553ad\\"
6998
typeof __VUE_HMR_RUNTIME__ !== 'undefined' && __VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)
7099
import.meta.hot.accept(({ default: updated, _rerender_only }) => {
@@ -75,21 +104,58 @@ import.meta.hot.accept(({ default: updated, _rerender_only }) => {
75104
}
76105
})
77106
import _export_sfc from '/@id/plugin-vue:export-helper'
107+
_sfc_main.fluent = _sfc_main.fluent || {};
108+
_sfc_main.fluent['en'] = en_ftl
109+
_sfc_main.fluent['da'] = da_ftl
78110
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/home/demivan/projects/fluent/rollup-plugin-fluent-vue/__tests__/fixtures/components/external.vue\\"]])"
79111
`;
80112

113+
exports[`external ftl file support > works with vue 3 in production 1`] = `
114+
"import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
115+
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
116+
const _sfc_main = {}
117+
import { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"/@id/vue\\"
118+
119+
function _sfc_render(_ctx, _cache) {
120+
return (_openBlock(), _createElementBlock(\\"div\\", null, [
121+
_createElementVNode(\\"div\\", null, _toDisplayString(_ctx.$t('hello-user', { userName: _ctx.userName })), 1 /* TEXT */),
122+
_createElementVNode(\\"div\\", null, _toDisplayString(_ctx.$t('shared-photos', { userName: _ctx.userName, photoCount: _ctx.photoCount, userGender: _ctx.userGender })), 1 /* TEXT */)
123+
]))
124+
}
125+
126+
127+
import _export_sfc from '/@id/plugin-vue:export-helper'
128+
_sfc_main.fluent = _sfc_main.fluent || {};
129+
_sfc_main.fluent['en'] = en_ftl
130+
_sfc_main.fluent['da'] = da_ftl
131+
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render]])"
132+
`;
133+
81134
exports[`external ftl file support > works with vue 3 rollup plugin 1`] = `
82135
"import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
83136
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
84137
const script = {}
85-
import { render } from \\"/fixtures/components/external.vue?vue&type=template&id=8d0ca2ca&lang.js\\"
138+
import { render } from \\"/fixtures/components/external.vue?vue&type=template&id=1e181a1e&lang.js\\"
139+
140+
141+
script.render = render
142+
script.fluent = script.fluent || {};
143+
script.fluent['en'] = en_ftl
144+
script.fluent['da'] = da_ftl
145+
export default script"
146+
`;
147+
148+
exports[`external ftl file support > works with vue 3 rollup plugin in production 1`] = `
149+
"import da_ftl from '/fixtures/ftl/da/components/external.vue.ftl?import';
150+
import en_ftl from '/fixtures/ftl/en/components/external.vue.ftl?import';
151+
const script = {}
152+
import { render } from \\"/fixtures/components/external.vue?vue&type=template&id=1e181a1e&lang.js\\"
86153
87154
88155
script.render = render
89156
script.fluent = script.fluent || {};
90157
script.fluent['en'] = en_ftl
91158
script.fluent['da'] = da_ftl
92-
script.__file = \\"__tests__/fixtures/components/external.vue\\"
93159
export default script"
94160
`;
95161

@@ -117,8 +183,6 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
117183
}
118184
119185
120-
_sfc_main.fluent = _sfc_main.fluent || {};
121-
_sfc_main.fluent['en'] = en_ftl
122186
_sfc_main.__hmrId = \\"f4d31108\\"
123187
typeof __VUE_HMR_RUNTIME__ !== 'undefined' && __VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)
124188
import.meta.hot.accept(({ default: updated, _rerender_only }) => {
@@ -129,5 +193,7 @@ import.meta.hot.accept(({ default: updated, _rerender_only }) => {
129193
}
130194
})
131195
import _export_sfc from '/@id/plugin-vue:export-helper'
196+
_sfc_main.fluent = _sfc_main.fluent || {};
197+
_sfc_main.fluent['en'] = en_ftl
132198
export default /*#__PURE__*/_export_sfc(_sfc_main, [['render',_sfc_render],['__file',\\"/home/demivan/projects/fluent/rollup-plugin-fluent-vue/__tests__/fixtures/components/external.setup.vue\\"]])"
133199
`;

__tests__/external.spec.ts

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { dirname, resolve } from 'path'
32
import { fileURLToPath } from 'url'
43
import { describe, expect, it } from 'vitest'
@@ -83,6 +82,28 @@ describe('external ftl file support', () => {
8382
expect(code).toMatchSnapshot()
8483
})
8584

85+
it('works with vue 3 in production', async() => {
86+
// Arrange
87+
// Act
88+
const code = await testBundle({
89+
root: baseDir,
90+
mode: 'production',
91+
plugins: [
92+
vue3(),
93+
fluentPlugin({
94+
external: {
95+
baseDir: resolve(baseDir, 'fixtures'),
96+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
97+
locales: ['en', 'da'],
98+
},
99+
}),
100+
],
101+
}, '/fixtures/components/external.vue')
102+
103+
// Assert
104+
expect(code).toMatchSnapshot()
105+
})
106+
86107
it('works with vue 3 rollup plugin', async() => {
87108
// Arrange
88109
// Act
@@ -104,6 +125,28 @@ describe('external ftl file support', () => {
104125
expect(code).toMatchSnapshot()
105126
})
106127

128+
it('works with vue 3 rollup plugin in production', async() => {
129+
// Arrange
130+
// Act
131+
const code = await testBundle({
132+
root: baseDir,
133+
mode: 'production',
134+
plugins: [
135+
rollupVue3(),
136+
fluentPlugin({
137+
external: {
138+
baseDir: resolve(baseDir, 'fixtures'),
139+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
140+
locales: ['en', 'da'],
141+
},
142+
}),
143+
],
144+
}, '/fixtures/components/external.vue')
145+
146+
// Assert
147+
expect(code).toMatchSnapshot()
148+
})
149+
107150
it('works with vue 2', async() => {
108151
// Arrange
109152
// Act
@@ -124,4 +167,26 @@ describe('external ftl file support', () => {
124167
// Assert
125168
expect(code).toMatchSnapshot()
126169
})
170+
171+
it('works with vue 2 in production', async() => {
172+
// Arrange
173+
// Act
174+
const code = await testBundle({
175+
root: baseDir,
176+
mode: 'production',
177+
plugins: [
178+
vue2(),
179+
fluentPlugin({
180+
external: {
181+
baseDir: resolve(baseDir, 'fixtures'),
182+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
183+
locales: ['en', 'da'],
184+
},
185+
}),
186+
],
187+
}, '/fixtures/components/external.vue')
188+
189+
// Assert
190+
expect(code).toMatchSnapshot()
191+
})
127192
})

src/index.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,20 @@ function getInsertInfo(source: string): InsertInfo {
3333
let target = null
3434

3535
// vite-plugin-vue2
36-
let insertPos = source.indexOf('__component__.options.__file')
37-
if (insertPos !== -1)
38-
target = '__component__.options'
36+
if (source.includes('__component__'))
37+
target = '__component__'
3938

4039
// rollup-plugin-vue
41-
if (insertPos === -1) {
42-
insertPos = source.indexOf('script.__file')
43-
if (insertPos !== -1)
44-
target = 'script'
45-
}
40+
if (source.includes('export default script'))
41+
target = 'script'
4642

4743
// @vitejs/plugin-vue
48-
if (insertPos === -1) {
49-
insertPos = source.indexOf('_sfc_main.__hmrId')
50-
if (insertPos !== -1)
51-
target = '_sfc_main'
44+
if (source.includes('_sfc_main')) {
45+
target = '_sfc_main'
5246
}
5347

48+
const insertPos = source.indexOf('export default')
49+
5450
if (insertPos === -1 || target === null)
5551
throw new Error('Could not parse vue component')
5652

0 commit comments

Comments
 (0)