1 | process.env.VUE_LOADER_TEST = true
|
2 |
|
3 | var path = require('path')
|
4 | var webpack = require('webpack')
|
5 | var MemoryFS = require('memory-fs')
|
6 | var jsdom = require('jsdom')
|
7 | var expect = require('chai').expect
|
8 | var rimraf = require('rimraf')
|
9 | var genId = require('vue-loader/lib/gen-id')
|
10 | var SourceMapConsumer = require('source-map').SourceMapConsumer
|
11 | var ExtractTextPlugin = require("extract-text-webpack-plugin")
|
12 | var compiler = require('vue-loader/lib/template-compiler')
|
13 | var normalizeNewline = require('normalize-newline')
|
14 |
|
15 | var vuxLoader = require('../src/index.js')
|
16 | var i18nParser = require('../libs/parse-i18n-function').parse
|
17 | const i18nParserForScript = require('../libs/replace-i18n-for-script').replace
|
18 | const getI18nBlock = require('../libs/get-i18n-block').get
|
19 | const getI18nBlockWithLocale = require('../libs/get-i18n-block').getWithLocale
|
20 |
|
21 | function getOptionsPlugin(config) {
|
22 | const match = config.plugins.filter(one => {
|
23 | return one.constructor.name === 'LoaderOptionsPlugin'
|
24 | })
|
25 | return match[0]
|
26 | }
|
27 |
|
28 |
|
29 | var loaderPath = 'expose-loader?vueModule!' + path.resolve(__dirname, '../src/index.js') + '!vue-loader'
|
30 | var mfs = new MemoryFS()
|
31 | var globalConfig = {
|
32 | output: {
|
33 | path: '/',
|
34 | filename: 'test.build.js'
|
35 | },
|
36 | module: {
|
37 | rules: [
|
38 | {
|
39 | test: /\.vue$/,
|
40 | loader: loaderPath
|
41 | }
|
42 | ]
|
43 | }
|
44 | }
|
45 |
|
46 | function bundle(options, vuxOptions, cb) {
|
47 | var vueOptions = options.vue
|
48 | delete options.vue
|
49 | var config = Object.assign(globalConfig, options)
|
50 |
|
51 |
|
52 | if (vueOptions) {
|
53 | config.plugins = (config.plugins || []).concat(new webpack.LoaderOptionsPlugin({
|
54 | vue: vueOptions
|
55 | }))
|
56 | }
|
57 | let basicVux = {
|
58 | options: {
|
59 | loaderString: loaderPath,
|
60 | rewriteLoaderString: false,
|
61 | isWebpack2: true,
|
62 | isTest: true
|
63 | }
|
64 | }
|
65 |
|
66 | if (vuxOptions.options) {
|
67 | for (let i in vuxOptions.options) {
|
68 | basicVux.options[i] = vuxOptions.options[i]
|
69 | }
|
70 | }
|
71 |
|
72 | if (vuxOptions.plugins) {
|
73 | basicVux.plugins = vuxOptions.plugins
|
74 | }
|
75 |
|
76 | config = vuxLoader.merge(config, basicVux)
|
77 |
|
78 | var webpackCompiler = webpack(config)
|
79 |
|
80 | webpackCompiler.outputFileSystem = mfs
|
81 | webpackCompiler.run(function (err, stats) {
|
82 | expect(err).to.be.null
|
83 | if (stats.compilation.errors.length) {
|
84 | stats.compilation.errors.forEach(function (err) {
|
85 | console.error(err.message)
|
86 | })
|
87 | }
|
88 | expect(stats.compilation.errors).to.be.empty
|
89 | cb(mfs.readFileSync('/test.build.js').toString())
|
90 | })
|
91 | }
|
92 |
|
93 | function test(options, vuxOptions, assert) {
|
94 | bundle(options, vuxOptions, function (code) {
|
95 | jsdom.env({
|
96 | html: '<!DOCTYPE html><html><head></head><body></body></html>',
|
97 | src: [code],
|
98 | done: function (err, window) {
|
99 | if (err) {
|
100 | console.log(err[0].data.error.stack)
|
101 | expect(err).to.be.null
|
102 | }
|
103 | assert(window, interopDefault(window.vueModule), window.vueModule)
|
104 | }
|
105 | })
|
106 | })
|
107 | }
|
108 |
|
109 | function mockRender(options, data) {
|
110 | return options.render.call(Object.assign({
|
111 | _v(val) {
|
112 | return val
|
113 | },
|
114 | _self: {},
|
115 | $createElement(tag, data, children) {
|
116 | if (Array.isArray(data)) {
|
117 | children = data
|
118 | data = null
|
119 | }
|
120 | return {
|
121 | tag: tag,
|
122 | data: data,
|
123 | children: children
|
124 | }
|
125 | },
|
126 | _m(index) {
|
127 | return options.staticRenderFns[index].call(this)
|
128 | },
|
129 | _s(str) {
|
130 | return String(str)
|
131 | }
|
132 | }, data))
|
133 | }
|
134 |
|
135 | function interopDefault(module) {
|
136 | return module ? module.__esModule ? module.default : module : module
|
137 | }
|
138 |
|
139 | var parse = require('../src/libs/import-parser')
|
140 |
|
141 | const str = parse(`<script>
|
142 | import {
|
143 | Group
|
144 | } from 'vux';
|
145 |
|
146 | `, function (opts) {
|
147 |
|
148 | })
|
149 |
|
150 | var themeParse = require('../src/libs/get-less-variables')
|
151 |
|
152 | var commomMapper = function (opts) {
|
153 | components = opts.components.map(function (one) {
|
154 | return one.newName
|
155 | })
|
156 | return `import { ${components.join(', ')} } from 'vux'`
|
157 | }
|
158 |
|
159 | var vuxMapper = function (opts) {
|
160 | let str = ''
|
161 | opts.components.forEach(function (one) {
|
162 | if (one.originalName === 'AlertPlugin') {
|
163 | str += `import ${one.newName} from 'vux/src/plugins/Alert'\n`
|
164 | } else if (one.originalName === 'ToastPlugin') {
|
165 | str += `import ${one.newName} from 'vux/src/plugins/Toast'\n`
|
166 | }
|
167 | })
|
168 | return str
|
169 | }
|
170 |
|
171 | describe('vux-loader', function () {
|
172 |
|
173 | describe('get i18n block', function () {
|
174 |
|
175 | it('basic', function () {
|
176 | const rs = getI18nBlock(`sfdsf<i18n>
|
177 | a:
|
178 | en: en_a
|
179 | zh-CN: zh-CN_a
|
180 | </i18n>sdfdsf`)
|
181 | expect(rs.a.en).to.equal('en_a')
|
182 | })
|
183 |
|
184 | it('return empty object for wrong format', function () {
|
185 | const rs = getI18nBlock(`sfdsf<i18n>
|
186 | a:
|
187 | en: en_a
|
188 | zh-CN: zh-CN_a
|
189 | </i18n>sdfdsf`)
|
190 | expect(JSON.stringify(rs)).to.equal('{}')
|
191 | })
|
192 |
|
193 |
|
194 | it('with locale', function () {
|
195 | const rs = getI18nBlockWithLocale({code: `sfdsf<i18n>
|
196 | a:
|
197 | en: en_a
|
198 | zh-CN: zh-CN_a
|
199 | </i18n>sdfdsf`,
|
200 | locale: 'en'})
|
201 | expect(rs.a).to.equal('en_a')
|
202 | })
|
203 |
|
204 | })
|
205 |
|
206 | describe('parse i18n for js', function () {
|
207 | const rs = i18nParserForScript(`this.$t('a')`, {})
|
208 | expect(rs).to.equal(`'a'`)
|
209 | })
|
210 |
|
211 | describe('parse i18n', function () {
|
212 |
|
213 | const map = {
|
214 | a: 'A',
|
215 | b: 'B',
|
216 | c: 'C',
|
217 | d: 'D'
|
218 | }
|
219 |
|
220 | const source1 = `<div :a="$t('a')">
|
221 | <p :c="d" e="f" g="hh">
|
222 | <span :options="['a', 'b', 'c', 'd']"></span>
|
223 | <span :obj="{a:'aa',b:'bb', c:$t('ee')}"></span>
|
224 | <span v-html="$t('sdfsdf')"></span>
|
225 | <span v-html="$t('sdfsdf') + $t('sfowewf') + $t('92fdf')"></span>
|
226 | {{ $t('dd') }}
|
227 | </p>
|
228 | </div>`
|
229 |
|
230 | const cases = [{
|
231 | raw: `<div :a="$t('a')"></div>`,
|
232 | rs: `<div :a="'A'"></div>`
|
233 | }, {
|
234 | raw: `<div :a="$t('x')"></div>`,
|
235 | rs: `<div :a="'x'"></div>`
|
236 | }, {
|
237 | raw: `<div :a='$t("a")'></div>`,
|
238 | rs: `<div :a="'A'"></div>`
|
239 | }, {
|
240 | raw: `<div v-html="$t('a')"></div>`,
|
241 | rs: `<div v-html="'A'"></div>`
|
242 | }, {
|
243 | raw: `<div :a="$t('a') + $t('b')"></div>`,
|
244 | rs: `<div :a="'A' + 'B'"></div>`
|
245 | }, {
|
246 | raw: `<div :a="$t('a') + 'B'"></div>`,
|
247 | rs: `<div :a="'A' + 'B'"></div>`
|
248 | }, {
|
249 | raw: `<div :a="$t('a') * 'B'"></div>`,
|
250 | rs: `<div :a="'A' * 'B'"></div>`
|
251 | }, {
|
252 | raw: `<div :a="{c: $t('a')}"></div>`,
|
253 | rs: `<div :a="{ c: 'A' }"></div>`
|
254 | }, {
|
255 | raw: `<div :a="{c: $t('a') + 'B'}"></div>`,
|
256 | rs: `<div :a="{ c: 'A' + 'B' }"></div>`
|
257 | }, {
|
258 | raw: `<div :a="{c: $t('a') + 'B', b: 'C' + $t('b')}"></div>`,
|
259 | rs: `<div :a="{
|
260 | c: 'A' + 'B',
|
261 | b: 'C' + 'B' }"></div>`
|
262 | }, {
|
263 | raw: `<div :a="[$t('a')]"></div>`,
|
264 | rs: `<div :a="['A']"></div>`
|
265 | }, {
|
266 | raw: `<div :a="[$t('a'),$t('b')]"></div>`,
|
267 | rs: `<div :a="[
|
268 | 'A',
|
269 | 'B' ]"></div>`
|
270 | }, {
|
271 | raw: `xx {{ $t('a') }}`,
|
272 | rs: `xx A`
|
273 | }, {
|
274 | raw: `xx {{ $t('a') }} {{$t('b')}}`,
|
275 | rs: `xx A B`
|
276 | }, {
|
277 | raw: `xx {{ $t('a') }} {{ $t('dfsf' + 'dsfdsf') }}`,
|
278 | rs: `xx A {{ $t('dfsf' + 'dsfdsf') }}`
|
279 | }, {
|
280 | raw: `xx {{ $tt('a') }}`,
|
281 | rs: `xx {{ $tt('a') }}`
|
282 | }]
|
283 |
|
284 | cases.forEach((one, index) => {
|
285 | it(`test ${index + 1}`, function () {
|
286 | const rs = i18nParser(one.raw, map)
|
287 | expect(rs === one.rs).to.equal(true)
|
288 | })
|
289 |
|
290 | })
|
291 |
|
292 | })
|
293 |
|
294 | describe('parse virtual component', function () {
|
295 | const parse = require('../src/libs/parse-virtual-component')
|
296 | it('basic', function () {
|
297 | const source = `<x-icon type="arrow-up-b" size="10" v-if="0 == 0"></x-icon>`
|
298 | const processed = parse(source, 'x-icon', function (query, a) {
|
299 | return '<svg ' + query.stringList + '></svg>'
|
300 | })
|
301 | expect(processed).to.equal('<svg type="arrow-up-b" size="10" v-if="0 == 0"></svg>')
|
302 | })
|
303 |
|
304 | it('basic', function () {
|
305 | const source = `<x-icon type="arrow-up-b" size="10" v-if="0 == 0"/>`
|
306 | const processed = parse(source, 'x-icon', function (query, a) {
|
307 | return '<svg ' + query.stringList + '></svg>'
|
308 | })
|
309 | expect(processed).to.equal('<svg type="arrow-up-b" size="10" v-if="0 == 0"></svg>')
|
310 | })
|
311 |
|
312 | it('basic', function () {
|
313 | const source = `<x-icon
|
314 | type="arrow-up-b" size="10"
|
315 | v-if="0 == 0"/>`
|
316 | const processed = parse(source, 'x-icon', function (query, a) {
|
317 | return '<svg ' + query.stringList + '></svg>'
|
318 | })
|
319 | expect(processed).to.equal('<svg type="arrow-up-b" size="10" v-if="0 == 0"></svg>')
|
320 | })
|
321 |
|
322 | it('basic', function () {
|
323 | const source = `<x-icon type="ios-ionic-outline" size="30"/>
|
324 | <x-icon type="ios-ionic-outline" size="30"></x-icon>`
|
325 | const processed = parse(source, 'x-icon', function (query, a) {
|
326 | return '<svg ' + query.stringList + '></svg>'
|
327 | })
|
328 | expect(processed).to.equal(`<svg type="ios-ionic-outline" size="30"></svg>
|
329 | <svg type="ios-ionic-outline" size="30"></svg>`)
|
330 | })
|
331 | })
|
332 |
|
333 | describe('parse virtual component with break line', function () {
|
334 | const parse = require('../src/libs/parse-virtual-component')
|
335 | it('basic', function () {
|
336 | const source = `<x-icon a="b"
|
337 | type="arrow-up-b"
|
338 | size="10"
|
339 | v-if="0 == 0"></x-icon>`
|
340 | const processed = parse(source, 'x-icon', function (query, a) {
|
341 | return '<svg ' + query.stringList + '></svg>'
|
342 | })
|
343 | expect(processed).to.equal('<svg a="b" type="arrow-up-b" size="10" v-if="0 == 0"></svg>')
|
344 | })
|
345 | })
|
346 |
|
347 | describe('parse virtual component', function () {
|
348 | const parse = require('../src/libs/parse-virtual-component')
|
349 | it('basic', function () {
|
350 | const source = `<x-icon a="b" c="d" class="e f" slot="icon"></x-icon>`
|
351 | const processed = parse(source, 'x-icon', function (query, a) {
|
352 | return '<svg ' + query.stringList + '></svg>'
|
353 | })
|
354 | expect(processed).to.equal('<svg a="b" c="d" class="e f" slot="icon"></svg>')
|
355 | })
|
356 | })
|
357 |
|
358 | describe('parse virtual component with click event', function () {
|
359 | const parse = require('../src/libs/parse-virtual-component')
|
360 | it('basic', function () {
|
361 | const source = `<x-icon a="b" c="d" class="e f" slot="icon" @click.native="handler"></x-icon>`
|
362 | const processed = parse(source, 'x-icon', function (query, a) {
|
363 | return '<svg ' + query.stringList + '></svg>'
|
364 | })
|
365 | expect(processed).to.equal('<svg a="b" c="d" class="e f" slot="icon" @click="handler"></svg>')
|
366 | })
|
367 | })
|
368 |
|
369 | describe('lib:get theme variables', function () {
|
370 | it('basic', function () {
|
371 | const rs = themeParse(path.resolve(__dirname, './vux-fixtures/less-theme-001.less'))
|
372 | expect(rs.a).to.equal('b')
|
373 | })
|
374 |
|
375 | it('ignore comments', function () {
|
376 | const rs = themeParse(path.resolve(__dirname, './vux-fixtures/less-theme-002.less'))
|
377 | expect(rs.a).to.equal('b')
|
378 | expect(rs.c).to.equal('d')
|
379 | expect(rs.d).to.equal('e')
|
380 | expect(rs.f).to.equal('g')
|
381 | })
|
382 |
|
383 | it('import files', function () {
|
384 | const rs = themeParse(path.resolve(__dirname, './vux-fixtures/less-theme-import.less'))
|
385 | expect(rs.x).to.equal('x')
|
386 | expect(rs.y).to.equal('z')
|
387 | })
|
388 | })
|
389 |
|
390 | describe('lib:import-parser', function () {
|
391 |
|
392 | let tests = [{
|
393 | title: 'basic',
|
394 | string: `import {A,B} from 'vux'`,
|
395 | rs: ['A', 'B']
|
396 | }, {
|
397 | title: 'basic',
|
398 | string: `import {A,B,} from 'vux'`,
|
399 | rs: ['A', 'B']
|
400 | }, {
|
401 | title: 'without space',
|
402 | string: `import{A,B} from 'vux'`,
|
403 | rs: ['A', 'B']
|
404 | }, {
|
405 | title: 'without space 2',
|
406 | string: `import {A,B}from 'vux'`,
|
407 | rs: ['A', 'B']
|
408 | }, {
|
409 | title: 'without space 3',
|
410 | string: `import{A,B}from 'vux'`,
|
411 | rs: ['A', 'B']
|
412 | }, {
|
413 | title: 'do not parse comments',
|
414 | string: `// import {A,B} from 'vux'
|
415 | import { C, D} from 'vux'`,
|
416 | rs: `\nimport { C, D } from 'vux'`
|
417 | }, {
|
418 | title: 'use as',
|
419 | string: `import {A,B as C} from 'vux'`,
|
420 | rs: ['A', 'C']
|
421 | }, {
|
422 | title: 'double quote',
|
423 | string: `import {A,B} from "vux"`,
|
424 | rs: ['A', 'B']
|
425 | }, {
|
426 | title: 'multi line and single quote',
|
427 | string: `import { A,
|
428 | B } from 'vux'`,
|
429 | rs: ['A', 'B']
|
430 | }, {
|
431 | title: 'multi line and double quote',
|
432 | string: `import { A,
|
433 | B } from "vux"`,
|
434 | rs: ['A', 'B']
|
435 | }, {
|
436 | title: 'no match',
|
437 | string: `import {A,B} from 'vvv'`,
|
438 | rs: `import {A,B} from 'vvv'`
|
439 | }, {
|
440 | title: 'more codes',
|
441 | string: `import C from 'XY'
|
442 | import { D } from 'ZW'
|
443 | import {A,B} from 'vvv'
|
444 | import { C } from 'vux'`,
|
445 | rs: `import C from 'XY'
|
446 | import { D } from 'ZW'
|
447 | import {A,B} from 'vvv'
|
448 | import { C } from 'vux'`
|
449 | }, {
|
450 | title: 'vux test2',
|
451 | string: `import {Group,Cell} from 'vux'
|
452 | import value2name from 'vux/src/filters/value2name'`,
|
453 | rs: `import { Group, Cell } from 'vux'
|
454 | import value2name from 'vux/src/filters/value2name'`
|
455 | }, {
|
456 | title: 'vux test3',
|
457 | string: `import {Group,
|
458 | Cell} from 'vux'
|
459 | import value2name from 'vux/src/filters/value2name'`,
|
460 | rs: `import { Group, Cell } from 'vux'
|
461 | import value2name from 'vux/src/filters/value2name'`
|
462 | }, {
|
463 | title: 'vux test4',
|
464 | string: `import { M1, M2 } from 'vux'
|
465 | import { mapMutations, mapState } from 'vuex'
|
466 | import { Group, Cell } from 'vux'
|
467 | import { Group1, Cell1 } from 'vux'
|
468 | import value2name from 'vux/src/filters/value2name'`,
|
469 | rs: `import { M1, M2 } from 'vux'
|
470 | import { mapMutations, mapState } from 'vuex'
|
471 | import { Group, Cell } from 'vux'
|
472 | import { Group1, Cell1 } from 'vux'
|
473 | import value2name from 'vux/src/filters/value2name'`
|
474 | }, {
|
475 | title: 'vux test5',
|
476 | string: `import {
|
477 | XX,
|
478 | YY} from 'vux'`,
|
479 | rs: `import { XX, YY } from 'vux'`
|
480 | }, {
|
481 | title: 'vux test6',
|
482 | string: `/**/
|
483 | import {Divider } from 'vux'`,
|
484 | rs: `/**/
|
485 | import { Divider } from 'vux'`
|
486 | }]
|
487 |
|
488 | tests.forEach(function (one) {
|
489 | it(one.title, function () {
|
490 | const rs = parse(one.string, commomMapper)
|
491 | if (typeof one.rs === 'string') {
|
492 | expect(rs).to.equal(one.rs)
|
493 | } else {
|
494 | expect(rs).to.equal(`import { ${one.rs.join(', ')} } from 'vux'`)
|
495 | }
|
496 | })
|
497 | })
|
498 |
|
499 | it('vux test', function () {
|
500 | const rs = parse(`import {AlertPlugin, ToastPlugin} from 'vux'`, vuxMapper)
|
501 | expect(rs).to.equal(`import AlertPlugin from 'vux/src/plugins/Alert'
|
502 | import ToastPlugin from 'vux/src/plugins/Toast'
|
503 | `)
|
504 | })
|
505 |
|
506 | it('vux test7', function () {
|
507 | const rs = parse(`import {AlertPlugin, ToastPlugin} from 'vux'
|
508 | // import { AlertPlugin } from 'vux'`, vuxMapper)
|
509 | expect(rs).to.equal(`import AlertPlugin from 'vux/src/plugins/Alert'
|
510 | import ToastPlugin from 'vux/src/plugins/Toast'
|
511 |
|
512 | `)
|
513 | })
|
514 |
|
515 | it('issue #1579 (1)', function () {
|
516 | const rs = parse(`import {
|
517 | AlertPlugin,
|
518 | ToastPlugin
|
519 | } from 'vux';`, vuxMapper)
|
520 | expect(rs).to.equal(`import AlertPlugin from 'vux/src/plugins/Alert'
|
521 | import ToastPlugin from 'vux/src/plugins/Toast'
|
522 | `)
|
523 | })
|
524 |
|
525 | it('issue #1579 (2)', function () {
|
526 | const rs = parse(`import {AlertPlugin,
|
527 | ToastPlugin
|
528 | } from 'vux'`, vuxMapper)
|
529 | expect(rs).to.equal(`import AlertPlugin from 'vux/src/plugins/Alert'
|
530 | import ToastPlugin from 'vux/src/plugins/Toast'
|
531 | `)
|
532 | })
|
533 |
|
534 | })
|
535 |
|
536 | describe('plugin:less-theme', function () {
|
537 |
|
538 | it('basic', function (done) {
|
539 | test({
|
540 | entry: './test/vux-fixtures/less-theme-basic.vue'
|
541 | }, {
|
542 | plugins: [{
|
543 | name: 'less-theme',
|
544 | path: './test/vux-fixtures/less-theme-basic.less'
|
545 | }]
|
546 | }, function (window, module, rawModule) {
|
547 | var vnode = mockRender(module, {
|
548 | msg: 'hi'
|
549 | })
|
550 | expect(vnode.tag).to.equal('p')
|
551 |
|
552 | var styles = window.document.querySelectorAll('style')
|
553 | expect(styles[0].textContent).to.contain('\n.p {\n color: red;\n}\n')
|
554 |
|
555 | done()
|
556 | })
|
557 | })
|
558 |
|
559 | })
|
560 |
|
561 | describe('plugin:style-parser', function () {
|
562 |
|
563 | it('basic', function (done) {
|
564 | test({
|
565 | entry: './test/vux-fixtures/style-parser-basic.vue'
|
566 | }, {
|
567 | plugins: [{
|
568 | name: 'less-theme',
|
569 | path: './test/vux-fixtures/less-theme-basic.less'
|
570 | }, {
|
571 | name: 'style-parser',
|
572 | fn: function (source) {
|
573 | return source.replace('@theme-p-color', 'yellow')
|
574 | }
|
575 | }]
|
576 | }, function (window, module, rawModule) {
|
577 | var vnode = mockRender(module, {
|
578 | msg: 'hi'
|
579 | })
|
580 | expect(vnode.tag).to.equal('p')
|
581 |
|
582 | var styles = window.document.querySelectorAll('style')
|
583 | expect(styles[0].textContent).to.contain('\n.p {\n color: yellow;\n}\n')
|
584 |
|
585 | done()
|
586 | })
|
587 | })
|
588 |
|
589 | })
|
590 |
|
591 | describe('plugin:template-feature-switch', function () {
|
592 |
|
593 | it('basic', function (done) {
|
594 | test({
|
595 | entry: './test/vux-fixtures/template-feature-switch-basic.vue'
|
596 | }, {
|
597 | plugins: [{
|
598 | name: 'template-feature-switch',
|
599 | features: {
|
600 | FEATURE1: true,
|
601 | FEATURE2: false
|
602 | }
|
603 | }]
|
604 | }, function (window, module, rawModule) {
|
605 | var vnode = mockRender(module, {
|
606 | msg: 'hi'
|
607 | })
|
608 |
|
609 | expect(vnode.tag).to.equal('div')
|
610 | expect(vnode.children[0].indexOf('ON FEATURE1') > -1).to.equal(true)
|
611 | expect(vnode.children[0].indexOf('OFF FEATURE2') > -1).to.equal(true)
|
612 | done()
|
613 | })
|
614 | })
|
615 |
|
616 | })
|
617 |
|
618 | describe('one instance', function () {
|
619 | it('should throw', function () {
|
620 | const webpackConfig = {
|
621 | plugins: []
|
622 | }
|
623 | const merge = function () {
|
624 | return vuxLoader.merge(webpackConfig, {
|
625 | options: {
|
626 | env: 'env1'
|
627 | },
|
628 | plugins: [{
|
629 | name: 'test1'
|
630 | }, {
|
631 | name: 'test1'
|
632 | }]
|
633 | })
|
634 | }
|
635 | expect(merge).to.throw(/only one instance is allowed/)
|
636 | })
|
637 | })
|
638 |
|
639 | describe('merge multi times', function () {
|
640 | it('should merge options', function () {
|
641 | const webpackConfig = {
|
642 | plugins: []
|
643 | }
|
644 | const config1 = vuxLoader.merge(webpackConfig, {
|
645 | options: {
|
646 | env: 'env1'
|
647 | }
|
648 | })
|
649 |
|
650 | expect(getOptionsPlugin(config1).options.vux.options.env).to.equal('env1')
|
651 |
|
652 | const config2 = vuxLoader.merge(config1, {
|
653 | options: {
|
654 | env: 'env2'
|
655 | }
|
656 | })
|
657 |
|
658 | expect(getOptionsPlugin(config2).options.vux.options.env).to.equal('env2')
|
659 | })
|
660 |
|
661 | it('should merge plugins with the same name', function () {
|
662 | const webpackConfig = {}
|
663 | const config1 = vuxLoader.merge(webpackConfig, {
|
664 | plugins: [{
|
665 | name: 'test1',
|
666 | arg: 1
|
667 | }]
|
668 | })
|
669 |
|
670 | expect(getOptionsPlugin(config1).options.vux.plugins.length).to.equal(1)
|
671 | expect(getOptionsPlugin(config1).options.vux.plugins[0].arg).to.equal(1)
|
672 |
|
673 | const config2 = vuxLoader.merge(config1, {
|
674 | plugins: [{
|
675 | name: 'test1',
|
676 | arg: 2
|
677 | }]
|
678 | })
|
679 |
|
680 | expect(getOptionsPlugin(config1).options.vux.plugins.length).to.equal(1)
|
681 | expect(getOptionsPlugin(config1).options.vux.plugins[0].arg).to.equal(2)
|
682 |
|
683 | })
|
684 |
|
685 | it('should delete plugin when env is change', function () {
|
686 | const webpackConfig = {}
|
687 | const config1 = vuxLoader.merge(webpackConfig, {
|
688 | options: {
|
689 | env: 'env1'
|
690 | },
|
691 | plugins: [{
|
692 | name: 'test1',
|
693 | arg: 1,
|
694 | envs: ['env1']
|
695 | }]
|
696 | })
|
697 |
|
698 | expect(config1.plugins[0].options.vux.plugins.length).to.equal(1)
|
699 |
|
700 | const config2 = vuxLoader.merge(config1, {
|
701 | options: {
|
702 | env: 'env2'
|
703 | }
|
704 | })
|
705 |
|
706 | expect(getOptionsPlugin(config1).options.vux.plugins.length).to.equal(0)
|
707 |
|
708 | })
|
709 |
|
710 | it('should merge plugins', function () {
|
711 | const webpackConfig = {}
|
712 | const config1 = vuxLoader.merge(webpackConfig, {
|
713 | options: {
|
714 | env: 'env1'
|
715 | },
|
716 | plugins: [{
|
717 | name: 'test1',
|
718 | arg: 1,
|
719 | envs: ['env1']
|
720 | }]
|
721 | })
|
722 |
|
723 | expect(getOptionsPlugin(config1).options.vux.allPlugins.length).to.equal(1)
|
724 | expect(getOptionsPlugin(config1).options.vux.plugins.length).to.equal(1)
|
725 |
|
726 | const config2 = vuxLoader.merge(config1, {
|
727 | plugins: [{
|
728 | name: 'test2'
|
729 | }]
|
730 | })
|
731 |
|
732 | expect(getOptionsPlugin(config2).options.vux.allPlugins.length).to.equal(2)
|
733 | expect(getOptionsPlugin(config2).options.vux.plugins.length).to.equal(2)
|
734 |
|
735 | const config3 = vuxLoader.merge(config2, {
|
736 | plugins: [{
|
737 | name: 'test3',
|
738 | envs: ['env3']
|
739 | }]
|
740 | })
|
741 |
|
742 | expect(getOptionsPlugin(config3).options.vux.allPlugins.length).to.equal(3)
|
743 | expect(getOptionsPlugin(config3).options.vux.plugins.length).to.equal(2)
|
744 |
|
745 | })
|
746 | })
|
747 |
|
748 | describe('plugin:script-parser', function () {
|
749 |
|
750 | it('fn function should work', function (done) {
|
751 | test({
|
752 | entry: './test/vux-fixtures/script-parser-fn.vue'
|
753 | }, {
|
754 | plugins: [{
|
755 | name: 'script-parser',
|
756 | fn: function (source) {
|
757 | return source.replace('AAAA', 'BBBB')
|
758 | }
|
759 | }]
|
760 | }, function (window, module, rawModule) {
|
761 | var vnode = mockRender(module, {
|
762 | msg: 'hi'
|
763 | })
|
764 | expect(vnode.tag).to.equal('p')
|
765 | expect(module.data().msg).to.equal('BBBB')
|
766 | done()
|
767 | })
|
768 | })
|
769 |
|
770 | it('fn function should not work with env', function (done) {
|
771 | test({
|
772 | entry: './test/vux-fixtures/script-parser-fn.vue'
|
773 | }, {
|
774 | options: {
|
775 | env: 'test'
|
776 | },
|
777 | plugins: [{
|
778 | name: 'script-parser',
|
779 | envs: ['production'],
|
780 | fn: function (source) {
|
781 | return source.replace('AAAA', 'BBBB')
|
782 | }
|
783 | }]
|
784 | }, function (window, module, rawModule) {
|
785 | var vnode = mockRender(module, {
|
786 | msg: 'hi'
|
787 | })
|
788 | expect(vnode.tag).to.equal('p')
|
789 | expect(module.data().msg).to.equal('AAAA')
|
790 | done()
|
791 | })
|
792 | })
|
793 | })
|
794 |
|
795 | describe('plugin:template-parser', function () {
|
796 |
|
797 | it('fn function should work', function (done) {
|
798 | test({
|
799 | entry: './test/vux-fixtures/template-parser-fn.vue'
|
800 | }, {
|
801 | plugins: [{
|
802 | name: 'template-parser',
|
803 | fn: function (source) {
|
804 | return source.replace('我们没有底线', '我是有底线的')
|
805 | }
|
806 | }]
|
807 | }, function (window, module, rawModule) {
|
808 | var vnode = mockRender(module, {
|
809 | msg: 'hi'
|
810 | })
|
811 | expect(vnode.tag).to.equal('p')
|
812 | expect(vnode.children[0]).to.equal('我是有底线的')
|
813 | done()
|
814 | })
|
815 | })
|
816 |
|
817 | it('replaceList param should work', function (done) {
|
818 | test({
|
819 | entry: './test/vux-fixtures/template-parser-fn.vue'
|
820 | }, {
|
821 | plugins: [{
|
822 | name: 'template-parser',
|
823 | replaceList: [{
|
824 | test: /我们没有/,
|
825 | replaceString: ''
|
826 | }, {
|
827 | test: /底线/,
|
828 | replaceString: '底线是什么'
|
829 | }]
|
830 | }]
|
831 | }, function (window, module, rawModule) {
|
832 | var vnode = mockRender(module, {
|
833 | msg: 'hi'
|
834 | })
|
835 | expect(vnode.tag).to.equal('p')
|
836 | expect(vnode.children[0]).to.equal('底线是什么')
|
837 | done()
|
838 | })
|
839 | })
|
840 |
|
841 | })
|
842 | })
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 |
|
1023 |
|
1024 |
|
1025 |
|
1026 |
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 |
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 |
|
1104 |
|
1105 |
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 |
|
1115 |
|
1116 |
|
1117 |
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 |
|
1125 |
|
1126 |
|
1127 |
|
1128 |
|
1129 |
|
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 |
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 |
|
1181 |
|
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|