Skip to content

Commit f3150f0

Browse files
committed
添加 exml-loader
1 parent 1c1657f commit f3150f0

File tree

5 files changed

+87
-30
lines changed

5 files changed

+87
-30
lines changed

egret-webpack-bundler/src/loaders/exml.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// const className = result.className.replace(/^skins./, 'eui.');
55
// const resource = this.resource.replace(this.rootContext || (this as any).options.context, '.');
66

7+
import * as eui from "@egret/eui-compiler";
8+
import * as webpack from 'webpack';
9+
import * as path from 'path';
710
// let code = `${STATIC}
811
// module.exports = ${result.code};
912
// if (window.generateEUI) {
@@ -22,6 +25,16 @@
2225
// return code;
2326
// };
2427

25-
export default function exmlLoader(content: string) {
26-
return "module.exports = 1111"
28+
const exmlLoader: webpack.loader.Loader = function (content) {
29+
30+
const { parser, emitter } = eui;
31+
const skinNode = parser.generateAST(content.toString());
32+
const jsEmitter = new emitter.JavaScriptEmitter();
33+
const relativePath = path.relative(this.rootContext, this.resourcePath).split("\\").join("/");
34+
jsEmitter.emitSkinNode(relativePath, skinNode);
35+
const result = `module.exports = ${jsEmitter.getResult()};`
36+
// const result = `module.exports = 1`
37+
return result;
2738
}
39+
40+
export default exmlLoader;

egret-webpack-bundler/src/loaders/theme.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import * as utils from './utils';
88

99
interface ThemePluginOptions {
1010
dirs?: string[];
11-
thmJS?: string;
12-
thmJSON?: string;
1311
exmlDeclare?: string;
1412
}
1513

@@ -33,8 +31,6 @@ export default class ThemePlugin {
3331
constructor(options: ThemePluginOptions) {
3432
this.options = {
3533
dirs: ['resource/eui_skins', 'resource/skins'], // 扫描目录
36-
thmJS: 'resource/default.thm.js', // must not empty
37-
thmJSON: 'resource/default.thm.json', // json供编辑器使用
3834
exmlDeclare: 'libs/ExmlDeclare.ts',
3935
...options,
4036
};
@@ -67,19 +63,41 @@ export default class ThemePlugin {
6763
utils.addWatchIgnore(compiler, thmJSPath);
6864
this.thmJS = new CachedFile(thmJSPath);
6965

70-
if (this.options.thmJSON) {
71-
const thmJSONPath = path.join(compiler.context, this.options.thmJSON);
72-
utils.addWatchIgnore(compiler, thmJSONPath);
73-
// this.thmJSON = new FileCacheWriter(thmJSONPath);
74-
}
66+
// if (this.options.thmJSON) {
67+
// const thmJSONPath = path.join(compiler.context, this.options.thmJSON);
68+
// utils.addWatchIgnore(compiler, thmJSONPath);
69+
// // this.thmJSON = new FileCacheWriter(thmJSONPath);
70+
// }
7571

7672
if (this.options.exmlDeclare) {
7773
const exmlDeclarePath = path.join(compiler.context, this.options.exmlDeclare);
7874
utils.addWatchIgnore(compiler, exmlDeclarePath);
7975
// this.exmlDeclare = new FileCacheWriter(exmlDeclarePath);
8076
}
8177

82-
const content = theme.data.exmls.map(exml => `require("./${path.relative('resource', exml).split("\\").join("/")}")`).join("\n");
78+
79+
80+
const requires = theme.data.exmls.map(exml => `require("./${path.relative(path.dirname(theme.filePath), exml).split("\\").join("/")}");`);
81+
const content = `window.skins = window.skins || {};
82+
window.generateEUI = window.generateEUI || {
83+
paths: {},
84+
styles: undefined,
85+
skins: ${JSON.stringify(theme.data.skins, null, '\t')},
86+
};
87+
${requires.join('\n')}
88+
module.exports = window.generateEUI;
89+
`;
90+
91+
// if (utils.isHot(this.compiler)) {
92+
// content += '\nif (module.hot) { module.hot.accept(); }';
93+
// }
94+
95+
96+
97+
98+
99+
100+
83101
this.thmJS.update(utils.generateContent(content));
84102

85103
// 更新文件系统缓存状态

eui-compiler/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { generateAST } from "./util/parser";
66
import { initTypings } from './util/typings';
77
import { ThemeData } from './theme';
88

9+
export const parser = require('./util/parser') as typeof import("./util/parser")
10+
export const emitter = {
11+
JavaScriptEmitter
12+
}
913

1014
export type EuiAstTransformer = (ast: AST_Skin) => AST_Skin
1115

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
// This file is automatically generated by build
2-
require("./eui_skins/ButtonSkin.exml")
3-
require("./eui_skins/CheckBoxSkin.exml")
4-
require("./eui_skins/HScrollBarSkin.exml")
5-
require("./eui_skins/HSliderSkin.exml")
6-
require("./eui_skins/ItemRendererSkin.exml")
7-
require("./eui_skins/PanelSkin.exml")
8-
require("./eui_skins/ProgressBarSkin.exml")
9-
require("./eui_skins/RadioButtonSkin.exml")
10-
require("./eui_skins/ScrollerSkin.exml")
11-
require("./eui_skins/TextInputSkin.exml")
12-
require("./eui_skins/ToggleSwitchSkin.exml")
13-
require("./eui_skins/VScrollBarSkin.exml")
14-
require("./eui_skins/VSliderSkin.exml")
2+
window.skins = window.skins || {};
3+
window.generateEUI = window.generateEUI || {
4+
paths: {},
5+
styles: undefined,
6+
skins: {
7+
"eui.Button": "resource/eui_skins/ButtonSkin.exml",
8+
"eui.CheckBox": "resource/eui_skins/CheckBoxSkin.exml",
9+
"eui.HScrollBar": "resource/eui_skins/HScrollBarSkin.exml",
10+
"eui.HSlider": "resource/eui_skins/HSliderSkin.exml",
11+
"eui.Panel": "resource/eui_skins/PanelSkin.exml",
12+
"eui.TextInput": "resource/eui_skins/TextInputSkin.exml",
13+
"eui.ProgressBar": "resource/eui_skins/ProgressBarSkin.exml",
14+
"eui.RadioButton": "resource/eui_skins/RadioButtonSkin.exml",
15+
"eui.Scroller": "resource/eui_skins/ScrollerSkin.exml",
16+
"eui.ToggleSwitch": "resource/eui_skins/ToggleSwitchSkin.exml",
17+
"eui.VScrollBar": "resource/eui_skins/VScrollBarSkin.exml",
18+
"eui.VSlider": "resource/eui_skins/VSliderSkin.exml",
19+
"eui.ItemRenderer": "resource/eui_skins/ItemRendererSkin.exml"
20+
},
21+
};
22+
require("./eui_skins/ButtonSkin.exml");
23+
require("./eui_skins/CheckBoxSkin.exml");
24+
require("./eui_skins/HScrollBarSkin.exml");
25+
require("./eui_skins/HSliderSkin.exml");
26+
require("./eui_skins/ItemRendererSkin.exml");
27+
require("./eui_skins/PanelSkin.exml");
28+
require("./eui_skins/ProgressBarSkin.exml");
29+
require("./eui_skins/RadioButtonSkin.exml");
30+
require("./eui_skins/ScrollerSkin.exml");
31+
require("./eui_skins/TextInputSkin.exml");
32+
require("./eui_skins/ToggleSwitchSkin.exml");
33+
require("./eui_skins/VScrollBarSkin.exml");
34+
require("./eui_skins/VSliderSkin.exml");
35+
module.exports = window.generateEUI;
36+

test-egret-project/scripts/config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ const config: ResourceManagerConfig = {
4747
// sources: ["main.js"],
4848
// target: "main.min.js"
4949
// }]),
50-
new RenamePlugin({
51-
verbose: true, hash: 'crc32', matchers: [
52-
{ from: "**/*.js", to: "[path][name]_[hash].[ext]" }
53-
]
54-
}),
50+
// new RenamePlugin({
51+
// verbose: true, hash: 'crc32', matchers: [
52+
// { from: "**/*.js", to: "[path][name]_[hash].[ext]" }
53+
// ]
54+
// }),
5555
new ManifestPlugin({ output: "manifest.json" })
5656
]
5757
}

0 commit comments

Comments
 (0)