| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,14 +4,15 @@ import { Buffer } from 'node:buffer' | |||
| 4 | 4 | import { resolve } from 'node:path' | |
| 5 | 5 | import { parse } from '../utils/parse' | |
| 6 | 6 | ||
| 7 | - export function createBuildContext(compiler: Compiler, compilation: Compilation, loaderContext?: LoaderContext): UnpluginBuildContext { | ||
| 7 | + export function createBuildContext(compiler: Compiler, compilation: Compilation, loaderContext?: LoaderContext, inputSourceMap?: any): UnpluginBuildContext { | ||
| 8 | 8 | return { | |
| 9 | 9 | getNativeBuildContext() { | |
| 10 | 10 | return { | |
| 11 | 11 | framework: 'rspack', | |
| 12 | 12 | compiler, | |
| 13 | 13 | compilation, | |
| 14 | 14 | loaderContext, | |
| 15 | + inputSourceMap, | ||
| 15 | 16 | } | |
| 16 | 17 | }, | |
| 17 | 18 | addWatchFile(file) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,7 +23,7 @@ export default async function transform( | |||
| 23 | 23 | const res = await handler.call( | |
| 24 | 24 | Object.assign( | |
| 25 | 25 | {}, | |
| 26 | - this._compilation && createBuildContext(this._compiler, this._compilation, this), | ||
| 26 | + this._compilation && createBuildContext(this._compiler, this._compilation, this, map), | ||
| 27 | 27 | context, | |
| 28 | 28 | ), | |
| 29 | 29 | source, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,9 +50,9 @@ export type TransformResult = string | { code: string, map?: SourceMapInput | So | |||
| 50 | 50 | export interface ExternalIdResult { id: string, external?: boolean | undefined } | |
| 51 | 51 | ||
| 52 | 52 | export type NativeBuildContext | |
| 53 | - = { framework: 'webpack', compiler: WebpackCompiler, compilation?: WebpackCompilation | undefined, loaderContext?: WebpackLoaderContext<{ unpluginName: string }> | undefined } | ||
| 53 | + = { framework: 'webpack', compiler: WebpackCompiler, compilation?: WebpackCompilation | undefined, loaderContext?: WebpackLoaderContext<{ unpluginName: string }> | undefined, inputSourceMap?: any } | ||
| 54 | 54 | | { framework: 'esbuild', build: PluginBuild } | |
| 55 | - | { framework: 'rspack', compiler: RspackCompiler, compilation: RspackCompilation, loaderContext?: RspackLoaderContext | undefined } | ||
| 55 | + | { framework: 'rspack', compiler: RspackCompiler, compilation: RspackCompilation, loaderContext?: RspackLoaderContext | undefined, inputSourceMap?: any } | ||
| 56 | 56 | | { framework: 'farm', context: FarmCompilationContext } | |
| 57 | 57 | | { framework: 'bun', build: BunPluginBuilder } | |
| 58 | 58 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,7 @@ export function getSource(fileSource: string | Uint8Array): sources.RawSource { | |||
| 30 | 30 | ) | |
| 31 | 31 | } | |
| 32 | 32 | ||
| 33 | - export function createBuildContext(options: ContextOptions, compiler: Compiler, compilation?: Compilation, loaderContext?: LoaderContext<{ unpluginName: string }>): UnpluginBuildContext { | ||
| 33 | + export function createBuildContext(options: ContextOptions, compiler: Compiler, compilation?: Compilation, loaderContext?: LoaderContext<{ unpluginName: string }>, inputSourceMap?: any): UnpluginBuildContext { | ||
| 34 | 34 | return { | |
| 35 | 35 | parse, | |
| 36 | 36 | addWatchFile(id) { | |
@@ -51,7 +51,7 @@ export function createBuildContext(options: ContextOptions, compiler: Compiler, | |||
| 51 | 51 | return options.getWatchFiles() | |
| 52 | 52 | }, | |
| 53 | 53 | getNativeBuildContext() { | |
| 54 | - return { framework: 'webpack', compiler, compilation, loaderContext } | ||
| 54 | + return { framework: 'webpack', compiler, compilation, loaderContext, inputSourceMap } | ||
| 55 | 55 | }, | |
| 56 | 56 | } | |
| 57 | 57 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,7 +24,7 @@ export default async function transform(this: LoaderContext<any>, source: string | |||
| 24 | 24 | getWatchFiles: () => { | |
| 25 | 25 | return this.getDependencies() | |
| 26 | 26 | }, | |
| 27 | - }, this._compiler!, this._compilation, this), context), | ||
| 27 | + }, this._compiler!, this._compilation, this, map), context), | ||
| 28 | 28 | source, | |
| 29 | 29 | this.resource, | |
| 30 | 30 | ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,14 +7,16 @@ describe('createBuildContext', () => { | |||
| 7 | 7 | const compiler = { name: 'testCompiler' } | |
| 8 | 8 | const compilation = { name: 'testCompilation' } | |
| 9 | 9 | const loaderContext = { name: 'testLoaderContext' } | |
| 10 | + const inputSourceMap = { name: 'inputSourceMap' } | ||
| 10 | 11 | ||
| 11 | - const buildContext = createBuildContext(compiler as any, compilation as any, loaderContext as any) | ||
| 12 | + const buildContext = createBuildContext(compiler as any, compilation as any, loaderContext as any, inputSourceMap as any) | ||
| 12 | 13 | ||
| 13 | 14 | expect(buildContext.getNativeBuildContext!()).toEqual({ | |
| 14 | 15 | framework: 'rspack', | |
| 15 | 16 | compiler, | |
| 16 | 17 | compilation, | |
| 17 | 18 | loaderContext, | |
| 19 | + inputSourceMap, | ||
| 18 | 20 | }) | |
| 19 | 21 | }) | |
| 20 | 22 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | - import { describe, expect, it, vi } from 'vitest' | ||
| 1 | + import type { NativeBuildContext, UnpluginBuildContext } from '../../../../src/types' | ||
| 2 | + import { assert, describe, expect, it, vi } from 'vitest' | ||
| 2 | 3 | import transform from '../../../../src/rspack/loaders/transform' | |
| 3 | 4 | ||
| 4 | 5 | describe('transform', () => { | |
@@ -35,10 +36,6 @@ describe('transform', () => { | |||
| 35 | 36 | const source = 'test source' | |
| 36 | 37 | const map = 'test map' | |
| 37 | 38 | ||
| 38 | - vi.mock('../../../../src/utils/filter', () => ({ | ||
| 39 | - normalizeObjectHook: vi.fn(() => ({ handler: vi.fn().mockRejectedValue(new Error('Handler error')), filter: vi.fn().mockReturnValue(true) })), | ||
| 40 | - })) | ||
| 41 | - | ||
| 42 | 39 | await transform.call(mockLoaderContext, source, map) | |
| 43 | 40 | ||
| 44 | 41 | expect(mockCallback).toHaveBeenCalledWith(expect.any(Error)) | |
@@ -63,13 +60,54 @@ describe('transform', () => { | |||
| 63 | 60 | const source = 'test source' | |
| 64 | 61 | const map = 'test map' | |
| 65 | 62 | ||
| 66 | - vi.mock('../../../../src/utils/filter', () => ({ | ||
| 67 | - normalizeObjectHook: vi.fn(() => ({ handler: vi.fn().mockRejectedValue(new Error('Handler error')), filter: vi.fn().mockReturnValue(true) })), | ||
| 68 | - })) | ||
| 69 | - | ||
| 70 | 63 | await transform.call(mockLoaderContext, source, map) | |
| 71 | 64 | ||
| 72 | 65 | expect(mockCallback).toHaveBeenCalledWith(expect.any(Error)) | |
| 73 | 66 | expect(mockCallback.mock.calls[0][0].message).toBe('Handler error') | |
| 74 | 67 | }) | |
| 68 | + | ||
| 69 | + it('should include input source map on native build context', async () => { | ||
| 70 | + const source = 'source code' | ||
| 71 | + const map = 'source map' | ||
| 72 | + const transformedCode = 'transformed code' | ||
| 73 | + const transformedMap = 'transformed map' | ||
| 74 | + | ||
| 75 | + let handlerSource: string | undefined | ||
| 76 | + let handlerId: string | undefined | ||
| 77 | + let handlerNativeBuildContext: NativeBuildContext | undefined | ||
| 78 | + const handlerMock = vi.fn().mockImplementation(function (this: UnpluginBuildContext, source: string, id: string) { | ||
| 79 | + handlerSource = source | ||
| 80 | + handlerId = id | ||
| 81 | + handlerNativeBuildContext = this.getNativeBuildContext?.() | ||
| 82 | + return { code: transformedCode, map: transformedMap } | ||
| 83 | + }) | ||
| 84 | + | ||
| 85 | + const mockCallback = vi.fn() | ||
| 86 | + const mockLoaderContext = { | ||
| 87 | + async: () => mockCallback, | ||
| 88 | + query: { | ||
| 89 | + plugin: { | ||
| 90 | + transform: { | ||
| 91 | + handler: handlerMock, | ||
| 92 | + filter: vi.fn().mockReturnValue(true), | ||
| 93 | + }, | ||
| 94 | + }, | ||
| 95 | + }, | ||
| 96 | + resource: 'test resource', | ||
| 97 | + addDependency: vi.fn(), | ||
| 98 | + getDependencies: vi.fn().mockReturnValue(['/path/to/dependency']), | ||
| 99 | + _compiler: {}, | ||
| 100 | + _compilation: {}, | ||
| 101 | + } as any | ||
| 102 | + | ||
| 103 | + await transform.call(mockLoaderContext as any, source, map) | ||
| 104 | + | ||
| 105 | + expect(handlerMock).toHaveBeenCalled() | ||
| 106 | + expect(handlerSource).toBe(source) | ||
| 107 | + expect(handlerId).toBe(mockLoaderContext.resource) | ||
| 108 | + assert(handlerNativeBuildContext?.framework === 'rspack') | ||
| 109 | + expect(handlerNativeBuildContext?.inputSourceMap).toBe(map) | ||
| 110 | + | ||
| 111 | + expect(mockCallback).toHaveBeenCalledWith(null, transformedCode, transformedMap) | ||
| 112 | + }) | ||
| 75 | 113 | }) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,6 +46,27 @@ describe('webpack - utils', () => { | |||
| 46 | 46 | expect.anything(), | |
| 47 | 47 | ) | |
| 48 | 48 | }) | |
| 49 | + | ||
| 50 | + it('should add expected values to native build context', () => { | ||
| 51 | + const options = { | ||
| 52 | + addWatchFile: vi.fn(), | ||
| 53 | + getWatchFiles: vi.fn(() => ['file1.js']), | ||
| 54 | + } | ||
| 55 | + const compiler = { name: 'testCompiler' } as Compiler | ||
| 56 | + const compilation = { name: 'testCompilation' } as Compilation | ||
| 57 | + const loaderContext = { name: 'testLoaderContext' } as unknown as LoaderContext<{ unpluginName: string }> | ||
| 58 | + const inputSourceMap = { name: 'inputSourceMap' } | ||
| 59 | + | ||
| 60 | + const buildContext = createBuildContext(options, compiler, compilation, loaderContext, inputSourceMap) | ||
| 61 | + | ||
| 62 | + expect(buildContext.getNativeBuildContext!()).toEqual({ | ||
| 63 | + framework: 'webpack', | ||
| 64 | + compiler, | ||
| 65 | + compilation, | ||
| 66 | + loaderContext, | ||
| 67 | + inputSourceMap, | ||
| 68 | + }) | ||
| 69 | + }) | ||
| 49 | 70 | }) | |
| 50 | 71 | ||
| 51 | 72 | describe('createContext', () => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | - import { describe, expect, it, vi } from 'vitest' | ||
| 1 | + import type { NativeBuildContext, UnpluginBuildContext } from '../../../../src/types' | ||
| 2 | + import { assert, describe, expect, it, vi } from 'vitest' | ||
| 2 | 3 | import transform from '../../../../src/webpack/loaders/transform' | |
| 3 | 4 | ||
| 4 | 5 | describe('transform loader', () => { | |
@@ -104,4 +105,40 @@ describe('transform loader', () => { | |||
| 104 | 105 | expect(handlerMock).toHaveBeenCalled() | |
| 105 | 106 | expect(mockCallback).toHaveBeenCalledWith(error) | |
| 106 | 107 | }) | |
| 108 | + | ||
| 109 | + it('should include input source map on native build context', async () => { | ||
| 110 | + const source = 'source code' | ||
| 111 | + const map = 'source map' | ||
| 112 | + const transformedCode = 'transformed code' | ||
| 113 | + const transformedMap = 'transformed map' | ||
| 114 | + | ||
| 115 | + let handlerSource: string | undefined | ||
| 116 | + let handlerId: string | undefined | ||
| 117 | + let handlerNativeBuildContext: NativeBuildContext | undefined | ||
| 118 | + const handlerMock = vi.fn().mockImplementation(function (this: UnpluginBuildContext, source: string, id: string) { | ||
| 119 | + handlerSource = source | ||
| 120 | + handlerId = id | ||
| 121 | + handlerNativeBuildContext = this.getNativeBuildContext?.() | ||
| 122 | + return { code: transformedCode, map: transformedMap } | ||
| 123 | + }) | ||
| 124 | + | ||
| 125 | + mockLoaderContext.query = { | ||
| 126 | + plugin: { | ||
| 127 | + transform: { | ||
| 128 | + handler: handlerMock, | ||
| 129 | + filter: vi.fn().mockReturnValue(true), | ||
| 130 | + }, | ||
| 131 | + }, | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + await transform.call(mockLoaderContext as any, source, map) | ||
| 135 | + | ||
| 136 | + expect(handlerMock).toHaveBeenCalled() | ||
| 137 | + expect(handlerSource).toBe(source) | ||
| 138 | + expect(handlerId).toBe(mockLoaderContext.resource) | ||
| 139 | + assert(handlerNativeBuildContext?.framework === 'webpack') | ||
| 140 | + expect(handlerNativeBuildContext?.inputSourceMap).toBe(map) | ||
| 141 | + | ||
| 142 | + expect(mockCallback).toHaveBeenCalledWith(null, transformedCode, transformedMap) | ||
| 143 | + }) | ||
| 107 | 144 | }) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments