forked from arcanis/pnp-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (154 loc) · 5.41 KB
/
index.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"use strict";
const path = require("path");
const {resolveModuleName} = require("ts-pnp");
module.exports = localPath => {
let pnp;
if (localPath) {
try {
pnp = require(path.resolve(localPath, ".pnp.js"));
} catch (error) {
throw new Error(`${localPath} doesn't have .pnp.js`);
}
} else {
try {
pnp = require("pnpapi");
} catch (error) {
// not in PnP; not a problem
}
}
function nothing() {
// ¯\_(ツ)_/¯
}
function getModuleLocator(module) {
console.log("getModuleLocator", module.filename);
const moduleLocation = typeof module === "string" ? module : module.filename;
if (!moduleLocation) throw new Error("The specified module doesn't seem to exist on the filesystem");
const moduleLocator = pnp.findPackageLocator(moduleLocation) || (/^[./]/.test(moduleLocation) && moduleLocation);
if (!moduleLocator) throw new Error("the specified module doesn't seem to be part of the dependency tree");
return moduleLocator;
}
function getDependencyLocator(sourceLocator, name) {
const {packageDependencies} = pnp.getPackageInformation(sourceLocator);
const reference = packageDependencies.get(name);
return {name, reference};
}
function getSourceLocation(sourceLocator) {
if (!sourceLocator) return null;
const sourceInformation = pnp.getPackageInformation(sourceLocator);
if (!sourceInformation) throw new Error("Couldn't find the package to use as resolution source");
if (!sourceInformation.packageLocation)
throw new Error(
"The package to use as resolution source seem to not have been installed - maybe it's a devDependency not installed in prod?"
);
return sourceInformation.packageLocation.replace(/\/?$/, "/");
}
function makeResolver(sourceLocator, filter) {
const sourceLocation = getSourceLocation(sourceLocator);
return resolver => {
const BACKWARD_PATH = /^\.\.([\\\/]|$)/;
const resolvedHook = resolver.ensureHook("resolve");
// Prevents the SymlinkPlugin from kicking in. We need the symlinks to be preserved because that's how we deal with peer dependencies ambiguities.
resolver.getHook("file").intercept({
register: tapInfo => {
return tapInfo.name !== "SymlinkPlugin"
? tapInfo
: Object.assign({}, tapInfo, {
fn: (request, resolveContext, callback) => {
callback();
}
});
}
});
// Register a plugin that will resolve bare imports into the package location on the filesystem before leaving the rest of the resolution to Webpack
resolver.getHook("before-module").tapAsync("PnpResolver", (requestContext, resolveContext, callback) => {
let request = requestContext.request;
let issuer = requestContext.context.issuer;
// When using require.context, issuer seems to be false (cf https://github.com/webpack/webpack-dev-server/blob/d0725c98fb752d8c0b1e8c9067e526e22b5f5134/client-src/default/index.js#L94)
if (!issuer) {
issuer = `${requestContext.path}/`;
// We only support issuer when they're absolute paths. I'm not sure the opposite can ever happen, but better check here.
} else if (!path.isAbsolute(issuer)) {
throw new Error(`Cannot successfully resolve this dependency - issuer not supported (${issuer})`);
}
if (filter) {
const relative = path.relative(filter, issuer);
if (path.isAbsolute(relative) || BACKWARD_PATH.test(relative)) {
return callback(null);
}
}
let resolutionIssuer = sourceLocation || issuer;
let resolution;
try {
resolution = pnp.resolveToUnqualified(request, resolutionIssuer, {considerBuiltins: false});
} catch (error) {
return callback(error);
}
resolver.doResolve(
resolvedHook,
Object.assign({}, requestContext, {
request: resolution
}),
null,
resolveContext,
callback
);
});
};
}
let exports = pnp
? {
apply: makeResolver(null)
}
: {
apply: nothing
};
exports.makePlugin = (locator, filter) =>
pnp
? {
apply: makeResolver(locator, filter)
}
: {
apply: nothing
};
exports.moduleLoader = module =>
pnp
? {
apply: makeResolver(getModuleLocator(module))
}
: {
apply: nothing
};
exports.topLevelLoader = pnp
? {
apply: makeResolver(pnp.topLevel)
}
: {
apply: nothing
};
exports.bind = (filter, module, dependency) =>
pnp
? {
apply: makeResolver(
dependency ? getDependencyLocator(getModuleLocator(module), dependency) : getModuleLocator(module),
filter
)
}
: {
apply: nothing
};
exports.tsLoaderOptions = (options = {}) =>
pnp
? Object.assign({}, options, {
resolveModuleName: resolveModuleName,
resolveTypeReferenceDirective: resolveModuleName
})
: options;
exports.forkTsCheckerOptions = (options = {}) =>
pnp
? Object.assign({}, options, {
resolveModuleNameModule: require.resolve("./ts"),
resolveTypeReferenceDirectiveModule: require.resolve("./ts")
})
: options;
return exports;
};