|
1 | | -const fs = require('fs'); |
2 | | -const path = require('path'); |
| 1 | +// Import 'fs' for file system operations and 'path' for path operations |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
3 | 4 |
|
4 | | -const upload = require('@cocreate/cli/src/commands/upload.js') |
| 5 | +// Import 'upload' function from CoCreate CLI to handle uploads |
| 6 | +const upload = require("@cocreate/cli/src/commands/upload.js"); |
5 | 7 |
|
| 8 | +/** |
| 9 | + * Class representing a Module Generator. |
| 10 | + * Generates module files based on a configuration and applies them to the Webpack compiler. |
| 11 | + */ |
6 | 12 | class ModuleGenerator { |
7 | | - constructor(modulesConfig) { |
8 | | - this.modulesConfig = modulesConfig; |
9 | | - } |
10 | | - |
11 | | - apply(compiler) { |
12 | | - let CoCreateConfig; |
13 | | - if (typeof this.modulesConfig === 'object') |
14 | | - CoCreateConfig = this.modulesConfig.config; |
15 | | - else { |
16 | | - try { |
17 | | - CoCreateConfig = require(this.modulesConfig.configPath); |
18 | | - } catch (error) { |
19 | | - console.error('Error loading CoCreate.config.js:', error); |
20 | | - throw error; // Stop compilation process if configuration is critical |
21 | | - } |
22 | | - } |
23 | | - let modulesGenerated = false |
24 | | - |
25 | | - compiler.hooks.beforeCompile.tapAsync('CoCreateLazyloader', (params, callback) => { |
26 | | - if (modulesGenerated) |
27 | | - callback(); |
28 | | - else { |
29 | | - let outputPath = this.modulesConfig.outputPath || './modules.js'; // Default path for generated module.js |
30 | | - |
31 | | - // Generate module content based on CoCreateConfig |
32 | | - let moduleContent = `import { dependency, lazyLoad } from '@cocreate/lazy-loader';\n\n`; |
33 | | - Object.entries(this.modulesConfig).forEach(([moduleName, moduleInfo]) => { |
34 | | - if (moduleName === 'outputPath' || typeof moduleInfo !== 'object') return; |
35 | | - if (moduleInfo.selector) { |
36 | | - // Generate lazyLoad statements for modules with selectors |
37 | | - moduleInfo.selector = moduleInfo.selector.replaceAll("'", '"') |
38 | | - moduleContent += `lazyLoad('${moduleName}', '${moduleInfo.selector}', () => import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`; |
39 | | - } else { |
40 | | - // Generate dependency statements for other modules |
41 | | - moduleContent += `dependency('${moduleName}', import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`; |
42 | | - } |
43 | | - }); |
44 | | - |
45 | | - // Write the module content to the specified outputPath |
46 | | - fs.writeFile(outputPath, moduleContent, (err) => { |
47 | | - if (err) { |
48 | | - console.error(`Error writing ${outputPath}:`, err); |
49 | | - callback(err); // Handle errors in async hook |
50 | | - } else { |
51 | | - modulesGenerated = true |
52 | | - console.log(`${outputPath} generated successfully.`); |
53 | | - callback(); // Proceed with compilation |
54 | | - } |
55 | | - }); |
56 | | - } |
57 | | - }); |
58 | | - } |
| 13 | + /** |
| 14 | + * Creates an instance of ModuleGenerator. |
| 15 | + * @param {Object} modulesConfig - The configuration for the modules. |
| 16 | + */ |
| 17 | + constructor(modulesConfig) { |
| 18 | + this.modulesConfig = modulesConfig; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Apply the module generation process to the Webpack compiler. |
| 23 | + * @param {Object} compiler - The Webpack compiler instance. |
| 24 | + */ |
| 25 | + apply(compiler) { |
| 26 | + let CoCreateConfig; |
| 27 | + |
| 28 | + if (typeof this.modulesConfig === "object") { |
| 29 | + CoCreateConfig = this.modulesConfig.config; |
| 30 | + } else { |
| 31 | + try { |
| 32 | + CoCreateConfig = require(this.modulesConfig.configPath); |
| 33 | + } catch (error) { |
| 34 | + console.error("Error loading CoCreate.config.js:", error); |
| 35 | + throw error; // Stop the compilation process if configuration is critical |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + let modulesGenerated = false; |
| 40 | + |
| 41 | + compiler.hooks.beforeCompile.tapAsync( |
| 42 | + "CoCreateLazyloader", |
| 43 | + (params, callback) => { |
| 44 | + if (modulesGenerated) callback(); |
| 45 | + else { |
| 46 | + let outputPath = |
| 47 | + this.modulesConfig.outputPath || "./modules.js"; // Default path for generated module.js |
| 48 | + |
| 49 | + // Generate module content based on CoCreateConfig |
| 50 | + let moduleContent = `import { dependency, lazyLoad } from '@cocreate/lazy-loader';\n\n`; |
| 51 | + Object.entries(this.modulesConfig).forEach( |
| 52 | + ([moduleName, moduleInfo]) => { |
| 53 | + if ( |
| 54 | + moduleName === "outputPath" || |
| 55 | + typeof moduleInfo !== "object" |
| 56 | + ) |
| 57 | + return; |
| 58 | + |
| 59 | + if (moduleInfo.selector) { |
| 60 | + // Generate lazyLoad statements for modules with selectors |
| 61 | + moduleInfo.selector = |
| 62 | + moduleInfo.selector.replaceAll("'", '"'); |
| 63 | + moduleContent += `lazyLoad('${moduleName}', '${moduleInfo.selector}', () => import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`; |
| 64 | + } else { |
| 65 | + // Generate dependency statements for other modules |
| 66 | + moduleContent += `dependency('${moduleName}', import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`; |
| 67 | + } |
| 68 | + } |
| 69 | + ); |
| 70 | + |
| 71 | + // Write the module content to the specified outputPath |
| 72 | + fs.writeFile(outputPath, moduleContent, (err) => { |
| 73 | + if (err) { |
| 74 | + console.error(`Error writing ${outputPath}:`, err); |
| 75 | + callback(err); // Handle errors in async hook |
| 76 | + } else { |
| 77 | + modulesGenerated = true; |
| 78 | + console.log( |
| 79 | + `${outputPath} generated successfully.` |
| 80 | + ); |
| 81 | + callback(); // Proceed with compilation |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + ); |
| 87 | + } |
59 | 88 | } |
60 | 89 |
|
| 90 | +/** |
| 91 | + * Class to handle file uploads, particularly useful during watch mode. |
| 92 | + */ |
61 | 93 | class FileUploader { |
62 | | - constructor(env, argv) { |
63 | | - this.env = env; |
64 | | - this.isWatching = false; |
65 | | - this.isWatch = argv.watch === true; |
66 | | - } |
67 | | - |
68 | | - apply(compiler) { |
69 | | - if (this.isWatch) { |
70 | | - |
71 | | - if (this.env.beforeCompilation) { |
72 | | - // Directly perform upload here |
73 | | - upload(process.cwd(), ['../', '-w']); |
74 | | - } |
75 | | - |
76 | | - if (this.env.afterCompilation) { |
77 | | - compiler.hooks.emit.tapAsync('watchFiles', (compilation, callback) => { |
78 | | - if (!this.isWatching) { |
79 | | - this.isWatching = true; |
80 | | - upload(process.cwd(), ['../', '-w']); |
81 | | - } |
82 | | - callback(); |
83 | | - }); |
84 | | - } |
85 | | - } |
86 | | - } |
| 94 | + /** |
| 95 | + * Creates an instance of FileUploader. |
| 96 | + * @param {Object} env - The environment variables. |
| 97 | + * @param {Object} argv - The arguments provided. |
| 98 | + */ |
| 99 | + constructor(env, argv) { |
| 100 | + this.env = env; |
| 101 | + this.isWatching = false; |
| 102 | + this.isWatch = argv.watch === true; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Apply the file uploader process to the Webpack compiler. |
| 107 | + * @param {Object} compiler - The Webpack compiler instance. |
| 108 | + */ |
| 109 | + apply(compiler) { |
| 110 | + if (this.isWatch) { |
| 111 | + if (this.env.beforeCompilation) { |
| 112 | + // Directly perform upload here |
| 113 | + upload(process.cwd(), ["../", "-w"]); |
| 114 | + } |
| 115 | + |
| 116 | + if (this.env.afterCompilation) { |
| 117 | + compiler.hooks.emit.tapAsync( |
| 118 | + "watchFiles", |
| 119 | + (compilation, callback) => { |
| 120 | + if (!this.isWatching) { |
| 121 | + this.isWatching = true; |
| 122 | + upload(process.cwd(), ["../", "-w"]); |
| 123 | + } |
| 124 | + callback(); |
| 125 | + } |
| 126 | + ); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
87 | 130 | } |
88 | 131 |
|
89 | | - |
| 132 | +/** |
| 133 | + * Class responsible for creating symbolic links after the build process. |
| 134 | + */ |
90 | 135 | class SymlinkCreator { |
91 | | - constructor(options) { |
92 | | - // Store options if necessary, or just hard-code paths |
93 | | - } |
94 | | - |
95 | | - apply(compiler) { |
96 | | - // Use compiler.hooks to tap into the Webpack build process |
97 | | - compiler.hooks.afterEmit.tap('SymlinkPlugin', (compilation) => { |
98 | | - // Perform symlink operations here |
99 | | - symlink('./dist', '../dist', 'dir'); |
100 | | - symlink('./node_modules/@cocreate/pwa/src/service-worker.js', '../service-worker.js', 'file'); |
101 | | - symlink('./node_modules/@cocreate/pwa/src/manifest.webmanifest', '../manifest.webmanifest', 'file'); |
102 | | - symlink('./node_modules/@cocreate/pwa/src/offline.html', '../offline.html', 'file'); |
103 | | - }); |
104 | | - |
105 | | - function symlink(target, destination, option) { |
106 | | - if (fs.existsSync(target)) { |
107 | | - target = path.resolve(target) |
108 | | - |
109 | | - if (!fs.existsSync(destination)) { |
110 | | - destination = path.resolve(destination) |
111 | | - |
112 | | - fs.symlink(target, destination, option, (err) => { |
113 | | - if (err) |
114 | | - console.log(err); |
115 | | - else |
116 | | - console.log("symlink added: ", target); |
117 | | - }) |
118 | | - |
119 | | - } |
120 | | - } |
121 | | - } |
122 | | - } |
| 136 | + /** |
| 137 | + * Creates an instance of SymlinkCreator. |
| 138 | + * @param {Object} [options] - Optional configurations for symlink creation. |
| 139 | + */ |
| 140 | + constructor(options) { |
| 141 | + // Store options if necessary, or just hard-code paths |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Apply the symlink creation process to the Webpack compiler. |
| 146 | + * @param {Object} compiler - The Webpack compiler instance. |
| 147 | + */ |
| 148 | + apply(compiler) { |
| 149 | + // Use compiler.hooks to tap into the Webpack build process |
| 150 | + compiler.hooks.afterEmit.tap("SymlinkPlugin", (compilation) => { |
| 151 | + // Perform symlink operations here |
| 152 | + symlink("./dist", "../dist", "dir"); |
| 153 | + symlink( |
| 154 | + "./node_modules/@cocreate/pwa/src/service-worker.js", |
| 155 | + "../service-worker.js", |
| 156 | + "file" |
| 157 | + ); |
| 158 | + symlink( |
| 159 | + "./node_modules/@cocreate/pwa/src/manifest.webmanifest", |
| 160 | + "../manifest.webmanifest", |
| 161 | + "file" |
| 162 | + ); |
| 163 | + symlink( |
| 164 | + "./node_modules/@cocreate/pwa/src/offline.html", |
| 165 | + "../offline.html", |
| 166 | + "file" |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + /** |
| 171 | + * Function to create a symlink if the target exists and the destination does not. |
| 172 | + * @param {string} target - Path to the target. |
| 173 | + * @param {string} destination - Path to the destination. |
| 174 | + * @param {string} option - Type of symlink ('file' or 'dir'). |
| 175 | + */ |
| 176 | + function symlink(target, destination, option) { |
| 177 | + if (fs.existsSync(target)) { |
| 178 | + target = path.resolve(target); |
| 179 | + |
| 180 | + if (!fs.existsSync(destination)) { |
| 181 | + destination = path.resolve(destination); |
| 182 | + |
| 183 | + fs.symlink(target, destination, option, (err) => { |
| 184 | + if (err) console.log(err); |
| 185 | + else console.log("symlink added: ", target); |
| 186 | + }); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
123 | 191 | } |
124 | 192 |
|
| 193 | +// Export the defined classes so they can be used in other modules or configurations |
125 | 194 | module.exports = { ModuleGenerator, FileUploader, SymlinkCreator }; |
0 commit comments