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

refactor: use webpack instead of webpack-sources #431

Merged
merged 7 commits into from
Nov 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@
"test": "nr test:build && vitest run --pool=forks",
"test:build": "jiti scripts/buildFixtures.ts"
},
"peerDependencies": {
"webpack-sources": "^3"
},
"peerDependenciesMeta": {
"webpack-sources": {
"optional": true
}
},
"dependencies": {
"acorn": "^8.14.0",
"webpack-virtual-modules": "^0.6.2"
Expand Down
31 changes: 14 additions & 17 deletions pnpm-lock.yaml

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

28 changes: 14 additions & 14 deletions src/webpack/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ export function contextOptionsFromCompilation(compilation: Compilation): Context
}
}

export function createBuildContext(options: ContextOptions, compiler: Compiler, compilation?: Compilation, loaderContext?: LoaderContext<{ unpluginName: string }>): UnpluginBuildContext {
const require = createRequire(import.meta.url)
const sources = require('webpack-sources') as typeof import('webpack-sources')
export function getSource(fileSource: string | Uint8Array) {
// Create a require function to load webpack-sources as webpack in order to maintain compatibility.
const webpackRequire = createRequire(require.resolve('webpack'))
const RawSource = (webpackRequire('webpack-sources') as typeof import('webpack-sources')).RawSource

return new RawSource(
typeof fileSource === 'string'
? fileSource
// Converting to string to support Webpack 4's RawSource.
: Buffer.from(fileSource.buffer).toString('utf-8'),
)
}

export function createBuildContext(options: ContextOptions, compiler: Compiler, compilation?: Compilation, loaderContext?: LoaderContext<{ unpluginName: string }>): UnpluginBuildContext {
return {
parse(code: string, opts: any = {}) {
return Parser.parse(code, {
Expand All @@ -45,17 +55,7 @@ export function createBuildContext(options: ContextOptions, compiler: Compiler,
throw new Error('unplugin/webpack: emitFile outside supported hooks (buildStart, buildEnd, load, transform, watchChange)')
compilation.emitAsset(
outFileName,
sources
? new sources.RawSource(
// @ts-expect-error types mismatch
typeof emittedFile.source === 'string'
? emittedFile.source
: Buffer.from(emittedFile.source),
) as any
: {
source: () => emittedFile.source,
size: () => emittedFile.source!.length,
},
getSource(emittedFile.source) as import('webpack').sources.Source,
)
}
},
Expand Down