-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile_in_memfs.js
133 lines (114 loc) · 4.24 KB
/
compile_in_memfs.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// puppeteer run js
// https://xiday.com/2019/09/21/puppeteer-run-js/
// npm install --save-dev webpack memfs @babel/core @babel/preset-env babel-loader typescript ts-loader
const webpack = require('webpack');
const memfs = require('memfs');
const fs = require('fs');
const path = require("path");
// List all files in a directory in Node.js recursively in a synchronous fashion
// https://gist.github.com/kethinov/6658166
const getFilePaths = (folderPath) => {
const entryPaths = fs.readdirSync(folderPath).map(entry => path.join(folderPath, entry));
const filePaths = entryPaths.filter(entryPath => fs.statSync(entryPath).isFile());
const dirPaths = entryPaths.filter(entryPath => !filePaths.includes(entryPath));
const dirFiles = dirPaths.reduce((prev, curr) => prev.concat(getFilePaths(curr)), []);
return [...filePaths, ...dirFiles];
};
const getMemfsFilePaths = (folderPath) => {
const entryPaths = memfs.readdirSync(folderPath).map(entry => path.join(folderPath, entry));
const filePaths = entryPaths.filter(entryPath => memfs.statSync(entryPath).isFile());
const dirPaths = entryPaths.filter(entryPath => !filePaths.includes(entryPath));
const dirFiles = dirPaths.reduce((prev, curr) => prev.concat(getMemfsFilePaths(curr)), []);
return [...filePaths, ...dirFiles];
};
const compileJavascript = file => {
const compiler = webpack({
mode: 'production',
// mode: 'development',
// devtool: 'inline-source-map',
entry: require.resolve(file),
output: {
filename: 'bundle.js',
path: '/build',
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
},
],
},
});
// direct webpack to output to memfs rather than to disk
compiler.outputFileSystem = memfs;
return new Promise((resolve, reject) => {
compiler.run(error => {
if (error) {
reject(error);
return;
}
const content = memfs.readFileSync('/build/bundle.js');
resolve(content.toString());
});
});
}
const compileTypescript = file => {
// create a directory structure in memfs that matches the real filesystem
// const rootDir = __dirname;
// const entryBase = path.parse(file).base; // compile_test_ts.ts
// const entryName = path.parse(file).name; // compile_test_ts
// const entryExt = path.parse(file).ext; // .ts
// const entry = path.join(rootDir, entryBase);
// const outputPath = path.join(rootDir, 'dist');
// const outputName = entryName + ".js";
// const outputFilename = path.join(outputPath, outputName);
// const rootExists = memfs.existsSync(rootDir);
// if (!rootExists) {
// memfs.mkdirpSync(rootDir);
// }
// const code = fs.readFileSync(entry);
// memfs.writeFileSync(entry, code);
const compiler = webpack({
mode: 'production',
// mode: 'development',
// devtool: 'inline-source-map',
// entry: entry,
// output: {
// filename: outputName,
// path: outputPath,
// },
entry: require.resolve(file),
output: {
filename: 'bundle.js',
path: '/build',
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'ts-loader',
},
],
},
});
// direct webpack to use memfs for file input
// compiler.inputFileSystem = memfs;
// direct webpack to output to memfs rather than to disk
compiler.outputFileSystem = memfs;
return new Promise((resolve, reject) => {
compiler.run(error => {
if (error) {
reject(error);
return;
}
//console.log(getMemfsFilePaths('/'));
const content = memfs.readFileSync('/build/bundle.js');
resolve(content.toString());
});
});
}
exports.compileJavascript = compileJavascript;
exports.compileTypescript = compileTypescript;