Skip to content

Commit 82dfa96

Browse files
committed
sdk: support multiple entry points for faster forks
1 parent 83d3add commit 82dfa96

File tree

2 files changed

+54
-101
lines changed

2 files changed

+54
-101
lines changed

sdk/bin/scrypted-webpack.js

+54-100
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@ catch (e) {
99
const path = require('path');
1010
const process = require('process');
1111
const fs = require('fs');
12-
const spawn = require('child_process').spawn;
1312
const cwd = process.cwd();
1413
const AdmZip = require('adm-zip');
1514
const os = require('os');
1615
const rimraf = require('rimraf');
1716
const webpack = require('webpack');
18-
const esbuild = require('esbuild');
19-
const ncp = require('ncp');
2017
const tmp = require('tmp');
2118

2219
if (fs.existsSync(path.resolve(cwd, 'src/main.py'))) {
@@ -44,102 +41,59 @@ if (fs.existsSync(path.resolve(cwd, 'src/main.py'))) {
4441
zip.addLocalFolder(zipfs, 'fs');
4542
zip.writeZip(path.join(out, 'plugin.zip'));
4643
}
47-
else if (false) {
44+
else {
4845
const packageJson = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json').toString()));
4946
const optionalDependencies = Object.keys(packageJson.optionalDependencies || {});
5047

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+
}
5651

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;
7771
}
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;
9972
}
10073
}
10174

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';
10876

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;
12578
if (process.env.NODE_ENV == 'production')
12679
out = path.resolve(cwd, 'dist');
12780
else
12881
out = path.resolve(cwd, 'out');
12982

130-
if (!entry) {
83+
if (!entries?.length) {
13184
console.warn('unable to locate src/main.js or src/main.ts');
13285
console.warn('if a custom webpack config is used, will fall back to an entry configured there');
86+
entries.push(undefined);
13387
}
13488

135-
var webpackCmd = path.resolve(cwd, 'node_modules/.bin/webpack-cli');
89+
let webpackCmd = path.resolve(cwd, 'node_modules/.bin/webpack-cli');
13690
if (!fs.existsSync(webpackCmd)) {
13791
webpackCmd = path.resolve(cwd, 'node_modules/@scrypted/sdk/node_modules/.bin/webpack-cli')
13892
}
13993
if (os.platform().startsWith('win')) {
14094
webpackCmd += '.cmd';
14195
}
142-
var zip = new AdmZip();
96+
const zip = new AdmZip();
14397

14498
const readme = path.join(cwd, 'README.md');
14599
if (fs.existsSync(readme)) {
@@ -162,11 +116,11 @@ else {
162116
if (out)
163117
rimraf.sync(out);
164118

165-
for (const runtime of runtimes) {
119+
for (let entry of entries) {
166120
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);
170124
if (fs.existsSync(customWebpackConfig)) {
171125
webpackConfig = customWebpackConfig;
172126
}
@@ -177,28 +131,28 @@ else {
177131
process.env.SCRYPTED_DEFAULT_WEBPACK_CONFIG = defaultWebpackConfig;
178132

179133
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;
198149
}
199-
config.output.path = out;
200-
config.output.filename = runtime.output;
201150

151+
config.entry = {
152+
main,
153+
};
154+
config.output.path = out;
155+
config.output.filename = entry?.output || defaultMainNodeJs;
202156

203157
for (const opt of optionalDependencies) {
204158
const t = tmp.tmpNameSync({
@@ -238,7 +192,7 @@ else {
238192
});
239193
}
240194

241-
var zipfs = path.join(cwd, 'fs');
195+
const zipfs = path.join(cwd, 'fs');
242196
if (fs.existsSync(zipfs))
243197
zip.addLocalFolder(zipfs, 'fs');
244198
zip.writeZip(path.join(out, 'plugin.zip'));

sdk/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"axios": "^1.6.5",
3636
"babel-loader": "^9.1.0",
3737
"babel-plugin-const-enum": "^1.1.0",
38-
"esbuild": "^0.15.9",
3938
"ncp": "^2.0.0",
4039
"raw-loader": "^4.0.2",
4140
"rimraf": "^3.0.2",

0 commit comments

Comments
 (0)