1
+ const { join, dirname } = require ( 'path' ) ;
2
+ const { merge } = require ( 'webpack-merge' ) ;
3
+ const globRegex = require ( 'glob-regex' ) ;
4
+
5
+ function getKarmaTestsRegex ( webpack ) {
6
+ const karmaConfig = require ( webpack . Utils . project . getProjectFilePath ( 'karma.conf.js' ) ) ;
7
+ let filesRegex = karmaConfig . filesRegex ||
8
+ new RegExp ( ( karmaConfig . filePatterns || [ ] ) . map ( ( glob ) =>
9
+ globRegex ( `./${ glob } ` ) . source // all webpack require.context start with `./` and glob-regex adds ^
10
+ ) . join ( '|' ) ) ;
11
+
12
+ if ( ! filesRegex || ! filesRegex . source ) {
13
+ webpack . Utils . log . warn ( "Karma files regex not found, falling back to tests/**/*.ts" ) ;
14
+ filesRegex = / t e s t s \/ .* \. t s / ;
15
+ }
16
+ return filesRegex ;
17
+ }
18
+
19
+ /**
20
+ * @param {typeof import("@nativescript/webpack") } webpack
21
+ */
22
+ module . exports = webpack => {
23
+ webpack . chainWebpack ( ( config , env ) => {
24
+ if ( env . karmaWebpack ) {
25
+ return setupKarmaBuild ( config , env , webpack ) ;
26
+ }
27
+
28
+ if ( env . unitTesting ) {
29
+ return setupUnitTestBuild ( config , env , webpack ) ;
30
+ }
31
+ } ) ;
32
+ } ;
33
+
34
+ /**
35
+ * @param {import("webpack-chain") } config
36
+ * @param {typeof import("@nativescript/webpack") } webpack
37
+ */
38
+ function setupKarmaBuild ( config , env , webpack ) {
39
+ const karmaWebpack = require ( 'karma-webpack/lib/webpack/defaults' ) ;
40
+ const defaults = karmaWebpack . create ( ) ;
41
+ delete defaults . optimization ;
42
+
43
+ karmaWebpack . create = ( ) => {
44
+ return defaults ;
45
+ } ;
46
+
47
+ config . entryPoints . clear ( ) ;
48
+ config . optimization . clear ( ) ;
49
+
50
+ config . plugins . delete ( 'WatchStatePlugin' ) ;
51
+ config . plugins . delete ( 'AngularCompilerPlugin' ) ;
52
+ config . plugins . delete ( 'AngularWebpackPlugin' ) ;
53
+ config . module . rules . delete ( 'angular' ) ;
54
+ // config.plugins.delete('CleanWebpackPlugin')
55
+ config . plugin ( 'DefinePlugin' ) . tap ( ( args ) => {
56
+ args [ 0 ] = merge ( args [ 0 ] , {
57
+ __TEST_RUNNER_STAY_OPEN__ : ! ! env . stayOpen ,
58
+ } ) ;
59
+
60
+ return args ;
61
+ } ) ;
62
+
63
+
64
+
65
+ config . output . delete ( 'path' ) ; // use temp path
66
+ config . output . set ( 'iife' , true ) ;
67
+ config . output . set ( 'libraryTarget' , 'global' ) ;
68
+ config . output . set ( 'clean' , true ) ;
69
+
70
+ config . module
71
+ . rule ( 'unit-test' )
72
+ . enforce ( 'post' )
73
+ . include . add ( webpack . Utils . platform . getEntryDirPath ( ) ) . end ( )
74
+ . test ( / \. ( t s | j s ) / )
75
+ . use ( 'unit-test-loader' )
76
+ . loader ( join ( __dirname , 'loaders' , 'unit-test-loader' ) )
77
+ . options ( {
78
+ appPath : webpack . Utils . platform . getEntryDirPath ( ) ,
79
+ platform : webpack . Utils . platform . getPlatformName ( )
80
+ } ) ;
81
+ }
82
+
83
+ /**
84
+ * @param {import("webpack-chain") } config
85
+ * @param {typeof import("@nativescript/webpack") } webpack
86
+ */
87
+ function setupUnitTestBuild ( config , env , webpack ) {
88
+ // config.plugins.delete('CleanWebpackPlugin');
89
+ // config.output.set('clean', false);
90
+
91
+ // harmless warnings
92
+ config . set (
93
+ 'ignoreWarnings' ,
94
+ ( config . get ( 'ignoreWarnings' ) || [ ] ) . concat ( [
95
+ / C a n ' t r e s o l v e ' @ n a t i v e s c r i p t \/ u n i t - t e s t - r u n n e r \/ a p p \/ s t o p - p r o c e s s .j s ' /
96
+ ] )
97
+ ) ;
98
+
99
+ const runnerPath = dirname (
100
+ require . resolve ( '@nativescript/unit-test-runner/package.json' )
101
+ ) ;
102
+ config . module . rule ( 'css' ) . include . add ( runnerPath ) ;
103
+ config . module . rule ( 'xml' ) . include . add ( runnerPath ) ;
104
+ config . module . rule ( 'js' ) . include . add ( runnerPath ) ;
105
+ const filesRegex = getKarmaTestsRegex ( webpack ) ;
106
+
107
+ config . plugin ( 'DefinePlugin' ) . tap ( ( args ) => {
108
+ args [ 0 ] = merge ( args [ 0 ] , {
109
+ 'global.TNS_WEBPACK' : true ,
110
+ } ) ;
111
+
112
+ return args ;
113
+ } ) ;
114
+
115
+ const entryPath = webpack . Utils . virtualModules . addVirtualEntry ( config , 'unit-test-runner' , `
116
+ // VIRTUAL ENTRY START
117
+ const context = require.context(
118
+ "~/",
119
+ /* deep: */ true,
120
+ /* filter: */ ${ filesRegex }
121
+ );
122
+ global.registerWebpackModules(context);
123
+ // VIRTUAL ENTRY END
124
+ ` ) ;
125
+
126
+ // config.entryPoints.clear()
127
+ config . entry ( 'bundle' )
128
+ . clear ( )
129
+ . add ( '@nativescript/core/globals/index.js' )
130
+ . add ( '@nativescript/core/bundle-entry-points' )
131
+ . add ( '@nativescript/unit-test-runner/app/bundle-app' )
132
+ // .add('@nativescript/unit-test-runner/app/entry')
133
+ . add ( entryPath ) ;
134
+ if ( webpack . Utils . platform . getPlatformName ( ) === 'android' ) {
135
+ config . entry ( 'bundle' )
136
+ . add ( '@nativescript/core/ui/frame' )
137
+ . add ( '@nativescript/core/ui/frame/activity' ) ;
138
+ }
139
+ }
0 commit comments