vite-plugin-blocklet 0.4.22 → 0.4.25
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/dist/index.cjs +157 -0
- package/index.js +6 -5
- package/libs/hmr.js +13 -9
- package/package.json +5 -1
- package/rollup.config.js +0 -7
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var Mcrypto = require('@ocap/mcrypto');
|
|
6
|
+
var util = require('@ocap/util');
|
|
7
|
+
var did = require('@arcblock/did');
|
|
8
|
+
var fs = require('fs');
|
|
9
|
+
var path = require('path');
|
|
10
|
+
var YAML = require('yaml');
|
|
11
|
+
|
|
12
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var Mcrypto__default = /*#__PURE__*/_interopDefaultLegacy(Mcrypto);
|
|
15
|
+
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
16
|
+
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
17
|
+
var YAML__default = /*#__PURE__*/_interopDefaultLegacy(YAML);
|
|
18
|
+
|
|
19
|
+
const { types } = Mcrypto__default["default"];
|
|
20
|
+
|
|
21
|
+
function toBlockletDid(name) {
|
|
22
|
+
const pk = util.toHex(Buffer.from(typeof name === 'string' ? name.trim() : name));
|
|
23
|
+
return did.fromPublicKey(pk, { role: types.RoleType.ROLE_ANY });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const isInBlocklet = !!process.env.BLOCKLET_PORT;
|
|
27
|
+
|
|
28
|
+
function createHmrPlugin({ version }) {
|
|
29
|
+
return {
|
|
30
|
+
name: 'blocklet:hmr',
|
|
31
|
+
apply: 'serve',
|
|
32
|
+
transform(code, id) {
|
|
33
|
+
if (isInBlocklet && version === 2) {
|
|
34
|
+
if (id.endsWith('/vite/dist/client/client.mjs')) {
|
|
35
|
+
let replacedCode = code;
|
|
36
|
+
replacedCode = replacedCode.replace("const base = __BASE__ || '/';\n", '');
|
|
37
|
+
replacedCode = replacedCode.replace(
|
|
38
|
+
'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
|
|
39
|
+
"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}` : ''}`;"
|
|
40
|
+
);
|
|
41
|
+
return replacedCode;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function createConfigPlugin() {
|
|
49
|
+
return {
|
|
50
|
+
name: 'blocklet:config',
|
|
51
|
+
configureServer(server) {
|
|
52
|
+
if (isInBlocklet) {
|
|
53
|
+
server.middlewares.use((req, res, next) => {
|
|
54
|
+
const prefix = req.headers['x-path-prefix'] || '/';
|
|
55
|
+
// blocklet server 会把设置的 base 从请求 url 中移除,所以需要再加回 base
|
|
56
|
+
if (!req.url.startsWith(prefix)) {
|
|
57
|
+
req.url = path__default["default"].join(prefix || '/', req.url);
|
|
58
|
+
}
|
|
59
|
+
return next();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
config(config, { command }) {
|
|
64
|
+
if (command === 'serve') {
|
|
65
|
+
const targetConfig = {};
|
|
66
|
+
if (!config.base) {
|
|
67
|
+
let base = process.env.BLOCKLET_DEV_MOUNT_POINT || '';
|
|
68
|
+
|
|
69
|
+
if (base) {
|
|
70
|
+
if (!base.startsWith('/')) {
|
|
71
|
+
base = `/${base}`;
|
|
72
|
+
}
|
|
73
|
+
if (!base.endsWith('/')) {
|
|
74
|
+
base = `${base}/`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
targetConfig.base = base;
|
|
78
|
+
}
|
|
79
|
+
if (!(config.server && config.server.port)) {
|
|
80
|
+
const port = process.env.BLOCKLET_PORT || 3000;
|
|
81
|
+
targetConfig.server = {
|
|
82
|
+
port,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return targetConfig;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (command === 'build') {
|
|
89
|
+
if (!config.base) {
|
|
90
|
+
try {
|
|
91
|
+
const blockletYamlPath = './blocklet.yml';
|
|
92
|
+
const blockletYaml = YAML__default["default"].parse(fs__default["default"].readFileSync(blockletYamlPath, 'utf8'));
|
|
93
|
+
const { name } = blockletYaml;
|
|
94
|
+
if (name) {
|
|
95
|
+
const did = toBlockletDid(name);
|
|
96
|
+
const base = `/.blocklet/proxy/${did}/`;
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
base,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
} catch (err) {
|
|
103
|
+
console.error(err);
|
|
104
|
+
return {};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {};
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function createMetaPlugin() {
|
|
115
|
+
return {
|
|
116
|
+
name: 'blocklet:meta',
|
|
117
|
+
transformIndexHtml(html) {
|
|
118
|
+
return {
|
|
119
|
+
html,
|
|
120
|
+
tags: [
|
|
121
|
+
{
|
|
122
|
+
// injectTo: 'head',
|
|
123
|
+
tag: 'script',
|
|
124
|
+
attrs: {
|
|
125
|
+
src: '__meta__.js',
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function createBlockletPlugin(options = {}) {
|
|
135
|
+
const { disableConfig = false, disableMeta = false, disableHmr = false } = options;
|
|
136
|
+
const plugins = [];
|
|
137
|
+
if (!disableMeta) {
|
|
138
|
+
plugins.push(createMetaPlugin());
|
|
139
|
+
}
|
|
140
|
+
if (!disableConfig) {
|
|
141
|
+
plugins.push(createConfigPlugin());
|
|
142
|
+
}
|
|
143
|
+
if (!disableHmr) {
|
|
144
|
+
plugins.push(createHmrPlugin(options));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return plugins;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const createBlockletHmr = createHmrPlugin;
|
|
151
|
+
const createBlockletConfig = createConfigPlugin;
|
|
152
|
+
const createBlockletMeta = createMetaPlugin;
|
|
153
|
+
|
|
154
|
+
exports.createBlockletConfig = createBlockletConfig;
|
|
155
|
+
exports.createBlockletHmr = createBlockletHmr;
|
|
156
|
+
exports.createBlockletMeta = createBlockletMeta;
|
|
157
|
+
exports.createBlockletPlugin = createBlockletPlugin;
|
package/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import createHmrPlugin from './libs/hmr.js';
|
|
2
2
|
import createConfigPlugin from './libs/config.js';
|
|
3
3
|
import createMetaPlugin from './libs/meta.js';
|
|
4
|
-
import { isInBlocklet } from './libs/utils.js';
|
|
5
4
|
|
|
6
5
|
export function createBlockletPlugin(options = {}) {
|
|
7
6
|
const { disableConfig = false, disableMeta = false, disableHmr = false } = options;
|
|
@@ -12,11 +11,13 @@ export function createBlockletPlugin(options = {}) {
|
|
|
12
11
|
if (!disableConfig) {
|
|
13
12
|
plugins.push(createConfigPlugin(options));
|
|
14
13
|
}
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
plugins.push(createHmrPlugin(options));
|
|
18
|
-
}
|
|
14
|
+
if (!disableHmr) {
|
|
15
|
+
plugins.push(createHmrPlugin(options));
|
|
19
16
|
}
|
|
20
17
|
|
|
21
18
|
return plugins;
|
|
22
19
|
}
|
|
20
|
+
|
|
21
|
+
export const createBlockletHmr = createHmrPlugin;
|
|
22
|
+
export const createBlockletConfig = createConfigPlugin;
|
|
23
|
+
export const createBlockletMeta = createMetaPlugin;
|
package/libs/hmr.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
import { isInBlocklet } from './utils.js';
|
|
2
|
+
|
|
3
|
+
export default function createHmrPlugin({ version }) {
|
|
2
4
|
return {
|
|
3
5
|
name: 'blocklet:hmr',
|
|
4
6
|
apply: 'serve',
|
|
5
7
|
transform(code, id) {
|
|
6
|
-
if (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
if (isInBlocklet && version === 2) {
|
|
9
|
+
if (id.endsWith('/vite/dist/client/client.mjs')) {
|
|
10
|
+
let replacedCode = code;
|
|
11
|
+
replacedCode = replacedCode.replace("const base = __BASE__ || '/';\n", '');
|
|
12
|
+
replacedCode = replacedCode.replace(
|
|
13
|
+
'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
|
|
14
|
+
"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}` : ''}`;"
|
|
15
|
+
);
|
|
16
|
+
return replacedCode;
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
},
|
|
16
20
|
};
|
package/package.json
CHANGED