-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundler.js
executable file
·67 lines (59 loc) · 1.67 KB
/
bundler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
'use strict';
const MemFS = require('memory-fs');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const path = require('path');
const fs = require('fs');
const inputFilename = process.argv[2];
const outputFilename = inputFilename.replace(/\.js$/, '.qbundled.js');
const compiler = webpack({
entry: inputFilename,
output: {
filename: outputFilename,
path: process.cwd()
},
optimization: {
minimize: false
},
target: 'node',
externals: [nodeExternals()]
});
const memfs = new MemFS();
compiler.outputFileSystem = memfs;
compiler.run((err, stats) => {
if (err) {
throw err;
}
const outputPath = path.resolve(process.cwd(), outputFilename);
const data = memfs.readFileSync(outputPath, 'utf8');
const packageLock = require(path.join(process.cwd(), 'package-lock.json'));
const packageLockJson = JSON.stringify(cleanLock(packageLock), null, 2);
fs.writeFileSync(outputPath, `${makePreamble()}
/**package-lock ${packageLockJson} **/
${data}`);
console.log(`bundle written to ${outputPath}`);
});
function cleanLock(lockData) {
delete lockData.name;
delete lockData.version;
if (lockData.dependencies) {
for (const dep of Object.values(lockData.dependencies)) {
cleanLock(dep);
}
}
return lockData;
}
function makePreamble() {
const loader = require.resolve('qdd/qdd-loader.js');
const data = `// BEGIN qdd loader
{
${fs.readFileSync(loader, 'utf8')}
module.treeNode = getLockTree(__filename);
currentParent = module;
}
// TODO this actually needs makeRequireFunction, not just module.require
require = module.require.bind(module);
// END qdd loader`;
return data;
}