vite-plugin-blocklet 0.11.1 → 0.11.2

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
@@ -694,6 +694,46 @@ function createEmbedPlugin(options) {
694
694
  ];
695
695
  }
696
696
 
697
+ function createAssetHostPlugin() {
698
+ return {
699
+ name: 'blocklet:asset-host',
700
+ async transformIndexHtml(html, ctx) {
701
+ if (ctx.bundle) {
702
+ const { did } = await getBlockletYAML();
703
+ const dynamicBaseAssetsCode = `globalThis.__toCdnUrl = filePath => {
704
+ const blockletBase = '/.blocklet/proxy/${did}/';
705
+ return window.blocklet.ASSET_CDN_HOST ? '//' + window.blocklet.ASSET_CDN_HOST + blockletBase + filePath : blockletBase + filePath;
706
+ }`;
707
+ return [
708
+ {
709
+ tag: 'script',
710
+ attrs: { type: 'module' },
711
+ children: dynamicBaseAssetsCode,
712
+ },
713
+ ];
714
+ }
715
+ return html;
716
+ },
717
+ async config(config, { command }) {
718
+ if (command === 'build') {
719
+ if (!config.experimental?.renderBuiltUrl) {
720
+ return {
721
+ experimental: {
722
+ renderBuiltUrl: (filename, { hostType }) => {
723
+ if (hostType === 'js') {
724
+ return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` };
725
+ }
726
+ },
727
+ },
728
+ }
729
+ }
730
+ }
731
+
732
+ return {};
733
+ },
734
+ };
735
+ }
736
+
697
737
  const argv = process.argv.slice(2);
698
738
  const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
699
739
 
@@ -836,6 +876,7 @@ function createBlockletPlugin(options = {}) {
836
876
  disableLoading = false,
837
877
  disableDebug = false,
838
878
  disableEmbed = false,
879
+ disableDynamicAssetHost = true,
839
880
  nodePolyfillsOptions,
840
881
  ...restOptions
841
882
  } = options;
@@ -864,6 +905,9 @@ function createBlockletPlugin(options = {}) {
864
905
  if (!disableEmbed) {
865
906
  plugins.push(createEmbedPlugin(restOptions));
866
907
  }
908
+ if (!disableDynamicAssetHost) {
909
+ plugins.push(createAssetHostPlugin());
910
+ }
867
911
 
868
912
  return plugins;
869
913
  }
package/index.js CHANGED
@@ -6,6 +6,7 @@ import createLoadingPlugin from './libs/loading.js';
6
6
  import createDebugPlugin from './libs/debug.js';
7
7
  import createExpressPlugin from './libs/express.js';
8
8
  import createEmbedPlugin from './libs/embed.js';
9
+ import createAssetHostPlugin from './libs/asset-host.js';
9
10
  import setupClient from './libs/client.js';
10
11
 
11
12
  /**
@@ -52,6 +53,7 @@ export function createBlockletPlugin(options = {}) {
52
53
  disableLoading = false,
53
54
  disableDebug = false,
54
55
  disableEmbed = false,
56
+ disableDynamicAssetHost = true,
55
57
  nodePolyfillsOptions,
56
58
  ...restOptions
57
59
  } = options;
@@ -80,6 +82,9 @@ export function createBlockletPlugin(options = {}) {
80
82
  if (!disableEmbed) {
81
83
  plugins.push(createEmbedPlugin(restOptions));
82
84
  }
85
+ if (!disableDynamicAssetHost) {
86
+ plugins.push(createAssetHostPlugin(restOptions));
87
+ }
83
88
 
84
89
  return plugins;
85
90
  }
@@ -0,0 +1,41 @@
1
+ import { getBlockletYAML } from './utils.js';
2
+
3
+ export default function createAssetHostPlugin() {
4
+ return {
5
+ name: 'blocklet:asset-host',
6
+ async transformIndexHtml(html, ctx) {
7
+ if (ctx.bundle) {
8
+ const { did } = await getBlockletYAML();
9
+ const dynamicBaseAssetsCode = `globalThis.__toCdnUrl = filePath => {
10
+ const blockletBase = '/.blocklet/proxy/${did}/';
11
+ return window.blocklet.ASSET_CDN_HOST ? '//' + window.blocklet.ASSET_CDN_HOST + blockletBase + filePath : blockletBase + filePath;
12
+ }`;
13
+ return [
14
+ {
15
+ tag: 'script',
16
+ attrs: { type: 'module' },
17
+ children: dynamicBaseAssetsCode,
18
+ },
19
+ ];
20
+ }
21
+ return html;
22
+ },
23
+ async config(config, { command }) {
24
+ if (command === 'build') {
25
+ if (!config.experimental?.renderBuiltUrl) {
26
+ return {
27
+ experimental: {
28
+ renderBuiltUrl: (filename, { hostType }) => {
29
+ if (hostType === 'js') {
30
+ return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` };
31
+ }
32
+ },
33
+ },
34
+ }
35
+ }
36
+ }
37
+
38
+ return {};
39
+ },
40
+ };
41
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-blocklet",
3
3
  "type": "module",
4
- "version": "0.11.1",
4
+ "version": "0.11.2",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -20,6 +20,9 @@
20
20
  "build": "rollup -c rollup.config.js",
21
21
  "prepublishOnly": "npm run build"
22
22
  },
23
+ "engines": {
24
+ "node": ">=20.0.0"
25
+ },
23
26
  "keywords": [],
24
27
  "author": "",
25
28
  "license": "ISC",