Skip to content

Commit a1d7009

Browse files
committed
fix: support Vite+ pnpm workspaces
1 parent 37b0c04 commit a1d7009

22 files changed

Lines changed: 431 additions & 201 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"acorn": "catalog:",
2727
"acorn-walk": "catalog:",
2828
"element-plus": "catalog:",
29+
"esbuild": "catalog:",
2930
"eslint": "catalog:",
3031
"eslint-config-prettier": "catalog:",
3132
"eslint-plugin-unused-imports": "catalog:",
@@ -47,7 +48,6 @@
4748
"solid-js": "catalog:",
4849
"terser": "catalog:",
4950
"tsdown": "catalog:",
50-
"tsx": "catalog:",
5151
"typescript": "catalog:",
5252
"typescript-eslint": "catalog:",
5353
"unimport": "catalog:",

packages/create-monkey/scripts/update.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pnpm exec tsx "./scripts/update_template_dependency.ts"
1+
node "./scripts/update_template_dependency.ts"
22

33
foreach ($file in Get-ChildItem){
44
if($file.name -like 'template-*'){
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# What's Changed
22

3-
- feat: support web workers in dev (#291)
3+
## v8.1.1
4+
5+
- fix: resolve project paths from the Vite root for Vite+ pnpm workspaces
6+
- chore: run maintainer TypeScript scripts with native Node.js

packages/vite-plugin-monkey/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-monkey",
3-
"version": "8.1.0",
3+
"version": "8.1.1",
44
"description": "A vite plugin server and build your.user.js for userscript engine like Tampermonkey and Violentmonkey and Greasemonkey",
55
"main": "src/node/index.ts",
66
"type": "module",
@@ -17,9 +17,10 @@
1717
"types": "dist/node/index.d.mts"
1818
},
1919
"scripts": {
20+
"test": "node --test test/workspace-root.test.ts",
2021
"prebuild": "tsc",
2122
"build": "tsdown",
22-
"postbuild": "tsx ./scripts/postbuild.ts",
23+
"postbuild": "node ./scripts/postbuild.ts",
2324
"postpublish": "curl -X PUT https://registry-direct.npmmirror.com/vite-plugin-monkey/sync"
2425
},
2526
"keywords": [

packages/vite-plugin-monkey/scripts/transform_context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const getNodeLeadingComment = (node: ts.Node) => {
5454

5555
const targetCode = [
5656
`// The current code file is automatically generated using the following command
57-
// pnpm -F vite-plugin-monkey exec tsx ./scripts/transform_context.ts
57+
// pnpm -F vite-plugin-monkey exec node ./scripts/transform_context.ts
5858
// This command is used to avoid the tedious process of manually copying the code`,
5959
`export {}`,
6060
`import { monkeyWindow as w } from './window.ts';`,

packages/vite-plugin-monkey/src/client/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// The current code file is automatically generated using the following command
2-
// pnpm -F vite-plugin-monkey exec tsx ./scripts/transform_context.ts
2+
// pnpm -F vite-plugin-monkey exec node ./scripts/transform_context.ts
33
// This command is used to avoid the tedious process of manually copying the code
44

55
export {};

packages/vite-plugin-monkey/src/node/index.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Plugin } from 'vite';
2+
import path from 'node:path';
23
import factorys from './plugins/index.ts';
34
import { resolvedOption } from './utils/option.ts';
45
import type { MonkeyOption, ResolvedMonkeyOption } from './utils/types.ts';
@@ -10,13 +11,33 @@ export type * from './types.ts';
1011
export * as cdn from './cdn.ts';
1112

1213
export default (pluginOption: MonkeyOption): Plugin[] => {
13-
let option: Promise<ResolvedMonkeyOption>;
14-
const getOption = () => {
15-
return (option ??= resolvedOption(pluginOption));
14+
let option: Promise<ResolvedMonkeyOption> | undefined;
15+
let root = '';
16+
const getOption = (configRoot = root) => {
17+
if (!option) {
18+
root = path.resolve(configRoot);
19+
option = resolvedOption(pluginOption, root);
20+
}
21+
return option;
1622
};
17-
return factorys
18-
.map((f) => f(getOption, pluginOption))
19-
.filter(Boolean) as Plugin[];
23+
const rootPlugin: Plugin = {
24+
name: 'monkey:root',
25+
config: {
26+
order: 'pre',
27+
handler(config) {
28+
// Vite+ may use the pnpm workspace root as cwd, so prefer the Vite root
29+
// to resolve the actual workspace package and its relative entry.
30+
root = path.resolve(config.root ?? '');
31+
option = undefined;
32+
},
33+
},
34+
};
35+
return [
36+
rootPlugin,
37+
...(factorys
38+
.map((f) => f(getOption, pluginOption))
39+
.filter(Boolean) as Plugin[]),
40+
];
2041
};
2142

2243
/**

packages/vite-plugin-monkey/src/node/plugins/buildBundle.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ const polyfillId = '\0vite/legacy-polyfills';
2727
const systemJsImportMapPrefix = `user`;
2828

2929
export const buildBundleFactory = (
30-
getOption: () => Promise<ResolvedMonkeyOption>,
30+
getOption: (root?: string) => Promise<ResolvedMonkeyOption>,
3131
): Plugin => {
3232
let option: ResolvedMonkeyOption;
3333
let viteConfig: ResolvedConfig;
3434
return {
3535
name: 'monkey:buildBundle',
3636
apply: 'build',
3737
enforce: 'post',
38-
async config() {
39-
option = await getOption();
38+
config: {
39+
order: 'post',
40+
async handler(config) {
41+
option = await getOption(config.root);
42+
},
4043
},
4144
async configResolved(resolvedConfig) {
4245
viteConfig = resolvedConfig;

packages/vite-plugin-monkey/src/node/plugins/config.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,50 @@ import type { Plugin } from 'vite';
22
import type { ResolvedMonkeyOption } from '../utils/types.ts';
33

44
export const configFactory = (
5-
getOption: () => Promise<ResolvedMonkeyOption>,
5+
getOption: (root?: string) => Promise<ResolvedMonkeyOption>,
66
): Plugin => {
77
let option: ResolvedMonkeyOption;
88
return {
99
name: 'monkey:config',
10-
async config(userConfig) {
11-
option = await getOption();
12-
return {
13-
resolve: {
14-
alias: {
15-
[option.clientAlias]: 'vite-plugin-monkey/dist/client',
16-
},
17-
},
18-
build: {
19-
assetsInlineLimit: Number.MAX_SAFE_INTEGER,
20-
chunkSizeWarningLimit: Number.MAX_SAFE_INTEGER,
21-
assetsDir: './',
22-
cssCodeSplit: false,
23-
minify: userConfig.build?.minify ?? false,
24-
cssMinify: userConfig.build?.cssMinify ?? true,
25-
sourcemap: false,
26-
rolldownOptions: {
27-
input: option.entry,
28-
onLog(level, log, defaultHandler) {
29-
// ignore top-level await warning
30-
if (
31-
level === 'warn' &&
32-
log.code === 'TOLERATED_TRANSFORM' &&
33-
log.message.includes('Top-level await is not available')
34-
) {
35-
return;
36-
}
37-
defaultHandler(level, log);
10+
config: {
11+
order: 'post',
12+
async handler(userConfig) {
13+
option = await getOption(userConfig.root);
14+
return {
15+
resolve: {
16+
alias: {
17+
[option.clientAlias]: 'vite-plugin-monkey/dist/client',
3818
},
39-
experimental: {
40-
// https://github.com/rolldown/rolldown/issues/9006
41-
attachDebugInfo: 'none',
19+
},
20+
build: {
21+
assetsInlineLimit: Number.MAX_SAFE_INTEGER,
22+
chunkSizeWarningLimit: Number.MAX_SAFE_INTEGER,
23+
assetsDir: './',
24+
cssCodeSplit: false,
25+
minify: userConfig.build?.minify ?? false,
26+
cssMinify: userConfig.build?.cssMinify ?? true,
27+
sourcemap: false,
28+
rolldownOptions: {
29+
input: option.entry,
30+
onLog(level, log, defaultHandler) {
31+
// ignore top-level await warning
32+
if (
33+
level === 'warn' &&
34+
log.code === 'TOLERATED_TRANSFORM' &&
35+
log.message.includes('Top-level await is not available')
36+
) {
37+
return;
38+
}
39+
defaultHandler(level, log);
40+
},
41+
experimental: {
42+
// https://github.com/rolldown/rolldown/issues/9006
43+
attachDebugInfo: 'none',
44+
},
4245
},
4346
},
44-
},
45-
};
47+
};
48+
},
4649
},
4750
};
4851
};

packages/vite-plugin-monkey/src/node/plugins/css.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default undefined;
5353
`.trimStart();
5454

5555
export const cssFactory = (
56-
getOption: () => Promise<ResolvedMonkeyOption>,
56+
getOption: (root?: string) => Promise<ResolvedMonkeyOption>,
5757
): Plugin => {
5858
let option: ResolvedMonkeyOption;
5959
const isCssImport = async (
@@ -83,15 +83,18 @@ export const cssFactory = (
8383
name: 'monkey:css',
8484
apply: 'build',
8585
enforce: 'post',
86-
async config() {
87-
option = await getOption();
88-
return {
89-
build: {
90-
rolldownOptions: {
91-
external: [cssModuleId],
86+
config: {
87+
order: 'post',
88+
async handler(config) {
89+
option = await getOption(config.root);
90+
return {
91+
build: {
92+
rolldownOptions: {
93+
external: [cssModuleId],
94+
},
9295
},
93-
},
94-
};
96+
};
97+
},
9598
},
9699
resolveId(source) {
97100
if (source.endsWith(staticCssIdSuffix)) return source;

0 commit comments

Comments
 (0)