@@ -4,7 +4,7 @@ const { version } = require('../package.json'); |
4 | 4 |
|
5 | 5 | const webpack = require('webpack'); |
6 | 6 | const path = require('path'); |
7 | | -const { existsSync } = require('fs'); |
| 7 | +const { existsSync, rmdirSync, readdir, unlink } = require('fs'); |
8 | 8 | const customConfig = path.resolve(process.cwd(), './webpack.config.js'); |
9 | 9 | const config = existsSync(customConfig) ? require(customConfig) : require('../webpack.config'); |
10 | 10 | const dotenv = require('dotenv') |
@@ -48,7 +48,7 @@ function getPort(port) { |
48 | 48 | return port || process.env['NULLSTACK_SERVER_PORT'] || process.env['PORT'] || 3000 |
49 | 49 | } |
50 | 50 |
|
51 | | -async function start({ input, port, env, mode = 'spa', hot }) { |
| 51 | +async function start({ input, port, env, mode = 'spa', cold, disk }) { |
52 | 52 | const environment = 'development' |
53 | 53 | console.log(` 🚀️ Starting your application in ${environment} mode...`); |
54 | 54 | loadEnv(env) |
@@ -60,18 +60,20 @@ async function start({ input, port, env, mode = 'spa', hot }) { |
60 | 60 | const ports = await getFreePorts() |
61 | 61 | process.env['NULSTACK_SERVER_PORT_YOU_SHOULD_NOT_CARE_ABOUT'] = ports[0] |
62 | 62 | process.env['NULSTACK_SERVER_SOCKET_PORT_YOU_SHOULD_NOT_CARE_ABOUT'] = ports[1] |
63 | | - process.env['NULLSTACK_ENVIRONMENT_HOT'] = (!!hot).toString() |
| 63 | + process.env['NULLSTACK_ENVIRONMENT_HOT'] = (!cold).toString() |
| 64 | + process.env['NULLSTACK_ENVIRONMENT_DISK'] = (!!disk).toString() |
64 | 65 | if (!process.env['NULLSTACK_PROJECT_DOMAIN']) process.env['NULLSTACK_PROJECT_DOMAIN'] = 'localhost' |
65 | 66 | if (!process.env['NULLSTACK_WORKER_PROTOCOL']) process.env['NULLSTACK_WORKER_PROTOCOL'] = 'http' |
66 | 67 | const target = `${process.env['NULLSTACK_WORKER_PROTOCOL']}://${process.env['NULLSTACK_PROJECT_DOMAIN']}:${process.env['NULSTACK_SERVER_PORT_YOU_SHOULD_NOT_CARE_ABOUT']}` |
| 68 | + const writeToDisk = disk ? true : (path) => path.includes('server') |
67 | 69 | const devServerOptions = { |
68 | 70 | hot: 'only', |
69 | 71 | open: false, |
70 | 72 | host: process.env['NULLSTACK_PROJECT_DOMAIN'], |
71 | 73 | devMiddleware: { |
72 | 74 | index: false, |
73 | 75 | stats: 'none', |
74 | | - writeToDisk: true, |
| 76 | + writeToDisk, |
75 | 77 | }, |
76 | 78 | client: { |
77 | 79 | overlay: { errors: true, warnings: false }, |
@@ -120,8 +122,18 @@ async function start({ input, port, env, mode = 'spa', hot }) { |
120 | 122 | webSocketServer: require.resolve('./socket'), |
121 | 123 | port: process.env['NULLSTACK_SERVER_PORT'] |
122 | 124 | }; |
123 | | - const clientCompiler = getCompiler({ environment, input }); |
124 | | - const server = new WebpackDevServer(devServerOptions, clientCompiler); |
| 125 | + const compiler = getCompiler({ environment, input, disk }); |
| 126 | + const outputPath = compiler.compilers[0].outputPath; |
| 127 | + readdir(outputPath, (err, files) => { |
| 128 | + if (err) throw err; |
| 129 | + for (const file of files) { |
| 130 | + if (file === '.cache') continue; |
| 131 | + unlink(path.join(outputPath, file), err => { |
| 132 | + if (err) throw err; |
| 133 | + }); |
| 134 | + } |
| 135 | + }); |
| 136 | + const server = new WebpackDevServer(devServerOptions, compiler); |
125 | 137 | const portChecker = require('express')().listen(process.env['NULLSTACK_SERVER_PORT'], () => { |
126 | 138 | portChecker.close() |
127 | 139 | server.startCallback(() => { |
@@ -155,7 +167,8 @@ program |
155 | 167 | .option('-p, --port <port>', 'Port number to run the server') |
156 | 168 | .option('-i, --input <input>', 'Path to project that will be started') |
157 | 169 | .option('-e, --env <name>', 'Name of the environment file that should be loaded') |
158 | | - .option('--hot', 'Enable hot module replacement') |
| 170 | + .option('-d, --disk', 'Write files to disk') |
| 171 | + .option('-c, --cold', 'Disable hot module replacement') |
159 | 172 | .helpOption('-h, --help', 'Learn more about this command') |
160 | 173 | .action(start) |
161 | 174 |
|
|