This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
helpers.js
95 lines (83 loc) · 2.09 KB
/
helpers.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
import path from 'path';
import MemoryFileSystem from 'memory-fs'; // eslint-disable-line import/no-extraneous-dependencies
import webpack from 'webpack';
export class PluginEnvironment {
constructor() {
this.events = [];
}
getEnvironmentStub() {
return {
plugin: (name, handler) => {
this.events.push({
name,
handler,
});
},
};
}
getEventBindings() {
return this.events;
}
}
export function compile(compiler) {
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
return reject(err);
}
return resolve(stats);
});
});
}
export function createCompiler(options = {}) {
const compiler = webpack(
Array.isArray(options)
? options
: {
mode: 'production',
bail: true,
cache: false,
entry: `${__dirname}/fixtures/entry.js`,
optimization: {
minimize: false,
},
output: {
pathinfo: false,
path: `${__dirname}/dist`,
filename: '[name].[chunkhash].js',
chunkFilename: '[id].[name].[chunkhash].js',
},
plugins: [],
...options,
}
);
compiler.outputFileSystem = new MemoryFileSystem();
return compiler;
}
export function countPlugins({ hooks }) {
return Object.keys(hooks).reduce((aggregate, name) => {
// eslint-disable-next-line no-param-reassign
aggregate[name] = Array.isArray(hooks[name].taps)
? hooks[name].taps.length
: 0;
return aggregate;
}, {});
}
export function removeCWD(str) {
return str.split(`${process.cwd()}/`).join('');
}
export function normalizeSourceMap(source) {
if (source.map && source.map.sources) {
// eslint-disable-next-line no-param-reassign
source.map.sources = source.map.sources.map((sourceFromMap) =>
path.relative(process.cwd(), sourceFromMap).replace(/\\/g, '/')
);
}
return source;
}
export function cleanErrorStack(error) {
return removeCWD(error.toString())
.split('\n')
.slice(0, 2)
.join('\n');
}