-
Notifications
You must be signed in to change notification settings - Fork 0
/
patchImportParserPlugin.js
116 lines (100 loc) · 4.84 KB
/
patchImportParserPlugin.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import { createHash } from 'crypto';
import requireFromNextDeps from './requireFromNextDeps';
const ContextDependencyHelpers = requireFromNextDeps('webpack/lib/dependencies/ContextDependencyHelpers');
const ImportDependenciesBlock = requireFromNextDeps('webpack/lib/dependencies/ImportDependenciesBlock');
const ImportEagerContextDependency = requireFromNextDeps('webpack/lib/dependencies/ImportEagerContextDependency');
const ImportEagerDependency = requireFromNextDeps('webpack/lib/dependencies/ImportEagerDependency');
const ImportLazyContextDependency = requireFromNextDeps('webpack/lib/dependencies/ImportLazyContextDependency');
const ImportLazyOnceContextDependency = requireFromNextDeps('webpack/lib/dependencies/ImportLazyOnceContextDependency');
const ImportParserPlugin = requireFromNextDeps('webpack/lib/dependencies/ImportParserPlugin');
const ImportWeakContextDependency = requireFromNextDeps('webpack/lib/dependencies/ImportWeakContextDependency');
const ImportWeakDependency = requireFromNextDeps('webpack/lib/dependencies/ImportWeakDependency');
const UnsupportedFeatureWarning = requireFromNextDeps('webpack/lib/UnsupportedFeatureWarning');
function getHashForResource(parser) {
return createHash('md5').update(parser.state.module.resource).digest('hex').slice(0, 20);
}
function getChunkNameFromPath(parser, path) {
const cleanPath = path
.replace(/^__cwd\//, '')
.replace(/[^\w]/g, '-');
return `chunks/${cleanPath}-${getHashForResource(parser)}`;
}
function getChunkNameForContext(parser, ) {
return `chunks/[request]-${getHashForResource(parser)}`;
}
function apply(parser) {
const options = this.options;
parser.plugin(['call System.import', 'import-call'], (expr) => {
if (expr.arguments.length !== 1) {
throw new Error("Incorrect number of arguments provided to 'import(module: string) -> Promise'.");
}
const param = parser.evaluateExpression(expr.arguments[0]);
let chunkName = null;
let mode = 'lazy';
const importOptions = parser.getCommentOptions(expr.range);
if (importOptions) {
if (typeof importOptions.webpackChunkName !== 'undefined') {
if (typeof importOptions.webpackChunkName !== 'string') {
parser.state.module.warnings.push(new UnsupportedFeatureWarning(parser.state.module, `\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`));
} else {
chunkName = importOptions.webpackChunkName;
}
}
if (typeof importOptions.webpackMode !== 'undefined') {
if (typeof importOptions.webpackMode !== 'string') {
parser.state.module.warnings.push(new UnsupportedFeatureWarning(parser.state.module, `\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`));
} else {
mode = importOptions.webpackMode;
}
}
}
if (chunkName === null) {
if (param.isString()) {
chunkName = getChunkNameFromPath(parser, param.string);
} else {
chunkName = getChunkNameForContext(parser, );
}
}
if (param.isString()) {
if (mode !== 'lazy' && mode !== 'eager' && mode !== 'weak') {
parser.state.module.warnings.push(new UnsupportedFeatureWarning(parser.state.module, `\`webpackMode\` expected 'lazy', 'eager' or 'weak', but received: ${mode}.`));
}
if (mode === 'eager') {
const dep = new ImportEagerDependency(param.string, expr.range);
parser.state.current.addDependency(dep);
} else if (mode === 'weak') {
const dep = new ImportWeakDependency(param.string, expr.range);
parser.state.current.addDependency(dep);
} else {
const depBlock = new ImportDependenciesBlock(param.string, expr.range, chunkName, parser.state.module, expr.loc);
parser.state.current.addBlock(depBlock);
}
return true;
} else {
if (mode !== 'lazy' && mode !== 'lazy-once' && mode !== 'eager' && mode !== 'weak') {
parser.state.module.warnings.push(new UnsupportedFeatureWarning(parser.state.module, `\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`));
}
let Dep = ImportLazyContextDependency;
if (mode === 'eager') {
Dep = ImportEagerContextDependency;
} else if (mode === 'weak') {
Dep = ImportWeakContextDependency;
} else if (mode === 'lazy-once') {
Dep = ImportLazyOnceContextDependency;
}
const dep = ContextDependencyHelpers.create(Dep, expr.range, param, expr, options, chunkName);
if (!dep) return;
dep.loc = expr.loc;
dep.optional = !!parser.scope.inTry;
parser.state.current.addDependency(dep);
return true;
}
});
}
export default function patchImportParserPlugin() {
ImportParserPlugin.prototype.apply = apply;
}