Skip to content

Commit 75b9b7a

Browse files
fix: work with importModule (#218)
1 parent 57c66b8 commit 75b9b7a

File tree

22 files changed

+379
-30
lines changed

22 files changed

+379
-30
lines changed

.cspell.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"opencollective",
1919
"Koppers",
2020
"sokra",
21-
"lifecycles"
21+
"lifecycles",
22+
"absolutify"
2223
],
23-
2424
"ignorePaths": [
2525
"CHANGELOG.md",
2626
"package.json",

.eslintignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/coverage
22
/dist
33
/node_modules
4-
/test/fixtures
4+
/test/fixtures
5+
/test/basic-loader-test/mod.js
6+
/test/basic-loader-test/mod1.js
7+
/test/basic-loader-test/mod2.js
8+

src/WorkerPool.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,37 @@ class PoolWorker {
234234
finalCallback();
235235
break;
236236
}
237+
case 'importModule': {
238+
const { request, options, questionId } = message;
239+
const { data } = this.jobs[id];
240+
241+
data
242+
.importModule(request, options)
243+
.then((result) => {
244+
this.writeJson({
245+
type: 'result',
246+
id: questionId,
247+
error: null,
248+
result,
249+
});
250+
})
251+
.catch((error) => {
252+
this.writeJson({
253+
type: 'result',
254+
id: questionId,
255+
error: error
256+
? {
257+
message: error.message,
258+
details: error.details,
259+
missing: error.missing,
260+
}
261+
: null,
262+
});
263+
});
264+
265+
finalCallback();
266+
break;
267+
}
237268
case 'resolve': {
238269
const { context, request, options, questionId } = message;
239270
const { data } = this.jobs[id];

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,25 @@ function pitch() {
3838
},
3939
},
4040
resourcePath: this.resourcePath,
41-
resource: this.resourcePath + (this.resourceQuery || ''),
41+
resourceQuery: this.resourceQuery,
42+
resourceFragment: this.resourceFragment,
43+
environment: this.environment,
44+
resource:
45+
this.resourcePath +
46+
(this.resourceQuery || '') +
47+
(this.resourceFragment || ''),
4248
sourceMap: this.sourceMap,
4349
emitError: this.emitError,
4450
emitWarning: this.emitWarning,
4551
getLogger: this.getLogger,
4652
loggers: {},
4753
loadModule: this.loadModule,
54+
importModule: this.importModule,
4855
resolve: this.resolve,
4956
getResolve: this.getResolve,
5057
target: this.target,
5158
mode: this.mode,
5259
minimize: this.minimize,
53-
resourceQuery: this.resourceQuery,
5460
optionsContext: this.rootContext || this.options.context,
5561
rootContext: this.rootContext,
5662
},

src/worker.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
119119
});
120120
nextQuestionId += 1;
121121
};
122+
const importModule = (request, options, callback) => {
123+
callbackMap[nextQuestionId] = callback;
124+
writeJson({
125+
type: 'importModule',
126+
id,
127+
questionId: nextQuestionId,
128+
request,
129+
options,
130+
});
131+
nextQuestionId += 1;
132+
};
122133

123134
const buildDependencies = [];
124135

