Skip to content

Commit

Permalink
fix(webpack): lazy import webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 28, 2024
1 parent 19c95b1 commit c267c44
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/webpack/context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Compilation, Compiler, LoaderContext } from 'webpack'
import type { Compilation, Compiler, LoaderContext, sources } from 'webpack'
import type { UnpluginBuildContext, UnpluginContext, UnpluginMessage } from '../types'
import { Buffer } from 'buffer'
import { createRequire } from 'module'
import { resolve } from 'path'
import process from 'process'
import { Parser } from 'acorn'
import * as webpack from 'webpack'

interface ContextOptions {
addWatchFile: (file: string) => void
Expand All @@ -22,7 +22,9 @@ export function contextOptionsFromCompilation(compilation: Compilation): Context
}
}

export function getSource(fileSource: string | Uint8Array): webpack.sources.RawSource {
const require = createRequire(import.meta.url)
export function getSource(fileSource: string | Uint8Array): sources.RawSource {
const webpack = require('webpack')
return new webpack.sources.RawSource(
typeof fileSource === 'string' ? fileSource : Buffer.from(fileSource.buffer),
)
Expand Down

0 comments on commit c267c44

Please sign in to comment.