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

fix: isFromTsImporter flag in worker virtual model #10273

Merged
merged 3 commits into from
Sep 28, 2022
Merged
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
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import type { DepsOptimizer } from '../optimizer'
import type { SSROptions } from '..'
import type { PackageCache, PackageData } from '../packages'
import { loadPackageData, resolvePackageData } from '../packages'
import { isWorkerRequest } from './worker'

const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
const normalizedEnvEntry = normalizePath(ENV_ENTRY)
Expand Down Expand Up @@ -140,13 +141,16 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}

if (importer) {
const _importer = isWorkerRequest(importer)
? splitFileAndPostfix(importer).file
: importer
if (
isTsRequest(importer) ||
isTsRequest(_importer) ||
resolveOpts.custom?.depScan?.loader?.startsWith('ts')
) {
options.isFromTsImporter = true
} else {
const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang
const moduleLang = this.getModuleInfo(_importer)?.meta?.vite?.lang
options.isFromTsImporter = moduleLang && isTsRequest(`.${moduleLang}`)
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export type WorkerType = 'classic' | 'module' | 'ignore'
export const WORKER_FILE_ID = 'worker_file'
const workerCache = new WeakMap<ResolvedConfig, WorkerCache>()

export function isWorkerRequest(id: string): boolean {
const query = parseRequest(id)
if (query && query[WORKER_FILE_ID] != null) {
return true
}
return false
}

function saveEmitWorkerAsset(
config: ResolvedConfig,
asset: EmittedAsset
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/__tests__/es/es-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe.runIf(isBuild)('build', () => {
test('inlined code generation', async () => {
const assetsDir = path.resolve(testDir, 'dist/es/assets')
const files = fs.readdirSync(assetsDir)
expect(files.length).toBe(28)
expect(files.length).toBe(27)
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const worker = files.find((f) => f.includes('my-worker'))
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/my-worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { msg as msgFromDep } from 'dep-to-optimize'
import { mode, msg } from './modules/workerImport'
import { mode, msg } from './modules/workerImport.js'
import { bundleWithPlugin } from './modules/test-plugin'
import viteSvg from './vite.svg'

Expand Down
2 changes: 1 addition & 1 deletion playground/worker/url-shared-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import constant from './modules/module0'
import constant from './modules/module0.js'

self.onconnect = (event) => {
const port = event.ports[0]
Expand Down
4 changes: 2 additions & 2 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import myWorker from '../my-worker?worker'
import InlineWorker from '../my-worker?worker&inline'
import myWorker from '../my-worker.ts?worker'
import InlineWorker from '../my-worker.ts?worker&inline'
import mySharedWorker from '../my-shared-worker?sharedworker&name=shared'
import TSOutputWorker from '../possible-ts-output-worker?worker'
import NestedWorker from '../worker-nested-worker?worker'
Expand Down