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

add moduleType support for on-resolve callback #2959

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/esbuild/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"errors"
"fmt"
"github.com/evanw/esbuild/internal/js_ast"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -920,6 +921,7 @@ func (service *serviceType) convertPlugins(key int, jsPlugins interface{}, activ
"errors": encodeMessages(result.Errors),
"warnings": encodeMessages(result.Warnings),
"path": result.Path,
"moduleType": result.ModuleType,
"external": result.External,
"sideEffects": result.SideEffects,
"namespace": result.Namespace,
Expand Down Expand Up @@ -996,6 +998,9 @@ func (service *serviceType) convertPlugins(key int, jsPlugins interface{}, activ
if value, ok := response["path"]; ok {
result.Path = value.(string)
}
if value, ok := response["moduleType"]; ok {
result.ModuleType = js_ast.ModuleType(value.(int))
}
if value, ok := response["namespace"]; ok {
result.Namespace = value.(string)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,11 @@ func RunOnResolvePlugins(
IsExternal: result.External,
PluginData: result.PluginData,
PrimarySideEffectsData: sideEffectsData,
ModuleTypeData: js_ast.ModuleTypeData{
Source: &logger.Source{},
Range: logger.Range{},
Type: result.ModuleType,
},
}, false, resolver.DebugMeta{}
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ type OnResolveResult struct {

PluginData interface{}
Path logger.Path
ModuleType js_ast.ModuleType
External bool
IsSideEffectFree bool
}
Expand Down
3 changes: 3 additions & 0 deletions lib/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,7 @@ let handlePlugins = async (
errors: replaceDetailsInMessages(response!.errors, details),
warnings: replaceDetailsInMessages(response!.warnings, details),
path: response!.path,
moduleType: response!.moduleType,
external: response!.external,
sideEffects: response!.sideEffects,
namespace: response!.namespace,
Expand Down Expand Up @@ -1371,6 +1372,7 @@ let handlePlugins = async (
let keys: OptionKeys = {}
let pluginName = getFlag(result, keys, 'pluginName', mustBeString)
let path = getFlag(result, keys, 'path', mustBeString)
let moduleType = getFlag(result, keys, 'moduleType', mustBeInteger)
let namespace = getFlag(result, keys, 'namespace', mustBeString)
let suffix = getFlag(result, keys, 'suffix', mustBeString)
let external = getFlag(result, keys, 'external', mustBeBoolean)
Expand All @@ -1385,6 +1387,7 @@ let handlePlugins = async (
response.id = id
if (pluginName != null) response.pluginName = pluginName
if (path != null) response.path = path
if (moduleType != null) response.moduleType = moduleType
if (namespace != null) response.namespace = namespace
if (suffix != null) response.suffix = suffix
if (external != null) response.external = external
Expand Down
2 changes: 2 additions & 0 deletions lib/shared/stdio_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export interface ResolveResponse {
warnings: types.Message[]

path: string
moduleType: number
external: boolean
sideEffects: boolean
namespace: string
Expand Down Expand Up @@ -202,6 +203,7 @@ export interface OnResolveResponse {
warnings?: types.PartialMessage[]

path?: string
moduleType?: number
external?: boolean
sideEffects?: boolean
namespace?: string
Expand Down
2 changes: 2 additions & 0 deletions lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export interface ResolveResult {
warnings: Message[]

path: string
moduleType: number
external: boolean
sideEffects: boolean
namespace: string
Expand Down Expand Up @@ -385,6 +386,7 @@ export interface OnResolveResult {
warnings?: PartialMessage[]

path?: string
moduleType?: number
external?: boolean
sideEffects?: boolean
namespace?: string
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
package api

import (
"github.com/evanw/esbuild/internal/js_ast"
"time"

"github.com/evanw/esbuild/internal/logger"
Expand Down Expand Up @@ -574,6 +575,7 @@ type ResolveResult struct {
Warnings []Message

Path string
ModuleType int
External bool
SideEffects bool
Namespace string
Expand Down Expand Up @@ -615,6 +617,7 @@ type OnResolveResult struct {
Warnings []Message

Path string
ModuleType js_ast.ModuleType
External bool
SideEffects SideEffects
Namespace string
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,7 @@ func (impl *pluginImpl) onResolve(options OnResolveOptions, callback func(OnReso
Namespace: response.Namespace,
IgnoredSuffix: response.Suffix,
}
result.ModuleType = response.ModuleType
result.External = response.External
result.IsSideEffectFree = response.SideEffects == SideEffectsFalse
result.PluginData = response.PluginData
Expand Down Expand Up @@ -2048,6 +2049,7 @@ func loadPlugins(initialOptions *BuildOptions, fs fs.FS, log logger.Log, caches
result.Warnings = convertMessagesToPublic(logger.Warning, msgs)
if resolveResult != nil {
result.Path = resolveResult.PathPair.Primary.Text
result.ModuleType = int(resolveResult.ModuleTypeData.Type)
result.External = resolveResult.IsExternal
result.SideEffects = resolveResult.PrimarySideEffectsData == nil
result.Namespace = resolveResult.PathPair.Primary.Namespace
Expand Down
11 changes: 11 additions & 0 deletions scripts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"js-yaml": "3.14.0",
"react": "17.0.1",
"source-map": "0.7.4",
"tslib": "2.5.0",
"typescript": "4.7.4"
}
}
32 changes: 32 additions & 0 deletions scripts/plugin-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,38 @@ let pluginTests = {
'Ignoring this import because "ns:plugin:ignored-false" was marked as having no side effects by plugin "name"')
},

async resolveModuleType({ esbuild, testDir }) {
const input = path.join(testDir, 'in.js')

await writeFileAsync(input, `
import { __assign } from 'tslib'
console.log(__assign)
`)

const result = await esbuild.build({
entryPoints: [input],
bundle: true,
write: false,
format: 'cjs',
logLevel: 'error',
plugins: [{
name: 'resolver',
setup(build) {
build.onResolve({ filter: /.*/ }, args => {
if (args.path === 'tslib') {
return {
path: path.resolve(require.resolve(args.path), '../modules/index.js'),
moduleType: 6
}
}
})
}
}]
})

assert(result.outputFiles[0].text.includes("__toESM(require_tslib(), 1)"))
},

async noResolveDirInFileModule({ esbuild, testDir }) {
const input = path.join(testDir, 'in.js')
const output = path.join(testDir, 'out.js')
Expand Down