/
createCompilerCallback.js
80 lines (56 loc) · 2.11 KB
/
createCompilerCallback.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
// @flow
import path from 'path';
import {
Compiler,
DllPlugin
} from 'webpack';
import createDebug from 'debug';
import findInstance from '../utilities/findInstance';
import createResourceMap from './createResourceMap';
const debug = createDebug('isomorphic-webpack');
export default (compiler: Compiler, callback: Function): Function => {
const dllPlugin = findInstance(compiler.options.plugins, DllPlugin);
const manifestPath = dllPlugin.options.path;
debug('manifestPath', manifestPath);
const outputFileSystem = compiler.outputFileSystem;
return (error: Object, stats: Object) => {
if (error) {
debug('compiler error:', error);
return;
}
if (stats.compilation.errors.length) {
debug('compilation error', stats.compilation.errors);
return;
}
if (stats.compilation.missingDependencies.length) {
debug('aborting compilation; missing dependencies', stats.compilation.missingDependencies);
return;
}
if (!outputFileSystem.existsSync(manifestPath)) {
throw new Error('Manifest file does not exist.');
}
const manifest = JSON.parse(outputFileSystem.readFileSync(manifestPath));
debug('manifest', manifest);
const requestMap = createResourceMap(manifest.content);
debug('requestMap', requestMap);
const files = stats.compilation.chunks[0].files.filter((file) => {
return /\.js$/.test(file);
});
const bundleName = files[files.length - 1];
debug('bundleName', bundleName);
const absoluteEntryBundleName = path.resolve(compiler.options.output.path, bundleName);
if (!outputFileSystem.existsSync(absoluteEntryBundleName)) {
throw new Error('Bundle file does not exist.');
}
if (!outputFileSystem.existsSync(absoluteEntryBundleName + '.map')) {
throw new Error('Bundle map file does not exist.');
}
const bundleCode = outputFileSystem.readFileSync(absoluteEntryBundleName, 'utf-8');
const bundleSourceMap = JSON.parse(outputFileSystem.readFileSync(absoluteEntryBundleName + '.map', 'utf-8'));
callback({
bundleCode,
bundleSourceMap,
requestMap
});
};
};