Skip to content

Commit ef53407

Browse files
committed
feat: plugins loaded from @cocreate/webpack
1 parent 85817ac commit ef53407

File tree

1 file changed

+6
-95
lines changed

1 file changed

+6
-95
lines changed

webpack.config.js

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const fs = require('fs');
1+
// const fs = require('fs');
22
const path = require('path');
3-
const upload = require('@cocreate/cli/src/commands/upload.js')
3+
const CoCreateConfig = require('./CoCreate.config')
4+
const { ModuleGenerator, fileUploader, SymlinkCreator } = require('@cocreate/webpack')
45

56

67
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
@@ -32,49 +33,9 @@ module.exports = async (env, argv) => {
3233
},
3334

3435
plugins: [
35-
{
36-
apply: (compiler) => {
37-
const CoCreateConfig = require('./CoCreate.config.js') || {};
38-
if (!CoCreateConfig.modules) {
39-
CoCreateConfig.modules = {}
40-
}
41-
42-
let modulesGenerated = false
43-
44-
compiler.hooks.beforeCompile.tapAsync('CoCreateLazyloader', (params, callback) => {
45-
if (modulesGenerated)
46-
callback();
47-
else {
48-
let outputPath = CoCreateConfig.modules.outputPath || './modules.js'; // Default path for generated module.js
49-
50-
// Generate module content based on CoCreateConfig
51-
let moduleContent = `import { dependency, lazyLoad } from '@cocreate/lazy-loader';\n\n`;
52-
Object.entries(CoCreateConfig.modules).forEach(([moduleName, moduleInfo]) => {
53-
if (moduleName === 'outputPath' || typeof moduleInfo !== 'object') return;
54-
if (moduleInfo.selector) {
55-
// Generate lazyLoad statements for modules with selectors
56-
moduleContent += `lazyLoad('${moduleName}', '${moduleInfo.selector}', () => import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`;
57-
} else {
58-
// Generate dependency statements for other modules
59-
moduleContent += `dependency('${moduleName}', import(/*webpackChunkName: "${moduleName}-chunk"*/ '${moduleInfo.import}'));\n`;
60-
}
61-
});
62-
63-
// Write the module content to the specified outputPath
64-
fs.writeFile(outputPath, moduleContent, (err) => {
65-
if (err) {
66-
console.error(`Error writing ${outputPath}:`, err);
67-
callback(err); // Handle errors in async hook
68-
} else {
69-
modulesGenerated = true
70-
console.log(`${outputPath} generated successfully.`);
71-
callback(); // Proceed with compilation
72-
}
73-
});
74-
}
75-
});
76-
},
77-
},
36+
new ModuleGenerator(CoCreateConfig.modules),
37+
new fileUploader(env),
38+
new SymlinkCreator(),
7839
new CleanWebpackPlugin(),
7940
new MiniCssExtractPlugin({
8041
filename: isProduction ? '[name].min.css' : '[name].css',
@@ -168,56 +129,6 @@ module.exports = async (env, argv) => {
168129

169130
};
170131

171-
// Hook into the 'done' event of the compiler to execute code when the build is complete.
172-
if (!isProduction) {
173-
config.plugins.push({
174-
apply: (compiler) => {
175-
symlink('./dist', '../dist', 'dir')
176-
symlink('./node_modules/@cocreate/pwa/src/service-worker.js', '../service-worker.js', 'file')
177-
symlink('./node_modules/@cocreate/pwa/src/manifest.webmanifest', '../manifest.webmanifest', 'file')
178-
symlink('./node_modules/@cocreate/pwa/src/offline.html', '../offline.html', 'file')
179-
},
180-
});
181-
}
182-
183-
if (isWatch) {
184-
if (env.beforeCompilation)
185-
upload(__dirname, ['../', '-w'])
186-
else if (env.afterCompilation) {
187-
config.plugins.push({
188-
apply: (compiler) => {
189-
// Hook into the "emit" event, which occurs after the compilation is complete
190-
compiler.hooks.emit.tapAsync('watchFiles', (compilation, callback) => {
191-
if (!isWatching) {
192-
isWatching = true
193-
upload(__dirname, ['../', '-w'])
194-
}
195-
// Don't forget to call the callback function to signal completion
196-
callback();
197-
});
198-
}
199-
})
200-
}
201-
}
202-
203132
return config;
204133

205134
}
206-
207-
function symlink(target, destination, option) {
208-
if (fs.existsSync(target)) {
209-
target = path.resolve(target)
210-
211-
if (!fs.existsSync(destination)) {
212-
destination = path.resolve(destination)
213-
214-
fs.symlink(target, destination, option, (err) => {
215-
if (err)
216-
console.log(err);
217-
else
218-
console.log("symlink added: ", target);
219-
})
220-
221-
}
222-
}
223-
}

0 commit comments

Comments
 (0)