Skip to content

Commit

Permalink
fix: ensure we use a resolved commonSourceDir when setting up
Browse files Browse the repository at this point in the history
 watch plugin

Close #230, Close #84
  • Loading branch information
stristr authored and develar committed Nov 22, 2018
1 parent fb6e9ef commit ae2a2f1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/electron-webpack-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "electron-webpack-ts",
"version": "3.0.0",
"version": "3.1.0",
"license": "MIT",
"author": "Vladimir Krivosheev <develar@gmail.com>",
"files": [],
"repository": "electron-userland/electron-webpack",
"dependencies": {
"fork-ts-checker-webpack-plugin": "^0.4.15",
"fork-ts-checker-webpack-plugin": "^0.5.0",
"ts-loader": "^5.3.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/electron-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"html-loader": "^1.0.0-alpha.0",
"html-webpack-plugin": "^3.2.0",
"lazy-val": "^1.0.3",
"mini-css-extract-plugin": "^0.4.4",
"mini-css-extract-plugin": "^0.4.5",
"node-loader": "^0.6.0",
"read-config-file": "^3.2.0",
"semver": "^5.6.0",
Expand All @@ -47,7 +47,7 @@
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10",
"webpack-merge": "^4.1.4",
"yargs": "^12.0.2"
"yargs": "^12.0.5"
},
"peerDependencies": {
"webpack": "^4.8.3"
Expand Down
22 changes: 21 additions & 1 deletion packages/electron-webpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export interface ConfigurationRequest {
packageMetadata: Lazy<{ [key: string]: any } | null> | null
}

export function getDefaultRelativeSystemDependentCommonSource(): string {
return path.join("src", "common")
}

/**
* Return configuration with resolved commonDistDirectory / commonSourceDirectory.
*/
export async function getElectronWebpackConfiguration(context: ConfigurationRequest): Promise<ElectronWebpackConfiguration> {
const result = await getConfig({
packageKey: "electronWebpack",
Expand All @@ -26,7 +33,20 @@ export async function getElectronWebpackConfiguration(context: ConfigurationRequ
configuration.commonDistDirectory = "dist"
}
if (configuration.commonSourceDirectory == null) {
configuration.commonSourceDirectory = "src/common"
configuration.commonSourceDirectory = getDefaultRelativeSystemDependentCommonSource()
}
configuration.commonDistDirectory = path.resolve(context.projectDir, configuration.commonDistDirectory)
configuration.commonSourceDirectory = path.resolve(context.projectDir, configuration.commonSourceDirectory)

if (configuration.renderer === undefined) {
configuration.renderer = {}
}
if (configuration.main === undefined) {
configuration.main = {}
}

if (configuration.projectDir == null) {
configuration.projectDir = context.projectDir
}
return configuration
}
2 changes: 1 addition & 1 deletion packages/electron-webpack/src/dev/dev-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function emptyMainOutput() {
projectDir,
packageMetadata: getPackageMetadata(projectDir),
})
const outDir = path.join(projectDir, electronWebpackConfig.commonDistDirectory!!, "main")
const outDir = path.join(electronWebpackConfig.commonDistDirectory!!, "main")
const files = await orNullIfFileNotExist(readdir(outDir))
if (files == null) {
return
Expand Down
17 changes: 6 additions & 11 deletions packages/electron-webpack/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class WebpackConfigurator {
readonly rules: Array<RuleSetRule> = []
readonly plugins: Array<Plugin> = []

// js must be first - e.g. iview has two files loading-bar.js and loading-bar.vue - when we require "loading-bar", js file must be resolved and not vue
// js must be first - e.g. iView has two files loading-bar.js and loading-bar.vue - when we require "loading-bar", js file must be resolved and not vue
readonly extensions: Array<string> = [".js", ".json", ".node"]

private _electronVersion: string | null = null
Expand All @@ -85,22 +85,16 @@ export class WebpackConfigurator {

readonly entryFiles: Array<string> = []

// electronWebpackConfiguration expected to be resolved (use getElectronWebpackConfiguration())
constructor(readonly type: ConfigurationType, readonly env: ConfigurationEnv, readonly electronWebpackConfiguration: ElectronWebpackConfiguration, readonly metadata: PackageMetadata) {
if (electronWebpackConfiguration.renderer === undefined) {
electronWebpackConfiguration.renderer = {}
}
if (electronWebpackConfiguration.main === undefined) {
electronWebpackConfiguration.main = {}
}

if (metadata.dependencies == null) {
metadata.dependencies = {}
}
if (metadata.devDependencies == null) {
metadata.devDependencies = {}
}

this.projectDir = electronWebpackConfiguration.projectDir || process.cwd()
this.projectDir = electronWebpackConfiguration.projectDir!!
this.isRenderer = type.startsWith("renderer")
process.env.BABEL_ENV = type

Expand All @@ -110,8 +104,8 @@ export class WebpackConfigurator {

this.sourceDir = this.getSourceDirectory(this.type)!!

this.commonSourceDirectory = path.resolve(this.projectDir, this.electronWebpackConfiguration.commonSourceDirectory!!)
this.commonDistDirectory = path.resolve(this.projectDir, this.electronWebpackConfiguration.commonDistDirectory!!)
this.commonSourceDirectory = this.electronWebpackConfiguration.commonSourceDirectory!!
this.commonDistDirectory = this.electronWebpackConfiguration.commonDistDirectory!!
}

/**
Expand Down Expand Up @@ -164,6 +158,7 @@ export class WebpackConfigurator {
}

async configure(entry?: { [key: string]: any } | null) {
// noinspection SpellCheckingInspection
this._configuration = {
context: this.projectDir,
devtool: this.isProduction || this.isTest ? "nosources-source-map" : "eval-source-map",
Expand Down
23 changes: 18 additions & 5 deletions packages/electron-webpack/src/targets/BaseTarget.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "path"
import { DefinePlugin, EnvironmentPlugin, HotModuleReplacementPlugin, LoaderOptionsPlugin } from "webpack"
import { getDefaultRelativeSystemDependentCommonSource } from "../config"
import { configureDll } from "../configurators/dll"
import { configureEslint } from "../configurators/eslint"
import { createBabelLoader } from "../configurators/js"
Expand All @@ -12,6 +13,7 @@ export class BaseTarget {
const rules = configurator.rules

const babelLoader = createBabelLoader(configurator)
// noinspection SpellCheckingInspection
if (configurator.type !== "main" && configurator.hasDependency("iview")) {
rules.push({
test: /iview.src.*?js$/,
Expand Down Expand Up @@ -96,6 +98,9 @@ export function configureFileLoader(prefix: string, limit = 10 * 1024) {
}

function isAncestor(file: string, dir: string) {
if (file === dir) {
return true
}
return file.length > dir.length && file[dir.length] === path.sep && file.startsWith(dir)
}

Expand Down Expand Up @@ -123,15 +128,23 @@ function configureDevelopmentPlugins(configurator: WebpackConfigurator) {
}

// watch common code
let commonSourceDir = configurator.electronWebpackConfiguration.commonSourceDirectory
if (commonSourceDir == null) {
let commonSourceDir = configurator.commonSourceDirectory
if (commonSourceDir.endsWith(path.sep + getDefaultRelativeSystemDependentCommonSource())) {
// not src/common, because it is convenient to just put some code into src to use it
commonSourceDir = path.join(configurator.projectDir, "src")
commonSourceDir = path.dirname(commonSourceDir)
}

const alienSourceDir = configurator.getSourceDirectory(configurator.type === "main" ? "renderer" : "main")

const sourceDir = configurator.getSourceDirectory(configurator.type)
configurator.plugins.push(new WatchFilterPlugin(file => {
return file === commonSourceDir || (isAncestor(file, commonSourceDir!!) && (alienSourceDir != null && !file.startsWith(alienSourceDir)))
if (sourceDir != null && isAncestor(file, sourceDir)) {
return true
}
else if (file === commonSourceDir || isAncestor(file, commonSourceDir!!)) {
return alienSourceDir == null || !isAncestor(file, alienSourceDir)
}
else {
return false
}
}, require("debug")(`electron-webpack:watch-${configurator.type}`)))
}

0 comments on commit ae2a2f1

Please sign in to comment.