diff --git a/src/core/createRspackCompiler.ts b/src/core/createRspackCompiler.ts index 9c9fe85..e31cc4a 100644 --- a/src/core/createRspackCompiler.ts +++ b/src/core/createRspackCompiler.ts @@ -1,9 +1,11 @@ -import path from 'node:path' +import { createRequire } from 'node:module' import type { Compiler, RspackOptions, RspackPluginInstance } from '@rspack/core' import * as rspackCore from '@rspack/core' import color from 'picocolors' import isCore from 'is-core-module' -import { packageDir, vfs } from './utils' +import { vfs } from './utils' + +const require = createRequire(import.meta.url) export interface CompilerOptions { cwd: string @@ -23,8 +25,14 @@ export function createCompiler( async function handler(err: Error | null, stats?: rspackCore.Stats) { const name = '[rspack:mock]' - const logError = stats?.compilation.getLogger(name).error - || ((...args: string[]) => console.error(color.red(name), ...args)) + const logError = (...args: any[]) => { + if (stats) { + stats.compilation.getLogger(name).error(...args) + } + else { + console.error(color.red(name), ...args) + } + } if (err) { logError(err.stack || err) @@ -121,7 +129,7 @@ function resolveRspackOptions({ rules: [ { test: /\.json5?$/, - loader: path.join(packageDir, 'json5-loader.cjs'), + loader: require.resolve('#json5-loader'), type: 'javascript/auto', }, { diff --git a/src/json5-loader.cts b/src/json5-loader.cts new file mode 100644 index 0000000..d5913b4 --- /dev/null +++ b/src/json5-loader.cts @@ -0,0 +1,8 @@ +import JSON5 from 'json5' + +module.exports = function (content: string): string { + if (!content) + return 'export default {}' + + return `export default ${JSON.stringify(JSON5.parse(content))}` +} diff --git a/src/json5-loader.ts b/src/json5-loader.ts deleted file mode 100644 index 1bdfe69..0000000 --- a/src/json5-loader.ts +++ /dev/null @@ -1,11 +0,0 @@ -import JSON5 from 'json5' -import type { LoaderDefinitionFunction } from '@rspack/core' - -const json5Loader: LoaderDefinitionFunction = function (content) { - if (!content) - return 'export default {}' - - return `export default ${JSON.stringify(JSON5.parse(content))}` -} - -export default json5Loader