|
1 | 1 | const fs = require('fs');
|
2 | 2 | const path = require('path');
|
3 | 3 | const uglifyjs = require('uglify-js');
|
| 4 | +const webpack = require('webpack'); |
4 | 5 |
|
5 |
| -module.exports = ({ dir, options }) => function () { |
6 |
| - const compiler = this; |
7 |
| - compiler.hooks.emit.tapAsync('EmitPlugin', async (compilation, callback) => { |
8 |
| - let prefix_text = ''; |
9 |
| - prefix_text += `window.gui_env="${options.env}";\n`; |
| 6 | +module.exports = async ({ dir, options }) => { |
| 7 | + let prefix_text = ''; |
| 8 | + prefix_text += `window.gui_env="${options.env}";\n`; |
10 | 9 |
|
11 |
| - // ----------------------------------------------- |
12 |
| - // Combine all images into a single js file |
13 |
| - // ----------------------------------------------- |
14 |
| - { |
15 |
| - let icons = 'window.icons = [];\n'; |
16 |
| - fs.readdirSync(dir).forEach(file => { |
17 |
| - // skip dotfiles |
18 |
| - if (file.startsWith('.')) |
19 |
| - return; |
20 |
| - // load image |
21 |
| - let buff = new Buffer.from(fs.readFileSync(dir + '/' + file)); |
22 |
| - // convert to base64 |
23 |
| - let base64data = buff.toString('base64'); |
24 |
| - // add to `window.icons` |
25 |
| - if (file.endsWith('.png')) |
26 |
| - icons += `window.icons['${file}'] = "data:image/png;base64,${base64data}";\n`; |
27 |
| - else if (file.endsWith('.svg')) |
28 |
| - icons += `window.icons['${file}'] = "data:image/svg+xml;base64,${base64data}";\n`; |
29 |
| - }); |
30 |
| - prefix_text += icons + '\n'; |
31 |
| - } |
| 10 | + // ----------------------------------------------- |
| 11 | + // Combine all images into a single js file |
| 12 | + // ----------------------------------------------- |
| 13 | + { |
| 14 | + let icons = 'window.icons = [];\n'; |
| 15 | + fs.readdirSync(dir).forEach(file => { |
| 16 | + // skip dotfiles |
| 17 | + if (file.startsWith('.')) |
| 18 | + return; |
| 19 | + // load image |
| 20 | + let buff = new Buffer.from(fs.readFileSync(dir + '/' + file)); |
| 21 | + // convert to base64 |
| 22 | + let base64data = buff.toString('base64'); |
| 23 | + // add to `window.icons` |
| 24 | + if (file.endsWith('.png')) |
| 25 | + icons += `window.icons['${file}'] = "data:image/png;base64,${base64data}";\n`; |
| 26 | + else if (file.endsWith('.svg')) |
| 27 | + icons += `window.icons['${file}'] = "data:image/svg+xml;base64,${base64data}";\n`; |
| 28 | + }); |
| 29 | + prefix_text += icons + '\n'; |
| 30 | + } |
32 | 31 |
|
33 |
| - // ----------------------------------------------- |
34 |
| - // Concat/merge the JS libraries and save them to ./dist/libs.js |
35 |
| - // ----------------------------------------------- |
36 |
| - { |
37 |
| - const lib_paths = require('./libPaths.cjs'); |
38 |
| - let js = ''; |
39 |
| - for(let i = 0; i < lib_paths.length; i++){ |
40 |
| - const file = path.join(__dirname, '../src/lib/', lib_paths[i]); |
41 |
| - // js |
42 |
| - if(file.endsWith('.js') && !file.endsWith('.min.js')){ |
43 |
| - let minified_code = await uglifyjs.minify(fs.readFileSync(file).toString(), {mangle: false}); |
44 |
| - if(minified_code && minified_code.code){ |
45 |
| - js += minified_code.code; |
46 |
| - if(options?.verbose) |
47 |
| - console.log('minified: ', file); |
48 |
| - } |
49 |
| - }else{ |
50 |
| - js += fs.readFileSync(file); |
| 32 | + // ----------------------------------------------- |
| 33 | + // Concat/merge the JS libraries and save them to ./dist/libs.js |
| 34 | + // ----------------------------------------------- |
| 35 | + { |
| 36 | + const lib_paths = require('./libPaths.cjs'); |
| 37 | + let js = ''; |
| 38 | + for(let i = 0; i < lib_paths.length; i++){ |
| 39 | + const file = path.join(__dirname, '../src/lib/', lib_paths[i]); |
| 40 | + // js |
| 41 | + if(file.endsWith('.js') && !file.endsWith('.min.js')){ |
| 42 | + let minified_code = await uglifyjs.minify(fs.readFileSync(file).toString(), {mangle: false}); |
| 43 | + if(minified_code && minified_code.code){ |
| 44 | + js += minified_code.code; |
51 | 45 | if(options?.verbose)
|
52 |
| - console.log('skipped minification: ', file); |
| 46 | + console.log('minified: ', file); |
53 | 47 | }
|
54 |
| - |
55 |
| - js += '\n\n\n'; |
| 48 | + }else{ |
| 49 | + js += fs.readFileSync(file); |
| 50 | + if(options?.verbose) |
| 51 | + console.log('skipped minification: ', file); |
56 | 52 | }
|
57 |
| - prefix_text += js; |
| 53 | + |
| 54 | + js += '\n\n\n'; |
58 | 55 | }
|
| 56 | + prefix_text += js; |
| 57 | + } |
| 58 | + |
| 59 | + return new webpack.BannerPlugin({ |
| 60 | + banner: prefix_text, |
| 61 | + raw: true, |
| 62 | + }); |
59 | 63 |
|
60 |
| - // ----------------------------------------------- |
61 |
| - // Webpack understands this code better than I do |
62 |
| - // ----------------------------------------------- |
63 |
| - Object.keys(compilation.assets).forEach((assetName) => { |
64 |
| - if (assetName.endsWith('.js')) { |
65 |
| - const asset = compilation.assets[assetName]; |
66 |
| - const originalSource = asset.source(); |
67 |
| - const newSource = `${prefix_text}\n${originalSource}`; |
68 |
| - compilation.assets[assetName] = { |
69 |
| - source: () => newSource, |
70 |
| - size: () => newSource.length, |
71 |
| - }; |
72 |
| - } |
73 |
| - }); |
| 64 | + // ----------------------------------------------- |
| 65 | + // Webpack understands this code better than I do |
| 66 | + // ----------------------------------------------- |
| 67 | + // Object.keys(compilation.assets).forEach((assetName) => { |
| 68 | + // if (assetName.endsWith('.js')) { |
| 69 | + // const asset = compilation.assets[assetName]; |
| 70 | + // const originalSource = asset.source(); |
| 71 | + // const newSource = `${prefix_text}\n${originalSource}`; |
| 72 | + // compilation.assets[assetName] = { |
| 73 | + // source: () => newSource, |
| 74 | + // size: () => newSource.length, |
| 75 | + // }; |
| 76 | + // } |
| 77 | + // }); |
74 | 78 |
|
75 |
| - console.log('END'); |
76 |
| - callback(); |
77 |
| - }); |
| 79 | + console.log('END'); |
78 | 80 | };
|
0 commit comments