UNPKG

7.4 kBJavaScriptView Raw
1import { ServiceType } from "@excitare/cli-types";
2import chalk from "chalk";
3import CopyWebpackPlugin from "copy-webpack-plugin";
4import path from "path";
5import TerserPlugin from "terser-webpack-plugin";
6import { logDebugOutput } from "./debug";
7import { createAliases } from "./fragments/aliases";
8import { createDevtoolTemplates } from "./fragments/devtoolTemplate";
9import { createNodeOptions } from "./fragments/nodeOptions";
10import { createResolveOptions } from "./fragments/resolve";
11import { createResolveLoader } from "./fragments/resolveLoader";
12import { createDeduplicationAliases } from "./moduleDeduplication";
13import { resolveModulePaths } from "./modulePaths";
14import { createLoaders } from "./options/loaders";
15import { createPlugins } from "./plugins";
16export function resolveModule(moduleStr, configuration, extraPaths, dev = true, printFound = false) {
17 const paths = [
18 configuration.rootDir,
19 configuration.context,
20 path.dirname(configuration.entry),
21 ...(extraPaths || [])
22 ].filter(el => el != null);
23 try {
24 if (dev) {
25 console.log(`Resolving ${chalk.gray(moduleStr)} in:\n${paths
26 .filter((el) => Boolean(el))
27 .map(el => " - " + path.resolve(el))
28 .join("\n")}`);
29 }
30 const resolved = path.resolve(require.resolve(moduleStr + "/package.json", {
31 paths: paths.filter((el) => Boolean(el))
32 }));
33 const { dir } = path.parse(resolved);
34 if (printFound) {
35 console.log(` - ${chalk.gray(moduleStr)}: ${chalk.greenBright("found")} in directory ${chalk.cyan(dir)}`);
36 }
37 return path.resolve(dir);
38 }
39 catch (err) {
40 if (printFound) {
41 console.log(` - ${chalk.gray(moduleStr)}: ${chalk.redBright("not found")}`);
42 }
43 return null;
44 }
45}
46export function createProductionConfig(type, configuration) {
47 console.log(" Module resolution");
48 const modulePaths = resolveModulePaths(type, configuration, false);
49 const { entry, entryNextDir, entryGraphqlPath, entryServiceDir, cliPath, graphqlPath, nextServerPath } = modulePaths;
50 const alias = {
51 ...createDeduplicationAliases(configuration, [configuration.entry, entryNextDir, entryServiceDir, entryGraphqlPath], configuration.bundleOptions.deduplication),
52 ...createAliases(type, modulePaths, "./production")
53 };
54 logDebugOutput(modulePaths, type, configuration, alias, process.env.WEBPACK_BUNDLE_ANALYZE === "true");
55 const externals = ["hiredis"];
56 const flags = {
57 minify: true,
58 tiny: true
59 };
60 return {
61 entry,
62 devtool: "source-map",
63 target: "node",
64 mode: "production",
65 resolve: createResolveOptions(alias),
66 resolveLoader: createResolveLoader(modulePaths, configuration, process.env.WEBPACK_BUNDLE_ANALYZE === "true"),
67 output: {
68 path: configuration.outputDir,
69 filename: configuration.outputName,
70 pathinfo: true,
71 futureEmitAssets: true,
72 ...createDevtoolTemplates(configuration)
73 },
74 externals: [
75 (context, request, callback) => {
76 if (configuration.externals.includes(request) || externals.includes(request)) {
77 return callback(null, "commonjs " + request);
78 }
79 if (/config$/.test(request)) {
80 if (/next\/config$/.test(request) ||
81 /next-server\/config$/.test(request) ||
82 (/next-server/.test(context) && /\/runtime-config$/.test(request))) {
83 return callback(null, "commonjs " + "next-server/config");
84 }
85 }
86 if (/^react([\\//]|$)/.test(request)) {
87 // console.log("Matched react: %s", request);
88 return callback(null, "commonjs " + request);
89 }
90 if (/^react-dom([\\//]|$)/.test(request)) {
91 // console.log("Matched react-dom: %s", request);
92 return callback(null, "commonjs " + request);
93 }
94 callback();
95 }
96 ],
97 node: createNodeOptions(type),
98 module: {
99 rules: createLoaders(cliPath, graphqlPath, configuration, true)
100 },
101 optimization: {
102 concatenateModules: flags.tiny,
103 namedModules: !flags.tiny,
104 namedChunks: !flags.tiny,
105 ...{
106 moduleIds: "hashed"
107 },
108 minimizer: flags.minify
109 ? [
110 new TerserPlugin({
111 cache: JSON.parse(process.env.terserDir),
112 parallel: false,
113 sourceMap: true,
114 terserOptions: {
115 ecma: 8,
116 compress: {
117 dead_code: true,
118 defaults: false,
119 unused: "keep_assign",
120 side_effects: true,
121 warnings: true
122 },
123 mangle: { ie8: false, keep_classnames: true, keep_fnames: true },
124 toplevel: false,
125 output: {
126 comments: false,
127 braces: true,
128 preamble: "// Code built by Excitare"
129 }
130 }
131 })
132 ]
133 : undefined,
134 minimize: flags.minify,
135 sideEffects: true,
136 usedExports: true,
137 providedExports: true,
138 removeEmptyChunks: true,
139 portableRecords: true
140 },
141 plugins: [
142 // new WebpackDeepScopeAnalysisPlugin(),
143 // new WebpackSourceMapSupport(),
144 ...createPlugins(configuration, false, modulePaths),
145 type === ServiceType.Frontend && nextServerPath != null
146 ? new CopyWebpackPlugin([
147 {
148 // Used in normal deployments
149 from: path.join(nextServerPath, "./dist/lib/runtime-config.js"),
150 to: "./node_modules/next-server/config.js"
151 },
152 {
153 // Used only in cloud deployments
154 from: path.join(nextServerPath, "./dist/lib/runtime-config.js"),
155 to: "./next-server-config.js"
156 }
157 ])
158 : null,
159 configuration.iconOptions != null
160 ? new CopyWebpackPlugin([
161 {
162 from: "**/*",
163 context: configuration.iconOptions.inputDir,
164 to: configuration.iconOptions.outDir,
165 toType: "dir"
166 }
167 ])
168 : null
169 ].filter(el => el != null)
170 };
171}
172//# sourceMappingURL=production.js.map
\No newline at end of file