Skip to content

Commit

Permalink
New JS flow
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jun 18, 2024
1 parent 41cb285 commit 7cecc56
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 1 deletion.
5 changes: 5 additions & 0 deletions assets/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@
"vendors": [
"@windwalker-io/core"
]
},
"dependencies": {
"fast-glob": "^3.3.2",
"glob": "^10.4.1",
"node-glob": "^1.2.0"
}
}
78 changes: 78 additions & 0 deletions assets/core/src/asset-bundler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { webpackBundle } from '@windwalker-io/fusion';
import { globSync } from 'glob';
import path from 'path';
import { findModules } from './asset-sync.mjs';

export async function bundleJS(source = 'src/Module', dest = 'www/assets/js/', options = {}) {
const mainFile = `./resources/assets/src/front/main.ts`;

const files = findFilesFromGlobArray([
...findModules('**/assets/*.ts'),
`${source}/**/*.ts`,
]);

let listJS = "{\n";

for (const file of files) {
let key = file.relativePath.replace(/assets$/, '').toLowerCase();
key = key.substring(0, key.lastIndexOf('.'));
listJS += `'${key}': () => import('./${file.fullpath}'),\n`;
}

listJS += "}";

let ts = `
import loader from '@windwalker-io/core/src/loader/core-loader.ts';
loader.register(${listJS});
export default loader;
`;

const base64 = Buffer.from(ts).toString('base64');
const dataUri = `data:text/javascript;base64,${base64}`;

return webpackBundle(
'',
`${dest}/app.js`,
(config) => {
config.devtool = false;
config.entry = dataUri;
config.output.uniqueName = 'app';
config.output.libraryTarget = 'module';
config.experiments.outputModule = true;
config.resolve.alias = {
'@main': path.resolve(mainFile)
};
}
);
}

function findFilesFromGlobArray(sources) {
let files = [];

for (const source of sources) {
files = [
...files,
...findFiles(source)
];
}

return files;
}

/**
* @param {string} src
*/
function findFiles(src) {
const i = src.lastIndexOf('**');

const path = src.substring(0, i);

return globSync(src).map((file) => {
return {
fullpath: file,
relativePath: file.substring(path.length)
};
});
}
64 changes: 64 additions & 0 deletions assets/core/src/core-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export class CoreLoader {
routes = {};

/**
* @param {Record<string, Function | string>} routes
* @returns {CoreLoader}
*/
register(routes) {
for (const route in routes) {
let target = routes[route];

this.add(route, target);
}

return this;
}

/**
* @param {string} route
* @param {string | Function} target
* @returns {CoreLoader}
*/
add(route, target) {
if (typeof target === 'string') {
target = () => import(target);
}

this.routes[route] = target;

return this;
}

/**
* @param {string} route
* @returns {CoreLoader}
*/
remove(route) {
delete this.routes[route];

return this;
}

/**
* @param {string} route
* @returns {Promise<*>}
*/
async import(route) {
const target = this.routes[route];

if (!target) {
throw new Error(`Unable to import file: ${route}, file not found.`);
}

return target();
}

reset() {
this.routes = {};

return this;
}
}

export const loader = new CoreLoader();
2 changes: 2 additions & 0 deletions assets/core/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
*/

export * from './asset-sync.mjs';
export * from './asset-bundler.mjs';
export * from './install-vendors.mjs';
export * from './core-loader.mjs';
45 changes: 45 additions & 0 deletions assets/core/src/loader/core-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export class CoreLoader {
routes: Record<string, Function> = {};

register(routes: Record<string, Function | string>) {
for (const route in routes) {
let target = routes[route];

this.add(route, target);
}

return this;
}

add(route: string, target: Function | string) {
if (typeof target === 'string') {
target = () => import(`${target as string}`);
}

this.routes[route] = target;
}

remove(route: string) {
delete this.routes[route];

return this;
}

async import<T = any>(route: string): Promise<T> {
const target = this.routes[route];

if (!target) {
throw new Error(`Unable to import file: ${route}, file not found.`);
}

return target();
}

reset() {
this.routes = {};

return this;
}
}

export default new CoreLoader();
2 changes: 1 addition & 1 deletion assets/fusion/src/utilities/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function webpackBasicConfig() {
use: [{
loader: 'babel-loader',
options: babelBasicOptions().get()
}, 'webpack-comment-remover-loader']
}]
}
]
},
Expand Down

0 comments on commit 7cecc56

Please sign in to comment.