File size: 8,655 Bytes
2434dca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";
const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server";
class ServeCommand {
    async apply(cli) {
        const loadDevServerOptions = () => {
            // eslint-disable-next-line @typescript-eslint/no-var-requires
            const devServer = require(WEBPACK_DEV_SERVER_PACKAGE);
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const options = cli.webpack.cli.getArguments(devServer.schema);
            // New options format
            // { flag1: {}, flag2: {} }
            return Object.keys(options).map((key) => {
                options[key].name = key;
                return options[key];
            });
        };
        await cli.makeCommand({
            name: "serve [entries...]",
            alias: ["server", "s"],
            description: "Run the webpack dev server and watch for source file changes while serving.",
            usage: "[entries...] [options]",
            pkg: "@webpack-cli/serve",
            dependencies: [WEBPACK_PACKAGE, WEBPACK_DEV_SERVER_PACKAGE],
        }, async () => {
            let devServerFlags = [];
            cli.webpack = await cli.loadWebpack();
            try {
                devServerFlags = loadDevServerOptions();
            }
            catch (error) {
                cli.logger.error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${error}`);
                process.exit(2);
            }
            const builtInOptions = cli.getBuiltInOptions();
            return [...builtInOptions, ...devServerFlags];
        }, 
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        async (entries, options) => {
            const builtInOptions = cli.getBuiltInOptions();
            let devServerFlags = [];
            try {
                devServerFlags = loadDevServerOptions();
            }
            catch (error) {
                // Nothing, to prevent future updates
            }
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const webpackCLIOptions = {};
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const devServerCLIOptions = {};
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const processors = [];
            for (const optionName in options) {
                const kebabedOption = cli.toKebabCase(optionName);
                const isBuiltInOption = builtInOptions.find(
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                (builtInOption) => builtInOption.name === kebabedOption);
                if (isBuiltInOption) {
                    webpackCLIOptions[optionName] = options[optionName];
                }
                else {
                    const needToProcess = devServerFlags.find(
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    (devServerOption) => devServerOption.name === kebabedOption && devServerOption.processor);
                    if (needToProcess) {
                        processors.push(needToProcess.processor);
                    }
                    devServerCLIOptions[optionName] = options[optionName];
                }
            }
            for (const processor of processors) {
                processor(devServerCLIOptions);
            }
            if (entries.length > 0) {
                webpackCLIOptions.entry = [...entries, ...(webpackCLIOptions.entry || [])];
            }
            webpackCLIOptions.argv = Object.assign(Object.assign({}, options), { env: Object.assign({ WEBPACK_SERVE: true }, options.env) });
            webpackCLIOptions.isWatchingLikeCommand = true;
            const compiler = await cli.createCompiler(webpackCLIOptions);
            if (!compiler) {
                return;
            }
            const servers = [];
            if (cli.needWatchStdin(compiler)) {
                process.stdin.on("end", () => {
                    Promise.all(servers.map((server) => {
                        return server.stop();
                    })).then(() => {
                        process.exit(0);
                    });
                });
                process.stdin.resume();
            }
            // eslint-disable-next-line @typescript-eslint/no-var-requires
            const DevServer = require(WEBPACK_DEV_SERVER_PACKAGE);
            try {
                // eslint-disable-next-line @typescript-eslint/no-var-requires
                require(`${WEBPACK_DEV_SERVER_PACKAGE}/package.json`).version;
            }
            catch (err) {
                cli.logger.error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${err}`);
                process.exit(2);
            }
            const compilers = cli.isMultipleCompiler(compiler) ? compiler.compilers : [compiler];
            const possibleCompilers = compilers.filter((compiler) => compiler.options.devServer);
            const compilersForDevServer = possibleCompilers.length > 0 ? possibleCompilers : [compilers[0]];
            const usedPorts = [];
            for (const compilerForDevServer of compilersForDevServer) {
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                const args = devServerFlags.reduce((accumulator, flag) => {
                    accumulator[flag.name] = flag;
                    return accumulator;
                }, {});
                const values = Object.keys(devServerCLIOptions).reduce(
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                (accumulator, name) => {
                    const kebabName = cli.toKebabCase(name);
                    if (args[kebabName]) {
                        accumulator[kebabName] = options[name];
                    }
                    return accumulator;
                }, {});
                const result = Object.assign({}, (compilerForDevServer.options.devServer || {}));
                const problems = (cli.webpack.cli && typeof cli.webpack.cli.processArguments === "function"
                    ? cli.webpack.cli
                    : DevServer.cli).processArguments(args, result, values);
                if (problems) {
                    const groupBy = (xs, key) => {
                        return xs.reduce((rv, x) => {
                            (rv[x[key]] = rv[x[key]] || []).push(x);
                            return rv;
                        }, {});
                    };
                    const problemsByPath = groupBy(problems, "path");
                    for (const path in problemsByPath) {
                        const problems = problemsByPath[path];
                        for (const problem of problems) {
                            cli.logger.error(`${cli.capitalizeFirstLetter(problem.type.replace(/-/g, " "))}${problem.value ? ` '${problem.value}'` : ""} for the '--${problem.argument}' option${problem.index ? ` by index '${problem.index}'` : ""}`);
                            if (problem.expected) {
                                cli.logger.error(`Expected: '${problem.expected}'`);
                            }
                        }
                    }
                    process.exit(2);
                }
                const devServerOptions = result;
                if (devServerOptions.port) {
                    const portNumber = Number(devServerOptions.port);
                    if (usedPorts.find((port) => portNumber === port)) {
                        throw new Error("Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --config-name flag to specify your desired config.");
                    }
                    usedPorts.push(portNumber);
                }
                try {
                    const server = new DevServer(devServerOptions, compiler);
                    await server.start();
                    servers.push(server);
                }
                catch (error) {
                    if (cli.isValidationError(error)) {
                        cli.logger.error(error.message);
                    }
                    else {
                        cli.logger.error(error);
                    }
                    process.exit(2);
                }
            }
        });
    }
}
exports.default = ServeCommand;