Skip to content

Commit

Permalink
feat: Added electron builder config function (#221)
Browse files Browse the repository at this point in the history
* Added electron builder config function

* Moved config to seperate file

* Moved electron builder config into seperate ts file

* Removed unneeded source map support
  • Loading branch information
VictorLeach96 authored and develar committed Nov 6, 2018
1 parent 377b0eb commit c4cf411
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 35 deletions.
17 changes: 0 additions & 17 deletions packages/electron-webpack/electron-builder.yml

This file was deleted.

1 change: 0 additions & 1 deletion packages/electron-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"out",
"*.js",
"scheme.json",
"electron-builder.yml",
"tsconfig-base.json",
"vendor"
],
Expand Down
24 changes: 24 additions & 0 deletions packages/electron-webpack/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readJson } from "fs-extra-p"
import { Lazy } from "lazy-val"
import * as path from "path"
import { orNullIfFileNotExist } from "./util"
import { getConfig } from "read-config-file"

export { ElectronWebpackConfiguration } from "./core"

export async function getPackageMetadata() {
const projectDir = process.cwd()
return await orNullIfFileNotExist(readJson(path.join(projectDir, "package.json")))
}

export async function getElectronWebpackConfig() {
const projectDir = process.cwd()
const packageMetadata = await getPackageMetadata()
const electronWebpackConfig = ((await getConfig({
packageKey: "electronWebpack",
configFilename: "electron-webpack",
projectDir,
packageMetadata: new Lazy(() => Promise.resolve(packageMetadata))
})) || {} as any).result || {}
return electronWebpackConfig
}
1 change: 1 addition & 0 deletions packages/electron-webpack/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ElectronWebpackConfiguration {
main?: ElectronWebpackConfigurationMain | null

commonSourceDirectory?: string | null
commonDistDirectory?: string | null

title?: string | boolean | null

Expand Down
4 changes: 3 additions & 1 deletion packages/electron-webpack/src/dev/dev-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { configure } from "../main"
import { getFreePort, orNullIfFileNotExist } from "../util"
import { DelayedFunction, getCommonEnv, logError, logProcess, logProcessErrorOutput } from "./devUtil"
import { startRenderer } from "./WebpackDevServerManager"
import { getElectronWebpackConfig } from "../config"

const projectDir = process.cwd()

Expand All @@ -19,7 +20,8 @@ const debug = require("debug")("electron-webpack")

// do not remove main.js to allow IDE to keep breakpoints
async function emptyMainOutput() {
const outDir = path.join(projectDir, "dist", "main")
const electronWebpackConfig = await getElectronWebpackConfig()
const outDir = path.join(projectDir, electronWebpackConfig.commonDistDirectory, "main")
const files = await orNullIfFileNotExist(readdir(outDir))
if (files == null) {
return
Expand Down
31 changes: 31 additions & 0 deletions packages/electron-webpack/src/electron-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getElectronWebpackConfig } from "./config"

export default async function() {
const electronWebpackConfig = await getElectronWebpackConfig()
return {
extraMetadata: {
main: "main.js"
},
files: [
{
from: ".",
filter: ["package.json"]
},
{
from: electronWebpackConfig.commonDistDirectory + "/main"
},
{
from: electronWebpackConfig.commonDistDirectory + "/renderer"
},
{
from: electronWebpackConfig.commonDistDirectory + "/renderer-dll"
}
],
extraResources: [
{
from: "static",
to: "static"
}
]
}
}
23 changes: 8 additions & 15 deletions packages/electron-webpack/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BluebirdPromise from "bluebird-lst"
import { readJson } from "fs-extra-p"
import { Lazy } from "lazy-val"
import * as path from "path"
import { getConfig, validateConfig } from "read-config-file"
import { validateConfig } from "read-config-file"
import { deepAssign } from "read-config-file/out/deepAssign"
import "source-map-support/register"
import { Configuration, Plugin, RuleSetRule } from "webpack"
Expand All @@ -13,7 +13,8 @@ import { ConfigurationEnv, ConfigurationType, ElectronWebpackConfiguration, Pack
import { BaseTarget } from "./targets/BaseTarget"
import { MainTarget } from "./targets/MainTarget"
import { BaseRendererTarget, RendererTarget } from "./targets/RendererTarget"
import { getFirstExistingFile, orNullIfFileNotExist } from "./util"
import { getFirstExistingFile } from "./util"
import { getElectronWebpackConfig, getPackageMetadata } from "./config"

export { ElectronWebpackConfiguration } from "./core"

Expand Down Expand Up @@ -59,6 +60,7 @@ export class WebpackConfigurator {

readonly sourceDir: string
readonly commonSourceDirectory: string
readonly commonDistDirectory: string

readonly debug = _debug(`electron-webpack:${this.type}`)

Expand Down Expand Up @@ -109,6 +111,7 @@ export class WebpackConfigurator {

const commonSourceDirectory = this.electronWebpackConfiguration.commonSourceDirectory
this.commonSourceDirectory = commonSourceDirectory == null ? path.join(this.projectDir, "src", "common") : path.resolve(this.projectDir, commonSourceDirectory)
this.commonDistDirectory = path.resolve(this.projectDir, this.electronWebpackConfiguration.commonDistDirectory || "dist")
}

/**
Expand Down Expand Up @@ -139,10 +142,6 @@ export class WebpackConfigurator {
}
}

get commonDistDirectory() {
return path.join(this.projectDir, "dist")
}

hasDependency(name: string) {
return name in this.metadata.dependencies || this.hasDevDependency(name)
}
Expand All @@ -155,7 +154,7 @@ export class WebpackConfigurator {
* Returns the names of devDependencies that match a given string or regex.
* If no matching dependencies are found, an empty array is returned.
*
* @return list of matching dependency names, e.g. `['@babel/preset-react', '@babel/preset-stage-0']`
* @return list of matching dependency names, e.g. `["@babel/preset-react", "@babel/preset-stage-0"]`
*/
getMatchingDevDependencies(options: GetMatchingDevDependenciesOptions = {}) {
const includes = options.includes || []
Expand Down Expand Up @@ -306,14 +305,7 @@ export async function createConfigurator(type: ConfigurationType, env: Configura
env = {}
}

const projectDir = (env.configuration || {}).projectDir || process.cwd()
const packageMetadata = await orNullIfFileNotExist(readJson(path.join(projectDir, "package.json")))
const electronWebpackConfig = ((await getConfig({
packageKey: "electronWebpack",
configFilename: "electron-webpack",
projectDir,
packageMetadata: new Lazy(() => Promise.resolve(packageMetadata))
})) || {} as any).result || {}
const electronWebpackConfig = await getElectronWebpackConfig()
if (env.configuration != null) {
deepAssign(electronWebpackConfig, env.configuration)
}
Expand All @@ -328,6 +320,7 @@ How to fix:
* Found? Check that the option in the appropriate place. e.g. "sourceDirectory" only in the "main" or "renderer", not in the root.
`
})
const packageMetadata = await getPackageMetadata()
return new WebpackConfigurator(type, env, electronWebpackConfig, packageMetadata)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/electron-webpack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"skipLibCheck": true
},
"include": [
"**/*.ts", "typings/*.d.ts"
"**/*.ts", "src/electron-builder.ts", "typings/*.d.ts"
]
}

0 comments on commit c4cf411

Please sign in to comment.