Support dynamic import

This commit is contained in:
the1812 2019-03-19 17:07:22 +08:00
parent c28a59464b
commit d8c32c68cb
10 changed files with 84 additions and 27 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.vscode/ .vscode/
builder/node/ builder/node/
builder/dotnet/Properties
.node_modules/ .node_modules/
package-lock.json package-lock.json
build.cache build.cache

View File

@ -1,6 +1,6 @@
// ==UserScript== // ==UserScript==
// @name Bilibili Evolved (Offline) // @name Bilibili Evolved (Offline)
// @version 245.99 // @version 246.06
// @description Bilibili Evolved 的离线版, 所有功能都已内置于脚本中. // @description Bilibili Evolved 的离线版, 所有功能都已内置于脚本中.
// @author Grant Howard, Coulomb-G // @author Grant Howard, Coulomb-G
// @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G) // @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G)
@ -1660,6 +1660,7 @@ class ResourceManager
constructor() constructor()
{ {
this.data = Resource.all; this.data = Resource.all;
this.skippedImport = [];
this.attributes = {}; this.attributes = {};
this.styleManager = new StyleManager(this); this.styleManager = new StyleManager(this);
const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor"); const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor");
@ -1712,7 +1713,12 @@ class ResourceManager
import(componentName) import(componentName)
{ {
const resource = Resource.all[componentName]; const resource = Resource.all[componentName];
if (resource && resource.type.name === "html") if (!resource)
{
this.skippedImport.push(componentName);
return;
}
if (resource.type.name === "html")
{ {
if (!resource.downloaded) if (!resource.downloaded)
{ {

View File

@ -1,6 +1,6 @@
// ==UserScript== // ==UserScript==
// @name Bilibili Evolved (Preview Offline) // @name Bilibili Evolved (Preview Offline)
// @version 245.99 // @version 246.06
// @description Bilibili Evolved 的预览离线版, 可以抢先体验新功能, 并且所有功能都已内置于脚本中. // @description Bilibili Evolved 的预览离线版, 可以抢先体验新功能, 并且所有功能都已内置于脚本中.
// @author Grant Howard, Coulomb-G // @author Grant Howard, Coulomb-G
// @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G) // @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G)
@ -1660,6 +1660,7 @@ class ResourceManager
constructor() constructor()
{ {
this.data = Resource.all; this.data = Resource.all;
this.skippedImport = [];
this.attributes = {}; this.attributes = {};
this.styleManager = new StyleManager(this); this.styleManager = new StyleManager(this);
const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor"); const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor");
@ -1712,7 +1713,12 @@ class ResourceManager
import(componentName) import(componentName)
{ {
const resource = Resource.all[componentName]; const resource = Resource.all[componentName];
if (resource && resource.type.name === "html") if (!resource)
{
this.skippedImport.push(componentName);
return;
}
if (resource.type.name === "html")
{ {
if (!resource.downloaded) if (!resource.downloaded)
{ {

View File

@ -1611,6 +1611,7 @@ class ResourceManager
constructor() constructor()
{ {
this.data = Resource.all; this.data = Resource.all;
this.skippedImport = [];
this.attributes = {}; this.attributes = {};
this.styleManager = new StyleManager(this); this.styleManager = new StyleManager(this);
const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor"); const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor");
@ -1663,7 +1664,12 @@ class ResourceManager
import(componentName) import(componentName)
{ {
const resource = Resource.all[componentName]; const resource = Resource.all[componentName];
if (resource && resource.type.name === "html") if (!resource)
{
this.skippedImport.push(componentName);
return;
}
if (resource.type.name === "html")
{ {
if (!resource.downloaded) if (!resource.downloaded)
{ {

View File

@ -1611,6 +1611,7 @@ class ResourceManager
constructor() constructor()
{ {
this.data = Resource.all; this.data = Resource.all;
this.skippedImport = [];
this.attributes = {}; this.attributes = {};
this.styleManager = new StyleManager(this); this.styleManager = new StyleManager(this);
const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor"); const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor");
@ -1663,7 +1664,12 @@ class ResourceManager
import(componentName) import(componentName)
{ {
const resource = Resource.all[componentName]; const resource = Resource.all[componentName];
if (resource && resource.type.name === "html") if (!resource)
{
this.skippedImport.push(componentName);
return;
}
if (resource.type.name === "html")
{ {
if (!resource.downloaded) if (!resource.downloaded)
{ {

View File

@ -12,17 +12,11 @@ namespace BilibiliEvolved.Build
public ProjectBuilder BuildClient() public ProjectBuilder BuildClient()
{ {
var source = File.ReadAllText("client/bilibili-evolved.js"); var source = File.ReadAllText("client/bilibili-evolved.js");
var importRegex = new Regex(@"import (.*) from [""'](.*)[""'];", RegexOptions.Compiled | RegexOptions.IgnoreCase); source = RegexReplacer.Replace(source, @"import (.*) from [""'](.*)[""'];", match =>
while (true)
{ {
var match = importRegex.Match(source);
if (!match.Success)
{
break;
}
var module = File.ReadAllText("client/" + match.Groups[2].Value.Replace("./", "") + ".js").Replace("export ", ""); var module = File.ReadAllText("client/" + match.Groups[2].Value.Replace("./", "") + ".js").Replace("export ", "");
source = source.Replace(match.Value, module); return module;
} });
File.WriteAllText(SourcePath, source); File.WriteAllText(SourcePath, source);
Source = File.ReadAllText(SourcePath); Source = File.ReadAllText(SourcePath);
WriteSuccess("Client Build complete."); WriteSuccess("Client Build complete.");

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace BilibiliEvolved.Build
{
static class RegexReplacer
{
public static string Replace(string text, string regexText, Func<Match, string> func)
{
var regex = new Regex(regexText, RegexOptions.Compiled | RegexOptions.IgnoreCase);
while (true)
{
var match = regex.Match(text);
if (!match.Success)
{
break;
}
text = text.Replace(match.Value, func(match));
}
return text;
}
}
}

View File

@ -142,23 +142,26 @@ namespace BilibiliEvolved.Build
{ {
if (!input.StartsWith("(() =>")) if (!input.StartsWith("(() =>"))
{ {
var importRegex = new Regex(@"import (.*) from [""'](.*)[""'];", RegexOptions.Compiled | RegexOptions.IgnoreCase); Func<string, string> convertToRuntimeSource = source =>
while (true)
{ {
var match = importRegex.Match(input);
if (!match.Success)
{
break;
}
var imported = match.Groups[1].Value.Replace(" as ", ":");
var source = match.Groups[2].Value;
var index = source.LastIndexOf("/"); var index = source.LastIndexOf("/");
if (index != -1) if (index != -1)
{ {
source = source.Remove(0, index + 1); source = source.Remove(0, index + 1);
} }
input = input.Replace(match.Value, $"const {imported} = resources.import(\"{source}\");"); return source;
} };
input = RegexReplacer.Replace(input, @"import (.*) from [""'](.*)[""'];", match =>
{
var imported = match.Groups[1].Value.Replace(" as ", ":");
var source = convertToRuntimeSource(match.Groups[2].Value);
return $"const {imported} = resources.import(\"{source}\");";
});
input = RegexReplacer.Replace(input, @" import\((.*)\)", match =>
{
var source = convertToRuntimeSource(match.Groups[1].Value);
return $" resources.importAsync(\"{source}\");";
});
input = @"(() => input = @"(() =>
{ {
return (settings, resources) => return (settings, resources) =>

Binary file not shown.

View File

@ -3,6 +3,7 @@ export class ResourceManager
constructor() constructor()
{ {
this.data = Resource.all; this.data = Resource.all;
this.skippedImport = [];
this.attributes = {}; this.attributes = {};
this.styleManager = new StyleManager(this); this.styleManager = new StyleManager(this);
const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor"); const styleMethods = Object.getOwnPropertyNames(StyleManager.prototype).filter(it => it !== "constructor");
@ -55,7 +56,12 @@ export class ResourceManager
import(componentName) import(componentName)
{ {
const resource = Resource.all[componentName]; const resource = Resource.all[componentName];
if (resource && resource.type.name === "html") if (!resource)
{
this.skippedImport.push(componentName);
return;
}
if (resource.type.name === "html")
{ {
if (!resource.downloaded) if (!resource.downloaded)
{ {