Skip to content

Commit

Permalink
latest source code
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-cen committed Jul 11, 2023
1 parent c6c00b7 commit b38b417
Show file tree
Hide file tree
Showing 209 changed files with 24,346 additions and 9,003 deletions.
25 changes: 25 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended"
],
"overrides": [
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint"
],
"rules": {
}
}
Empty file added Releases.md
Empty file.
93 changes: 88 additions & 5 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import builtins from 'builtin-modules';
import * as dotenv from 'dotenv';
import esbuild from "esbuild";
import process from "process";
import builtins from 'builtin-modules'
import fs from 'fs'
import path from 'path'
import { copy } from 'esbuild-plugin-copy';
import * as dotenv from 'dotenv'
import watPlugin from 'esbuild-plugin-wat';
import fs from 'fs';
import path from 'path';
import process from "process";
dotenv.config()

const banner =
Expand All @@ -21,6 +21,88 @@ const prev = (process.argv[2] === 'preview');
const buildv = prod || prev


import findCacheDir from 'find-cache-dir';

function inlineWorkerPlugin(extraConfig) {
return {
name: 'esbuild-plugin-inline-worker',

setup(build) {
build.onLoad(
{filter: /\.worker\.(js|jsx|ts|tsx)$/},
async ({path: workerPath}) => {
// let workerCode = await fs.promises.readFile(workerPath, {
// encoding: 'utf-8',
// });

let workerCode = await buildWorker(workerPath, extraConfig);
return {
contents: `import inlineWorker from '__inline-worker'
export default function Worker() {
return inlineWorker(${JSON.stringify(workerCode)});
}
`,
loader: 'js',
};
}
);

const name = { name: 'Superstate Worker'}

const inlineWorkerFunctionCode = `
export default function inlineWorker(scriptText) {
let blob = new Blob([scriptText], {type: 'text/javascript'});
let url = URL.createObjectURL(blob);
let worker = new Worker(url, ${JSON.stringify(name)});
URL.revokeObjectURL(url);
return worker;
}
`;

build.onResolve({filter: /^__inline-worker$/}, ({path}) => {
return {path, namespace: 'inline-worker'};
});
build.onLoad({filter: /.*/, namespace: 'inline-worker'}, () => {
return {contents: inlineWorkerFunctionCode, loader: 'js'};
});
},
};
}



let cacheDir = findCacheDir({
name: 'esbuild-plugin-inline-worker',
create: true,
});

async function buildWorker(workerPath, extraConfig) {
let scriptNameParts = path.basename(workerPath).split('.');
scriptNameParts.pop();
scriptNameParts.push('js');
let scriptName = scriptNameParts.join('.');
let bundlePath = path.resolve(cacheDir, scriptName);

if (extraConfig) {
delete extraConfig.entryPoints;
delete extraConfig.outfile;
delete extraConfig.outdir;
delete extraConfig.workerName;
}

await esbuild.build({
entryPoints: [workerPath],
bundle: true,
minify: true,
outfile: bundlePath,
target: 'es2017',
format: 'esm',
...extraConfig,
});

return fs.promises.readFile(bundlePath, {encoding: 'utf-8'});
}

const preactCompatPlugin = {
name: "preact-compat",
setup(build) {
Expand Down Expand Up @@ -76,6 +158,7 @@ esbuild.build({
outfile: outputDir+'/main.js',
plugins: [renamePlugin,
preactCompatPlugin,
inlineWorkerPlugin(),
watPlugin(),
...(buildv ? [copy({
resolveFrom: 'cwd',
Expand Down
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
declare module '*.css';
declare module "*.css";
Loading

0 comments on commit b38b417

Please sign in to comment.