Skip to content

Commit 708707f

Browse files
committed
fix: externalize mswCore and fix replaceCoreImports
1 parent 25f1f01 commit 708707f

7 files changed

Lines changed: 28 additions & 21 deletions

File tree

‎config/replaceCoreImports.js‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
const CORE_ESM_IMPORT_PATTERN = /from ["'](#core(.*))["'](;)?/gm
2-
const CORE_CJS_IMPORT_PATTERN = /require\(["'](#core(.*))["']\)(;)?/gm
1+
const CORE_ESM_IMPORT_PATTERN = /(from|import)\s+["'](#core(.*?))["'](;)?/gm
2+
const CORE_CJS_IMPORT_PATTERN = /require\(["'](#core(.*?))["']\)(;)?/gm
33

44
function getCoreImportPattern(isEsm) {
55
return isEsm ? CORE_ESM_IMPORT_PATTERN : CORE_CJS_IMPORT_PATTERN
66
}
77

88
export function hasCoreImports(fileContents, isEsm) {
9-
return getCoreImportPattern(isEsm).test(fileContents)
9+
return fileContents.search(getCoreImportPattern(isEsm)) !== -1
1010
}
1111

1212
export function replaceCoreImports(moduleFilePath, fileContents, isEsm) {
13+
if (isEsm) {
14+
return fileContents.replace(
15+
CORE_ESM_IMPORT_PATTERN,
16+
(_, keyword, __, maybeSubmodulePath, maybeSemicolon) => {
17+
const submodulePath = maybeSubmodulePath || '/index'
18+
/**
19+
* @note Although all .d.ts are considered ESM, append different
20+
* file extension for d.mts files.
21+
*/
22+
const extension = moduleFilePath.endsWith('.d.mts') ? '.mjs' : ''
23+
const semicolon = maybeSemicolon || ''
24+
25+
return `${keyword} "../core${submodulePath}${extension}"${semicolon}`
26+
},
27+
)
28+
}
29+
1330
return fileContents.replace(
14-
getCoreImportPattern(isEsm),
31+
CORE_CJS_IMPORT_PATTERN,
1532
(_, __, maybeSubmodulePath, maybeSemicolon) => {
1633
const submodulePath = maybeSubmodulePath || '/index'
17-
/**
18-
* @note Although all .d.ts are considered ESM, append different
19-
* file extension for d.mts files.
20-
*/
21-
const extension = moduleFilePath.endsWith('.d.mts') ? '.mjs' : ''
2234
const semicolon = maybeSemicolon || ''
2335

24-
return isEsm
25-
? `from "../core${submodulePath}${extension}"${semicolon}`
26-
: `require("../core${submodulePath}")${semicolon}`
36+
return `require("../core${submodulePath}")${semicolon}`
2737
},
2838
)
2939
}

‎src/core/handlers/RequestHandler.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import {
77
import type { ResponseResolutionContext } from '../utils/executeHandlers'
88
import type { MaybePromise } from '../typeUtils'
99
import type { HttpResponse } from '../HttpResponse'
10-
import {
11-
type StrictRequest,
12-
type DefaultUnsafeFetchResponse,
13-
} from '../HttpResponse'
10+
import type { StrictRequest, DefaultUnsafeFetchResponse } from '../HttpResponse'
1411
import type { GraphQLRequestBody } from '../../graphql/graphql-handler'
1512
import { devUtils } from '../utils/internal/devUtils'
1613
import { getRawSetCookie } from '../utils/HttpResponse/decorators'

‎src/core/utils/executeHandlers.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RequestHandler } from '../handlers/RequestHandler'
2-
import { type RequestHandlerExecutionResult } from '../handlers/RequestHandler'
2+
import type { RequestHandlerExecutionResult } from '../handlers/RequestHandler'
33

44
export interface HandlersExecutionResult {
55
handler: RequestHandler

‎src/graphql/graphql-handler.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type GraphQLResolverExtras,
99
} from './graphql-handler'
1010
import { HttpResponse } from '../core/HttpResponse'
11-
import { type ResponseResolver } from '../core/handlers/RequestHandler'
11+
import type { ResponseResolver } from '../core/handlers/RequestHandler'
1212

1313
const resolver: ResponseResolver<GraphQLResolverExtras<{ userId: string }>> = ({
1414
variables,

‎src/graphql/graphql-handler.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { devUtils } from '#core/utils/internal/devUtils'
3434
import { getAllRequestCookies } from '#core/utils/request/getRequestCookies'
3535
import { kDefaultContentType, type StrictRequest } from '#core/HttpResponse'
3636
import { getAllAcceptedMimeTypes } from '#core/utils/request/getAllAcceptedMimeTypes'
37-
import { type ResponseResolutionContext } from '#core/utils/executeHandlers'
37+
import type { ResponseResolutionContext } from '#core/utils/executeHandlers'
3838

3939
export interface DocumentTypeDecoration<
4040
Result = { [key: string]: any },

‎src/graphql/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export {
1717
type GraphQLCustomPredicate,
1818
} from './graphql-handler'
1919

20-
export { type ParsedGraphQLRequest } from './parse-graphql-request'
20+
export type { ParsedGraphQLRequest } from './parse-graphql-request'

‎tsup.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const graphqlConfig = defineConfig({
6868
posix: true,
6969
dotRelative: true,
7070
}),
71-
external: [ecosystemDependencies],
71+
external: [mswCore, ecosystemDependencies],
7272
format: ['esm', 'cjs'],
7373
outDir: './lib/graphql',
7474
bundle: false,

0 commit comments

Comments
 (0)