Browse Source

Add Plugin Bundle && More Modern Builder

master
186526 1 year ago
parent
commit
c86c77caf6
Signed by: 186526 GPG Key ID: C7EB1E6B8CC5E51D
11 changed files with 316 additions and 39 deletions
  1. 38
      .devcontainer/devcontainer.json
  2. 3
      .gitignore
  3. 0
      app.js
  4. 111
      examples/bundle.js
  5. 15
      package.json
  6. 152
      src/builder/bundler.js
  7. 8
      src/config/blogging.config.js
  8. 6
      src/core/index.js
  9. 14
      src/js/index.js
  10. 4
      src/plugins/marked/package.json
  11. 4
      yarn.lock

38
.devcontainer/devcontainer.json

@ -1,27 +1,25 @@
{
"name": "Node.js",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 10, 12, 14
"args": { "VARIANT": "12" }
},
"name": "Node.js",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 10, 12, 14
"args": { "VARIANT": "12" }
},
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint"
],
// Add the IDs of extensions you want installed when the container is created.
"extensions": ["dbaeumer.vscode-eslint"],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node"
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node"
}

3
.gitignore

@ -4,4 +4,5 @@ dist
src/**/*prod*
src/**/dist/*
.vercel
.devcontainer
.devcontainer
src/**/*bundle.js

0
app.js

111
examples/bundle.js

@ -0,0 +1,111 @@
((a) => {
class event {
constructor(name, cancelable = false, bubbles = false) {
this.name = name;
this.cancelable = cancelable;
this.bubbles = bubbles;
this.Event = new Event(name, { bubbles: bubbles, cancelable: cancelable });
}
dispatch(message, obj = window) {
if ("blogging_events" in window) {
window.blogging_events.push({
event: this.name,
message: message,
trigger: false,
});
} else {
window.blogging_events = [
{ event: this.name, message: message, trigger: false },
];
}
obj.dispatchEvent(this.Event);
}
dispatchEvent(message, obj = window) {
this.dispatch(obj);
}
async listen(eventlistener, trigger = false, obj = window) {
obj.addEventListener(this.name, (e) => {
for (let i in window.blogging_events) {
let x = window.blogging_events[i];
if (x.event === this.name && x.trigger === trigger) {
eventlistener(x.message, e);
x.trigger = true;
return;
}
}
eventlistener(null, e);
});
}
}
a.forEach(e => {
(async () => {
new event(e.config.listen)
.listen(() => {
for (let i of e.code.js) {
((i) => {
let p = document.createElement("script");
p.innerHTML = i.code;
p.type = ((a) => {
if (a.module === true) {
return "module";
} else {
return null;
}
})(i);
p.async = i.async;
p.defer = i.defer;
document.querySelector("head").appendChild(p);
})(i);
}
for (let i of e.code.css) {
(i => {
let p = document.createElement("style");
p.innerHTML = i;
document.querySelector("head").appendChild(p);
})(i);
}
const t = {
name: e.config.name,
type: e.config.type,
listen: e.config.listen,
bundle: true,
load: {
js: e.code.js,
css: e.code.css,
}
};
if ("blogging_plugins" in window) {
window.blogging_plugins.push(t);
new event("loadedplugin").dispatch({
load: true,
name: e.config.name,
});
} else {
window.blogging_plugins = [t];
new event("loadedplugin").dispatch({
load: true,
name: e.config.name,
});
}
});
})(e);
});
})([
{
config: {
"name": "marked",
"type": "render",
"listen": "render",
}, code: {
js: [
{
code: "console.log(123)",
module: true,
async: true,
defer: false,
}
],
css: []
}
}
]);

15
package.json

