@@ -9,14 +9,11 @@ catch (e) {
9
9
const path = require ( 'path' ) ;
10
10
const process = require ( 'process' ) ;
11
11
const fs = require ( 'fs' ) ;
12
- const spawn = require ( 'child_process' ) . spawn ;
13
12
const cwd = process . cwd ( ) ;
14
13
const AdmZip = require ( 'adm-zip' ) ;
15
14
const os = require ( 'os' ) ;
16
15
const rimraf = require ( 'rimraf' ) ;
17
16
const webpack = require ( 'webpack' ) ;
18
- const esbuild = require ( 'esbuild' ) ;
19
- const ncp = require ( 'ncp' ) ;
20
17
const tmp = require ( 'tmp' ) ;
21
18
22
19
if ( fs . existsSync ( path . resolve ( cwd , 'src/main.py' ) ) ) {
@@ -44,102 +41,59 @@ if (fs.existsSync(path.resolve(cwd, 'src/main.py'))) {
44
41
zip . addLocalFolder ( zipfs , 'fs' ) ;
45
42
zip . writeZip ( path . join ( out , 'plugin.zip' ) ) ;
46
43
}
47
- else if ( false ) {
44
+ else {
48
45
const packageJson = JSON . parse ( fs . readFileSync ( path . join ( cwd , 'package.json' ) . toString ( ) ) ) ;
49
46
const optionalDependencies = Object . keys ( packageJson . optionalDependencies || { } ) ;
50
47
51
- let out ;
52
- if ( process . env . NODE_ENV == 'production' )
53
- out = path . resolve ( cwd , 'dist' ) ;
54
- else
55
- out = path . resolve ( cwd , 'out' ) ;
48
+ if ( packageJson . scrypted . babel ) {
49
+ process . env . SCRYPTED_WEBPACK_BABEL = 'true' ;
50
+ }
56
51
57
- let outfile = path . join ( out , 'main.nodejs.js' ) ;
58
-
59
- esbuild . build ( {
60
- entryPoints : [ 'src/main.ts' ] ,
61
- bundle : true ,
62
- outfile,
63
- platform : 'node' ,
64
- format : 'cjs' ,
65
- // loader: { '.json': 'file' },
66
- external : [
67
- ...Object . keys ( optionalDependencies ) ,
68
- ] ,
69
- sourcemap : true ,
70
- } )
71
- . then ( ( ) => {
72
- const zip = new AdmZip ( ) ;
73
-
74
- const readme = path . join ( cwd , 'README.md' ) ;
75
- if ( fs . existsSync ( readme ) ) {
76
- zip . addLocalFile ( readme ) ;
52
+ const defaultMainNodeJs = 'main.nodejs.js' ;
53
+ const entries = [ ] ;
54
+ if ( packageJson . exports ) {
55
+ for ( const [ key , value ] of Object . entries ( packageJson . exports ) ) {
56
+ entries . push ( {
57
+ filename : key ,
58
+ output : value ,
59
+ } ) ;
60
+ }
61
+ }
62
+ else {
63
+ for ( const search of [ 'src/main.js' , 'src/main.ts' ] ) {
64
+ const resolved = path . resolve ( cwd , search ) ;
65
+ if ( fs . existsSync ( resolved ) ) {
66
+ entries . push ( {
67
+ filename : search ,
68
+ output : defaultMainNodeJs ,
69
+ } ) ;
70
+ break ;
77
71
}
78
-
79
- zip . addLocalFile ( outfile ) ;
80
- zip . addLocalFile ( outfile + '.map' ) ;
81
-
82
- const zipfs = path . join ( cwd , 'fs' ) ;
83
- if ( fs . existsSync ( zipfs ) )
84
- zip . addLocalFolder ( zipfs , 'fs' ) ;
85
- zip . writeZip ( path . join ( out , 'plugin.zip' ) ) ;
86
- } )
87
- . catch ( e => process . nextTick ( ( ) => {
88
- console . error ( e ) ;
89
- throw new Error ( e ) ;
90
- } ) ) ;
91
- }
92
- else {
93
- var entry ;
94
- for ( var search of [ 'src/main.js' , 'src/main.ts' ] ) {
95
- var resolved = path . resolve ( cwd , search ) ;
96
- if ( fs . existsSync ( resolved ) ) {
97
- entry = resolved ;
98
- break ;
99
72
}
100
73
}
101
74
102
- const packageJson = JSON . parse ( fs . readFileSync ( path . join ( cwd , 'package.json' ) . toString ( ) ) ) ;
103
- const optionalDependencies = Object . keys ( packageJson . optionalDependencies || { } ) ;
104
-
105
- if ( packageJson . scrypted . babel ) {
106
- process . env . SCRYPTED_WEBPACK_BABEL = 'true' ;
107
- }
75
+ const nodeWebpackConfig = 'webpack.nodejs.config.js' ;
108
76
109
- const runtimes = [
110
- // {
111
- // config: 'webpack.duktape.config.js',
112
- // output: 'main.js',
113
- // },
114
- // {
115
- // config: 'webpack.quickjs.config.js',
116
- // output: 'main.quickjs.js',
117
- // },
118
- {
119
- config : 'webpack.nodejs.config.js' ,
120
- output : 'main.nodejs.js' ,
121
- } ,
122
- ] ;
123
-
124
- var out ;
77
+ let out ;
125
78
if ( process . env . NODE_ENV == 'production' )
126
79
out = path . resolve ( cwd , 'dist' ) ;
127
80
else
128
81
out = path . resolve ( cwd , 'out' ) ;
129
82
130
- if ( ! entry ) {
83
+ if ( ! entries ?. length ) {
131
84
console . warn ( 'unable to locate src/main.js or src/main.ts' ) ;
132
85
console . warn ( 'if a custom webpack config is used, will fall back to an entry configured there' ) ;
86
+ entries . push ( undefined ) ;
133
87
}
134
88
135
- var webpackCmd = path . resolve ( cwd , 'node_modules/.bin/webpack-cli' ) ;
89
+ let webpackCmd = path . resolve ( cwd , 'node_modules/.bin/webpack-cli' ) ;
136
90
if ( ! fs . existsSync ( webpackCmd ) ) {
137
91
webpackCmd = path . resolve ( cwd , 'node_modules/@scrypted/sdk/node_modules/.bin/webpack-cli' )
138
92
}
139
93
if ( os . platform ( ) . startsWith ( 'win' ) ) {
140
94
webpackCmd += '.cmd' ;
141
95
}
142
- var zip = new AdmZip ( ) ;
96
+ const zip = new AdmZip ( ) ;
143
97
144
98
const readme = path . join ( cwd , 'README.md' ) ;
145
99
if ( fs . existsSync ( readme ) ) {
@@ -162,11 +116,11 @@ else {
162
116
if ( out )
163
117
rimraf . sync ( out ) ;
164
118
165
- for ( const runtime of runtimes ) {
119
+ for ( let entry of entries ) {
166
120
await new Promise ( ( resolve , reject ) => {
167
- var webpackConfig ;
168
- var customWebpackConfig = path . resolve ( cwd , runtime . config ) ;
169
- const defaultWebpackConfig = path . resolve ( __dirname , '..' , runtime . config ) ;
121
+ let webpackConfig ;
122
+ const customWebpackConfig = path . resolve ( cwd , nodeWebpackConfig ) ;
123
+ const defaultWebpackConfig = path . resolve ( __dirname , '..' , nodeWebpackConfig ) ;
170
124
if ( fs . existsSync ( customWebpackConfig ) ) {
171
125
webpackConfig = customWebpackConfig ;
172
126
}
@@ -177,28 +131,28 @@ else {
177
131
process . env . SCRYPTED_DEFAULT_WEBPACK_CONFIG = defaultWebpackConfig ;
178
132
179
133
const config = require ( webpackConfig ) ;
180
- if ( entry ) {
181
- // a standard entrypoint was found
182
- config . entry = {
183
- main : entry ,
184
- } ;
185
- } else {
186
- // try to use an entrypoint specified in the webpack config
187
- if ( ! config ?. entry ?. main ) {
188
- console . error ( "webpack config does not supply an entry file" ) ;
189
- return 1 ;
190
- }
191
- var resolved = path . resolve ( cwd , config . entry . main ) ;
192
- if ( fs . existsSync ( resolved ) ) {
193
- config . entry . main = resolved
194
- } else {
195
- console . error ( "entry file specified in webpack config does not exist" ) ;
196
- return 1 ;
197
- }
134
+ entry ||= {
135
+ filename : config ?. entry ?. main ,
136
+ output : defaultMainNodeJs ,
137
+ } ;
138
+
139
+ if ( ! entry ?. filename ) {
140
+ console . error ( "no main.ts or main.js was found, and webpack config does not supply an entry file." ) ;
141
+ console . error ( entry ?. filename ) ;
142
+ return 1 ;
143
+ }
144
+
145
+ const main = path . resolve ( cwd , entry . filename ) ;
146
+ if ( ! fs . existsSync ( main ) ) {
147
+ console . error ( "entry file specified in webpack config does not exist" ) ;
148
+ return 1 ;
198
149
}
199
- config . output . path = out ;
200
- config . output . filename = runtime . output ;
201
150
151
+ config . entry = {
152
+ main,
153
+ } ;
154
+ config . output . path = out ;
155
+ config . output . filename = entry ?. output || defaultMainNodeJs ;
202
156
203
157
for ( const opt of optionalDependencies ) {
204
158
const t = tmp . tmpNameSync ( {
@@ -238,7 +192,7 @@ else {
238
192
} ) ;
239
193
}
240
194
241
- var zipfs = path . join ( cwd , 'fs' ) ;
195
+ const zipfs = path . join ( cwd , 'fs' ) ;
242
196
if ( fs . existsSync ( zipfs ) )
243
197
zip . addLocalFolder ( zipfs , 'fs' ) ;
244
198
zip . writeZip ( path . join ( out , 'plugin.zip' ) ) ;
0 commit comments