forked from vercel/next.js
/
index.ts
153 lines (139 loc) · 4.45 KB
/
index.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Compiler, compilation } from 'webpack'
import {
IConformanceTestResult,
IWebpackConformanceTest,
IConformanceAnomaly,
IGetAstNodeResult,
NodeInspector,
} from './TestInterface'
import { NodePath } from 'ast-types/lib/node-path'
import { visit } from 'recast'
export { MinificationConformanceCheck } from './checks/minification-conformance-check'
// export { ReactSyncScriptsConformanceTest } from './tests/react-sync-scripts-conformance';
export interface IWebpackConformancePluginOptions {
tests: IWebpackConformanceTest[]
}
interface VisitorMap {
[key: string]: (path: NodePath) => void
}
export default class WebpackConformancePlugin {
private tests: IWebpackConformanceTest[]
private errors: Array<IConformanceAnomaly>
private warnings: Array<IConformanceAnomaly>
private compiler?: Compiler
constructor(options: IWebpackConformancePluginOptions) {
this.tests = []
if (options.tests) {
this.tests.push(...options.tests)
}
this.errors = []
this.warnings = []
}
private gatherResults(results: Array<IConformanceTestResult>): void {
results.forEach(result => {
if (result.result === 'FAILED') {
result.errors && this.errors.push(...result.errors)
result.warnings && this.warnings.push(...result.warnings)
}
})
}
private buildStartedHandler = (
compilation: compilation.Compilation,
callback: () => void
) => {
const buildStartedResults: IConformanceTestResult[] = this.tests.map(
test => {
if (test.buildStared && this.compiler) {
return test.buildStared(this.compiler.options)
}
return {
result: 'SUCCESS',
} as IConformanceTestResult
}
)
Promise.all(buildStartedResults).then(
(results: Array<IConformanceTestResult>) => {
this.gatherResults(results)
}
)
callback()
}
private buildCompletedHandler = (
compilation: compilation.Compilation,
cb: () => void
): void => {
const buildCompletedResults: IConformanceTestResult[] = this.tests.map(
test => {
if (test.buildCompleted) {
return test.buildCompleted(compilation.assets)
}
return {
result: 'SUCCESS',
} as IConformanceTestResult
}
)
this.gatherResults(buildCompletedResults)
compilation.errors.push(...this.errors)
compilation.warnings.push(...this.warnings)
cb()
}
private parserHandler = (factory: compilation.NormalModuleFactory): void => {
const JS_TYPES = ['auto', 'esm', 'dynamic']
const collectedVisitors: Map<string, [NodeInspector?]> = new Map()
// Collect all intereseted visitors from all tests.
this.tests.forEach(test => {
if (test.getAstNode) {
const getAstNodeCallbacks: IGetAstNodeResult[] = test.getAstNode()
getAstNodeCallbacks.forEach(result => {
if (!collectedVisitors.has(result.visitor)) {
collectedVisitors.set(result.visitor, [])
}
// @ts-ignore
collectedVisitors.get(result.visitor).push(result.inspectNode)
})
}
})
// Do an extra walk per module and add interested visitors to the walk.
for (const type of JS_TYPES) {
factory.hooks.parser
.for('javascript/' + type)
.tap(this.constructor.name, parser => {
parser.hooks.program.tap(this.constructor.name, (ast: any) => {
const visitors: VisitorMap = {}
const that = this
for (const visitorKey of collectedVisitors.keys()) {
visitors[visitorKey] = function(path: NodePath) {
const callbacks = collectedVisitors.get(visitorKey) || []
callbacks.forEach(cb => {
if (!cb) {
return
}
const { request } = parser.state.module
const outcome = cb(path, { request })
that.gatherResults([outcome])
})
this.traverse(path)
return false
}
}
visit(ast, visitors)
})
})
}
}
public apply(compiler: Compiler) {
this.compiler = compiler
compiler.hooks.make.tapAsync(
this.constructor.name,
this.buildStartedHandler
)
compiler.hooks.emit.tapAsync(
this.constructor.name,
this.buildCompletedHandler
)
compiler.hooks.normalModuleFactory.tap(
this.constructor.name,
this.parserHandler
)
}
}