@ -2,19 +2,20 @@
"name": "blogging-next",
"version": "1.0.0",
"description": "A Blog Platform who running at Browser",
"main": "app.js",
"main": "src/js/index.js",
"type": "module",
"private": true,
"scripts": {
"dev": "yarn build:plugins;yarn dev:blogging",
"dev:blogging": "snowpack dev",
"build": "yarn clean;yarn build:plugins;yarn build:blogging",
"build": "yarn clean;yarn package:plugins;yarn build:blogging",
"build:plugins": "lerna exec --scope @blogging/plugin-* \"yarn build;\"",
"build:blogging": "snowpack build;cp examples/config.json build/config.json",
"package": "yarn clean;yarn package:blogging",
"package:plugins": "yarn run build:plugins;node src/builder/bundler.js > src/plugins/bundle.js;minify src/plugins/bundle.js > src/plugins/bundle.prod.js",
"package:blogging": "yarn build;mkdir dist;tar -czvf ./dist/blogging-master.tar.gz ./build",
"package:web": "yarn build;http-server -p8080 build;yarn clean",
"format": "prettier --write \"*[^build]/**/*[^prod][email protected](css|js|html|json)\"",
"format": "prettier --write \"*[^build]/**/*[^prod][^bundle][email protected](css|js|html|json)\"",
"clean": "rm -rf ./dist/ ./build/"
},
"repository": {
@ -34,13 +35,13 @@
"src/plugins/*"
],
"devDependencies": {
"@snowpack/plugin-webpack": "^2.1.1",
"http-server": "^0.12.3",
"lerna": "^3.22.1",
"minify": "^6.0.1",
"prettier": "^2.1.2",
"snowpack": "^2.16.1",
"yarn": "^1.22.10"
},
"dependencies": {
"@snowpack/plugin-webpack": "^2.1.1",
"snowpack": "^2.16.1"
}
"dependencies": {}
}

152
src/builder/bundler.js

@ -0,0 +1,152 @@
import fs from "fs";
import { config } from "../config/blogging.config.js";
let maincode = `((a) => {
class event {
constructor(name, cancelable = false, bubbles = false) {
this.name = name;
this.cancelable = cancelable;
this.bubbles = bubbles;
this.Event = new Event(name, { bubbles: bubbles, cancelable: cancelable });
}
dispatch(message, obj = window) {
if ("blogging_events" in window) {
window.blogging_events.push({
event: this.name,
message: message,
trigger: false,
});
} else {
window.blogging_events = [
{ event: this.name, message: message, trigger: false },
];
}
obj.dispatchEvent(this.Event);
}
dispatchEvent(message, obj = window) {
this.dispatch(obj);
}
async listen(eventlistener, trigger = false, obj = window) {
obj.addEventListener(this.name, (e) => {
for (let i in window.blogging_events) {
let x = window.blogging_events[i];
if (x.event === this.name && x.trigger === trigger) {
eventlistener(x.message, e);
x.trigger = true;
return;
}
}
eventlistener(null, e);
});
}
}
a.forEach(e => {
(async () => {
new event(e.config.listen)
.listen(() => {
for (let i of e.code.js) {
((i) => {
let p = document.createElement("script");
p.innerHTML = i.code;
p.type = ((a) => {
if (a.module === true) {
return "module";
} else {
return null;
}
})(i);
p.async = i.async;
p.defer = i.defer;
document.querySelector("head").appendChild(p);
})(i);
}
for (let i of e.code.css) {
(i => {
let p = document.createElement("style");
p.innerHTML = i;
document.querySelector("head").appendChild(p);
})(i);
}
const t = {
name: e.config.name,
type: e.config.type,
listen: e.config.listen,
bundle: true,
load: {
js: e.code.js,
css: e.code.css,
}
};
if ("blogging_plugins" in window) {
window.blogging_plugins.push(t);
new event("loadedplugin").dispatch({
load: true,
name: e.config.name,
});
} else {
window.blogging_plugins = [t];
new event("loadedplugin").dispatch({
load: true,
name: e.config.name,
});
}
});
})(e);
});
})(`;
function geturl(a, b) {
return new URL(
a,
((a) => {
let t = "/";
let d = new URL(a);
for (let i of d.pathname.split("/").slice(1, -1)) {
t = t + i + "/";
}
return d.protocol + "//" + d.host + t;
})("file://" + b.replace("../", "dbldot/").replace("./", "dot/"))
).href
.replace("file://", "")
.replace("dbldot/", "../")
.replace("dot/", "./");
}
var a = [];
config.plugins.plugin.forEach((e) => {
let tmp = e.manifest.replace("./blogging_plugins", "src/plugins");
let orgin = JSON.parse(
fs.readFileSync(
e.manifest.replace("./blogging_plugins", "src/plugins"),
"utf8"
)
);
let json = {
config: {
name: orgin.name,
type: orgin.type,
listen: orgin.listen,
},
code: {
js: ((e = []) => {
let b = [];
e.forEach((a) => {
b.push({
code: fs.readFileSync(geturl(a.url, tmp), "utf8"),
module: a.module,
async: a.async,
defer: a.defer,
});
});
return b;
})(orgin.load.js),
css: ((e = []) => {
let b = [];
e.forEach((a) => {
b.push(fs.readFileSync(geturl(a.url, tmp), "utf8"));
});
return b;
})(orgin.load.css),
},
};
a.push(json);
});
maincode += JSON.stringify(a) + ")";
console.log(maincode);

