From ceee40c912bd54f3039bdf555383c3a1540bc14b Mon Sep 17 00:00:00 2001 From: DavidJones Date: Tue, 24 Oct 2023 10:03:56 +0800 Subject: [PATCH] =?UTF-8?q?ci(custom):=20=E5=85=BC=E5=AE=B9=20ghpages=20pu?= =?UTF-8?q?blichpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- @dumi-plugin/dumi-plugin-node-stackblitz/src/index.ts | 4 ++-- .../src/StackblitzWebpackPlugin.ts | 11 ++--------- .../src/client/StackblitzLiveDemo.ts | 11 +++++++++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/@dumi-plugin/dumi-plugin-node-stackblitz/src/index.ts b/@dumi-plugin/dumi-plugin-node-stackblitz/src/index.ts index cfd0485a..1124e69a 100644 --- a/@dumi-plugin/dumi-plugin-node-stackblitz/src/index.ts +++ b/@dumi-plugin/dumi-plugin-node-stackblitz/src/index.ts @@ -6,7 +6,7 @@ export interface NodeStackblitzOptions { } export default createDumiPlugin('nodeStackblitz', async (api, { pushWebpackPlugin }) => { - const { publicPath, nodeStackblitz = {} } = api.userConfig || {} + const { nodeStackblitz = {} } = api.userConfig || {} api.describe({ key: 'nodeStackblitz', @@ -23,5 +23,5 @@ export default createDumiPlugin('nodeStackblitz', async ( }, }) - pushWebpackPlugin(StackblitzWebpackPlugin.PLUGIN_NAME, new StackblitzWebpackPlugin({ ...nodeStackblitz, publicPath })) + pushWebpackPlugin(StackblitzWebpackPlugin.PLUGIN_NAME, new StackblitzWebpackPlugin(nodeStackblitz)) }) diff --git a/@webpack-plugin/stackblitz-webpack-plugin/src/StackblitzWebpackPlugin.ts b/@webpack-plugin/stackblitz-webpack-plugin/src/StackblitzWebpackPlugin.ts index 1347e714..0026d531 100644 --- a/@webpack-plugin/stackblitz-webpack-plugin/src/StackblitzWebpackPlugin.ts +++ b/@webpack-plugin/stackblitz-webpack-plugin/src/StackblitzWebpackPlugin.ts @@ -1,4 +1,3 @@ -import { trimEnd, trimStart } from 'lodash' import { SeedWebpackPlugin, type SeedWebpackPluginOptions } from '@dumlj/seed-webpack-plugin' import { yarnWorkspaces, type ProjectInWorkspaces } from '@dumlj/shell-lib' import fs from 'fs-extra' @@ -13,7 +12,6 @@ export interface Project extends ProjectInWorkspaces { } export interface StackblitzWebpackPluginOptions extends SeedWebpackPluginOptions { - publicPath?: string manifest?: string ignored?: string[] files?: string[] @@ -22,7 +20,6 @@ export interface StackblitzWebpackPluginOptions extends SeedWebpackPluginOptions export class StackblitzWebpackPlugin extends SeedWebpackPlugin { static PLUGIN_NAME = 'stackblitz-webpack-plugin' - protected publicPath: string protected manifest: string protected ignored: string[] protected files: string[] @@ -30,7 +27,6 @@ export class StackblitzWebpackPlugin extends SeedWebpackPlugin { constructor(options?: StackblitzWebpackPluginOptions) { super(options) - this.publicPath = options?.publicPath this.manifest = options?.manifest || '/stackblitz-assets.json' this.ignored = [].concat(options?.ignored || [], [ '**/node_modules/**', @@ -199,11 +195,8 @@ export class StackblitzWebpackPlugin extends SeedWebpackPlugin { } public applyScript(compiler: Compiler) { - const { context, webpack, options } = compiler - const { output } = options || {} - - const publicPath = this.publicPath || (typeof output.publicPath === 'string' ? output.publicPath : '/') - const finalPublicPath = trimEnd(publicPath, '/') + '/' + trimStart(this.manifest, '/') + const { context, webpack } = compiler + const finalPublicPath = this.manifest const plugins = [ new webpack.DefinePlugin({ __STACKBLITZ_MANIFEST__: JSON.stringify(finalPublicPath) }), diff --git a/@webpack-plugin/stackblitz-webpack-plugin/src/client/StackblitzLiveDemo.ts b/@webpack-plugin/stackblitz-webpack-plugin/src/client/StackblitzLiveDemo.ts index 73434495..fe9246c2 100644 --- a/@webpack-plugin/stackblitz-webpack-plugin/src/client/StackblitzLiveDemo.ts +++ b/@webpack-plugin/stackblitz-webpack-plugin/src/client/StackblitzLiveDemo.ts @@ -1,7 +1,14 @@ import Stackblitz, { type EmbedOptions } from '@stackblitz/sdk' import Zip from 'jszip' +import { trimEnd, trimStart } from 'lodash' declare const __STACKBLITZ_MANIFEST__: string +declare const __webpack_public_path__: string | (() => string) + +const withPublicPath = (url: string) => { + const baseUrl = typeof __webpack_public_path__ === 'function' ? __webpack_public_path__() : __webpack_public_path__ + return trimEnd(baseUrl, '/') + '/' + trimStart(url, '/') +} interface MANIFEST_ASSETS_STATS { examples: Record @@ -43,7 +50,7 @@ export class StackblitzLiveDemo extends HTMLElement { return this.MANIFEST_CACHE } - const response = await fetch(__STACKBLITZ_MANIFEST__) + const response = await fetch(withPublicPath(__STACKBLITZ_MANIFEST__)) const stats: MANIFEST_ASSETS_STATS = await response.json() this.MANIFEST_CACHE = stats return stats @@ -58,7 +65,7 @@ export class StackblitzLiveDemo extends HTMLElement { const response = await Promise.all( deps.map(async (file) => { - const response = await fetch(`/${file}.zip`) + const response = await fetch(withPublicPath(`/${file}.zip`)) const buffer = await response.arrayBuffer() const zip = new Zip() const { files } = await zip.loadAsync(buffer, { createFolders: false })