Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: offline-only export #2802

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test/frontend/artifacts/*
.DS_Store

frontend-dist
frontend-dist-*
node_modules
.parcel-cache
frontend/dist
Expand Down
39 changes: 39 additions & 0 deletions frontend-bundler/.parcelrc-static
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"extends": "@parcel/config-default",
"resolvers": [
"parcel-resolver-like-a-browser",
"..."
],
"reporters": [
"...",
"parcel-reporter-bundle-manifest"
],
"transformers": {
"*.svg": [
"...",
"@parcel/transformer-inline"
],
"**/alegreya*.woff2": [
"...",
"@parcel/transformer-inline"
],
"**/Vollkorn*.woff2": [
"...",
"@parcel/transformer-inline"
]
},
"optimizers": {
"*.svg": [
"...",
"@parcel/optimizer-data-url"
],
"**/alegreya*.woff2": [
"...",
"@parcel/optimizer-data-url"
],
"**/Vollkorn*.woff2": [
"...",
"@parcel/optimizer-data-url"
]
}
}
3 changes: 3 additions & 0 deletions frontend-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"start": "cd ../frontend && parcel --dist-dir ../frontend-dist --config ../frontend-bundler/.parcelrc editor.html index.html error.jl.html",
"build": "cd ../frontend && parcel build --no-source-maps --public-url . --dist-dir ../frontend-dist --config ../frontend-bundler/.parcelrc editor.html index.html error.jl.html && node ../frontend-bundler/add_sri.js ../frontend-dist/editor.html",
"build-static": "cd ../frontend && parcel build --no-source-maps --public-url . --dist-dir ../frontend-dist-static --config ../frontend-bundler/.parcelrc-static editor.html",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
Expand All @@ -26,6 +27,8 @@
"devDependencies": {
"@parcel/optimizer-blob-url": "^2.11.0",
"@parcel/optimizer-data-url": "^2.11.0",
"@parcel/transformer-inline": "^2.11.0",
"@parcel/transformer-inline-string": "^2.11.0",
"@plutojl/posthtml-crossorigin": "^1.0.0",
"@types/node": "^22",
"posthtml": "^0.16.6",
Expand Down
28 changes: 21 additions & 7 deletions src/notebook/Export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,48 @@
using Base64
using HypertextLiteral
import URIs

using MIMEs: mime_from_path
const default_binder_url = "https://mybinder.org/v2/gh/fonsp/pluto-on-binder/v$(string(PLUTO_VERSION))"

const cdn_version_override = nothing
# const cdn_version_override = "2a48ae2"
const distdir = "frontend-dist-static"

if cdn_version_override !== nothing
@warn "Reminder to fonsi: Using a development version of Pluto for CDN assets. The binder button might not work. You should not see this on a released version of Pluto." cdn_version_override
end

cdnified_editor_html(; kwargs...) = cdnified_html("editor.html"; kwargs...)

function file2base64dataurl(path::AbstractString)
string("data:", mime_from_path(path), ";base64,", base64encode(read(path)))
end

function cdnified_html(filename::AbstractString;
version::Union{Nothing,VersionNumber,AbstractString}=nothing,
pluto_cdn_root::Union{Nothing,AbstractString}=nothing,
base64assets::Bool=true,
)
should_use_bundled_cdn = version ∈ (nothing, PLUTO_VERSION) && pluto_cdn_root === nothing

something(
if should_use_bundled_cdn
try
original = read(project_relative_path("frontend-dist", filename), String)
original = read(project_relative_path(distdir, filename), String)

cdn_root = "https://cdn.jsdelivr.net/gh/fonsp/Pluto.jl@$(string(PLUTO_VERSION))/frontend-dist/"

@debug "Using CDN for Pluto assets:" cdn_root

replace_with_cdn(original) do url
@info "$url"
contains(string(url), "escape_txt_for_html") && return url
# Because parcel creates filenames with a hash in them, we can check if the file exists locally to make sure that everything is in order.
@assert isfile(project_relative_path("frontend-dist", url)) "Could not find the file $(project_relative_path("frontend-dist", url)) locally, that's a bad sign."

@assert isfile(project_relative_path(distdir, url)) "Could not find the file $(project_relative_path(distdir, url)) locally, that's a bad sign."
@info "let's see it $url, $should_use_bundled_cdn "
if base64assets
return file2base64dataurl(project_relative_path(distdir, url))
end
URIs.resolvereference(cdn_root, url) |> string
end
catch e
Expand All @@ -46,7 +57,9 @@
cdn_root = something(pluto_cdn_root, "https://cdn.jsdelivr.net/gh/fonsp/Pluto.jl@$(something(cdn_version_override, string(something(version, PLUTO_VERSION))))/frontend/")

@debug "Using CDN for Pluto assets:" cdn_root

if base64assets
@warn("Trying to use bundled assets for $filename. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.")

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for editor.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.

Check warning on line 61 in src/notebook/Export.jl

View workflow job for this annotation

GitHub Actions / PlutoSliderServer.jl

Trying to use bundled assets for index.html. It's impossible to base64 include Pluto in unbundled mode. If you _really_ need this contact us.
end
replace_with_cdn(original) do url
URIs.resolvereference(cdn_root, url) |> string
end
Expand Down Expand Up @@ -114,7 +127,7 @@
header_html::AbstractString="",
)::String

cdnified = cdnified_editor_html(; version, pluto_cdn_root)
cdnified = cdnified_editor_html(; version, pluto_cdn_root, base64assets=true)

length(statefile_js) > 32000000 && @error "Statefile embedded in HTML is very large. The file can be opened with Chrome and Safari, but probably not with Firefox. If you are using PlutoSliderServer to generate this file, then we recommend the setting `baked_statefile=false`. If you are not using PlutoSliderServer, then consider reducing the size of figures and output in the notebook." length(statefile_js)

Expand All @@ -138,6 +151,7 @@

function replace_at_least_once(s, pair)
from, to = pair
@info "replacing once at least once" s from to
@assert occursin(from, s)
replace(s, pair)
end
Expand Down Expand Up @@ -223,7 +237,7 @@
s
else
url = only(next_match.captures)
if occursin("//", url) || url ∈ dont_cdnify
if occursin("//", url) || url ∈ dont_cdnify || occursin("data:", url)
# skip this one
replace_with_cdn(cdnify, s, nextind(s, next_match.offset))
else
Expand Down
Loading