8
src/config/blogging.config.js

@ -1,4 +1,10 @@
const config = {
plugins: [{ manifest: "./blogging_plugins/marked/manifest.json" }],
plugins: {
plugin: [{ manifest: "./blogging_plugins/marked/manifest.json" }],
bundle: { file: "./blogging_plugins/bundle.prod.js" },
},
parser: {
files_type: [{ type: "markdown", use: "marked" }],
},
};
export { config };

6
src/core/index.js

@ -2,7 +2,7 @@ import { plugin } from "./plugins/";
import { event } from "./event/";
import { parser } from "./parse/";
class blogging {
constructor(config, plugins) {
constructor(config) {
this.config = config;
this.plugins = config.plugins;
this.on = event;
@ -23,9 +23,7 @@ class blogging {
window.blogging_handler,
((a) => {
if (typeof a != "object") {
return {
files_type: [{ type: "markdown", use: "marked" }],
};
return this.config.parser;
} else {
return a;
}

14
src/js/index.js

@ -1,10 +1,18 @@
import { config } from "../config/blogging.config.js";
import { blogging } from "../core/index";
import { blogging } from "../core/index.js";
import env from "../__snowpack__/env.js";
window.$bl = new blogging({
el: "#blogging",
plugins: config.plugins,
plugins: config.plugins.plugin,
parser: config.parser,
});
$bl.reg_plugins();
if (env.MODE === "production") {
let a = document.createElement("script");
a.src = config.plugins.bundle.file;
document.head.appendChild(a);
} else {
$bl.reg_plugins();
}
new $bl.on("loadedplugin").listen((message, event) => {
console.log(message);
});

4
src/plugins/marked/package.json

@ -7,9 +7,11 @@
"license": "MIT",
"sourceType": "module",
"dependencies": {
"marked": "^1.2.5"
},
"devDependencies":{
"browserify": "^17.0.0",
"esmify": "^2.1.1",
"marked": "^1.2.5",
"minify": "^6.0.1"
},
"scripts": {

4
yarn.lock

@ -6371,8 +6371,8 @@ [email protected]^1.0.0:
[email protected]^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/minify/-/minify-6.0.1.tgz#d53413fc2e67b47642269087bc912da60ab6492e"
integrity sha512-JMG5VruvghXZ1VnPCffnpESUzrhNPTT/uNogew9CmPdd3zmohSEt8/HhPxpR7CiXnBYWExW2gGoagZxTy9rnLg==
resolved "https://registry.npm.taobao.org/minify/download/minify-6.0.1.tgz#d53413fc2e67b47642269087bc912da60ab6492e"
integrity sha1-1TQT/C5ntHZCJpCHvJEtpgq2SS4=
dependencies:
clean-css "^4.1.6"
css-b64-images "~0.2.5"
Loading…
Cancel
Save