Skip to content

Commit

Permalink
Add implementation from PrairieLearn/PrairieLearn (#1)
Browse files Browse the repository at this point in the history
* Add implementation from PrairieLearn/PrairieLearn

Derives from PrairieLearn/PrairieLearn#9900

* remove unnecessary eslint ignore

* Remove unused EXCALIDRAW_ASSET_PATH

We are bundling all necessary assets into JS/CSS so that we no
longer need to statically serve an `excalidraw-dist/`. In case
of a package upgrade in the future breaking this contract, we
could either point the URL to a non-existent path to catch errors
or let it fall back to the default unpkg mirror. We choose the
latter.
  • Loading branch information
nishanthkarthik authored Jun 17, 2024
1 parent b155c0e commit fd37afb
Show file tree
Hide file tree
Showing 6 changed files with 1,831 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
node_modules
*.tgz
.idea
70 changes: 70 additions & 0 deletions esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { promises } from 'fs';

import * as esbuild from 'esbuild';

/* Export and embed JSON as JS */
const JSONLoader = {
name: 'json-loader',
setup({ onLoad }) {
onLoad({ filter: /\.json$/ }, async (args) => {
const text = await promises.readFile(args.path, 'utf8');
return { contents: `export default ${text}`, loader: 'js' };
});
},
};

const TextReplacer = ({ include, pattern, loader }) => ({
name: 'text-replacer',
setup({ onLoad }) {
onLoad({ filter: include }, async (args) => {
let contents = await promises.readFile(args.path, 'utf-8');
for (const [from, to] of pattern) {
contents = contents.replaceAll(from, to);
}
return { contents, loader };
});
},
});

await esbuild.build({
entryPoints: ['./src/index.js'],
outdir: './dist',
bundle: true,
splitting: true,
minify: true,
format: 'esm',
define: {
'process.env.IS_PREACT': 'false',
},
plugins: [
JSONLoader,
TextReplacer({
include: /.js$/,
loader: 'js',
pattern: [
/* Prevent bootstrap style from hijacking visibility
* https://github.com/excalidraw/excalidraw/issues/3825 */
['dropdown-menu', 'ex-dropdown-menu'],
],
}),
],
});

await esbuild.build({
entryPoints: ['./src/index.css'],
outdir: './dist',
bundle: true,
minify: true,
loader: { '.woff2': 'dataurl' },
plugins: [
TextReplacer({
include: /.css$/,
loader: 'css',
pattern: [
/* Prevent bootstrap style from hijacking visibility
* https://github.com/excalidraw/excalidraw/issues/3825 */
['.dropdown-menu', '.ex-dropdown-menu'],
],
}),
],
});
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@prairielearn/excalidraw-builds",
"type": "module",
"version": "1.0.0",
"description": "Excalidraw bundled with React",
"license": "MIT",
"main": "dist/index.js",
"files": ["dist"],
"repository": "[email protected]:PrairieLearn/excalidraw-builds.git",
"scripts": {
"build": "node esbuild.config.js"
},
"devDependencies": {
"@excalidraw/excalidraw": "0.17.1-2f9526d",
"react": "18.3.1",
"react-dom": "18.3.1",
"esbuild": "^0.21.3"
}
}
1 change: 1 addition & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '@excalidraw/excalidraw/index.css';
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';

import * as ExcalidrawLib from '@excalidraw/excalidraw';

export { React, ReactDOM, ExcalidrawLib };
Loading

0 comments on commit fd37afb

Please sign in to comment.