Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call all js plugins onResolve and onLoad functions #552

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 74 additions & 54 deletions lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,89 +486,109 @@ export function createChannel(streamIn: StreamIn): StreamOut {
request.plugins.push(plugin);
}

pluginCallbacks.set(buildKey, async (request) => {
switch (request.command) {
case 'resolve': {
pluginCallbacks.set(
buildKey,
async (request): Promise<protocol.OnResolveResponse> => {
if (request.command === "resolve") {
let response: protocol.OnResolveResponse = {};
let result: types.OnResolveResult | undefined;
for (let id of request.ids) {
try {
let callback = onResolveCallbacks[id];
let result = await callback({
let newResult = await callback({
path: request.path,
importer: request.importer,
namespace: request.namespace,
importer: request.importer,
resolveDir: request.resolveDir,
});

if (result != null) {
if (typeof result !== 'object') throw new Error('Expected resolver plugin to return an object');
let keys: OptionKeys = {};
let pluginName = getFlag(result, keys, 'pluginName', mustBeString);
let path = getFlag(result, keys, 'path', mustBeString);
let namespace = getFlag(result, keys, 'namespace', mustBeString);
let external = getFlag(result, keys, 'external', mustBeBoolean);
let errors = getFlag(result, keys, 'errors', mustBeArray);
let warnings = getFlag(result, keys, 'warnings', mustBeArray);
checkForInvalidFlags(result, keys);

if (newResult) {
result = newResult;
response.id = id;
if (pluginName != null) response.pluginName = pluginName;
if (path != null) response.path = path;
if (namespace != null) response.namespace = namespace;
if (external != null) response.external = external;
if (errors != null) response.errors = sanitizeMessages(errors);
if (warnings != null) response.warnings = sanitizeMessages(warnings);
break;
}
} catch (e) {
return { id, errors: [await extractErrorMessageV8(e, streamIn)] };
return {
id,
errors: [await extractErrorMessageV8(e, streamIn)],
};
}
}
if (result != null) {
if (typeof result !== "object")
throw new Error("Expected resolver plugin to return an object");
let keys: OptionKeys = {};
let pluginName = getFlag(result, keys, "pluginName", mustBeString); // TODO there can be many plugins
let path = getFlag(result, keys, "path", mustBeString);
let namespace = getFlag(result, keys, "namespace", mustBeString);
let external = getFlag(result, keys, "external", mustBeBoolean);
let errors = getFlag(result, keys, "errors", mustBeArray);
let warnings = getFlag(result, keys, "warnings", mustBeArray);
checkForInvalidFlags(result, keys);

if (pluginName != null) response.pluginName = pluginName;
if (path != null) response.path = path;
if (namespace != null) response.namespace = namespace;
if (external != null) response.external = external;
if (errors != null) response.errors = sanitizeMessages(errors);
if (warnings != null)
response.warnings = sanitizeMessages(warnings);
}
return response;
}

case 'load': {
} else if (request.command === "load") {
let response: protocol.OnLoadResponse = {};
let result: types.OnLoadResult | undefined;
for (let id of request.ids) {
try {
let callback = onLoadCallbacks[id];
let result = await callback({
let newResult = await callback({
path: request.path,
namespace: request.namespace,
});

if (result != null) {
if (typeof result !== 'object') throw new Error('Expected loader plugin to return an object');
let keys: OptionKeys = {};
let pluginName = getFlag(result, keys, 'pluginName', mustBeString);
let contents = getFlag(result, keys, 'contents', mustBeStringOrUint8Array);
let resolveDir = getFlag(result, keys, 'resolveDir', mustBeString);
let loader = getFlag(result, keys, 'loader', mustBeString);
let errors = getFlag(result, keys, 'errors', mustBeArray);
let warnings = getFlag(result, keys, 'warnings', mustBeArray);
checkForInvalidFlags(result, keys);

response.id = id;
if (pluginName != null) response.pluginName = pluginName;
if (contents instanceof Uint8Array) response.contents = contents;
else if (contents != null) response.contents = protocol.encodeUTF8(contents);
if (resolveDir != null) response.resolveDir = resolveDir;
if (loader != null) response.loader = loader;
if (errors != null) response.errors = sanitizeMessages(errors);
if (warnings != null) response.warnings = sanitizeMessages(warnings);
break;
if (newResult) {
result = newResult
response.id = id
}
} catch (e) {
return { id, errors: [await extractErrorMessageV8(e, streamIn)] };
return {
id,
errors: [await extractErrorMessageV8(e, streamIn)],
};
}
}

if (result != null) {
if (typeof result !== "object")
throw new Error("Expected loader plugin to return an object");
let keys: OptionKeys = {};
let pluginName = getFlag(result, keys, "pluginName", mustBeString);
let contents = getFlag(
result,
keys,
"contents",
mustBeStringOrUint8Array
);
let resolveDir = getFlag(result, keys, "resolveDir", mustBeString);
let loader = getFlag(result, keys, "loader", mustBeString);
let errors = getFlag(result, keys, "errors", mustBeArray);
let warnings = getFlag(result, keys, "warnings", mustBeArray);
checkForInvalidFlags(result, keys);
if (pluginName != null) response.pluginName = pluginName;
if (contents instanceof Uint8Array) response.contents = contents;
else if (contents != null)
response.contents = protocol.encodeUTF8(contents);
if (resolveDir != null) response.resolveDir = resolveDir;
if (loader != null) response.loader = loader;
if (errors != null) response.errors = sanitizeMessages(errors);
if (warnings != null)
response.warnings = sanitizeMessages(warnings);
}

return response;
} else {
throw new Error(`Invalid command: ` + request!.command);
}

default:
throw new Error(`Invalid command: ` + (request as any).command);
}
});
);

return () => pluginCallbacks.delete(buildKey);
};
Expand Down
6 changes: 4 additions & 2 deletions scripts/plugin-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ let pluginTests = {
{
name: 'plugin4',
setup(build) {
build.onResolve({ filter: /^.*$/ }, () => { trace.push('not called') })
build.onResolve({ filter: /^.*$/ }, () => { trace.push('called third') })
},
}
],
Expand All @@ -238,6 +238,7 @@ let pluginTests = {
assert.deepStrictEqual(trace, [
'called first',
'called second',
'called third'
])
},

Expand Down Expand Up @@ -279,7 +280,7 @@ let pluginTests = {
{
name: 'plugin4',
setup(build) {
build.onLoad({ filter: /^.*$/, namespace: 'file' }, () => { trace.push('not called') })
build.onLoad({ filter: /^.*$/, namespace: 'file' }, () => { trace.push('called third') })
},
},
],
Expand All @@ -289,6 +290,7 @@ let pluginTests = {
assert.deepStrictEqual(trace, [
'called first',
'called second',
'called third',
])
},

Expand Down