@@ -141,6 +152,22 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
141152
});
142153
nextQuestionId += 1;
143154
},
155+
// eslint-disable-next-line consistent-return
156+
importModule: (request, options, callback) => {
157+
if (callback) {
158+
importModule(request, options, callback);
159+
} else {
160+
return new Promise((resolve, reject) => {
161+
importModule(request, options, (err, result) => {
162+
if (err) {
163+
reject(err);
164+
} else {
165+
resolve(result);
166+
}
167+
});
168+
});
169+
}
170+
},
144171
resolve: (context, request, callback) => {
145172
resolveWithOptions(context, request, callback);
146173
},
@@ -343,6 +370,8 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
343370
target: data.target,
344371
minimize: data.minimize,
345372
resourceQuery: data.resourceQuery,
373+
resourceFragment: data.resourceFragment,
374+
environment: data.environment,
346375
rootContext: data.rootContext,
347376
// eslint-disable-next-line no-underscore-dangle
348377
_compilation: data._compilation,
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Works with test-loader: errors 1`] = `
4+
[
5+
[ModuleError: Module Error (from ../../dist/index.js):
6+
Test Message Error],
7+
]
8+
`;
9+
10+
exports[`Works with test-loader: result 1`] = `
11+
{
12+
"addBuildDependency": "function",
13+
"addContextDependency": "function",
14+
"addDependency": "function",
15+
"addMissingDependency": "function",
16+
"async": "function",
17+
"cacheable": "function",
18+
"callback": "function",
19+
"clearDependencies": "function",
20+
"context": "<cwd>/test/basic-loader-test",
21+
"currentRequest": "<cwd>/test/basic-loader-test/test-loader.js??ruleSet[1].rules[0].use[1]!<cwd>/test/basic-loader-test/file.js?q=1#hash",
22+
"data": null,
23+
"dependency": "function",
24+
"emitError": "function",
25+
"emitFile": "undefined",
26+
"emitWarning": "function",
27+
"environment": {
28+
"arrowFunction": true,
29+
"asyncFunction": true,
30+
"const": true,
31+
"destructuring": true,
32+
"forOf": true,
33+
"optionalChaining": true,
34+
"templateLiteral": true,
35+
},
36+
"getContextDependencies": "function",
37+
"getContextDependenciesResult": [
38+
"<cwd>/test/basic-loader-test/directory",
39+
],
40+
"getDependencies": "function",
41+
"getDependenciesResult": [
42+
"<cwd>/test/basic-loader-test/file.js",
43+
"<cwd>/test/basic-loader-test/dep1.js",
44+
"<cwd>/test/basic-loader-test/dep.js",
45+
],
46+
"getLogger": "function",
47+
"getMissingDependencies": "function",
48+
"getMissingDependenciesResult": [],
49+
"getOptions": "function",
50+
"getResolve": "function",
51+
"importModule": "function",
52+
"importModuleResult1": {
53+
"default": "http://test.com/first/777312cffc01c1457868.less",
54+
},
55+
"importModuleResult2": {
56+
"default": "http://test.com/first/777312cffc01c1457868.less",
57+
},
58+
"loadModule": "function",
59+
"loadModuleResult": {
60+
"map": null,
61+
"source": "const test = require('./mod1');
62+
63+
module.exports = new URL('./style.less', import.meta.url);
64+
",
65+
},
66+
"loaderIndex": 0,
67+
"loaders": [
68+
{
69+
"data": null,
70+
"fragment": "",
71+
"ident": "ruleSet[1].rules[0].use[1]",
72+
"normalExecuted": true,
73+
"options": {
74+
"test": {},
75+
},
76+
"path": "<cwd>/test/basic-loader-test/test-loader.js",
77+
"pitchExecuted": true,
78+
"query": "??ruleSet[1].rules[0].use[1]",
79+
"request": "<cwd>/test/basic-loader-test/test-loader.js??ruleSet[1].rules[0].use[1]",
80+
},
81+
],
82+
"mode": "none",
83+
"options": {
84+
"test": {},
85+
},
86+
"previousRequest": "",
87+
"query": {
88+
"test": {},
89+
},
90+
"remainingRequest": "<cwd>/test/basic-loader-test/file.js?q=1#hash",
91+
"request": "<cwd>/test/basic-loader-test/test-loader.js??ruleSet[1].rules[0].use[1]!<cwd>/test/basic-loader-test/file.js?q=1#hash",
92+
"resolve": "function",
93+
"resource": "<cwd>/test/basic-loader-test/file.js?q=1#hash",
94+
"resourceFragment": "#hash",
95+
"resourcePath": "<cwd>/test/basic-loader-test/file.js",
96+
"resourceQuery": "?q=1",
97+
"rootContext": "<cwd>/test/basic-loader-test",
98+
"sourceMap": false,
99+
"target": "web",
100+
"utils": {
101+
"absolutify": "undefined",
102+
"contextify": "undefined",
103+
"createHash": "undefined",
104+
},
105+
"version": 2,
106+
"webpack": true,
107+
}
108+
`;
109+
110+
exports[`Works with test-loader: warnings 1`] = `
111+
[
112+
[ModuleWarning: Module Warning (from ../../dist/index.js):
113+
Test Message Warning],
114+
]
115+
`;

test/basic-loader-test/build-dep.js

Whitespace-only changes.

test/basic-loader-test/dep.js

Whitespace-only changes.

test/basic-loader-test/dep1.js

Whitespace-only changes.

test/basic-loader-test/directory/file.js

Whitespace-only changes.

0 commit comments

Comments
 (0)