1- const { resolve, join } = require ( "path" ) ;
2- const { closeSync, openSync } = require ( "fs" ) ;
1+ const { relative , resolve, join } = require ( "path" ) ;
2+ const { closeSync, openSync, writeFileSync } = require ( "fs" ) ;
33const validateOptions = require ( "schema-utils" ) ;
44
55const ProjectSnapshotGenerator = require ( "../../snapshot/android/project-snapshot-generator" ) ;
6- const { resolveAndroidAppPath } = require ( "../../projectHelpers" ) ;
6+ const { resolveAndroidAppPath, getAndroidProjectPath } = require ( "../../projectHelpers" ) ;
77const schema = require ( "./options.json" ) ;
88
9+ const SNAPSHOT_ENTRY_NAME = "snapshot-entry" ;
10+ const SNAPSHOT_ENTRY_MODULE = `${ SNAPSHOT_ENTRY_NAME } .js` ;
11+
912exports . NativeScriptSnapshotPlugin = ( function ( ) {
1013 function NativeScriptSnapshotPlugin ( options ) {
1114 NativeScriptSnapshotPlugin . validateSchema ( options ) ;
12- if ( options . chunk ) {
13- options . chunks = options . chunks || [ ] ;
14- options . chunks . push ( options . chunk ) ;
15- }
1615
1716 ProjectSnapshotGenerator . call ( this , options ) ;
1817
19- if ( this . options . webpackConfig ) {
20- if ( this . options . webpackConfig . output && this . options . webpackConfig . output . libraryTarget ) {
21- this . options . webpackConfig . output . libraryTarget = undefined ;
22- }
18+ const { webpackConfig } = this . options ;
19+ NativeScriptSnapshotPlugin . removeLibraryTarget ( webpackConfig ) ;
20+
21+ const { entry } = webpackConfig ;
22+ if ( typeof entry === "string" || Array . isArray ( entry ) ) {
23+ webpackConfig . entry = { bundle : entry } ;
24+ }
25+
26+ NativeScriptSnapshotPlugin . ensureSnapshotModuleEntry ( this . options ) ;
27+ }
28+
29+ NativeScriptSnapshotPlugin . removeLibraryTarget = function ( webpackConfig ) {
30+ const { output } = webpackConfig ;
31+ if ( output ) {
32+ output . libraryTarget = undefined ;
2333 }
2434 }
2535
36+ NativeScriptSnapshotPlugin . ensureSnapshotModuleEntry = function ( options ) {
37+ const { webpackConfig, requireModules, chunks, projectRoot } = options ;
38+
39+ const androidProjectPath = getAndroidProjectPath ( { projectRoot : projectRoot } ) ;
40+ const snapshotEntryPath = join ( androidProjectPath , SNAPSHOT_ENTRY_MODULE ) ;
41+ const snapshotEntryContent = requireModules . map ( mod => `require('${ mod } ')` ) . join ( ";" ) ;
42+ writeFileSync ( snapshotEntryPath , snapshotEntryContent , { encoding : "utf8" } ) ;
43+
44+ // add the module to the entry points to make sure it's content is evaluated
45+ webpackConfig . entry [ SNAPSHOT_ENTRY_NAME ] = relative ( webpackConfig . context , snapshotEntryPath ) ;
46+
47+ // prepend the module to the script that will be snapshotted
48+ chunks . unshift ( SNAPSHOT_ENTRY_NAME ) ;
49+
50+ // ensure that the runtime is installed only in the snapshotted chunk
51+ webpackConfig . optimization . runtimeChunk = { name : SNAPSHOT_ENTRY_NAME } ;
52+ }
53+
2654 NativeScriptSnapshotPlugin . validateSchema = function ( options ) {
2755 if ( ! options . chunk && ! options . chunks ) {
2856 const error = NativeScriptSnapshotPlugin . extendError ( { message : `No chunks specified!` } ) ;
@@ -31,12 +59,16 @@ exports.NativeScriptSnapshotPlugin = (function() {
3159
3260 try {
3361 validateOptions ( schema , options , "NativeScriptSnapshotPlugin" ) ;
62+
63+ if ( options . chunk ) {
64+ options . chunks = options . chunks || [ ] ;
65+ options . chunks . push ( options . chunk ) ;
66+ }
3467 } catch ( error ) {
3568 throw new Error ( error . message ) ;
3669 }
3770 }
3871
39- // inherit ProjectSnapshotGenerator
4072 NativeScriptSnapshotPlugin . prototype = Object . create ( ProjectSnapshotGenerator . prototype ) ;
4173 NativeScriptSnapshotPlugin . prototype . constructor = NativeScriptSnapshotPlugin ;
4274
0 commit comments