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

Commit 736c4fe

Browse files
authored
Trim empty lines in ftl source content (#2)
`vite-plugin-vue2` pads SFC file sections with newlines - trim those
1 parent 07224a9 commit 736c4fe

File tree

5 files changed

+892
-11
lines changed

5 files changed

+892
-11
lines changed

__tests__/__snapshots__/test.spec.ts.snap

Lines changed: 151 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ function render(_ctx, _cache) {
1616
function block0 (Component) {
1717
const target = Component.options || Component;
1818
target.fluent = target.fluent || {};
19-
target.fluent['en'] = new bundle.FluentResource(\`
20-
# Simple things are simple.
19+
target.fluent['en'] = new bundle.FluentResource(\`# Simple things are simple.
2120
hello-user = Hello, {$userName}!
2221
2322
# Complex things are possible.
@@ -29,8 +28,7 @@ shared-photos =
2928
[male] his stream
3029
[female] her stream
3130
*[other] their stream
32-
}.
33-
\`);
31+
}.\`);
3432
}
3533
3634
const script = {};
@@ -61,8 +59,7 @@ function render(_ctx, _cache) {
6159
function block0 (Component) {
6260
const target = Component.options || Component;
6361
target.fluent = target.fluent || {};
64-
target.fluent['en'] = new bundle.FluentResource(\`
65-
# Simple things are simple.
62+
target.fluent['en'] = new bundle.FluentResource(\`# Simple things are simple.
6663
hello-user = Hello, {$userName}!
6764
6865
# Complex things are possible.
@@ -74,8 +71,7 @@ shared-photos =
7471
[male] his stream
7572
[female] her stream
7673
*[other] their stream
77-
}.
78-
\`);
74+
}.\`);
7975
}
8076
8177
const script = {};
@@ -87,3 +83,150 @@ script.__file = \\"__tests__/fixtures/test.vue\\";
8783
module.exports = script;
8884
"
8985
`;
86+
87+
exports[`rollup plugin > works with vue 2 1`] = `
88+
"'use strict';
89+
90+
var bundle = require('@fluent/bundle');
91+
92+
var render = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',[_c('div',[_vm._v(_vm._s(_vm.$t('hello-user', { userName: _vm.userName })))]),_c('div',[_vm._v(_vm._s(_vm.$t('shared-photos', { userName: _vm.userName, photoCount: _vm.photoCount, userGender: _vm.userGender })))])])};
93+
var staticRenderFns = [];
94+
render._withStripped = true;
95+
96+
function normalizeComponent (
97+
scriptExports,
98+
render,
99+
staticRenderFns,
100+
functionalTemplate,
101+
injectStyles,
102+
scopeId,
103+
moduleIdentifier, /* server only */
104+
shadowMode /* vue-cli only */
105+
) {
106+
// Vue.extend constructor export interop
107+
var options = typeof scriptExports === 'function'
108+
? scriptExports.options
109+
: scriptExports;
110+
111+
// render functions
112+
if (render) {
113+
options.render = render;
114+
options.staticRenderFns = staticRenderFns;
115+
options._compiled = true;
116+
}
117+
118+
// functional template
119+
if (functionalTemplate) {
120+
options.functional = true;
121+
}
122+
123+
// scopedId
124+
if (scopeId) {
125+
options._scopeId = 'data-v-' + scopeId;
126+
}
127+
128+
var hook;
129+
if (moduleIdentifier) { // server build
130+
hook = function (context) {
131+
// 2.3 injection
132+
context =
133+
context || // cached call
134+
(this.$vnode && this.$vnode.ssrContext) || // stateful
135+
(this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext); // functional
136+
// 2.2 with runInNewContext: true
137+
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
138+
context = __VUE_SSR_CONTEXT__;
139+
}
140+
// inject component styles
141+
if (injectStyles) {
142+
injectStyles.call(this, context);
143+
}
144+
// register component module identifier for async chunk inferrence
145+
if (context && context._registeredComponents) {
146+
context._registeredComponents.add(moduleIdentifier);
147+
}
148+
};
149+
// used by ssr in case component is cached and beforeCreate
150+
// never gets called
151+
options._ssrRegister = hook;
152+
} else if (injectStyles) {
153+
hook = shadowMode
154+
? function () {
155+
injectStyles.call(
156+
this,
157+
(options.functional ? this.parent : this).$root.$options.shadowRoot
158+
);
159+
}
160+
: injectStyles;
161+
}
162+
163+
if (hook) {
164+
if (options.functional) {
165+
// for template-only hot-reload because in that case the render fn doesn't
166+
// go through the normalizer
167+
options._injectStyles = hook;
168+
// register for functional component in vue file
169+
var originalRender = options.render;
170+
options.render = function renderWithStyleInjection (h, context) {
171+
hook.call(context);
172+
return originalRender(h, context)
173+
};
174+
} else {
175+
// inject component registration as beforeCreate hook
176+
var existing = options.beforeCreate;
177+
options.beforeCreate = existing
178+
? [].concat(existing, hook)
179+
: [hook];
180+
}
181+
}
182+
183+
return {
184+
exports: scriptExports,
185+
options: options
186+
}
187+
}
188+
189+
function block0 (Component) {
190+
const target = Component.options || Component;
191+
target.fluent = target.fluent || {};
192+
target.fluent['en'] = new bundle.FluentResource(\`# Simple things are simple.
193+
hello-user = Hello, {$userName}!
194+
195+
# Complex things are possible.
196+
shared-photos =
197+
{$userName} {$photoCount ->
198+
[one] added one photo
199+
*[other] added {$photoCount} new photos
200+
} to {$userGender ->
201+
[male] his stream
202+
[female] her stream
203+
*[other] their stream
204+
}.\`);
205+
}
206+
207+
const __vue2_script = {};
208+
const __cssModules = {};
209+
var __component__ = /*#__PURE__*/normalizeComponent(
210+
__vue2_script,
211+
render,
212+
staticRenderFns,
213+
false,
214+
__vue2_injectStyles,
215+
null,
216+
null,
217+
null
218+
);
219+
220+
function __vue2_injectStyles (context) {
221+
for(let o in __cssModules){
222+
this[o] = __cssModules[o];
223+
}
224+
}
225+
if (typeof block0 === 'function') block0(__component__);
226+
227+
__component__.options.__file = \\"__tests__/fixtures/test.vue\\";
228+
var test = /*#__PURE__*/(function () { return __component__.exports })();
229+
230+
module.exports = test;
231+
"
232+
`;

__tests__/test.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { fileURLToPath } from 'url'
44

55
import { rollup, RollupOptions } from 'rollup'
66
import vue from 'rollup-plugin-vue'
7+
import { createVuePlugin } from 'vite-plugin-vue2'
78

89
import fluentPlugin from '../src'
910

@@ -38,6 +39,21 @@ describe('rollup plugin', () => {
3839
expect(code).toMatchSnapshot()
3940
})
4041

42+
it('works with vue 2', async () => {
43+
// Arrange
44+
// Act
45+
const code = await testBundle({
46+
input: resolve(baseDir, 'fixtures/test.vue'),
47+
plugins: [
48+
createVuePlugin(),
49+
fluentPlugin()
50+
]
51+
})
52+
53+
// Assert
54+
expect(code).toMatchSnapshot()
55+
})
56+
4157
it('custom blockType', async () => {
4258
// Arrange
4359
// Act

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "rollup-plugin-fluent-vue",
33
"version": "3.0.0",
4-
"description": "Rollup and Vite plugin with custom block support for fluent-vue",
4+
"description": "Rollup and Vite plugin for custom block support for fluent-vue",
55
"keywords": [
66
"localization",
77
"l10n",
@@ -60,6 +60,8 @@
6060
"tsup": "^5.11.9",
6161
"typescript": "*",
6262
"vite": "^2.7.7",
63-
"vitest": "^0.0.114"
63+
"vite-plugin-vue2": "^1.9.3",
64+
"vitest": "^0.0.114",
65+
"vue-template-compiler": "^2.6.14"
6466
}
6567
}

0 commit comments

Comments
 (0)