Skip to content

Commit 2bf60cb

Browse files
authored
Merge pull request #5084 from VisActor/feat/react-vtable-19
fix: vue build error
2 parents cb1e225 + 576d64a commit 2bf60cb

2 files changed

Lines changed: 113 additions & 12 deletions

File tree

tools/bundler/bin/index.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
11
#!/usr/bin/env node
22
"use strict";
33

4-
require('../output/bootstrap');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const childProcess = require('child_process');
7+
8+
const bootstrapPath = path.join(__dirname, '../output/bootstrap');
9+
10+
function tryRequireBootstrap() {
11+
return require(bootstrapPath);
12+
}
13+
14+
try {
15+
tryRequireBootstrap();
16+
} catch (err) {
17+
const isMissingBootstrap =
18+
err &&
19+
err.code === 'MODULE_NOT_FOUND' &&
20+
typeof err.message === 'string' &&
21+
(err.message.includes(bootstrapPath) || err.message.includes('output/bootstrap'));
22+
23+
if (!isMissingBootstrap) {
24+
throw err;
25+
}
26+
27+
const projectRoot = path.join(__dirname, '..');
28+
const tsconfigPath = path.join(projectRoot, 'tsconfig.json');
29+
const outputDir = path.join(projectRoot, 'output');
30+
31+
if (!fs.existsSync(tsconfigPath)) {
32+
throw err;
33+
}
34+
35+
try {
36+
const tscBin = require.resolve('typescript/bin/tsc', { paths: [projectRoot] });
37+
fs.mkdirSync(outputDir, { recursive: true });
38+
const result = childProcess.spawnSync(process.execPath, [tscBin, '--project', tsconfigPath], { stdio: 'inherit' });
39+
if (result.status !== 0) {
40+
process.exit(result.status || 1);
41+
}
42+
} catch (compileErr) {
43+
throw err;
44+
}
45+
46+
tryRequireBootstrap();
47+
}

tools/bundler/src/logic/rollup.config.ts

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,68 @@ import Alias from '@rollup/plugin-alias';
1515
import postcss from 'rollup-plugin-postcss';
1616
import strip from '@rollup/plugin-strip';
1717
import vue from '@vitejs/plugin-vue';
18+
import * as ts from 'typescript';
1819
import * as path from 'path';
1920
import { Config } from './config';
2021

2122
const useTypescriptPlugin2 = process.env.USE_TYPESCRIPT2 === 'true';
22-
const typescript = useTypescriptPlugin2 ? require('rollup-plugin-typescript2') : require('@rollup/plugin-typescript');
23+
const typescript1 = require('@rollup/plugin-typescript');
24+
const typescript2 = require('rollup-plugin-typescript2');
25+
26+
function transpileVueTsSfc(): Plugin {
27+
return {
28+
name: 'transpile-vue-ts-sfc',
29+
transform(code, id) {
30+
if (!id.includes('?vue')) {
31+
return null;
32+
}
33+
if (!id.includes('&lang.ts') && !id.includes('&lang.tsx')) {
34+
return null;
35+
}
36+
const result = ts.transpileModule(code, {
37+
compilerOptions: {
38+
target: ts.ScriptTarget.ES2017,
39+
module: ts.ModuleKind.ESNext,
40+
jsx: ts.JsxEmit.Preserve,
41+
sourceMap: true
42+
},
43+
fileName: id
44+
});
45+
return {
46+
code: result.outputText,
47+
map: result.sourceMapText ? JSON.parse(result.sourceMapText) : null
48+
};
49+
}
50+
};
51+
}
52+
53+
function transpileTsForRollup(): Plugin {
54+
return {
55+
name: 'transpile-ts-for-rollup',
56+
transform(code, id) {
57+
if (id.endsWith('.d.ts')) {
58+
return null;
59+
}
60+
const isTsLike = id.endsWith('.ts') || id.endsWith('.tsx') || id.includes('&lang.ts') || id.includes('&lang.tsx');
61+
if (!isTsLike) {
62+
return null;
63+
}
64+
const result = ts.transpileModule(code, {
65+
compilerOptions: {
66+
target: ts.ScriptTarget.ES2017,
67+
module: ts.ModuleKind.ESNext,
68+
jsx: ts.JsxEmit.Preserve,
69+
sourceMap: true
70+
},
71+
fileName: id
72+
});
73+
return {
74+
code: result.outputText,
75+
map: result.sourceMapText ? JSON.parse(result.sourceMapText) : null
76+
};
77+
}
78+
};
79+
}
2380

2481
function getExternal(
2582
rawPackageJson: RawPackageJson,
@@ -42,25 +99,26 @@ export function getRollupOptions(
4299
config: Config,
43100
tsconfigOverride?: Record<string, unknown>
44101
): RollupOptions {
45-
const tsOption = {
46-
tsconfig: path.resolve(projectRoot, config.tsconfig)
47-
};
48-
49-
if (useTypescriptPlugin2 && tsconfigOverride) {
50-
(tsOption as any).tsconfigOverride = tsconfigOverride;
51-
}
102+
const tsconfigPath = path.resolve(projectRoot, config.tsconfig);
103+
const typescriptPlugin: Plugin = useTypescriptPlugin2
104+
? typescript2(tsconfigOverride ? { tsconfig: tsconfigPath, tsconfigOverride } : { tsconfig: tsconfigPath })
105+
: typescript1({ tsconfig: tsconfigPath });
52106
return {
53107
input: entry,
54108
external: getExternal(rawPackageJson, config.external),
55109
...config.rollupOptions,
56110
plugins: [
57-
resolve(),
111+
resolve({
112+
extensions: ['.mjs', '.js', '.jsx', '.json', '.node', '.ts', '.tsx', '.mts', '.cts', '.vue']
113+
}),
58114
commonjs(),
59115
vue(),
60116
// typescript(),
61-
babel({ ...babelPlugins, babelHelpers: 'bundled' }),
62117
replace({ ...config.envs, preventAssignment: true }),
63-
typescript(tsOption),
118+
transpileVueTsSfc(),
119+
typescriptPlugin,
120+
...(useTypescriptPlugin2 ? [transpileTsForRollup()] : []),
121+
babel({ ...babelPlugins, babelHelpers: 'bundled' }),
64122
postcss({
65123
extensions: ['.css']
66124
}),

0 commit comments

Comments
 (0)