22import path from 'path'
33import webpack from 'webpack'
44import merge from 'webpack-merge'
5- // import MiniCssExtractPlugin from " mini-css-extract-plugin";
5+ import MiniCssExtractPlugin from ' mini-css-extract-plugin'
66import { fs as mfs } from 'memfs'
77
8+ import { JSDOM , VirtualConsole } from 'jsdom'
9+
810import { VueLoaderPlugin , VueLoaderOptions } from '../dist/index'
911
1012const baseConfig : webpack . Configuration = {
1113 mode : 'development' ,
1214 devtool : false ,
1315 output : {
1416 path : '/' ,
15- filename : 'test.build.js'
16- } ,
17- resolve : {
18- alias : {
19- // this isn't technically needed, since the default `vue` entry for bundlers
20- // is a simple `export * from '@vue/runtime-dom`. However having this
21- // extra re-export somehow causes webpack to always invalidate the module
22- // on the first HMR update and causes the page to reload.
23- vue : '@vue/runtime-dom'
24- }
17+ filename : 'test.build.js' ,
2518 } ,
2619 resolveLoader : {
2720 alias : {
28- 'vue-loader' : require . resolve ( '../dist' )
29- }
21+ 'vue-loader' : require . resolve ( '../dist' ) ,
22+ } ,
3023 } ,
3124 module : {
3225 rules : [
3326 {
3427 test : / \. v u e $ / ,
35- use : 'vue-loader'
28+ use : 'vue-loader' ,
3629 } ,
3730 {
3831 test : / \. p n g $ / ,
3932 use : {
4033 loader : 'url-loader' ,
41- options : { limit : 8192 }
42- }
34+ options : { limit : 8192 } ,
35+ } ,
4336 } ,
4437 {
4538 test : / \. c s s $ / ,
4639 use : [
47- // {
48- // loader: MiniCssExtractPlugin.loader,
49- // options: { hmr: true }
50- // },
51- 'css-loader'
52- ]
53- }
54- ]
40+ {
41+ loader : MiniCssExtractPlugin . loader ,
42+ options : { hmr : true } ,
43+ } ,
44+ 'css-loader' ,
45+ ] ,
46+ } ,
47+ ] ,
5548 } ,
5649 plugins : [
57- new VueLoaderPlugin ( )
58- // new MiniCssExtractPlugin({
59- // filename: '[name].css'
60- // })
61- ]
50+ new VueLoaderPlugin ( ) ,
51+ new webpack . DefinePlugin ( {
52+ __VUE_OPTIONS_API__ : true ,
53+ __VUE_PROD_DEVTOOLS__ : false ,
54+ } ) ,
55+ new MiniCssExtractPlugin ( {
56+ filename : '[name].css' ,
57+ } ) ,
58+ ] ,
6259}
6360
6461type BundleOptions = webpack . Configuration & {
6562 vue ?: VueLoaderOptions
6663 modify ?: ( config : webpack . Configuration ) => void
67- suppressJSDOMConsole ?: boolean
6864}
6965
70- export function bundle ( options : BundleOptions , wontThrowError ?: boolean ) {
66+ export function bundle (
67+ options : BundleOptions ,
68+ wontThrowError ?: boolean
69+ ) : Promise < {
70+ code : string
71+ stats : webpack . Stats
72+ } > {
7173 let config : BundleOptions = merge ( { } , baseConfig , options )
7274
7375 if ( config . vue && config . module ) {
7476 const vueOptions = options . vue
7577 delete config . vue
7678 const vueIndex = config . module . rules . findIndex (
77- r => r . test instanceof RegExp && r . test . test ( '.vue' )
79+ ( r ) => r . test instanceof RegExp && r . test . test ( '.vue' )
7880 )
7981 const vueRule = config . module . rules [ vueIndex ]
8082 config . module . rules [ vueIndex ] = Object . assign ( { } , vueRule , {
81- options : vueOptions
83+ options : vueOptions ,
8284 } )
8385 }
8486
@@ -88,9 +90,9 @@ export function bundle(options: BundleOptions, wontThrowError?: boolean) {
8890 entry : require . resolve ( './fixtures/entry' ) ,
8991 resolve : {
9092 alias : {
91- '~target' : path . resolve ( __dirname , './fixtures' , vueFile )
92- }
93- }
93+ '~target' : path . resolve ( __dirname , './fixtures' , vueFile ) ,
94+ } ,
95+ } ,
9496 } )
9597 }
9698
@@ -102,7 +104,7 @@ export function bundle(options: BundleOptions, wontThrowError?: boolean) {
102104 const webpackCompiler = webpack ( config )
103105 webpackCompiler . outputFileSystem = Object . assign (
104106 {
105- join : path . join . bind ( path )
107+ join : path . join . bind ( path ) ,
106108 } ,
107109 mfs
108110 )
@@ -113,7 +115,7 @@ export function bundle(options: BundleOptions, wontThrowError?: boolean) {
113115 if ( ! wontThrowError ) {
114116 expect ( err ) . toBeNull ( )
115117 if ( errors && errors . length ) {
116- errors . forEach ( error => {
118+ errors . forEach ( ( error ) => {
117119 console . error ( error . message )
118120 } )
119121 }
@@ -125,9 +127,47 @@ export function bundle(options: BundleOptions, wontThrowError?: boolean) {
125127 } else {
126128 resolve ( {
127129 code : mfs . readFileSync ( '/test.build.js' ) . toString ( ) ,
128- stats
130+ stats,
129131 } )
130132 }
131133 } )
132134 } )
133135}
136+
137+ export async function mockBundleAndRun (
138+ options : BundleOptions ,
139+ wontThrowError ?: boolean
140+ ) {
141+ const { code, stats } = await bundle ( options , wontThrowError )
142+
143+ let jsdomError
144+ const dom = new JSDOM (
145+ `<!DOCTYPE html><html><head></head><body></body></html>` ,
146+ {
147+ runScripts : 'outside-only' ,
148+ virtualConsole : new VirtualConsole ( ) ,
149+ }
150+ )
151+ try {
152+ dom . window . eval ( code )
153+ } catch ( e ) {
154+ console . error ( `JSDOM error:\n${ e . stack } ` )
155+ jsdomError = e
156+ }
157+
158+ const { window } = dom
159+ const { module, exports, instance } = window
160+
161+ return {
162+ window,
163+
164+ module,
165+ exports,
166+ instance,
167+
168+ code,
169+ stats,
170+
171+ jsdomError,
172+ }
173+ }
0 commit comments