|
| 1 | +process.env.VUE_LOADER_TEST = true |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | +const { expect } = require('chai') |
| 5 | +const { |
| 6 | + mfs, |
| 7 | + loaderPath, |
| 8 | + bundle, |
| 9 | + test, |
| 10 | + mockRender |
| 11 | +} = require('./shared') |
| 12 | + |
| 13 | +const normalizeNewline = require('normalize-newline') |
| 14 | +const ExtractTextPlugin = require('extract-text-webpack-plugin') |
| 15 | +const SourceMapConsumer = require('source-map').SourceMapConsumer |
| 16 | + |
| 17 | +describe('advanced features', () => { |
| 18 | + it('pre/post loaders', done => { |
| 19 | + test({ |
| 20 | + entry: 'basic.vue', |
| 21 | + vue: { |
| 22 | + preLoaders: { |
| 23 | + js: require.resolve('./mock-loaders/js'), |
| 24 | + css: require.resolve('./mock-loaders/css') |
| 25 | + }, |
| 26 | + postLoaders: { |
| 27 | + html: require.resolve('./mock-loaders/html') |
| 28 | + } |
| 29 | + } |
| 30 | + }, (window, module) => { |
| 31 | + const vnode = mockRender(module, { |
| 32 | + msg: 'hi' |
| 33 | + }) |
| 34 | + // <h2 class="green">{{msg}}</h2> |
| 35 | + expect(vnode.tag).to.equal('h2') |
| 36 | + expect(vnode.data.staticClass).to.equal('green') |
| 37 | + expect(vnode.children[0].text).to.equal('hi') |
| 38 | + |
| 39 | + expect(module.data().msg).to.contain('Changed!') |
| 40 | + let style = window.document.querySelector('style').textContent |
| 41 | + style = normalizeNewline(style) |
| 42 | + expect(style).to.contain('comp-a h2 {\n color: #00f;\n}') |
| 43 | + done() |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + it('support chaining with other loaders', done => { |
| 48 | + const mockLoaderPath = require.resolve('./mock-loaders/js') |
| 49 | + test({ |
| 50 | + entry: 'basic.vue', |
| 51 | + modify: config => { |
| 52 | + config.module.rules[0].loader = loaderPath + '!' + mockLoaderPath |
| 53 | + } |
| 54 | + }, (window, module) => { |
| 55 | + expect(module.data().msg).to.equal('Changed!') |
| 56 | + done() |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('expose filename', done => { |
| 61 | + test({ |
| 62 | + entry: 'basic.vue' |
| 63 | + }, (window, module, rawModule) => { |
| 64 | + expect(module.__file).to.equal(path.normalize('test/fixtures/basic.vue')) |
| 65 | + done() |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + it('extract CSS', done => { |
| 70 | + bundle({ |
| 71 | + entry: 'extract-css.vue', |
| 72 | + vue: { |
| 73 | + loaders: { |
| 74 | + css: ExtractTextPlugin.extract('css-loader'), |
| 75 | + stylus: ExtractTextPlugin.extract('css-loader?sourceMap!stylus-loader') |
| 76 | + } |
| 77 | + }, |
| 78 | + plugins: [ |
| 79 | + new ExtractTextPlugin('test.output.css') |
| 80 | + ] |
| 81 | + }, (code, warnings) => { |
| 82 | + let css = mfs.readFileSync('/test.output.css').toString() |
| 83 | + css = normalizeNewline(css) |
| 84 | + expect(css).to.contain('h1 {\n color: #f00;\n}') |
| 85 | + expect(css).to.contain('h2 {\n color: green;\n}') |
| 86 | + done() |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('extract CSS using option', done => { |
| 91 | + bundle({ |
| 92 | + entry: 'extract-css.vue', |
| 93 | + vue: { |
| 94 | + extractCSS: true |
| 95 | + }, |
| 96 | + plugins: [ |
| 97 | + new ExtractTextPlugin('test.output.css') |
| 98 | + ] |
| 99 | + }, (code, warnings) => { |
| 100 | + let css = mfs.readFileSync('/test.output.css').toString() |
| 101 | + css = normalizeNewline(css) |
| 102 | + expect(css).to.contain('h1 {\n color: #f00;\n}') |
| 103 | + expect(css).to.contain('h2 {\n color: green;\n}') |
| 104 | + done() |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + it('extract CSS using option (passing plugin instance)', done => { |
| 109 | + const plugin = new ExtractTextPlugin('test.output.css') |
| 110 | + bundle({ |
| 111 | + entry: 'extract-css.vue', |
| 112 | + vue: { |
| 113 | + extractCSS: plugin |
| 114 | + }, |
| 115 | + plugins: [ |
| 116 | + plugin |
| 117 | + ] |
| 118 | + }, (code, warnings) => { |
| 119 | + let css = mfs.readFileSync('/test.output.css').toString() |
| 120 | + css = normalizeNewline(css) |
| 121 | + expect(css).to.contain('h1 {\n color: #f00;\n}') |
| 122 | + expect(css).to.contain('h2 {\n color: green;\n}') |
| 123 | + done() |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + it('pre-processors with extract css', done => { |
| 128 | + test({ |
| 129 | + entry: 'pre.vue', |
| 130 | + vue: { |
| 131 | + extractCSS: true |
| 132 | + }, |
| 133 | + plugins: [ |
| 134 | + new ExtractTextPlugin('test.output.css') |
| 135 | + ] |
| 136 | + }, (window, module) => { |
| 137 | + const vnode = mockRender(module) |
| 138 | + |
| 139 | + expect(vnode.children[0].tag).to.equal('h1') |
| 140 | + expect(vnode.children[1].tag).to.equal('comp-a') |
| 141 | + expect(vnode.children[2].tag).to.equal('comp-b') |
| 142 | + |
| 143 | + expect(module.data().msg).to.contain('Hello from coffee!') |
| 144 | + |
| 145 | + let css = mfs.readFileSync('/test.output.css').toString() |
| 146 | + css = normalizeNewline(css) |
| 147 | + expect(css).to.contain('body {\n font: 100% Helvetica, sans-serif;\n color: #999;\n}') |
| 148 | + |
| 149 | + done() |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + it('source map', done => { |
| 154 | + bundle({ |
| 155 | + entry: 'basic.vue', |
| 156 | + devtool: '#source-map' |
| 157 | + }, (code, warnings) => { |
| 158 | + const map = mfs.readFileSync('/test.build.js.map', 'utf-8') |
| 159 | + const smc = new SourceMapConsumer(JSON.parse(map)) |
| 160 | + let line |
| 161 | + let col |
| 162 | + const targetRE = /^\s+msg: 'Hello from Component A!'/ |
| 163 | + code.split(/\r?\n/g).some((l, i) => { |
| 164 | + if (targetRE.test(l)) { |
| 165 | + line = i + 1 |
| 166 | + col = 0 |
| 167 | + return true |
| 168 | + } |
| 169 | + }) |
| 170 | + const pos = smc.originalPositionFor({ |
| 171 | + line: line, |
| 172 | + column: col |
| 173 | + }) |
| 174 | + expect(pos.source.indexOf('basic.vue') > -1) |
| 175 | + expect(pos.line).to.equal(9) |
| 176 | + done() |
| 177 | + }) |
| 178 | + }) |
| 179 | + |
| 180 | + it('multiple rule definitions', done => { |
| 181 | + test({ |
| 182 | + modify: config => { |
| 183 | + // remove default rule |
| 184 | + config.module.rules.shift() |
| 185 | + }, |
| 186 | + entry: './test/fixtures/multiple-rules.js', |
| 187 | + module: { |
| 188 | + rules: [ |
| 189 | + { |
| 190 | + test: /\.vue$/, |
| 191 | + oneOf: [ |
| 192 | + { |
| 193 | + include: /-1\.vue$/, |
| 194 | + loader: loaderPath, |
| 195 | + options: { |
| 196 | + postcss: [ |
| 197 | + css => { |
| 198 | + css.walkDecls('font-size', decl => { |
| 199 | + decl.value = `${parseInt(decl.value, 10) * 2}px` |
| 200 | + }) |
| 201 | + } |
| 202 | + ], |
| 203 | + compilerModules: [{ |
| 204 | + postTransformNode: el => { |
| 205 | + el.staticClass = '"multiple-rule-1"' |
| 206 | + } |
| 207 | + }] |
| 208 | + } |
| 209 | + }, |
| 210 | + { |
| 211 | + include: /-2\.vue$/, |
| 212 | + loader: loaderPath, |
| 213 | + options: { |
| 214 | + postcss: [ |
| 215 | + css => { |
| 216 | + css.walkDecls('font-size', decl => { |
| 217 | + decl.value = `${parseInt(decl.value, 10) / 2}px` |
| 218 | + }) |
| 219 | + } |
| 220 | + ], |
| 221 | + compilerModules: [{ |
| 222 | + postTransformNode: el => { |
| 223 | + el.staticClass = '"multiple-rule-2"' |
| 224 | + } |
| 225 | + }] |
| 226 | + } |
| 227 | + } |
| 228 | + ] |
| 229 | + } |
| 230 | + ] |
| 231 | + } |
| 232 | + }, (window, module) => { |
| 233 | + const vnode1 = mockRender(window.rules[0]) |
| 234 | + const vnode2 = mockRender(window.rules[1]) |
| 235 | + expect(vnode1.data.staticClass).to.equal('multiple-rule-1') |
| 236 | + expect(vnode2.data.staticClass).to.equal('multiple-rule-2') |
| 237 | + const styles = window.document.querySelectorAll('style') |
| 238 | + const expr = /\.multiple-rule-\d\s*\{\s*font-size:\s*([.0-9]+)px;/ |
| 239 | + for (let i = 0, l = styles.length; i < l; i++) { |
| 240 | + const content = styles[i].textContent |
| 241 | + if (expr.test(content)) { |
| 242 | + expect(parseFloat(RegExp.$1)).to.be.equal(14) |
| 243 | + } |
| 244 | + } |
| 245 | + done() |
| 246 | + }) |
| 247 | + }) |
| 248 | + |
| 249 | + it('thread mode', done => { |
| 250 | + test({ |
| 251 | + entry: 'basic.vue', |
| 252 | + vue: { |
| 253 | + threadMode: true |
| 254 | + } |
| 255 | + }, (window, module, rawModule) => { |
| 256 | + const vnode = mockRender(module, { |
| 257 | + msg: 'hi' |
| 258 | + }) |
| 259 | + |
| 260 | + // <h2 class="red">{{msg}}</h2> |
| 261 | + expect(vnode.tag).to.equal('h2') |
| 262 | + expect(vnode.data.staticClass).to.equal('red') |
| 263 | + expect(vnode.children[0].text).to.equal('hi') |
| 264 | + |
| 265 | + expect(module.data().msg).to.contain('Hello from Component A!') |
| 266 | + let style = window.document.querySelector('style').textContent |
| 267 | + style = normalizeNewline(style) |
| 268 | + expect(style).to.contain('comp-a h2 {\n color: #f00;\n}') |
| 269 | + done() |
| 270 | + }) |
| 271 | + }) |
| 272 | +}) |
0 commit comments