vite-plugin-blocklet 0.1.2 → 0.4.19

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 CHANGED
@@ -1,14 +1,22 @@
1
- export function createBlockletPlugin() {
2
- return {
3
- name: 'vite-plugin-blocklet',
4
- transform(code, id) {
5
- if (id.endsWith('/vite/dist/client/client.mjs')) {
6
- const replacedCode = code.replace(
7
- 'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
8
- "let tmpPort = __HMR_PORT__;\nif (window.blocklet) {\ntmpPort = new URL(window.location.href).port;\n}\nconst socketHost = `${__HMR_HOSTNAME__ || location.hostname}${tmpPort ? `:${tmpPort}` : ''}`;",
9
- );
10
- return replacedCode;
11
- }
12
- },
13
- };
1
+ import createHmrPlugin from './libs/hmr';
2
+ import createConfigPlugin from './libs/config';
3
+ import createMetaPlugin from './libs/meta';
4
+ import { isInBlocklet } from './libs/utils';
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,57 @@
1
+ import fs from 'fs';
2
+ import YAML from 'yaml';
3
+ import { toBlockletDid } from './utils';
4
+
5
+ export default function createConfigPlugin() {
6
+ return {
7
+ name: 'blocklet:config',
8
+ config(config, { command }) {
9
+ if (command === 'serve') {
10
+ const targetConfig = {};
11
+ if (!config.base) {
12
+ let base = process.env.BLOCKLET_DEV_MOUNT_POINT || '';
13
+
14
+ if (base) {
15
+ if (!base.startsWith('/')) {
16
+ base = `/${base}`;
17
+ }
18
+ if (!base.endsWith('/')) {
19
+ base = `${base}/`;
20
+ }
21
+ }
22
+ targetConfig.base = base;
23
+ }
24
+ if (!config.server.port) {
25
+ const port = process.env.BLOCKLET_PORT || 3000;
26
+ targetConfig.server = {
27
+ port,
28
+ };
29
+ }
30
+ return targetConfig;
31
+ }
32
+
33
+ if (command === 'build') {
34
+ if (!config.base) {
35
+ try {
36
+ const blockletYamlPath = './blocklet.yml';
37
+ const blockletYaml = YAML.parse(fs.readFileSync(blockletYamlPath, 'utf8'));
38
+ const { name } = blockletYaml;
39
+ if (name) {
40
+ const did = toBlockletDid(name);
41
+ const base = `/.blocklet/proxy/${did}/`;
42
+
43
+ return {
44
+ base,
45
+ };
46
+ }
47
+ } catch (err) {
48
+ console.error(err);
49
+ return {};
50
+ }
51
+ }
52
+ }
53
+
54
+ return {};
55
+ },
56
+ };
57
+ }
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.1.2",
4
+ "version": "0.4.19",
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.70.1"
17
+ "rollup": "^2.76.0"
18
+ },
19
+ "dependencies": {
20
+ "@arcblock/did": "^1.17.4",
21
+ "@ocap/mcrypto": "^1.17.4",
22
+ "@ocap/util": "^1.17.4",
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
@@ -1,7 +1,7 @@
1
1
  export default {
2
2
  input: 'index.js',
3
3
  output: {
4
- file: 'index.cjs',
4
+ file: 'dist/index.cjs',
5
5
  format: 'cjs',
6
6
  },
7
7
  };
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