Skip to content

Commit

Permalink
[gem-book] sandpack support history
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Sep 11, 2024
1 parent e0cbebf commit d908a8d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/duoyun-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pnpm test
# build watch
pnpm start
# development, view example and api
pnpm docs
pnpm run docs
```
4 changes: 2 additions & 2 deletions packages/gem-book/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"build:cli": "esbuild ./src/bin/index.ts --tsconfig=./tsconfig.cli.json --outdir=./bin --platform=node --sourcemap --bundle --external:anymatch --external:cheerio --external:chokidar --external:jimp --external:marked --external:yaml --external:front-matter --external:commander --external:webpack --external:ts-loader --external:typescript --external:webpack-dev-server --external:html-webpack-plugin --external:copy-webpack-plugin --external:workbox-webpack-plugin",
"start:cli": "pnpm build:cli --watch",
"docs": "node ./bin docs",
"start:docs": "cross-env GEM_BOOK_DEV=true nodemon --watch bin --exec \"pnpm docs\"",
"start:docs": "cross-env GEM_BOOK_DEV=true nodemon --watch bin --exec \"pnpm run docs\"",
"start": "concurrently npm:start:cli npm:start:docs",
"build:website": "pnpm build:cli && pnpm docs --build --ga G-7X2Z4B2KV0",
"build:website": "pnpm build:cli && pnpm run docs --build --ga G-7X2Z4B2KV0",
"build": "pnpm build:cli && tsc -p ./tsconfig.build.json",
"test": "cross-env NODE_OPTIONS=--no-experimental-fetch wtr",
"prepublishOnly": "pnpm build"
Expand Down
19 changes: 14 additions & 5 deletions packages/gem-book/src/bin/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,22 @@ export async function buildApp(dir: string, options: Required<CliUniqueConfig>,
}),
{
apply(compiler: Compiler) {
options.fallbackLanguage &&
compiler.hooks.compilation.tap('htmlWebpackInjectAttributesPlugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).afterTemplateExecution.tapAsync('MyPlugin', (data, cb) => {
compiler.hooks.compilation.tap('htmlWebpackInjectAttributesPlugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).afterTemplateExecution.tapAsync('MyPlugin', (data, cb) => {
if (options.fallbackLanguage) {
data.html = data.html.replace('<html>', `<html lang="${options.fallbackLanguage}">`);
cb(null, data);
});
}
// 参数有 _html_ 时直接渲染 _html_
data.html =
`<script>
const content = new URLSearchParams(location.search).get('_html_');
if (content) {
try {document.write(decodeURIComponent(content) + '<!--');} catch {}
}
</script>` + data.html;
cb(null, data);
});
});
},
},
new DefinePlugin({
Expand Down
9 changes: 7 additions & 2 deletions packages/gem-book/src/plugins/sandpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,13 @@ class _GbpSandpackElement extends GemBookPluginElement {
.join('')}
<script type="module">${code}</script>
`;
URL.revokeObjectURL(iframe.src);
iframe.src = URL.createObjectURL(new Blob([htmlCode], { type: 'text/html' }));
if (document.head?.firstElementChild?.textContent?.includes('_html_')) {
const url = new URL(`./?${new URLSearchParams({ _html_: encodeURIComponent(htmlCode) })}`, location.href);
iframe.src = url.href;
} else {
URL.revokeObjectURL(iframe.src);
iframe.src = URL.createObjectURL(new Blob([htmlCode], { type: 'text/html' }));
}
};
compile();
this.#state({ status: 'done' });
Expand Down
6 changes: 4 additions & 2 deletions packages/gem-devtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"name": "gem-devtools",
"version": "2.0.0-alpha.0",
"version": "2.0.0-alpha.1+5cccf43",
"description": "Gem devtools",
"scripts": {
"update:version": "node ./scripts/update.js",
Expand Down Expand Up @@ -31,5 +32,6 @@
"bugs": {
"url": "https://github.com/mantou132/gem/issues"
},
"homepage": "https://github.com/mantou132/gem#readme"
"homepage": "https://github.com/mantou132/gem#readme",
"gitHead": "5cccf43261f8eae0c76a80ec90e243fb880b10a9"
}
4 changes: 2 additions & 2 deletions packages/gem-devtools/scripts/update.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const pkg = require('../public/manifest.json');
const pkg = require('../src/manifest.json');
pkg.version = require('../package.json').version;
require('fs').writeFileSync('./public/manifest.json', JSON.stringify(pkg, null, 2));
require('fs').writeFileSync('./src/manifest.json', JSON.stringify(pkg, null, 2));
4 changes: 2 additions & 2 deletions packages/gem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"typings": "index.d.ts",
"scripts": {
"docs": "GEM_BOOK_REPLACE=true gem-book docs",
"docs:remote": "pnpm docs",
"build:docs": "pnpm docs --build",
"docs:remote": "pnpm run docs",
"build:docs": "pnpm run docs --build",
"build:module": "tsc -p tsconfig.build.json",
"build:lib": "vite build",
"build": "pnpm build:module && pnpm build:lib",
Expand Down

0 comments on commit d908a8d

Please sign in to comment.