Skip to content

Commit 5b3e5de

Browse files
committed
add webpack config option support
1 parent d51be8c commit 5b3e5de

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

src/renderer/compiler.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,38 @@ const vm = require('vm');
77
const webpack = require('webpack');
88
const nodeVersion = require('node-version');
99
const nodeExternals = require('webpack-node-externals');
10+
const webpackMerge = require('webpack-merge');
1011

1112
const cacheMap: Map<string, any> = new Map();
1213
const compilingWaitingQueueMap: Map<string, Array<{
1314
resolve: (result: any) => void,
1415
reject: (e: any) => void
1516
}>> = new Map();
1617

18+
const defaultOptions: CompilerOptions = {
19+
basePath: __dirname,
20+
watch: false,
21+
global: Object.create(null),
22+
config: Object.create(null),
23+
outputPath: '/tmp/vue_ssr',
24+
};
25+
26+
/**
27+
* Compiler Class
28+
*
29+
* @class Compiler
30+
* @implements {ICompiler}
31+
*/
1732
class Compiler implements ICompiler {
33+
static cacheMap: Map<string, any>;
1834
fs: FileSystem;
19-
basePath: string;
20-
watch: boolean;
21-
contextGlobal: ?Object;
35+
options: CompilerOptions;
2236
constructor(fs: FileSystem, options?: CompilerOptions) {
37+
this.options = Object.assign({}, defaultOptions, options);
2338
this.fs = fs;
24-
this.basePath = options ? options.basePath : __dirname;
25-
this.watch = options ? options.watch : false;
26-
this.contextGlobal = options ? options.global : Object.create(null);
39+
delete this.options.config.output;
2740
}
41+
2842
/**
2943
* dynamic import
3044
* e.g.
@@ -35,8 +49,8 @@ class Compiler implements ICompiler {
3549
* @memberof Compiler
3650
*/
3751
import(request: string): Promise<any> {
38-
if (cacheMap.has(request)) {
39-
return Promise.resolve(cacheMap.get(request));
52+
if (Compiler.cacheMap.has(request)) {
53+
return Promise.resolve(Compiler.cacheMap.get(request));
4054
}
4155
const compilingWaitingQueue = compilingWaitingQueueMap.get(request);
4256
if (compilingWaitingQueue) {
@@ -63,7 +77,9 @@ class Compiler implements ICompiler {
6377
const webpackConfig = this.getConfig(fileMap);
6478
const serverCompiler = webpack(webpackConfig);
6579
serverCompiler.outputFileSystem = this.fs;
66-
const runner = this.watch ? cb => serverCompiler.watch({}, cb) : cb => serverCompiler.run(cb);
80+
const runner = this.options.watch
81+
? cb => serverCompiler.watch({}, cb)
82+
: cb => serverCompiler.run(cb);
6783
return new Promise((resolve, reject) => {
6884
runner((error, stats) => {
6985
if (error) {
@@ -99,7 +115,7 @@ class Compiler implements ICompiler {
99115
return this.compile(filePaths).then(() => Promise.all(filePaths.map(filePath =>
100116
new Promise((resolve, reject) => {
101117
const fileName = Compiler.getFileNameByPath(filePath);
102-
this.fs.readFile(`/temp/vue_ssr/${fileName}.js`, (error, data) => {
118+
this.fs.readFile(path.normalize(`${this.options.outputPath}/${fileName}.js`), (error, data) => {
103119
const compilingWaitingQueue = compilingWaitingQueueMap.get(filePath);
104120
if (error) {
105121
if (compilingWaitingQueue) {
@@ -110,7 +126,7 @@ class Compiler implements ICompiler {
110126
}
111127

112128
const object = this.getObject(data.toString());
113-
cacheMap.set(filePath, object);
129+
Compiler.cacheMap.set(filePath, object);
114130
if (compilingWaitingQueue) {
115131
compilingWaitingQueue.forEach(callback => callback.resolve(object));
116132
}
@@ -127,7 +143,7 @@ class Compiler implements ICompiler {
127143
* @memberof Compiler
128144
*/
129145
getObject(sourceFile: string): any {
130-
const sandboxGlobal = Object.assign({}, global, { module, require }, this.contextGlobal);
146+
const sandboxGlobal = Object.assign({}, global, { module, require }, this.options.global);
131147
const sandbox = vm.createContext(sandboxGlobal);
132148
return vm.runInContext(sourceFile, sandbox);
133149
}
@@ -144,11 +160,11 @@ class Compiler implements ICompiler {
144160
[...fileMap.entries()].forEach(([fileName, filePath]) => {
145161
entry[fileName] = [filePath];
146162
});
147-
const config = {
163+
const defaultConfig = {
148164
entry,
149165
target: 'node',
150166
output: {
151-
path: '/temp/vue_ssr',
167+
path: this.options.outputPath,
152168
filename: '[name].js',
153169
libraryTarget: 'commonjs2',
154170
},
@@ -190,25 +206,19 @@ class Compiler implements ICompiler {
190206
babelrc: false,
191207
},
192208
},
193-
},
194-
{
195-
test: /(?!.*\.js|.*\.vue)^.*$/,
196-
use: {
197-
loader: 'null-loader',
198-
},
199209
}],
200210
},
201211
externals: [nodeExternals()],
202-
context: this.basePath,
212+
context: this.options.basePath,
203213
plugins: [
204214
new webpack.DefinePlugin({
205215
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
206216
'process.env.VUE_ENV': '"server"',
207217
}),
208218
],
209219
};
210-
config.entry = entry;
211-
return config;
220+
221+
return webpackMerge(defaultConfig, this.options.config);
212222
}
213223
/**
214224
* get file name by path
@@ -219,7 +229,11 @@ class Compiler implements ICompiler {
219229
* @memberof Compiler
220230
*/
221231
static getFileNameByPath(filePath: string): string {
222-
return filePath.split(path.sep).join('_').replace(':', '_');
232+
const pathHexStr: string = (new Buffer(filePath)).toString('hex');
233+
return `${path.basename(filePath)}.${pathHexStr}`;
223234
}
224235
}
236+
237+
Compiler.cacheMap = cacheMap;
238+
225239
module.exports = Compiler;

0 commit comments

Comments
 (0)