1- // Webpack uses this to work with directories
21const fs = require ( 'fs' ) ;
32const path = require ( 'path' ) ;
4- const util = require ( 'util' ) ;
3+ const upload = require ( '@cocreate/cli/src/commands/upload.js' )
54
65const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' ) ;
76const TerserPlugin = require ( "terser-webpack-plugin" ) ;
87const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
98const CssMinimizerPlugin = require ( 'css-minimizer-webpack-plugin' ) ;
109
11-
12- // const getLogger = require('webpack-log');
13- // const log = getLogger({ name: 'webpack-batman' });
14- // log.info(__filename);
15- // let version = require('./package.json').version;
16- // log.info(version);
1710module . exports = async ( env , argv ) => {
18- let isProduction = false
19- if ( argv . mode === 'production' )
20- isProduction = true
11+ const isProduction = argv . mode === 'production'
12+ const isWatch = argv . watch === true ;
2113
2214 const config = {
2315 // Path to your entry point. From this file Webpack will begin his work
@@ -40,8 +32,6 @@ module.exports = async (env, argv) => {
4032 plugins : [
4133 new CleanWebpackPlugin ( ) ,
4234 new MiniCssExtractPlugin ( {
43- // Options similar to the same options in webpackOptions.output
44-
4535 filename : isProduction ? '[name].min.css' : '[name].css' ,
4636 // chunkFilename: isProduction ? '[name].min.css' : '[name].css',
4737 chunkFilename : ( path ) => {
@@ -54,7 +44,7 @@ module.exports = async (env, argv) => {
5444 ] ,
5545
5646 devServer : {
57- hot : true ,
47+ hot : true
5848 } ,
5949
6050 mode : isProduction ? 'production' : 'development' ,
@@ -99,7 +89,6 @@ module.exports = async (env, argv) => {
9989 ]
10090 } ,
10191
102-
10392 optimization : {
10493 minimize : true ,
10594 minimizer : [
@@ -133,17 +122,41 @@ module.exports = async (env, argv) => {
133122
134123 } ;
135124
136- // Check if pwa service-worker is available
137- try {
138- const serviceWorkerPath = './node_modules/@cocreate/pwa/src/service-worker.js' ;
139- const access = util . promisify ( fs . access ) ;
140- await access ( serviceWorkerPath , fs . constants . F_OK ) ;
141- config . entry [ 'service-worker' ] = serviceWorkerPath
142- } catch ( error ) {
143- console . error ( 'PWA service-worker.js does not exist' ) ;
125+ // Hook into the 'done' event of the compiler to execute code when the build is complete.
126+ if ( ! isProduction ) {
127+ config . plugins . push ( {
128+ apply : ( compiler ) => {
129+ // console.log('Webpack build is complete in development mode!');
130+ symlink ( './dist' , '../dist' , 'dir' )
131+ symlink ( './node_modules/@cocreate/pwa/src/service-worker.js' , '../service-worker.js' , 'file' )
132+ symlink ( './node_modules/@cocreate/pwa/src/manifest.webmanifest' , '../manifest.webmanifest' , 'file' )
133+ symlink ( './node_modules/@cocreate/pwa/src/offline.html' , '../offline.html' , 'file' )
134+ } ,
135+ } ) ;
136+ }
137+
138+ if ( isWatch ) {
139+ upload ( __dirname , [ '../' , '-w' ] )
144140 }
145141
146142 return config ;
147143
148144}
149145
146+ function symlink ( target , destination , option ) {
147+ if ( fs . existsSync ( target ) ) {
148+ target = path . resolve ( target )
149+
150+ if ( ! fs . existsSync ( destination ) ) {
151+ destination = path . resolve ( destination )
152+
153+ fs . symlink ( target , destination , option , ( err ) => {
154+ if ( err )
155+ console . log ( err ) ;
156+ else
157+ console . log ( "symlink added: " , target ) ;
158+ } )
159+
160+ }
161+ }
162+ }
0 commit comments