-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41cb285
commit 7cecc56
Showing
6 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters