Skip to content

Commit

Permalink
add "on-" to stdio protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 23, 2021
1 parent f38dcd3 commit 8361c88
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 48 deletions.
14 changes: 7 additions & 7 deletions cmd/esbuild/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"github.com/evanw/esbuild/pkg/cli"
)

type responseCallback = func(interface{})
type rebuildCallback = func(uint32) []byte
type watchStopCallback = func()
type serverStopCallback = func()
type responseCallback func(interface{})
type rebuildCallback func(uint32) []byte
type watchStopCallback func()
type serverStopCallback func()

type serviceType struct {
mutex sync.Mutex
Expand Down Expand Up @@ -631,7 +631,7 @@ func (service *serviceType) convertPlugins(key int, jsPlugins interface{}) ([]ap
result := api.OnStartResult{}

response := service.sendRequest(map[string]interface{}{
"command": "start",
"command": "on-start",
"key": key,
}).(map[string]interface{})

Expand Down Expand Up @@ -685,7 +685,7 @@ func (service *serviceType) convertPlugins(key int, jsPlugins interface{}) ([]ap
}

response := service.sendRequest(map[string]interface{}{
"command": "resolve",
"command": "on-resolve",
"key": key,
"ids": ids,
"path": args.Path,
Expand Down Expand Up @@ -764,7 +764,7 @@ func (service *serviceType) convertPlugins(key int, jsPlugins interface{}) ([]ap
}

response := service.sendRequest(map[string]interface{}{
"command": "load",
"command": "on-load",
"key": key,
"ids": ids,
"path": args.Path,
Expand Down
6 changes: 3 additions & 3 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,10 @@ func logPluginMessages(
msg.Data.Location = tracker.MsgLocationOrNil(importPathRange)
} else {
sanitizeLocation(res, msg.Data.Location)
if msg.Data.Location.File == "" && importSource != nil {
msg.Data.Location.File = importSource.PrettyPath
}
if importSource != nil {
if msg.Data.Location.File == "" {
msg.Data.Location.File = importSource.PrettyPath
}
msg.Notes = append(msg.Notes, tracker.MsgData(importPathRange,
fmt.Sprintf("The plugin %q was triggered by this import", name)))
}
Expand Down
12 changes: 6 additions & 6 deletions lib/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,21 +539,21 @@ export function createChannel(streamIn: StreamIn): StreamOut {
break;
}

case 'start': {
case 'on-start': {
let callback = pluginCallbacks.get(request.key);
if (!callback) sendResponse(id, {});
else sendResponse(id, await callback!(request) as any);
break;
}

case 'resolve': {
case 'on-resolve': {
let callback = pluginCallbacks.get(request.key);
if (!callback) sendResponse(id, {});
else sendResponse(id, await callback!(request) as any);
break;
}

case 'load': {
case 'on-load': {
let callback = pluginCallbacks.get(request.key);
if (!callback) sendResponse(id, {});
else sendResponse(id, await callback!(request) as any);
Expand Down Expand Up @@ -749,7 +749,7 @@ export function createChannel(streamIn: StreamIn): StreamOut {

const callback: PluginCallback = async (request) => {
switch (request.command) {
case 'start': {
case 'on-start': {
let response: protocol.OnStartResponse = { errors: [], warnings: [] };
await Promise.all(onStartCallbacks.map(async ({ name, callback, note }) => {
try {
Expand All @@ -772,7 +772,7 @@ export function createChannel(streamIn: StreamIn): StreamOut {
return response;
}

case 'resolve': {
case 'on-resolve': {
let response: protocol.OnResolveResponse = {}, name = '', callback, note;
for (let id of request.ids) {
try {
Expand Down Expand Up @@ -823,7 +823,7 @@ export function createChannel(streamIn: StreamIn): StreamOut {
return response;
}

case 'load': {
case 'on-load': {
let response: protocol.OnLoadResponse = {}, name = '', callback, note;
for (let id of request.ids) {
try {
Expand Down
6 changes: 3 additions & 3 deletions lib/shared/stdio_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export interface AnalyzeMetafileResponse {
}

export interface OnStartRequest {
command: 'start';
command: 'on-start';
key: number;
}

Expand All @@ -148,7 +148,7 @@ export interface OnStartResponse {
}

export interface OnResolveRequest {
command: 'resolve';
command: 'on-resolve';
key: number;
ids: number[];
path: string;
Expand Down Expand Up @@ -178,7 +178,7 @@ export interface OnResolveResponse {
}

export interface OnLoadRequest {
command: 'load';
command: 'on-load';
key: number;
ids: number[];
path: string;
Expand Down
57 changes: 29 additions & 28 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ func (w *watcher) tryToFindDirtyPath() string {
items = append(items, path)
}
rand.Seed(time.Now().UnixNano())
for i := int32(len(items) - 1); i > 0; i-- { // FisherYates shuffle
for i := int32(len(items) - 1); i > 0; i-- { // Fisher-Yates shuffle
j := rand.Int31n(i + 1)
items[i], items[j] = items[j], items[i]
}
Expand Down Expand Up @@ -1421,7 +1421,7 @@ type pluginImpl struct {
plugin config.Plugin
}

func (impl *pluginImpl) OnStart(callback func() (OnStartResult, error)) {
func (impl *pluginImpl) onStart(callback func() (OnStartResult, error)) {
impl.plugin.OnStart = append(impl.plugin.OnStart, config.OnStart{
Name: impl.plugin.Name,
Callback: func() (result config.OnStartResult) {
Expand All @@ -1445,7 +1445,28 @@ func (impl *pluginImpl) OnStart(callback func() (OnStartResult, error)) {
})
}

func (impl *pluginImpl) OnResolve(options OnResolveOptions, callback func(OnResolveArgs) (OnResolveResult, error)) {
func importKindToResolveKind(kind ast.ImportKind) ResolveKind {
switch kind {
case ast.ImportEntryPoint:
return ResolveEntryPoint
case ast.ImportStmt:
return ResolveJSImportStatement
case ast.ImportRequire:
return ResolveJSRequireCall
case ast.ImportDynamic:
return ResolveJSDynamicImport
case ast.ImportRequireResolve:
return ResolveJSRequireResolve
case ast.ImportAt, ast.ImportAtConditional:
return ResolveCSSImportRule
case ast.ImportURL:
return ResolveCSSURLToken
default:
panic("Internal error")
}
}

func (impl *pluginImpl) onResolve(options OnResolveOptions, callback func(OnResolveArgs) (OnResolveResult, error)) {
filter, err := config.CompileFilterForPlugin(impl.plugin.Name, "OnResolve", options.Filter)
if filter == nil {
impl.log.Add(logger.Error, nil, logger.Range{}, err.Error())
Expand All @@ -1457,32 +1478,12 @@ func (impl *pluginImpl) OnResolve(options OnResolveOptions, callback func(OnReso
Filter: filter,
Namespace: options.Namespace,
Callback: func(args config.OnResolveArgs) (result config.OnResolveResult) {
var kind ResolveKind
switch args.Kind {
case ast.ImportEntryPoint:
kind = ResolveEntryPoint
case ast.ImportStmt:
kind = ResolveJSImportStatement
case ast.ImportRequire:
kind = ResolveJSRequireCall
case ast.ImportDynamic:
kind = ResolveJSDynamicImport
case ast.ImportRequireResolve:
kind = ResolveJSRequireResolve
case ast.ImportAt, ast.ImportAtConditional:
kind = ResolveCSSImportRule
case ast.ImportURL:
kind = ResolveCSSURLToken
default:
panic("Internal error")
}

response, err := callback(OnResolveArgs{
Path: args.Path,
Importer: args.Importer.Text,
Namespace: args.Importer.Namespace,
ResolveDir: args.ResolveDir,
Kind: kind,
Kind: importKindToResolveKind(args.Kind),
PluginData: args.PluginData,
})
result.PluginName = response.PluginName
Expand Down Expand Up @@ -1521,7 +1522,7 @@ func (impl *pluginImpl) OnResolve(options OnResolveOptions, callback func(OnReso
})
}

func (impl *pluginImpl) OnLoad(options OnLoadOptions, callback func(OnLoadArgs) (OnLoadResult, error)) {
func (impl *pluginImpl) onLoad(options OnLoadOptions, callback func(OnLoadArgs) (OnLoadResult, error)) {
filter, err := config.CompileFilterForPlugin(impl.plugin.Name, "OnLoad", options.Filter)
if filter == nil {
impl.log.Add(logger.Error, nil, logger.Range{}, err.Error())
Expand Down Expand Up @@ -1602,10 +1603,10 @@ func loadPlugins(initialOptions *BuildOptions, fs fs.FS, log logger.Log) (plugin

item.Setup(PluginBuild{
InitialOptions: initialOptions,
OnStart: impl.OnStart,
OnStart: impl.onStart,
OnEnd: onEnd,
OnResolve: impl.OnResolve,
OnLoad: impl.OnLoad,
OnResolve: impl.onResolve,
OnLoad: impl.onLoad,
})

plugins = append(plugins, impl.plugin)
Expand Down
3 changes: 2 additions & 1 deletion scripts/plugin-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,7 @@ let pluginTests = {
let esbuildFromBuild
await esbuild.build({
entryPoints: ['xyz'],
write: false,
plugins: [{
name: 'plugin',
setup(build) {
Expand All @@ -2067,7 +2068,7 @@ let pluginTests = {
assert.deepStrictEqual({ ...esbuildFromBuild }, { ...esbuild })
},

async onResolveInvalidPathSuffix({ testDir, esbuild }) {
async onResolveInvalidPathSuffix({ esbuild }) {
try {
await esbuild.build({
entryPoints: ['foo'],
Expand Down

0 comments on commit 8361c88

Please sign in to comment.