Skip to content

Commit f75d59c

Browse files
committed
feat: remove module resolving errors
1 parent 3812766 commit f75d59c

File tree

8 files changed

+64
-29
lines changed

8 files changed

+64
-29
lines changed

bun.lockb

-136 Bytes
Binary file not shown.

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Document</title>
7-
<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
8-
<script>eruda.init();</script>
7+
<!--<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
8+
<script>eruda.init();</script>-->
99
</head>
1010
<body>
1111
<script src="/playground/main.jsx" type="module"></script>

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@
170170
}
171171
},
172172
"patchedDependencies": {
173-
"@scratch/paper@0.11.0": "patches/@scratch%2Fpaper@0.11.0.patch",
174-
"scratch-paint@3.0.20": "patches/scratch-paint@3.0.20.patch",
175-
"parse-color@1.0.0": "patches/parse-color@1.0.0.patch"
173+
"scratch-paint@3.0.20": "patches/scratch-paint@3.0.20.patch"
176174
}
177175
}

patches/@scratch%2Fpaper@0.11.0.patch

Lines changed: 0 additions & 10 deletions
This file was deleted.

patches/parse-color@1.0.0.patch

Lines changed: 0 additions & 13 deletions
This file was deleted.

plugins/cjs.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { Plugin } from 'vite'
2+
import * as esbuild from 'esbuild'
3+
import * as path from 'node:path'
4+
5+
const CJS_PATCHES = [
6+
'@scratch/paper',
7+
'parse-color',
8+
'react-popover'
9+
]
10+
11+
const cached = new Map<string, string>()
12+
const getBuild = async (id: string) => {
13+
if (cached.has(id)) {
14+
return cached.get(id)
15+
}
16+
const result = await esbuild.build({
17+
entryPoints: [id],
18+
format: 'esm',
19+
platform: 'browser',
20+
target: 'esnext',
21+
bundle: true,
22+
write: false,
23+
logLevel: 'error',
24+
})
25+
const code = result.outputFiles?.[0].text
26+
if (!code) {
27+
throw result.errors[0]
28+
}
29+
cached.set(id, code)
30+
return code
31+
}
32+
33+
export const cjsPlugin = (): Plugin => {
34+
return {
35+
name: 'vite-plugin-cjs',
36+
async load(id, options) {
37+
let isPatch = false
38+
for (const patch of CJS_PATCHES) {
39+
if (id.includes(`node_modules/${patch}`)) {
40+
isPatch = true
41+
break
42+
}
43+
}
44+
if (!isPatch) {
45+
return
46+
}
47+
const code = await getBuild(id)
48+
if (!code) {
49+
return
50+
}
51+
console.log('patched', id)
52+
return {
53+
code
54+
}
55+
},
56+
}
57+
}

src/containers/paint-editor-wrapper.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PropTypes from 'prop-types';
22
import React from 'react';
33
import bindAll from 'lodash.bindall';
44
import VM from 'scratch-vm';
5-
//import PaintEditor from 'scratch-paint';
5+
import PaintEditor from 'scratch-paint';
66
import {inlineSvgFonts} from 'scratch-svg-renderer';
77

88
import {connect} from 'react-redux';

vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from 'node:path'
44
import * as fs from 'node:fs/promises'
55
import * as url from 'node:url'
66
import { existsSync } from 'node:fs'
7+
import { cjsPlugin } from './plugins/cjs.ts'
78

89
const scratchGuiPlugin = (): Plugin => {
910
let resolvedConfig!: ResolvedConfig
@@ -120,9 +121,11 @@ const allModuleCSSPlugin = (): Plugin => {
120121
}*/
121122
}
122123
}
124+
123125
export default defineConfig({
124126
plugins: [
125127
allModuleCSSPlugin(),
128+
cjsPlugin(),
126129
reactVirtualized(),
127130
scratchGuiPlugin()
128131
],

0 commit comments

Comments
 (0)