-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathRendererTarget.ts
More file actions
221 lines (191 loc) · 6.82 KB
/
RendererTarget.ts
File metadata and controls
221 lines (191 loc) · 6.82 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { outputFile, readFile } from "fs-extra"
import { Lazy } from "lazy-val"
import * as path from "path"
import { getConfig } from "read-config-file"
import { DefinePlugin } from "webpack"
import { getDllAssets } from "../configurators/dll"
import { configureVueRenderer } from "../configurators/vue/vue"
import { WebpackConfigurator } from "../main"
import { statOrNull } from "../util"
import { BaseTarget, configureFileLoader } from "./BaseTarget"
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
export class BaseRendererTarget extends BaseTarget {
constructor() {
super()
}
configureRules(configurator: WebpackConfigurator): void {
super.configureRules(configurator)
configurator.extensions.push(".css")
const miniLoaders = [MiniCssExtractPlugin.loader, { loader: "css-loader", options: { modules: "global" } }]
const cssHotLoader = configurator.isProduction ? miniLoaders : ["css-hot-loader"].concat(miniLoaders)
if (!configurator.isProduction) {
// https://github.com/shepherdwind/css-hot-loader/issues/37
configurator.entryFiles.unshift("css-hot-loader/hotModuleReplacement")
}
configurator.rules.push(
{
test: /\.css$/,
use: cssHotLoader,
},
{
test: /\.less$/,
use: cssHotLoader.concat("less-loader"),
},
{
test: /\.s([ac])ss$/,
use: cssHotLoader.concat("sass-loader"),
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: {
loader: "url-loader",
options: configureFileLoader("imgs")
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: "url-loader",
options: configureFileLoader("media"),
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: {
loader: "url-loader",
options: configureFileLoader("fonts")
}
},
)
if (configurator.hasDevDependency("ejs-html-loader")) {
configurator.rules.push({
test: /\.ejs$/,
loader: "ejs-html-loader",
})
}
if (configurator.hasDependency("vue")) {
configureVueRenderer(configurator)
}
else {
configurator.rules.push({
test: /\.(html)$/,
use: {
loader: "html-loader",
}
})
}
}
async configurePlugins(configurator: WebpackConfigurator): Promise<void> {
configurator.debug("Add ExtractTextPlugin plugin")
configurator.plugins.push(new MiniCssExtractPlugin({filename: `${configurator.type === "renderer-dll" ? "vendor" : "styles"}.css`}))
await BaseTarget.prototype.configurePlugins.call(this, configurator)
}
}
export class RendererTarget extends BaseRendererTarget {
constructor() {
super()
}
async configurePlugins(configurator: WebpackConfigurator): Promise<void> {
// not configurable for now, as in the electron-vue
const customTemplateFile = path.join(configurator.projectDir, configurator.rendererTemplate)
const HtmlWebpackPlugin = require("html-webpack-plugin")
const nodeModulePath = configurator.isProduction ? null : path.resolve(require.resolve("electron"), "..", "..")
let template
if (await statOrNull(customTemplateFile)) {
template = await readFile(customTemplateFile, {encoding: "utf8"})
}
else {
template = getDefaultIndexTemplate()
}
configurator.plugins.push(new HtmlWebpackPlugin({
filename: "index.html",
template: await generateIndexFile(configurator, nodeModulePath, template),
minify: false,
nodeModules: nodeModulePath
}))
if (configurator.isProduction) {
configurator.plugins.push(new DefinePlugin({
__static: `process.resourcesPath + "/${configurator.staticSourceDirectory}"`
}))
}
else {
const contentBase = [path.join(configurator.projectDir, configurator.staticSourceDirectory), path.join(configurator.commonDistDirectory, "renderer-dll")];
(configurator.config as any).devServer = {
contentBase,
host: process.env.ELECTRON_WEBPACK_WDS_HOST || "localhost",
port: process.env.ELECTRON_WEBPACK_WDS_PORT || 9080,
hot: true,
overlay: true,
}
}
await BaseRendererTarget.prototype.configurePlugins.call(this, configurator)
}
}
async function computeTitle(configurator: WebpackConfigurator): Promise<string | null | undefined> {
const titleFromOptions = configurator.electronWebpackConfiguration.title
if (titleFromOptions == null || titleFromOptions === false) {
return null
}
if (titleFromOptions !== true) {
return titleFromOptions
}
let title: string | null | undefined = (configurator.metadata as any).productName
if (title == null) {
const electronBuilderConfig = await getConfig<any>({
packageKey: "build",
configFilename: "electron-builder",
projectDir: configurator.projectDir,
packageMetadata: new Lazy(() => Promise.resolve(configurator.metadata))
})
if (electronBuilderConfig != null) {
title = electronBuilderConfig.result.productName
}
}
if (title == null) {
title = configurator.metadata.name
}
return title
}
function getDefaultIndexTemplate() {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="app"></div>
</body>
</html>`
}
async function generateIndexFile(configurator: WebpackConfigurator, nodeModulePath: string | null, template: string) {
// do not use add-asset-html-webpack-plugin - no need to copy vendor files to output (in dev mode will be served directly, in production copied)
const assets = await getDllAssets(path.join(configurator.commonDistDirectory, "renderer-dll"), configurator)
const scripts: Array<string> = []
const css: Array<string> = []
for (const asset of assets) {
if (asset.endsWith(".js")) {
scripts.push(`<script type="text/javascript" src="${asset}"></script>`)
}
else {
css.push(`<link rel="stylesheet" href="${asset}">`)
}
}
const title = await computeTitle(configurator)
const filePath = path.join(configurator.commonDistDirectory, ".renderer-index-template.html")
let html = template
if (title) {
html = html.replace("</head>", `<title>${title}</title></head>`)
}
if (nodeModulePath) {
html = html.replace("</head>", `<script>require('module').globalPaths.push("${nodeModulePath.replace(/\\/g, "/")}")</script></head>`)
}
if (process.env.ELECTRON_WEBPACK_DISABLE_SOURCE_MAP_SUPPORT) {
html = html.replace("</head>", '<script>require("source-map-support/source-map-support.js").install()</script></head>');
}
if (scripts.length) {
html = html.replace("</head>", `${scripts.join("")}</head>`)
}
if (css.length) {
html = html.replace("</head>", `${css.join("")}</head>`)
}
await outputFile(filePath, html)
return `!!html-loader?minimize=false!${filePath}`
}