-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-resourses-webpack-plugin.js
45 lines (39 loc) · 1.6 KB
/
load-resourses-webpack-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const fs = require('fs');
class LoadResourcesWebpackPlugin {
apply(compiler) {
let plugin = {
name: "LoadResourcesWebpackPlugin"
}
compiler.hooks.beforeRun.tap(plugin, (compiler) => {
let filename = './src/_resources.js';
let rcDirPath = 'assets/objects';
let resources = fs.readdirSync(rcDirPath);
let content = '';
resources.forEach((rc) => {
// cut file extension
let varName = rc.slice(0, rc.lastIndexOf('.'));
let extension = rc.slice(rc.lastIndexOf('.') + 1);
if (varName == '' || extension != 'png') {
return;
}
let rcImportItem = 'import ' + varName + ' from \'../' + rcDirPath + '/' + rc + '\';\n';
content += rcImportItem;
});
content += "\nexport function loadResources(scene) {\n";
// scene.load.image('air_conditioning', air_conditioning);
resources.forEach((rc) => {
// cut file extension
let varName = rc.slice(0, rc.lastIndexOf('.'));
let extension = rc.slice(rc.lastIndexOf('.') + 1);
if (varName == '' || extension != 'png') {
return;
}
let rcImportItem = '\tscene.load.image(\'' + varName + '\', ' + varName + ');\n';
content += rcImportItem;
});
content += "}\n";
fs.writeFileSync(filename, content);
});
}
}
module.exports = LoadResourcesWebpackPlugin;