Skip to content

Commit b925167

Browse files
committed
chore(plugin-vue): pre-copy vue-loader
1 parent a078c23 commit b925167

File tree

9 files changed

+108
-16
lines changed

9 files changed

+108
-16
lines changed

packages/plugin-vue/package.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,40 @@
2020
"main": "./dist/index.cjs",
2121
"types": "./dist/index.d.ts",
2222
"files": [
23-
"dist"
23+
"dist",
24+
"compiled"
2425
],
2526
"scripts": {
2627
"build": "rslib build",
2728
"dev": "rslib build -w",
2829
"bump": "pnpx bumpp --no-tag"
2930
},
3031
"dependencies": {
31-
"vue-loader": "^17.4.2",
32-
"webpack": "^5.102.1"
32+
"chalk": "^4.1.0",
33+
"hash-sum": "^2.0.0",
34+
"webpack": "^5.102.1",
35+
"watchpack": "^2.4.0"
3336
},
3437
"devDependencies": {
3538
"@rsbuild/core": "workspace:*",
3639
"@rslib/core": "0.15.1",
3740
"@scripts/test-helper": "workspace:*",
3841
"@types/node": "^22.18.11",
3942
"typescript": "^5.9.3",
40-
"vue": "^3.5.22"
43+
"vue": "^3.5.22",
44+
"vue-loader": "^17.4.2"
4145
},
4246
"peerDependencies": {
4347
"@rsbuild/core": "1.x"
4448
},
49+
"peerDependenciesMeta": {
50+
"@vue/compiler-sfc": {
51+
"optional": true
52+
},
53+
"vue": {
54+
"optional": true
55+
}
56+
},
4557
"publishConfig": {
4658
"access": "public",
4759
"registry": "https://registry.npmjs.org/"
Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
13
import { dualPackage } from '@rsbuild/config/rslib.config.ts';
4+
import { defineConfig, rsbuild } from '@rslib/core';
25

3-
export default dualPackage;
6+
const pluginTrimVueLoaderPackageJson: rsbuild.RsbuildPlugin = {
7+
name: 'trim-vue-loader-package-json',
8+
setup(api) {
9+
api.onBeforeBuild(() => {
10+
const cwd = process.cwd();
11+
const sourceRoot = path.join(cwd, 'node_modules/vue-loader');
12+
const targetRoot = path.join(cwd, 'compiled/vue-loader');
13+
14+
fs.rmSync(targetRoot, { recursive: true, force: true });
15+
16+
fs.cpSync(path.join(sourceRoot, 'dist'), targetRoot, {
17+
recursive: true,
18+
});
19+
20+
for (const file of ['package.json', 'LICENSE']) {
21+
fs.copyFileSync(
22+
path.join(sourceRoot, file),
23+
path.join(targetRoot, file),
24+
);
25+
}
26+
});
27+
28+
api.onAfterBuild(() => {
29+
const pkgPath = path.join(
30+
process.cwd(),
31+
'compiled/vue-loader/package.json',
32+
);
33+
const pkgJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
34+
const fields = ['name', 'author', 'version', 'funding', 'license'];
35+
const trimmed = Object.fromEntries(
36+
fields
37+
.filter((field) => field in pkgJson)
38+
.map((field) => [field, pkgJson[field]]),
39+
);
40+
fs.writeFileSync(
41+
pkgPath,
42+
`${JSON.stringify(trimmed, null, 2)}\n`,
43+
'utf8',
44+
);
45+
});
46+
},
47+
};
48+
49+
export default defineConfig(
50+
rsbuild.mergeRsbuildConfig(dualPackage, {
51+
lib: [],
52+
plugins: [pluginTrimVueLoaderPackageJson],
53+
}),
54+
);

packages/plugin-vue/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { createRequire } from 'node:module';
22
import type { EnvironmentConfig, RsbuildPlugin } from '@rsbuild/core';
3-
import { type VueLoaderOptions, VueLoaderPlugin } from 'vue-loader';
3+
import {
4+
type VueLoaderOptions,
5+
VueLoaderPlugin,
6+
} from '../compiled/vue-loader/index.js';
47
import { applySplitChunksRule } from './splitChunks.js';
58

69
const require = createRequire(import.meta.url);
@@ -90,7 +93,7 @@ export function pluginVue(options: PluginVueOptions = {}): RsbuildPlugin {
9093
.rule(CHAIN_ID.RULE.VUE)
9194
.test(VUE_REGEXP)
9295
.use(CHAIN_ID.USE.VUE)
93-
.loader(require.resolve('vue-loader'))
96+
.loader(require.resolve('../compiled/vue-loader/index.js'))
9497
.options(vueLoaderOptions);
9598

9699
// Support for lang="postcss" and lang="pcss" in SFC

packages/plugin-vue/tests/__snapshots__/index.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`plugin-vue > should add vue-loader and VueLoaderPlugin correctly 1`] =
55
"test": /\\\\\\.vue\\$/,
66
"use": [
77
{
8-
"loader": "<ROOT>/node_modules/<PNPM_INNER>/vue-loader/dist/index.js",
8+
"loader": "<ROOT>/packages/plugin-vue/compiled/vue-loader/index.js",
99
"options": {
1010
"compilerOptions": {
1111
"preserveWhitespace": false,
@@ -41,7 +41,7 @@ exports[`plugin-vue > should allow to configure vueLoader options 1`] = `
4141
"test": /\\\\\\.vue\\$/,
4242
"use": [
4343
{
44-
"loader": "<ROOT>/node_modules/<PNPM_INNER>/vue-loader/dist/index.js",
44+
"loader": "<ROOT>/packages/plugin-vue/compiled/vue-loader/index.js",
4545
"options": {
4646
"compilerOptions": {
4747
"preserveWhitespace": false,

packages/plugin-vue/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"path": "../core"
1212
}
1313
],
14-
"include": ["src"]
14+
"include": ["src", "compiled"]
1515
}

patches/vue-loader.patch

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/dist/pluginWebpack5.js b/dist/pluginWebpack5.js
2+
index 7c5eadf2ad45157aed2fd23a83ae4b1a2fb4b8ac..b9cd4eb701c366fa7f7f4782a20d4e3568705a94 100644
3+
--- a/dist/pluginWebpack5.js
4+
+++ b/dist/pluginWebpack5.js
5+
@@ -61,6 +61,7 @@ const ruleSetCompiler = new RuleSetCompiler([
6+
new BasicEffectRulePlugin('resolve'),
7+
new BasicEffectRulePlugin('generator'),
8+
new BasicEffectRulePlugin('layer'),
9+
+ new BasicEffectRulePlugin('with'),
10+
new UseEffectRulePlugin(),
11+
]);
12+
class VueLoaderPlugin {

pnpm-lock.yaml

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ packages:
1010
- '!**/create-rsbuild/template-*/**'
1111
- '!**/test-temp-*/**'
1212

13+
engineStrict: true
14+
1315
hoistPattern: []
1416

1517
patchedDependencies:
@@ -18,6 +20,6 @@ patchedDependencies:
1820
1921
2022
url-loader: patches/[email protected]
23+
vue-loader: patches/vue-loader.patch
2124

2225
strictPeerDependencies: false
23-
engineStrict: true

rstest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig({
1313
alias,
1414
},
1515
output: {
16-
externals: ['@rsbuild/core'],
16+
externals: ['@rsbuild/core', /\/compiled\/vue-loader/],
1717
},
1818
name: 'node',
1919
globals: true,

0 commit comments

Comments
 (0)