Skip to content

Commit 5c4e08a

Browse files
committed
feat: improve json5-loader imports
1 parent 78661a0 commit 5c4e08a

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/core/createRspackCompiler.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import path from 'node:path'
1+
import { createRequire } from 'node:module'
22
import type { Compiler, RspackOptions, RspackPluginInstance } from '@rspack/core'
33
import * as rspackCore from '@rspack/core'
44
import color from 'picocolors'
55
import isCore from 'is-core-module'
6-
import { packageDir, vfs } from './utils'
6+
import { vfs } from './utils'
7+
8+
const require = createRequire(import.meta.url)
79

810
export interface CompilerOptions {
911
cwd: string
@@ -23,8 +25,14 @@ export function createCompiler(
2325

2426
async function handler(err: Error | null, stats?: rspackCore.Stats) {
2527
const name = '[rspack:mock]'
26-
const logError = stats?.compilation.getLogger(name).error
27-
|| ((...args: string[]) => console.error(color.red(name), ...args))
28+
const logError = (...args: any[]) => {
29+
if (stats) {
30+
stats.compilation.getLogger(name).error(...args)
31+
}
32+
else {
33+
console.error(color.red(name), ...args)
34+
}
35+
}
2836

2937
if (err) {
3038
logError(err.stack || err)
@@ -121,7 +129,7 @@ function resolveRspackOptions({
121129
rules: [
122130
{
123131
test: /\.json5?$/,
124-
loader: path.join(packageDir, 'json5-loader.cjs'),
132+
loader: require.resolve('#json5-loader'),
125133
type: 'javascript/auto',
126134
},
127135
{

src/json5-loader.cts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import JSON5 from 'json5'
2+
3+
module.exports = function (content: string): string {
4+
if (!content)
5+
return 'export default {}'
6+
7+
return `export default ${JSON.stringify(JSON5.parse(content))}`
8+
}

src/json5-loader.ts

-11
This file was deleted.

0 commit comments

Comments
 (0)