Skip to content

Commit 7cf0898

Browse files
committed
feat: new plugin classes
- ModuleGenerator to gerate a module.js file with lazy loading - fileUploader to start in watch mode and upload changed files
1 parent 57c489d commit 7cf0898

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/index.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const fs = require('fs');
2+
const upload = require('@cocreate/cli/src/commands/upload.js')
3+
4+
class ModuleGenerator {
5+
constructor(modulesConfig) {
6+
this.modulesConfig = modulesConfig;
7+
}
8+
9+
apply(compiler) {
10+
let CoCreateConfig;
11+
if (typeof this.modulesConfig === 'object')
12+
CoCreateConfig = this.modulesConfig.config;
13+
else {
14+
try {
15+
CoCreateConfig = require(this.modulesConfig.configPath);
16+
} catch (error) {
17+
console.error('Error loading CoCreate.config.js:', error);
18+
throw error; // Stop compilation process if configuration is critical
19+
}
20+
}
21+
let modulesGenerated = false
22+
23+
compiler.hooks.beforeCompile.tapAsync('CoCreateLazyloader', (params, callback) => {
24+
if (modulesGenerated)
25+
callback();
26+
else {
27+
let outputPath = this.modulesConfig.outputPath || './modules.js'; // Default path for generated module.js
28+
29+
// Generate module content based on CoCreateConfig
30+
let moduleContent = `import { dependency, lazyLoad } from '@cocreate/lazy-loader';\n\n`;
31+
Object.entries(this.modulesConfig).forEach(([moduleName, moduleInfo]) => {
32+
if (moduleName === 'outputPath' || typeof moduleInfo !== 'object') return;
33+
if (moduleInfo.selector) {
34+
// Generate lazyLoad statements for modules with selectors
35+
moduleContent += `lazyLoad('${moduleName}', '${moduleInfo.selector}', () => import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`;
36+
} else {
37+
// Generate dependency statements for other modules
38+
moduleContent += `dependency('${moduleName}', import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`;
39+
}
40+
});
41+
42+
// Write the module content to the specified outputPath
43+
fs.writeFile(outputPath, moduleContent, (err) => {
44+
if (err) {
45+
console.error(`Error writing ${outputPath}:`, err);
46+
callback(err); // Handle errors in async hook
47+
} else {
48+
modulesGenerated = true
49+
console.log(`${outputPath} generated successfully.`);
50+
callback(); // Proceed with compilation
51+
}
52+
});
53+
}
54+
});
55+
}
56+
}
57+
class fileUploader {
58+
constructor(env) {
59+
this.env = env;
60+
this.isWatching = false;
61+
}
62+
63+
apply(compiler) {
64+
if (this.env.beforeCompilation) {
65+
// Directly perform upload here
66+
upload(process.cwd(), ['../', '-w']);
67+
}
68+
69+
if (this.env.afterCompilation) {
70+
compiler.hooks.emit.tapAsync('watchFiles', (compilation, callback) => {
71+
if (!this.isWatching) {
72+
this.isWatching = true;
73+
upload(process.cwd(), ['../', '-w']);
74+
}
75+
callback();
76+
});
77+
}
78+
}
79+
}
80+
81+
82+
module.exports = { ModuleGenerator, fileUploader };

0 commit comments

Comments
 (0)