@blocklet/pages-kit-runtime 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/lib/cjs/block-studio/build-lib.js +95 -0
- package/lib/cjs/block-studio/generate-wrapper-code.js +123 -0
- package/lib/cjs/block-studio/init-resource-router.js +166 -0
- package/lib/cjs/block-studio/plugins/_theme.js +7 -0
- package/lib/cjs/block-studio/plugins/index.js +19 -0
- package/lib/cjs/block-studio/plugins/vite-plugin-block-studio.js +176 -0
- package/lib/cjs/block-studio/plugins/vite-plugin-html-transform.js +245 -0
- package/lib/cjs/block-studio/plugins/vite-plugin-remote-script-localizer.js +211 -0
- package/lib/cjs/block-studio/utils.js +53 -0
- package/lib/cjs/client.js +24 -0
- package/lib/cjs/components/create-resource.js +32 -0
- package/lib/cjs/components/index.js +17 -0
- package/lib/cjs/index.js +2 -0
- package/lib/cjs/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/types/index.js +5 -0
- package/lib/cjs/utils/index.js +1 -0
- package/lib/esm/block-studio/build-lib.js +89 -0
- package/lib/esm/block-studio/generate-wrapper-code.js +87 -0
- package/lib/esm/block-studio/init-resource-router.js +124 -0
- package/lib/esm/block-studio/plugins/_theme.js +4 -0
- package/lib/esm/block-studio/plugins/index.js +3 -0
- package/lib/esm/block-studio/plugins/vite-plugin-block-studio.js +140 -0
- package/lib/esm/block-studio/plugins/vite-plugin-html-transform.js +241 -0
- package/lib/esm/block-studio/plugins/vite-plugin-remote-script-localizer.js +175 -0
- package/lib/esm/block-studio/utils.js +43 -0
- package/lib/esm/client.js +17 -0
- package/lib/esm/components/create-resource.js +29 -0
- package/lib/esm/components/index.js +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -0
- package/lib/esm/types/index.js +2 -0
- package/lib/esm/utils/index.js +1 -0
- package/lib/types/block-studio/build-lib.d.ts +3 -0
- package/lib/types/block-studio/generate-wrapper-code.d.ts +5 -0
- package/lib/types/block-studio/init-resource-router.d.ts +5 -0
- package/lib/types/block-studio/plugins/_theme.d.ts +1 -0
- package/lib/types/block-studio/plugins/index.d.ts +3 -0
- package/lib/types/block-studio/plugins/vite-plugin-block-studio.d.ts +6 -0
- package/lib/types/block-studio/plugins/vite-plugin-html-transform.d.ts +5 -0
- package/lib/types/block-studio/plugins/vite-plugin-remote-script-localizer.d.ts +8 -0
- package/lib/types/block-studio/utils.d.ts +14 -0
- package/lib/types/client.d.ts +11 -0
- package/lib/types/components/create-resource.d.ts +7 -0
- package/lib/types/components/index.d.ts +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -0
- package/lib/types/types/index.d.ts +39 -0
- package/lib/types/utils/index.d.ts +0 -0
- package/package.json +169 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/* eslint-disable no-await-in-loop */
|
|
11
|
+
/* eslint-disable no-console */
|
|
12
|
+
import chalk from 'chalk';
|
|
13
|
+
import { execSync, spawn } from 'child_process';
|
|
14
|
+
import { findComponentFiles } from './utils';
|
|
15
|
+
export function buildLib(options) {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
const workingDir = (options === null || options === void 0 ? void 0 : options.cwd) || process.cwd();
|
|
19
|
+
// Scan for all index files
|
|
20
|
+
const allBlocks = findComponentFiles({ cwd: workingDir }).map((entry) => {
|
|
21
|
+
return entry.blockName;
|
|
22
|
+
});
|
|
23
|
+
const filterModules = process.argv.includes('--filter')
|
|
24
|
+
? (_a = process.argv[process.argv.indexOf('--filter') + 1]) === null || _a === void 0 ? void 0 : _a.split(',')
|
|
25
|
+
: ((_b = process.env.BLOCK_FILTER) === null || _b === void 0 ? void 0 : _b.split(',')) || null;
|
|
26
|
+
const ignoreViteLog = false; // process.argv.includes('--log') ? false : process.env.BLOCK_LOG !== 'true';
|
|
27
|
+
const multiMode = process.argv.includes('--multi') || process.env.BLOCK_MULTI === 'true';
|
|
28
|
+
const libDir = 'lib';
|
|
29
|
+
const blocks = allBlocks.filter((name) => !filterModules || filterModules.includes(name || ''));
|
|
30
|
+
if (!blocks.length) {
|
|
31
|
+
console.log(chalk.yellow('No blocks founds'));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// Clean up the lib directory
|
|
35
|
+
yield execSync(`rm -rf ${libDir}`, { cwd: workingDir });
|
|
36
|
+
console.log(chalk.gray('Clean up the lib directory'));
|
|
37
|
+
// Start to build
|
|
38
|
+
console.log(chalk.cyan(`Start to build ${blocks.length} blocks in parallel\n`));
|
|
39
|
+
const canBuildBlocks = multiMode ? blocks : [blocks.join(',')];
|
|
40
|
+
const configFile = 'vite-lib.config';
|
|
41
|
+
const hasConfig = execSync(`ls ${configFile}.* 2>/dev/null || true`, {
|
|
42
|
+
cwd: workingDir,
|
|
43
|
+
encoding: 'utf-8',
|
|
44
|
+
}).trim() !== '';
|
|
45
|
+
try {
|
|
46
|
+
yield Promise.all(canBuildBlocks.map((blockName) => {
|
|
47
|
+
console.log(chalk.blue(`Building ${blockName} Block...`));
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const build = spawn('vite', [
|
|
50
|
+
'build',
|
|
51
|
+
'--filter',
|
|
52
|
+
`"${blockName}"`,
|
|
53
|
+
'--outDir',
|
|
54
|
+
libDir,
|
|
55
|
+
'--emptyOutDir=false',
|
|
56
|
+
hasConfig ? `--config ${configFile}.*` : '',
|
|
57
|
+
multiMode ? '--multi' : '',
|
|
58
|
+
], {
|
|
59
|
+
stdio: ignoreViteLog ? ['inherit', 'ignore', 'ignore'] : 'inherit',
|
|
60
|
+
shell: true,
|
|
61
|
+
cwd: workingDir,
|
|
62
|
+
});
|
|
63
|
+
build.on('close', (code) => {
|
|
64
|
+
if (code === 0) {
|
|
65
|
+
console.log(chalk.green(`Build ${blockName} successfully`));
|
|
66
|
+
resolve();
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
reject(new Error(`Build failed with code ${code}`));
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}));
|
|
74
|
+
console.log(chalk.green(`\nBuild ${blocks.length} blocks successfully`));
|
|
75
|
+
console.log(chalk.gray(`Generate the ${libDir} directory successfully`));
|
|
76
|
+
process.exit(0);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
console.error(chalk.red('Build failed:'), err);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const cwdArg = process.argv.indexOf('--cwd');
|
|
85
|
+
const cwd = cwdArg !== -1 ? process.argv[cwdArg + 1] : undefined;
|
|
86
|
+
buildLib({ cwd }).catch((err) => {
|
|
87
|
+
console.error(chalk.red('Build failed:'), err);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export function generateWrapperCode(_a) {
|
|
11
|
+
return __awaiter(this, arguments, void 0, function* ({ project, state }) {
|
|
12
|
+
const projectName = (project.name || project.id).toLowerCase().replaceAll(/[^a-z0-9]/g, '_');
|
|
13
|
+
const packageName = `@pages-kit-project/${projectName}`;
|
|
14
|
+
const packageJson = JSON.stringify({
|
|
15
|
+
name: packageName,
|
|
16
|
+
version: '0.0.1',
|
|
17
|
+
main: 'index.cjs',
|
|
18
|
+
module: 'index.js',
|
|
19
|
+
types: 'index.d.ts',
|
|
20
|
+
peerDependencies: {
|
|
21
|
+
'@blocklet/pages-kit-runtime': 'latest',
|
|
22
|
+
},
|
|
23
|
+
dependencies: {
|
|
24
|
+
'@blocklet/pages-kit-runtime': 'latest',
|
|
25
|
+
'@blocklet/pages-kit': 'latest',
|
|
26
|
+
},
|
|
27
|
+
exports: {
|
|
28
|
+
'.': {
|
|
29
|
+
require: './index.cjs',
|
|
30
|
+
import: './index.js',
|
|
31
|
+
types: './index.d.ts',
|
|
32
|
+
},
|
|
33
|
+
'./client': {
|
|
34
|
+
require: './client.cjs',
|
|
35
|
+
import: './client.js',
|
|
36
|
+
types: './client.d.ts',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
}, null, 2);
|
|
40
|
+
const index = `\
|
|
41
|
+
import { ProjectRuntime } from '@blocklet/pages-kit-runtime/client';
|
|
42
|
+
|
|
43
|
+
window.__PAGE_STATE__ = ${JSON.stringify(state, null, 2)};
|
|
44
|
+
window.__PROJECT_ID__ = "${project.id}";
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const ${projectName} = new ProjectRuntime({
|
|
48
|
+
state: window.__PAGE_STATE__,
|
|
49
|
+
projectId: window.__PROJECT_ID__,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export default ${projectName}
|
|
53
|
+
`;
|
|
54
|
+
const client = index;
|
|
55
|
+
const tsFiles = [
|
|
56
|
+
{ fileName: 'index.ts', content: index },
|
|
57
|
+
{ fileName: 'client.ts', content: client },
|
|
58
|
+
];
|
|
59
|
+
const ts = yield import('typescript');
|
|
60
|
+
const result = (yield Promise.all(tsFiles.map((_a) => __awaiter(this, [_a], void 0, function* ({ fileName, content }) {
|
|
61
|
+
const cjs = ts.transpileModule(content, {
|
|
62
|
+
fileName,
|
|
63
|
+
compilerOptions: { module: ts.ModuleKind.CommonJS },
|
|
64
|
+
});
|
|
65
|
+
const esm = ts.transpileModule(content, {
|
|
66
|
+
fileName,
|
|
67
|
+
compilerOptions: { module: ts.ModuleKind.ESNext },
|
|
68
|
+
});
|
|
69
|
+
return [
|
|
70
|
+
{
|
|
71
|
+
fileName: fileName.replace(/\.ts$/, '.cjs'),
|
|
72
|
+
content: cjs.outputText,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
fileName: fileName.replace(/\.ts$/, '.js'),
|
|
76
|
+
content: esm.outputText,
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
fileName: fileName.replace(/\.ts$/, '.d.ts'),
|
|
80
|
+
content,
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
})))).flat();
|
|
84
|
+
return [...result, { fileName: 'package.json', content: packageJson }];
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
export default generateWrapperCode;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { getResourceExportDir } from '@blocklet/sdk/lib/component';
|
|
11
|
+
import { spawn } from 'child_process';
|
|
12
|
+
import { Router } from 'express';
|
|
13
|
+
import fs from 'fs';
|
|
14
|
+
import path, { join } from 'path';
|
|
15
|
+
import { findComponentFiles } from './utils';
|
|
16
|
+
const DID = 'z2qa7rr3eUyVnWp2PCxEVARuUfLFh6cE5V2xV';
|
|
17
|
+
const RESOURCE_TYPE = 'block';
|
|
18
|
+
const allTag = '@ALL_COMPONENTS';
|
|
19
|
+
export function copyFile(src, dest) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const readStream = fs.createReadStream(src);
|
|
22
|
+
const writeStream = fs.createWriteStream(dest);
|
|
23
|
+
readStream.on('error', reject);
|
|
24
|
+
writeStream.on('error', reject);
|
|
25
|
+
writeStream.on('finish', resolve);
|
|
26
|
+
readStream.pipe(writeStream);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export function copyDirectory(src, dest) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
yield fs.promises.mkdir(dest, { recursive: true });
|
|
32
|
+
const entries = yield fs.promises.readdir(src, { withFileTypes: true });
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
const srcPath = path.join(src, entry.name);
|
|
35
|
+
const destPath = path.join(dest, entry.name);
|
|
36
|
+
if (entry.isDirectory()) {
|
|
37
|
+
yield copyDirectory(srcPath, destPath);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
yield copyFile(srcPath, destPath);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export function copyRecursive(src, dest) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const srcStats = yield fs.promises.stat(src);
|
|
48
|
+
if (srcStats.isDirectory()) {
|
|
49
|
+
yield copyDirectory(src, dest);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
yield copyFile(src, dest);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const getExportDir = (projectId, releaseId) => {
|
|
57
|
+
const exportDir = getResourceExportDir({ projectId, releaseId });
|
|
58
|
+
const dir = join(exportDir, DID, RESOURCE_TYPE);
|
|
59
|
+
return dir;
|
|
60
|
+
};
|
|
61
|
+
export const initResourceRouter = Router();
|
|
62
|
+
initResourceRouter.get('/resources', (_req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
const resources = [];
|
|
64
|
+
const rootDir = process.cwd();
|
|
65
|
+
const files = findComponentFiles({ cwd: rootDir });
|
|
66
|
+
for (const file of files) {
|
|
67
|
+
// const filePath = path.join(rootDir, file);
|
|
68
|
+
// const fileContent = fs.readFileSync(filePath, 'utf8');
|
|
69
|
+
const { blockName } = file;
|
|
70
|
+
resources.push({
|
|
71
|
+
id: blockName,
|
|
72
|
+
name: blockName,
|
|
73
|
+
description: '',
|
|
74
|
+
images: [],
|
|
75
|
+
url: '',
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
res.json({
|
|
79
|
+
resources: [
|
|
80
|
+
{
|
|
81
|
+
id: allTag,
|
|
82
|
+
name: 'All Blocks',
|
|
83
|
+
description: 'Select All Blocks',
|
|
84
|
+
images: [],
|
|
85
|
+
url: '',
|
|
86
|
+
children: resources,
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
}));
|
|
91
|
+
initResourceRouter.post('/resources', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
92
|
+
const { resources, projectId, releaseId } = req.body;
|
|
93
|
+
// ignore all tag
|
|
94
|
+
const componentIds = resources.filter((resource) => resource.id !== allTag);
|
|
95
|
+
try {
|
|
96
|
+
// Execute build command with filtered components
|
|
97
|
+
const buildProcess = spawn('pnpm', ['run', 'build-lib'], {
|
|
98
|
+
stdio: 'inherit',
|
|
99
|
+
shell: true,
|
|
100
|
+
env: Object.assign(Object.assign({}, process.env), { FORCE_COLOR: '1', BLOCK_FILTER: componentIds.join(',') }),
|
|
101
|
+
});
|
|
102
|
+
yield new Promise((resolve, reject) => {
|
|
103
|
+
buildProcess.on('close', (code) => {
|
|
104
|
+
if (code === 0)
|
|
105
|
+
resolve(null);
|
|
106
|
+
else
|
|
107
|
+
reject(new Error(`Build process exited with code ${code}`));
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
const dir = getExportDir(projectId, releaseId);
|
|
111
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
112
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
113
|
+
const rootDir = process.cwd();
|
|
114
|
+
const tmpPackage = join(rootDir, 'lib');
|
|
115
|
+
yield copyRecursive(tmpPackage, dir);
|
|
116
|
+
fs.rmSync(tmpPackage, { recursive: true, force: true });
|
|
117
|
+
res.json({ success: true });
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
console.error('Build failed:', error);
|
|
121
|
+
res.status(500).json({ error: 'Build failed' });
|
|
122
|
+
}
|
|
123
|
+
}));
|
|
124
|
+
export default initResourceRouter;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
// eslint-disable-next-line import/no-unresolved
|
|
11
|
+
// import typescript from '@rollup/plugin-typescript';
|
|
12
|
+
import { readFileSync, existsSync } from 'fs';
|
|
13
|
+
import * as path from 'path';
|
|
14
|
+
import pages, { DefaultPageStrategy } from 'vite-plugin-react-pages';
|
|
15
|
+
import { findComponentFiles, setBlockEntryFilesPattern, getBlockEntryFilesPattern, getBlockName, logger, } from '../utils';
|
|
16
|
+
import { initHtmlPreviewTransformPlugin, VIRTUAL_MODULE_ID } from './vite-plugin-html-transform';
|
|
17
|
+
// import initRemoteScriptLocalizerPlugin from './vite-plugin-remote-script-localizer';
|
|
18
|
+
export function initBlockStudioPlugins(options) {
|
|
19
|
+
const workingDir = (options === null || options === void 0 ? void 0 : options.cwd) || process.cwd();
|
|
20
|
+
const entryFilesPattern = (options === null || options === void 0 ? void 0 : options.entryFilesPattern) || getBlockEntryFilesPattern();
|
|
21
|
+
setBlockEntryFilesPattern(entryFilesPattern);
|
|
22
|
+
// fallback to __dirname if _theme.tsx not exists
|
|
23
|
+
const pagesDir = existsSync(path.join(workingDir, '_theme.tsx')) ? workingDir : __dirname.replace('cjs', 'esm');
|
|
24
|
+
logger.log('initBlockStudioPlugins options', {
|
|
25
|
+
cwd: workingDir,
|
|
26
|
+
entryFilesPattern: getBlockEntryFilesPattern(),
|
|
27
|
+
pagesDir,
|
|
28
|
+
});
|
|
29
|
+
return [
|
|
30
|
+
// {
|
|
31
|
+
// name: 'vite-plugin-block-studio-dev',
|
|
32
|
+
// enforce: 'pre',
|
|
33
|
+
// transformIndexHtml(html) {
|
|
34
|
+
// // Ensure required content exists in index.html
|
|
35
|
+
// if (!html.includes('<div id="root"></div>')) {
|
|
36
|
+
// html = html.replace(/<body[^>]*>/, '$&<div id="root"></div>');
|
|
37
|
+
// }
|
|
38
|
+
// if (!html.includes('/@pages-infra/main.js')) {
|
|
39
|
+
// html = html.replace('</body>', '<script type="module" src="/@pages-infra/main.js"></script></body>');
|
|
40
|
+
// }
|
|
41
|
+
// return html;
|
|
42
|
+
// },
|
|
43
|
+
// },
|
|
44
|
+
{
|
|
45
|
+
name: 'vite-plugin-block-studio-build',
|
|
46
|
+
enforce: 'pre',
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
48
|
+
config(_config) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
var _a;
|
|
51
|
+
const filterModules = process.argv.includes('--filter')
|
|
52
|
+
? (_a = process.argv[process.argv.indexOf('--filter') + 1]) === null || _a === void 0 ? void 0 : _a.split(',')
|
|
53
|
+
: null;
|
|
54
|
+
const multiMode = !!process.argv.includes('--multi');
|
|
55
|
+
const name = yield import(`${workingDir}/package.json`).then((res) => res.name).catch(() => 'MyLib');
|
|
56
|
+
return {
|
|
57
|
+
build: {
|
|
58
|
+
cssCodeSplit: false,
|
|
59
|
+
lib: {
|
|
60
|
+
name,
|
|
61
|
+
entry: Object.assign({}, Object.fromEntries(findComponentFiles({ cwd: workingDir })
|
|
62
|
+
.map((entry) => {
|
|
63
|
+
const { blockName } = entry;
|
|
64
|
+
const { isHtml } = entry;
|
|
65
|
+
const { file } = entry;
|
|
66
|
+
if (isHtml) {
|
|
67
|
+
return [blockName, `${VIRTUAL_MODULE_ID}?dir=${path.dirname(file)}`];
|
|
68
|
+
}
|
|
69
|
+
return [blockName, path.resolve(workingDir, file)];
|
|
70
|
+
})
|
|
71
|
+
.filter(Boolean)
|
|
72
|
+
.filter(([blockName]) => !filterModules || filterModules.includes(blockName || '')))),
|
|
73
|
+
// multiMode not support umd
|
|
74
|
+
formats: ['es', multiMode ? 'umd' : 'cjs'],
|
|
75
|
+
fileName: (format, entryName) => `${format}/${entryName}.js`,
|
|
76
|
+
},
|
|
77
|
+
rollupOptions: {
|
|
78
|
+
// make sure to externalize deps that shouldn't be bundled
|
|
79
|
+
// into your library
|
|
80
|
+
external: ['react', 'crypto'],
|
|
81
|
+
output: {
|
|
82
|
+
// Provide global variables to use in the UMD build
|
|
83
|
+
globals: {
|
|
84
|
+
react: 'React',
|
|
85
|
+
},
|
|
86
|
+
paths: {
|
|
87
|
+
// Redirect 'react' imports to '@blocklet/pages-kit/builtin/react'
|
|
88
|
+
// react: '@blocklet/pages-kit/builtin/react',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
pages({
|
|
99
|
+
pagesDir,
|
|
100
|
+
pageStrategy: new DefaultPageStrategy({
|
|
101
|
+
extraFindPages: (_pagesDir, helpers) => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
helpers.watchFiles(workingDir, getBlockEntryFilesPattern(), function fileHandler(file, api) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
const { relative, path: filePath } = file;
|
|
105
|
+
const isHtml = relative.endsWith('.html');
|
|
106
|
+
const blockName = getBlockName(relative);
|
|
107
|
+
const pageId = `/${blockName}`;
|
|
108
|
+
api.addPageData({
|
|
109
|
+
pageId,
|
|
110
|
+
dataPath: isHtml ? `${VIRTUAL_MODULE_ID}?dir=${path.dirname(filePath)}` : filePath,
|
|
111
|
+
staticData: isHtml
|
|
112
|
+
? {
|
|
113
|
+
isHtmlPreview: true,
|
|
114
|
+
dataPath: filePath,
|
|
115
|
+
blockName,
|
|
116
|
+
}
|
|
117
|
+
: Object.assign(Object.assign({}, (yield helpers.extractStaticData(file))), { code: readFileSync(file.path, 'utf-8'), dataPath: filePath, blockName }),
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}),
|
|
122
|
+
}),
|
|
123
|
+
}),
|
|
124
|
+
// https://github.com/rollup/plugins/tree/master/packages/typescript#noforceemit
|
|
125
|
+
// typescript({
|
|
126
|
+
// declaration: true,
|
|
127
|
+
// emitDeclarationOnly: true,
|
|
128
|
+
// noForceEmit: true,
|
|
129
|
+
// declarationDir: path.resolve(workingDir, 'lib/types'),
|
|
130
|
+
// rootDir: path.resolve(workingDir),
|
|
131
|
+
// }),
|
|
132
|
+
initHtmlPreviewTransformPlugin(),
|
|
133
|
+
// initRemoteScriptLocalizerPlugin({
|
|
134
|
+
// tempDir: 'temp/remote-scripts', // 可选,默认值
|
|
135
|
+
// maxConcurrent: 5, // 可选,默认值
|
|
136
|
+
// timeout: 30 * 1000, // 可选,默认值 30 秒
|
|
137
|
+
// }),
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
export default initBlockStudioPlugins;
|