vite-plugin-blocklet 0.1.2 → 0.4.21
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/index.js +21 -13
- package/libs/config.js +70 -0
- package/libs/hmr.js +17 -0
- package/libs/meta.js +19 -0
- package/libs/utils.js +14 -0
- package/package.json +16 -8
- package/rollup.config.js +1 -1
- package/index.cjs +0 -20
- package/pnpm-lock.yaml +0 -25
package/index.js
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import createHmrPlugin from './libs/hmr.js';
|
|
2
|
+
import createConfigPlugin from './libs/config.js';
|
|
3
|
+
import createMetaPlugin from './libs/meta.js';
|
|
4
|
+
import { isInBlocklet } from './libs/utils.js';
|
|
5
|
+
|
|
6
|
+
export function createBlockletPlugin(options = {}) {
|
|
7
|
+
const { disableConfig = false, disableMeta = false, disableHmr = false } = options;
|
|
8
|
+
const plugins = [];
|
|
9
|
+
if (!disableMeta) {
|
|
10
|
+
plugins.push(createMetaPlugin(options));
|
|
11
|
+
}
|
|
12
|
+
if (!disableConfig) {
|
|
13
|
+
plugins.push(createConfigPlugin(options));
|
|
14
|
+
}
|
|
15
|
+
if (isInBlocklet) {
|
|
16
|
+
if (!disableHmr) {
|
|
17
|
+
plugins.push(createHmrPlugin(options));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return plugins;
|
|
14
22
|
}
|
package/libs/config.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import YAML from 'yaml';
|
|
4
|
+
import { toBlockletDid, isInBlocklet } from './utils.js';
|
|
5
|
+
|
|
6
|
+
export default function createConfigPlugin() {
|
|
7
|
+
return {
|
|
8
|
+
name: 'blocklet:config',
|
|
9
|
+
configureServer(server) {
|
|
10
|
+
if (isInBlocklet) {
|
|
11
|
+
server.middlewares.use((req, res, next) => {
|
|
12
|
+
const prefix = req.headers['x-path-prefix'] || '/';
|
|
13
|
+
// blocklet server 会把设置的 base 从请求 url 中移除,所以需要再加回 base
|
|
14
|
+
if (!req.url.startsWith(prefix)) {
|
|
15
|
+
req.url = path.join(prefix || '/', req.url);
|
|
16
|
+
}
|
|
17
|
+
return next();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
config(config, { command }) {
|
|
22
|
+
if (command === 'serve') {
|
|
23
|
+
const targetConfig = {};
|
|
24
|
+
if (!config.base) {
|
|
25
|
+
let base = process.env.BLOCKLET_DEV_MOUNT_POINT || '';
|
|
26
|
+
|
|
27
|
+
if (base) {
|
|
28
|
+
if (!base.startsWith('/')) {
|
|
29
|
+
base = `/${base}`;
|
|
30
|
+
}
|
|
31
|
+
if (!base.endsWith('/')) {
|
|
32
|
+
base = `${base}/`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
targetConfig.base = base;
|
|
36
|
+
}
|
|
37
|
+
if (!(config.server && config.server.port)) {
|
|
38
|
+
const port = process.env.BLOCKLET_PORT || 3000;
|
|
39
|
+
targetConfig.server = {
|
|
40
|
+
port,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return targetConfig;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (command === 'build') {
|
|
47
|
+
if (!config.base) {
|
|
48
|
+
try {
|
|
49
|
+
const blockletYamlPath = './blocklet.yml';
|
|
50
|
+
const blockletYaml = YAML.parse(fs.readFileSync(blockletYamlPath, 'utf8'));
|
|
51
|
+
const { name } = blockletYaml;
|
|
52
|
+
if (name) {
|
|
53
|
+
const did = toBlockletDid(name);
|
|
54
|
+
const base = `/.blocklet/proxy/${did}/`;
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
base,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error(err);
|
|
62
|
+
return {};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {};
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
package/libs/hmr.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default function createHmrPlugin() {
|
|
2
|
+
return {
|
|
3
|
+
name: 'blocklet:hmr',
|
|
4
|
+
apply: 'serve',
|
|
5
|
+
transform(code, id) {
|
|
6
|
+
if (id.endsWith('/vite/dist/client/client.mjs')) {
|
|
7
|
+
let replacedCode = code;
|
|
8
|
+
replacedCode = replacedCode.replace("const base = __BASE__ || '/';\n", '');
|
|
9
|
+
replacedCode = replacedCode.replace(
|
|
10
|
+
'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
|
|
11
|
+
"const base = __BASE__ || '/';\nlet tmpPort = __HMR_PORT__;\nif (window.blocklet) {\ntmpPort = new URL(window.location.href).port + base;\n}\nconst socketHost = `${__HMR_HOSTNAME__ || location.hostname}${tmpPort ? `:${tmpPort}` : ''}`;"
|
|
12
|
+
);
|
|
13
|
+
return replacedCode;
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
package/libs/meta.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default function createMetaPlugin() {
|
|
2
|
+
return {
|
|
3
|
+
name: 'blocklet:meta',
|
|
4
|
+
transformIndexHtml(html) {
|
|
5
|
+
return {
|
|
6
|
+
html,
|
|
7
|
+
tags: [
|
|
8
|
+
{
|
|
9
|
+
// injectTo: 'head',
|
|
10
|
+
tag: 'script',
|
|
11
|
+
attrs: {
|
|
12
|
+
src: '__meta__.js',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
package/libs/utils.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Mcrypto from '@ocap/mcrypto';
|
|
2
|
+
import { toHex } from '@ocap/util';
|
|
3
|
+
import { fromPublicKey } from '@arcblock/did';
|
|
4
|
+
|
|
5
|
+
const { types } = Mcrypto;
|
|
6
|
+
|
|
7
|
+
export function toBlockletDid(name) {
|
|
8
|
+
const pk = toHex(Buffer.from(typeof name === 'string' ? name.trim() : name));
|
|
9
|
+
return fromPublicKey(pk, { role: types.RoleType.ROLE_ANY });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const isInBlocklet = !!process.env.BLOCKLET_PORT;
|
|
13
|
+
|
|
14
|
+
export { isInBlocklet };
|
package/package.json
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-blocklet",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.21",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"require": "./index.cjs",
|
|
9
|
+
"require": "./dist/index.cjs",
|
|
10
10
|
"import": "./index.js"
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"test": "echo \"Error: no test specified\" && exit 1",
|
|
15
|
-
"build": "rollup -c rollup.config.js"
|
|
16
|
-
},
|
|
17
13
|
"keywords": [],
|
|
18
14
|
"author": "",
|
|
19
15
|
"license": "ISC",
|
|
20
16
|
"devDependencies": {
|
|
21
|
-
"rollup": "^2.
|
|
17
|
+
"rollup": "^2.76.0"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@arcblock/did": "^1.17.5",
|
|
21
|
+
"@ocap/mcrypto": "^1.17.5",
|
|
22
|
+
"@ocap/util": "^1.17.5",
|
|
23
|
+
"yaml": "^2.1.1"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
27
|
+
"watch": "rollup -w -c rollup.config.js",
|
|
28
|
+
"build": "rollup -c rollup.config.js",
|
|
29
|
+
"postinstall": "npm run build"
|
|
22
30
|
}
|
|
23
|
-
}
|
|
31
|
+
}
|
package/rollup.config.js
CHANGED
package/index.cjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function createBlockletPlugin() {
|
|
6
|
-
return {
|
|
7
|
-
name: 'vite-plugin-blocklet',
|
|
8
|
-
transform(code, id) {
|
|
9
|
-
if (id.endsWith('/vite/dist/client/client.mjs')) {
|
|
10
|
-
const replacedCode = code.replace(
|
|
11
|
-
'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
|
|
12
|
-
"let tmpPort = __HMR_PORT__;\nif (window.blocklet) {\ntmpPort = new URL(window.location.href).port;\n}\nconst socketHost = `${__HMR_HOSTNAME__ || location.hostname}${tmpPort ? `:${tmpPort}` : ''}`;",
|
|
13
|
-
);
|
|
14
|
-
return replacedCode;
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
exports.createBlockletPlugin = createBlockletPlugin;
|
package/pnpm-lock.yaml
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
lockfileVersion: 5.3
|
|
2
|
-
|
|
3
|
-
specifiers:
|
|
4
|
-
rollup: ^2.70.1
|
|
5
|
-
|
|
6
|
-
devDependencies:
|
|
7
|
-
rollup: 2.70.1
|
|
8
|
-
|
|
9
|
-
packages:
|
|
10
|
-
|
|
11
|
-
/fsevents/2.3.2:
|
|
12
|
-
resolution: {integrity: sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==}
|
|
13
|
-
engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
|
|
14
|
-
os: [darwin]
|
|
15
|
-
requiresBuild: true
|
|
16
|
-
dev: true
|
|
17
|
-
optional: true
|
|
18
|
-
|
|
19
|
-
/rollup/2.70.1:
|
|
20
|
-
resolution: {integrity: sha512-CRYsI5EuzLbXdxC6RnYhOuRdtz4bhejPMSWjsFLfVM/7w/85n2szZv6yExqUXsBdz5KT8eoubeyDUDjhLHEslA==}
|
|
21
|
-
engines: {node: '>=10.0.0'}
|
|
22
|
-
hasBin: true
|
|
23
|
-
optionalDependencies:
|
|
24
|
-
fsevents: 2.3.2
|
|
25
|
-
dev: true
|