1
- var webpack = require ( "webpack" ) ;
2
- var ConcatSource = require ( "webpack/lib/ConcatSource" ) ;
3
- var shelljs = require ( "shelljs" ) ;
4
- var path = require ( "path" ) ;
1
+ var sources = require ( "webpack-sources" ) ;
5
2
var fs = require ( "fs" ) ;
6
-
7
- var tnsPackage = "tns-core-modules" ;
8
- var tnsModulesDir = path . join ( "node_modules" , tnsPackage ) ;
9
-
10
- var platform = process . env . PLATFORM ;
11
- var platformOutDir = process . env . PLATFORM_DIR ;
12
-
13
- exports . readPackageJson = function ( dir ) {
14
- var packageJson = path . join ( dir , "package.json" ) ;
15
- if ( shelljs . test ( "-f" , packageJson ) ) {
16
- return JSON . parse ( shelljs . cat ( packageJson ) ) ;
17
- } else {
18
- return { } ;
19
- }
20
- } ;
21
-
22
- exports . getPackageMain = function ( packageDir ) {
23
- if ( shelljs . test ( "-f" , packageDir + ".js" ) ) {
24
- return packageDir + ".js" ;
25
- }
26
-
27
- var data = exports . readPackageJson ( packageDir ) ;
28
- if ( data . main ) {
29
- var main = data . main ;
30
- if ( / \. j s $ / i. test ( main ) ) {
31
- return path . join ( packageDir , main ) ;
32
- } else {
33
- return path . join ( packageDir , main + ".js" ) ;
34
- }
35
- } else {
36
- var indexPath = path . join ( packageDir , "index.js" ) ;
37
- if ( shelljs . test ( "-f" , indexPath ) ) {
38
- return path . join ( packageDir , "index.js" ) ;
39
- } else {
40
- throw new Error ( "Main module not found for: " + packageDir ) ;
41
- }
42
- }
43
- } ;
44
-
45
- exports . writePackageJson = function writePackageJson ( dir , data ) {
46
- var packageJson = path . join ( dir , "package.json" ) ;
47
- fs . writeFileSync ( packageJson , JSON . stringify ( data , null , 4 ) , 'utf8' ) ;
48
- } ;
49
-
50
- function endWithJs ( fileName ) {
51
- if ( / \. j s $ / i. test ( fileName ) ) {
52
- return fileName ;
53
- } else {
54
- return fileName + ".js" ;
55
- }
56
- }
57
-
58
- exports . getEntryPoint = function getEntryPoint ( appDir ) {
59
- var packageJson = exports . readPackageJson ( appDir ) ;
60
- var entryModule = null ;
61
- if ( packageJson . bundleMain ) {
62
- entryModule = endWithJs ( packageJson . bundleMain ) ;
63
- } else {
64
- entryModule = exports . getPackageMain ( appDir ) ;
65
- }
66
-
67
- // Strip leading dir name and return just the submodule.
68
- return entryModule . replace ( / ^ [ ^ \\ \/ ] + [ \\ \/ ] / , "./" ) ;
69
- } ;
70
-
71
- exports . getBundleDestination = function getBundleDestination ( appDir ) {
72
- var packageJson = exports . readPackageJson ( appDir ) ;
73
- var bundleOutput = "bundle.js" ;
74
- if ( packageJson . bundleOutput ) {
75
- bundleOutput = packageJson . bundleOutput ;
76
- }
77
- return path . join ( platformOutDir , appDir , bundleOutput ) ;
78
- } ;
3
+ var path = require ( "path" ) ;
79
4
80
5
//HACK: changes the JSONP chunk eval function to `global["nativescriptJsonp"]`
81
6
// applied to tns-java-classes.js only
82
- function FixJsonpPlugin ( options ) {
7
+ exports . NativeScriptJsonpPlugin = function ( options ) {
83
8
}
84
9
85
- FixJsonpPlugin . prototype . apply = function ( compiler ) {
86
- compiler . plugin ( 'compilation' , function ( compilation , params ) {
87
- compilation . plugin ( "optimize-chunk-assets" , function ( chunks , callback ) {
88
- chunks . forEach ( function ( chunk ) {
89
- chunk . files . forEach ( function ( file ) {
90
- if ( file === "tns-java-classes .js" ) {
10
+ exports . NativeScriptJsonpPlugin . prototype . apply = function ( compiler ) {
11
+ compiler . plugin ( 'compilation' , function ( compilation , params ) {
12
+ compilation . plugin ( "optimize-chunk-assets" , function ( chunks , callback ) {
13
+ chunks . forEach ( function ( chunk ) {
14
+ chunk . files . forEach ( function ( file ) {
15
+ if ( file === "vendor .js" ) {
91
16
var src = compilation . assets [ file ] ;
92
17
var code = src . source ( ) ;
93
18
var match = code . match ( / w i n d o w \[ " n a t i v e s c r i p t J s o n p " \] / ) ;
94
19
if ( match ) {
95
- compilation . assets [ file ] = new ConcatSource ( code . replace ( / w i n d o w \[ " n a t i v e s c r i p t J s o n p " \] / g, "global[\"nativescriptJsonp\"]" ) ) ;
20
+ compilation . assets [ file ] = new sources . ConcatSource ( code . replace ( / w i n d o w \[ " n a t i v e s c r i p t J s o n p " \] / g, "global[\"nativescriptJsonp\"]" ) ) ;
96
21
}
97
22
}
98
23
} ) ;
@@ -102,66 +27,35 @@ FixJsonpPlugin.prototype.apply = function(compiler) {
102
27
} ) ;
103
28
} ;
104
29
30
+ exports . GenerateBundleStarterPlugin = function ( bundles ) {
31
+ this . bundles = bundles ;
32
+ }
105
33
106
- exports . getConfig = function getConfig ( userDefined ) {
107
- var platformOutDir = process . env . PLATFORM_DIR ;
108
- var appOutDir = path . join ( platformOutDir , "app" ) ;
109
-
110
- if ( ! userDefined . context ) {
111
- userDefined . context = path . resolve ( "./app" ) ;
112
- }
113
- if ( ! userDefined . entry ) {
114
- userDefined . entry = {
115
- "bundle" : exports . getEntryPoint ( "./app" ) ,
116
- } ;
117
- if ( platform === "android" ) {
118
- userDefined . entry [ "tns-java-classes" ] = "./tns-java-classes" ;
119
- }
120
- }
121
- if ( ! userDefined . output ) {
122
- userDefined . output = {
123
- pathinfo : true ,
124
- path : appOutDir ,
125
- libraryTarget : "commonjs2" ,
126
- filename : "[name].js" ,
127
- jsonpFunction : "nativescriptJsonp" ,
128
- } ;
129
- }
130
- if ( ! userDefined . resolve ) {
131
- userDefined . resolve = { } ;
132
- }
133
- if ( ! userDefined . resolve . extensions ) {
134
- userDefined . resolve . extensions = [ ] ;
135
- }
136
- userDefined . resolve . extensions . push ( "" ) ;
137
- userDefined . resolve . extensions . push ( ".js" ) ;
138
- userDefined . resolve . extensions . push ( "." + platform + ".js" ) ;
139
-
140
- if ( ! userDefined . resolve . modulesDirectories ) {
141
- userDefined . resolve . modulesDirectories = [ ] ;
142
- }
143
- userDefined . resolve . modulesDirectories . push ( "node_modules/tns-core-modules" ) ;
144
- userDefined . resolve . modulesDirectories . push ( "node_modules" ) ;
34
+ exports . GenerateBundleStarterPlugin . prototype = {
35
+ apply : function ( compiler ) {
36
+ var plugin = this ;
37
+ plugin . webpackContext = compiler . options . context ;
145
38
146
- if ( ! userDefined . plugins ) {
147
- userDefined . plugins = [ ] ;
148
- }
149
- if ( ! userDefined . resolverPlugins ) {
150
- userDefined . resolverPlugins = [ ] ;
151
- }
152
- userDefined . plugins . push (
153
- new webpack . DefinePlugin ( {
154
- global : 'global' ,
155
- __dirname : '__dirname' ,
156
- "global.TNS_WEBPACK" : 'true' ,
157
- } )
158
- ) ;
159
- if ( platform === "android" ) {
160
- userDefined . plugins . push ( new webpack . optimize . CommonsChunkPlugin (
161
- "tns-java-classes" , "tns-java-classes.js" , Infinity ) ) ;
162
- }
39
+ compiler . plugin ( 'emit' , function ( compilation , cb ) {
40
+ console . log ( " GenerateBundleStarterPlugin: " + plugin . webpackContext ) ;
163
41
164
- userDefined . plugins . push ( new FixJsonpPlugin ( ) ) ;
42
+ compilation . assets [ "package.json" ] = plugin . generatePackageJson ( ) ;
43
+ compilation . assets [ "starter.js" ] = plugin . generateStarterModule ( ) ;
165
44
166
- return userDefined ;
45
+ cb ( ) ;
46
+ } ) ;
47
+ } ,
48
+ generatePackageJson : function ( ) {
49
+ var packageJsonPath = path . join ( this . webpackContext , "package.json" ) ;
50
+ var packageData = JSON . parse ( fs . readFileSync ( packageJsonPath , "utf8" ) ) ;
51
+ packageData . main = "starter" ;
52
+
53
+ return new sources . RawSource ( JSON . stringify ( packageData , null , 4 ) ) ;
54
+ } ,
55
+ generateStarterModule : function ( ) {
56
+ var moduleSource = this . bundles . map ( function ( bundle ) {
57
+ return "require(\"" + bundle + "\");" ;
58
+ } ) . join ( "\n" ) ;
59
+ return new sources . RawSource ( moduleSource ) ;
60
+ } ,
167
61
} ;
0 commit comments