1
1
const { resolve, join } = require ( "path" ) ;
2
2
const { closeSync, openSync } = require ( "fs" ) ;
3
+ const validateOptions = require ( "schema-utils" ) ;
3
4
4
- const ProjectSnapshotGenerator = require ( "../snapshot/android/project-snapshot-generator" ) ;
5
- const { resolveAndroidAppPath } = require ( "../projectHelpers" ) ;
5
+ const ProjectSnapshotGenerator = require ( "../../snapshot/android/project-snapshot-generator" ) ;
6
+ const { resolveAndroidAppPath } = require ( "../../projectHelpers" ) ;
7
+ const schema = require ( "./options.json" ) ;
6
8
7
9
exports . NativeScriptSnapshotPlugin = ( function ( ) {
8
10
function NativeScriptSnapshotPlugin ( options ) {
9
- ProjectSnapshotGenerator . call ( this , options ) ; // Call the parent constructor
10
-
11
- if ( ! this . options . chunk ) {
12
- throw new Error ( "No chunk specified." ) ;
13
- }
11
+ NativeScriptSnapshotPlugin . validateSchema ( options ) ;
14
12
15
- console . dir ( )
13
+ ProjectSnapshotGenerator . call ( this , options ) ; // Call the parent constructor
16
14
17
15
if ( this . options . webpackConfig ) {
18
16
if ( this . options . webpackConfig . output && this . options . webpackConfig . output . libraryTarget ) {
@@ -29,15 +27,28 @@ exports.NativeScriptSnapshotPlugin = (function() {
29
27
}
30
28
}
31
29
30
+ NativeScriptSnapshotPlugin . validateSchema = function ( options ) {
31
+ if ( ! options . chunk ) {
32
+ const error = NativeScriptSnapshotPlugin . extendError ( { message : `No chunk specified!` } ) ;
33
+ throw error ;
34
+ }
35
+
36
+ try {
37
+ validateOptions ( schema , options , "NativeScriptSnapshotPlugin" ) ;
38
+ } catch ( error ) {
39
+ throw new Error ( error . message ) ;
40
+ }
41
+ }
42
+
32
43
// inherit ProjectSnapshotGenerator
33
44
NativeScriptSnapshotPlugin . prototype = Object . create ( ProjectSnapshotGenerator . prototype ) ;
34
45
NativeScriptSnapshotPlugin . prototype . constructor = NativeScriptSnapshotPlugin ;
35
46
36
- NativeScriptSnapshotPlugin . prototype . getTnsJavaClassesBuildPath = function ( ) {
47
+ NativeScriptSnapshotPlugin . prototype . getTnsJavaClassesBuildPath = function ( ) {
37
48
return resolve ( this . getBuildPath ( ) , "../tns-java-classes.js" ) ;
38
49
}
39
50
40
- NativeScriptSnapshotPlugin . prototype . generate = function ( webpackChunk ) {
51
+ NativeScriptSnapshotPlugin . prototype . generate = function ( webpackChunk ) {
41
52
const options = this . options ;
42
53
43
54
const inputFile = join ( options . webpackConfig . output . path , webpackChunk . files [ 0 ] ) ;
@@ -62,7 +73,7 @@ exports.NativeScriptSnapshotPlugin = (function() {
62
73
} ) ;
63
74
}
64
75
65
- NativeScriptSnapshotPlugin . prototype . apply = function ( compiler ) {
76
+ NativeScriptSnapshotPlugin . prototype . apply = function ( compiler ) {
66
77
const options = this . options ;
67
78
68
79
// Generate tns-java-classes.js file
@@ -73,26 +84,38 @@ exports.NativeScriptSnapshotPlugin = (function() {
73
84
} ) ;
74
85
75
86
// Generate snapshots
76
- compiler . plugin ( "after-emit" , function ( compilation , callback ) {
87
+ compiler . plugin ( "after-emit" , function ( compilation , callback ) {
77
88
debugger ;
78
89
const chunkToSnapshot = compilation . chunks . find ( chunk => chunk . name == options . chunk ) ;
79
90
if ( ! chunkToSnapshot ) {
80
- throw new Error ( `No chunk named '${ options . chunk } ' found.` ) ;
91
+ const error = NativeScriptSnapshotPlugin . extendError ( { message : `No chunk named '${ options . chunk } ' found.` } ) ;
92
+ compilation . errors . push ( error ) ;
93
+ return callback ( ) ;
81
94
}
82
95
83
96
this . generate ( chunkToSnapshot )
84
97
. then ( ( ) => {
85
98
console . log ( "Successfully generated snapshots!" ) ;
86
- callback ( ) ;
99
+ return callback ( ) ;
87
100
} )
88
101
. catch ( ( error ) => {
89
- console . error ( "Snapshot generation failed with the following error:" ) ;
90
- console . error ( error ) ;
91
- callback ( ) ;
102
+ const extendedError = NativeScriptSnapshotPlugin . extendError ( { originalError : error } ) ;
103
+ compilation . errors . push ( extendedError ) ;
104
+ return callback ( ) ;
92
105
} ) ;
93
-
94
106
} . bind ( this ) ) ;
95
107
}
96
108
109
+ NativeScriptSnapshotPlugin . extendError = function ( { originalError, message } = { } ) {
110
+ const header = `NativeScriptSnapshot. Snapshot generation failed!\n` ;
111
+ if ( originalError ) {
112
+ originalError . message = `${ header } ${ originalError . message } ` ;
113
+ return originalError ;
114
+ }
115
+
116
+ const newMessage = message ? `${ header } ${ message } ` : header ;
117
+ return new Error ( newMessage ) ;
118
+ } ;
119
+
97
120
return NativeScriptSnapshotPlugin ;
98
121
} ) ( ) ;
0 commit comments