Skip to content

Commit

Permalink
ci(custom): 兼容 ghpages publichpath
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidKk committed Oct 24, 2023
1 parent 28d7635 commit ceee40c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions @dumi-plugin/dumi-plugin-node-stackblitz/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface NodeStackblitzOptions {
}

export default createDumiPlugin<NodeStackblitzOptions>('nodeStackblitz', async (api, { pushWebpackPlugin }) => {
const { publicPath, nodeStackblitz = {} } = api.userConfig || {}
const { nodeStackblitz = {} } = api.userConfig || {}

api.describe({
key: 'nodeStackblitz',
Expand All @@ -23,5 +23,5 @@ export default createDumiPlugin<NodeStackblitzOptions>('nodeStackblitz', async (
},
})

pushWebpackPlugin(StackblitzWebpackPlugin.PLUGIN_NAME, new StackblitzWebpackPlugin({ ...nodeStackblitz, publicPath }))
pushWebpackPlugin(StackblitzWebpackPlugin.PLUGIN_NAME, new StackblitzWebpackPlugin(nodeStackblitz))
})
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -13,7 +12,6 @@ export interface Project extends ProjectInWorkspaces {
}

export interface StackblitzWebpackPluginOptions extends SeedWebpackPluginOptions {
publicPath?: string
manifest?: string
ignored?: string[]
files?: string[]
Expand All @@ -22,15 +20,13 @@ 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[]

constructor(options?: StackblitzWebpackPluginOptions) {
super(options)

this.publicPath = options?.publicPath
this.manifest = options?.manifest || '/stackblitz-assets.json'
this.ignored = [].concat(options?.ignored || [], [
'**/node_modules/**',
Expand Down Expand Up @@ -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) }),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, string[]>
Expand Down Expand Up @@ -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
Expand All @@ -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 })
Expand Down

0 comments on commit ceee40c

Please sign in to comment.