vite-plugin-blocklet 0.4.80 → 0.5.0

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 CHANGED
@@ -2,19 +2,25 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var vite = require('vite');
6
+ var semver = require('semver');
5
7
  var Mcrypto = require('@ocap/mcrypto');
6
8
  var util = require('@ocap/util');
7
9
  var did = require('@arcblock/did');
8
10
  var fs = require('fs');
9
11
  var path = require('path');
10
12
  var YAML = require('yaml');
13
+ var getPort = require('get-port');
14
+ var httpProxyMiddleware = require('http-proxy-middleware');
11
15
 
12
16
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
17
 
18
+ var semver__default = /*#__PURE__*/_interopDefaultLegacy(semver);
14
19
  var Mcrypto__default = /*#__PURE__*/_interopDefaultLegacy(Mcrypto);
15
20
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
16
21
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
17
22
  var YAML__default = /*#__PURE__*/_interopDefaultLegacy(YAML);
23
+ var getPort__default = /*#__PURE__*/_interopDefaultLegacy(getPort);
18
24
 
19
25
  const { types } = Mcrypto__default["default"];
20
26
 
@@ -25,20 +31,25 @@ function toBlockletDid(name) {
25
31
 
26
32
  const isInBlocklet = !!process.env.BLOCKLET_PORT;
27
33
 
28
- function createHmrPlugin({ version }) {
34
+ function createHmrPlugin({ version = vite.version } = {}) {
29
35
  return {
30
36
  name: 'blocklet:hmr',
31
37
  apply: 'serve',
32
- transform(code, id) {
33
- if (isInBlocklet && version === 2) {
34
- if (id.endsWith('/vite/dist/client/client.mjs')) {
35
- let replacedCode = code;
38
+ async transform(code, id) {
39
+ if (isInBlocklet && id.endsWith('/vite/dist/client/client.mjs')) {
40
+ const pureVersion = semver__default["default"].major(version);
41
+ let replacedCode = code;
42
+ if (pureVersion === 2) {
36
43
  replacedCode = replacedCode.replace("const base = __BASE__ || '/';\n", '');
37
44
  replacedCode = replacedCode.replace(
38
45
  'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
39
46
  "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
47
  );
41
48
  return replacedCode;
49
+ } else if (pureVersion === 3) {
50
+ let replacedCode = code;
51
+ replacedCode = replacedCode.replace('__HMR_PORT__', 'undefined');
52
+ return replacedCode;
42
53
  }
43
54
  }
44
55
  },
@@ -115,22 +126,60 @@ function createMetaPlugin() {
115
126
  return {
116
127
  name: 'blocklet:meta',
117
128
  transformIndexHtml(html) {
129
+ const tags = [];
130
+ // 如果 index.html 中没有设置 title,则自动注入 blocklet.yml 中的 title
131
+ if (!/<title>(.*?)<\/title>/.test(html)) {
132
+ const blockletYamlPath = './blocklet.yml';
133
+ const blockletYaml = YAML__default["default"].parse(fs__default["default"].readFileSync(blockletYamlPath, 'utf8'));
134
+ const { title } = blockletYaml;
135
+
136
+ tags.push({
137
+ tag: 'title',
138
+ children: title,
139
+ });
140
+ }
141
+ // 默认注入 __blocklet__.js 文件
142
+ tags.push({
143
+ // injectTo: 'head',
144
+ tag: 'script',
145
+ attrs: {
146
+ src: '__blocklet__.js',
147
+ },
148
+ });
149
+
118
150
  return {
119
151
  html,
120
- tags: [
121
- {
122
- // injectTo: 'head',
123
- tag: 'script',
124
- attrs: {
125
- src: '__blocklet__.js',
126
- },
127
- },
128
- ],
152
+ tags,
129
153
  };
130
154
  },
131
155
  };
132
156
  }
133
157
 
158
+ const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
159
+
160
+ async function setupClient(app, server, options = {}) {
161
+ if (!isProduction) {
162
+ const randomport = await getPort__default["default"]();
163
+ const { host = 'localhost', protocol = 'ws', port = randomport } = options;
164
+ const hmrPath = `/_vite_websocket_${port}`;
165
+ // 以中间件模式创建 Vite 服务器
166
+ const vite$1 = await vite.createServer({
167
+ server: {
168
+ middlewareMode: true,
169
+ hmr: {
170
+ port,
171
+ path: hmrPath,
172
+ },
173
+ },
174
+ });
175
+ // 将 vite 的 connect 实例作中间件使用
176
+ const wsProxy = httpProxyMiddleware.createProxyMiddleware(`${protocol}://${host}:${port}`);
177
+ app.use(hmrPath, wsProxy);
178
+ server.on('upgrade', wsProxy.upgrade);
179
+ app.use(vite$1.middlewares);
180
+ }
181
+ }
182
+
134
183
  function createBlockletPlugin(options = {}) {
135
184
  const { disableConfig = false, disableMeta = false, disableHmr = false } = options;
136
185
  const plugins = [];
@@ -147,11 +196,8 @@ function createBlockletPlugin(options = {}) {
147
196
  return plugins;
148
197
  }
149
198
 
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;
199
+ exports.createBlockletConfig = createConfigPlugin;
200
+ exports.createBlockletHmr = createHmrPlugin;
201
+ exports.createBlockletMeta = createMetaPlugin;
157
202
  exports.createBlockletPlugin = createBlockletPlugin;
203
+ exports.setupClient = setupClient;
package/index.js CHANGED
@@ -1,6 +1,7 @@
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 setupClient from './libs/client';
4
5
 
5
6
  export function createBlockletPlugin(options = {}) {
6
7
  const { disableConfig = false, disableMeta = false, disableHmr = false } = options;
@@ -18,6 +19,9 @@ export function createBlockletPlugin(options = {}) {
18
19
  return plugins;
19
20
  }
20
21
 
21
- export const createBlockletHmr = createHmrPlugin;
22
- export const createBlockletConfig = createConfigPlugin;
23
- export const createBlockletMeta = createMetaPlugin;
22
+ export {
23
+ setupClient,
24
+ createHmrPlugin as createBlockletHmr,
25
+ createConfigPlugin as createBlockletConfig,
26
+ createMetaPlugin as createBlockletMeta,
27
+ };
package/libs/client.js ADDED
@@ -0,0 +1,28 @@
1
+ import getPort from 'get-port';
2
+ import { createServer } from 'vite';
3
+ import { createProxyMiddleware } from 'http-proxy-middleware';
4
+
5
+ const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
6
+
7
+ export default async function setupClient(app, server, options = {}) {
8
+ if (!isProduction) {
9
+ const randomport = await getPort();
10
+ const { host = 'localhost', protocol = 'ws', port = randomport } = options;
11
+ const hmrPath = `/_vite_websocket_${port}`;
12
+ // 以中间件模式创建 Vite 服务器
13
+ const vite = await createServer({
14
+ server: {
15
+ middlewareMode: true,
16
+ hmr: {
17
+ port,
18
+ path: hmrPath,
19
+ },
20
+ },
21
+ });
22
+ // 将 vite 的 connect 实例作中间件使用
23
+ const wsProxy = createProxyMiddleware(`${protocol}://${host}:${port}`);
24
+ app.use(hmrPath, wsProxy);
25
+ server.on('upgrade', wsProxy.upgrade);
26
+ app.use(vite.middlewares);
27
+ }
28
+ }
package/libs/hmr.js CHANGED
@@ -1,19 +1,26 @@
1
+ import { version as viteVersion } from 'vite';
2
+ import semver from 'semver';
1
3
  import { isInBlocklet } from './utils.js';
2
4
 
3
- export default function createHmrPlugin({ version }) {
5
+ export default function createHmrPlugin({ version = viteVersion } = {}) {
4
6
  return {
5
7
  name: 'blocklet:hmr',
6
8
  apply: 'serve',
7
- transform(code, id) {
8
- if (isInBlocklet && version === 2) {
9
- if (id.endsWith('/vite/dist/client/client.mjs')) {
10
- let replacedCode = code;
9
+ async transform(code, id) {
10
+ if (isInBlocklet && id.endsWith('/vite/dist/client/client.mjs')) {
11
+ const pureVersion = semver.major(version);
12
+ let replacedCode = code;
13
+ if (pureVersion === 2) {
11
14
  replacedCode = replacedCode.replace("const base = __BASE__ || '/';\n", '');
12
15
  replacedCode = replacedCode.replace(
13
16
  'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
14
17
  "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
18
  );
16
19
  return replacedCode;
20
+ } else if (pureVersion === 3) {
21
+ let replacedCode = code;
22
+ replacedCode = replacedCode.replace('__HMR_PORT__', 'undefined');
23
+ return replacedCode;
17
24
  }
18
25
  }
19
26
  },
package/libs/meta.js CHANGED
@@ -1,18 +1,34 @@
1
+ import fs from 'fs';
2
+ import YAML from 'yaml';
3
+
1
4
  export default function createMetaPlugin() {
2
5
  return {
3
6
  name: 'blocklet:meta',
4
7
  transformIndexHtml(html) {
8
+ const tags = [];
9
+ // 如果 index.html 中没有设置 title,则自动注入 blocklet.yml 中的 title
10
+ if (!/<title>(.*?)<\/title>/.test(html)) {
11
+ const blockletYamlPath = './blocklet.yml';
12
+ const blockletYaml = YAML.parse(fs.readFileSync(blockletYamlPath, 'utf8'));
13
+ const { title } = blockletYaml;
14
+
15
+ tags.push({
16
+ tag: 'title',
17
+ children: title,
18
+ });
19
+ }
20
+ // 默认注入 __blocklet__.js 文件
21
+ tags.push({
22
+ // injectTo: 'head',
23
+ tag: 'script',
24
+ attrs: {
25
+ src: '__blocklet__.js',
26
+ },
27
+ });
28
+
5
29
  return {
6
30
  html,
7
- tags: [
8
- {
9
- // injectTo: 'head',
10
- tag: 'script',
11
- attrs: {
12
- src: '__blocklet__.js',
13
- },
14
- },
15
- ],
31
+ tags,
16
32
  };
17
33
  },
18
34
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-blocklet",
3
3
  "type": "module",
4
- "version": "0.4.80",
4
+ "version": "0.5.0",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -27,9 +27,15 @@
27
27
  "rollup": "^2.79.1"
28
28
  },
29
29
  "dependencies": {
30
- "@arcblock/did": "^1.17.23",
31
- "@ocap/mcrypto": "^1.17.23",
32
- "@ocap/util": "^1.17.23",
30
+ "@arcblock/did": "^1.18.6",
31
+ "@ocap/mcrypto": "^1.18.6",
32
+ "@ocap/util": "^1.18.6",
33
+ "get-port": "^5.1.1",
34
+ "http-proxy-middleware": "^2.0.6",
35
+ "semver": "^7.3.8",
33
36
  "yaml": "^2.1.3"
37
+ },
38
+ "peerDependencies": {
39
+ "vite": ">=2<4"
34
40
  }
35
41
  }