Skip to content

Commit

Permalink
feat: improve json5-loader imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Aug 7, 2024
1 parent 78661a0 commit 5c4e08a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
18 changes: 13 additions & 5 deletions src/core/createRspackCompiler.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -121,7 +129,7 @@ function resolveRspackOptions({
rules: [
{
test: /\.json5?$/,
loader: path.join(packageDir, 'json5-loader.cjs'),
loader: require.resolve('#json5-loader'),
type: 'javascript/auto',
},
{
Expand Down
8 changes: 8 additions & 0 deletions src/json5-loader.cts
Original file line number Diff line number Diff line change
@@ -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))}`
}
11 changes: 0 additions & 11 deletions src/json5-loader.ts

This file was deleted.

0 comments on commit 5c4e08a

Please sign in to comment.