Skip to content

Commit

Permalink
fix(client-electron): include every 3rd party license
Browse files Browse the repository at this point in the history
fixes #205
  • Loading branch information
marcincichocki authored Sep 16, 2021
1 parent 08919a9 commit 9dcfbce
Show file tree
Hide file tree
Showing 20 changed files with 478 additions and 310 deletions.
108 changes: 73 additions & 35 deletions configs/common.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,88 @@
import { execSync } from 'child_process';
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
import { DefinePlugin, RuleSetRule, WebpackPluginInstance } from 'webpack';
import { join, resolve } from 'path';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import { Configuration, DefinePlugin } from 'webpack';

function git(command: string) {
return execSync(`git ${command}`, { encoding: 'utf-8' }).trim();
}

const pkg = require('../package.json');

export const commonPlugins: WebpackPluginInstance[] = [
new DefinePlugin({
GIT_COMMIT_DATE: JSON.stringify(
git('log -1 --format=%cd --date=iso-strict')
),
GIT_COMMIT_SHA: JSON.stringify(git('rev-parse HEAD')),
VERSION: JSON.stringify(pkg.version),
HOMEPAGE_URL: JSON.stringify(pkg.homepage),
BUGS_URL: JSON.stringify(pkg.bugs),
PRODUCT_NAME: JSON.stringify(pkg.build.productName),
APP_ID: JSON.stringify(pkg.build.appId),
BUILD_PLATFORM: JSON.stringify(process.platform),
}),
new LicenseWebpackPlugin({
outputFilename: 'THIRD_PARTY_LICENSES.txt',
}) as any,
];

export const commonRules: RuleSetRule[] = [
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: 'ts-loader',
},
{
test: /\.(ttf|svg|png)$/,
type: 'asset/resource',
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
];
export const defineConstantsPlugin = new DefinePlugin({
GIT_COMMIT_DATE: JSON.stringify(git('log -1 --format=%cd --date=iso-strict')),
GIT_COMMIT_SHA: JSON.stringify(git('rev-parse HEAD')),
VERSION: JSON.stringify(pkg.version),
HOMEPAGE_URL: JSON.stringify(pkg.homepage),
BUGS_URL: JSON.stringify(pkg.bugs),
PRODUCT_NAME: JSON.stringify(pkg.build.productName),
APP_ID: JSON.stringify(pkg.build.appId),
BUILD_PLATFORM: JSON.stringify(process.platform),
});

export const root = resolve(__dirname, '..');

export function getCSPMetaTagConfig(content: string) {
return {
'http-equiv': 'Content-Security-Policy',
content,
};
}

type ConfigurationCallback = (
isProduction: boolean,
env: any,
options: any
) => Configuration;

export function getConfig(
configOrCallback: Configuration | ConfigurationCallback
) {
return (env: any, options: any) => {
const defaultConfig: Configuration = {
mode: 'development',
context: join(root, './src/electron'),
output: {
path: join(root, 'dist'),
filename: '[name].js',
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
plugins: [new TsconfigPathsPlugin()],
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: 'ts-loader',
},
{
test: /\.(ttf|svg|png)$/,
type: 'asset/resource',
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
optimization: {
// NOTE: This allow WebpackLicensePlugin to work with ESNext module,
// which is required for treeshaking.
concatenateModules: false,
},
};

const isProduction = options.mode === 'production';
const config =
typeof configOrCallback === 'function'
? configOrCallback(isProduction, env, options)
: configOrCallback;

return {
...defaultConfig,
...config,
};
};
}
31 changes: 12 additions & 19 deletions configs/webpack.config.main.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { join } from 'path';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import webpack from 'webpack';
import { commonPlugins, commonRules } from './common';
import WebpackLicensePlugin from 'webpack-license-plugin';
import { defineConstantsPlugin, getConfig } from './common';

export const config: webpack.Configuration = {
mode: 'development',
entry: join(__dirname, '../src/electron/main/index.ts'),
export const config = getConfig({
target: 'electron-main',
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
plugins: [new TsconfigPathsPlugin()],
entry: {
main: './main/index.ts',
},
output: {
path: join(__dirname, '../dist'),
filename: 'main.js',
},
module: {
rules: [...commonRules],
},
plugins: [...commonPlugins],
};
plugins: [
defineConstantsPlugin,
new WebpackLicensePlugin({
outputFilename: 'main-licenses.json',
}),
],
});
31 changes: 12 additions & 19 deletions configs/webpack.config.preload.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { join } from 'path';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import webpack from 'webpack';
import { commonPlugins, commonRules } from './common';
import WebpackLicensePlugin from 'webpack-license-plugin';
import { defineConstantsPlugin, getConfig } from './common';

export const config: webpack.Configuration = {
mode: 'development',
entry: join(__dirname, '../src/electron/renderer/preload/index.ts'),
export const config = getConfig({
target: 'electron-preload',
output: {
path: join(__dirname, '../dist'),
filename: 'preload.js',
entry: {
preload: './renderer/preload/index.ts',
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
plugins: [new TsconfigPathsPlugin()],
},
module: {
rules: [...commonRules],
},
plugins: [...commonPlugins],
};
plugins: [
defineConstantsPlugin,
new WebpackLicensePlugin({
outputFilename: 'preload-licenses.json',
}),
],
});
35 changes: 15 additions & 20 deletions configs/webpack.config.renderer.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { join } from 'path';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import webpack from 'webpack';
import { commonPlugins, commonRules, getCSPMetaTagConfig } from './common';
import WebpackLicensePlugin from 'webpack-license-plugin';
import {
defineConstantsPlugin,
getConfig,
getCSPMetaTagConfig,
} from './common';

const pkg = require('../package.json');
const allowedSources =
"default-src 'none'; img-src data:; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; connect-src 'self' https:; font-src 'self';";
const csp = getCSPMetaTagConfig(allowedSources);

export const config: webpack.Configuration = {
mode: 'development',
entry: join(__dirname, '../src/electron/renderer/index.tsx'),
export const config = getConfig({
target: 'electron-renderer',
output: {
path: join(__dirname, '../dist'),
filename: 'renderer.js',
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
plugins: [new TsconfigPathsPlugin()],
},
module: {
rules: [...commonRules],
entry: {
renderer: './renderer/index.tsx',
},
plugins: [
...commonPlugins,
defineConstantsPlugin,
new WebpackLicensePlugin({
outputFilename: 'renderer-licenses.json',
}),
new HtmlWebpackPlugin({
filename: 'renderer.html',
filename: '[name].html',
title: pkg.build.productName,
meta: { csp },
}),
],
};
});
47 changes: 23 additions & 24 deletions configs/webpack.config.worker.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { join } from 'path';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import webpack from 'webpack';
import { commonPlugins, commonRules } from './common';
import WebpackLicensePlugin from 'webpack-license-plugin';
import { defineConstantsPlugin, getConfig, root } from './common';

export const config: webpack.Configuration = {
mode: 'development',
entry: join(__dirname, '../src/electron/worker/index.ts'),
const externalPackages = ['sharp', 'tesseract.js', 'screenshot-desktop'];
const externalEnteries = externalPackages.map((n) => [n, `commonjs ${n}`]);
const externals = Object.fromEntries(externalEnteries);

function getAbsolutePathToExternalPackage(name: string) {
return join(root, 'node_modules', name);
}

export const config = getConfig({
target: 'electron-renderer',
output: {
path: join(__dirname, '../dist'),
filename: 'worker.js',
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
plugins: [new TsconfigPathsPlugin()],
},
module: {
rules: [...commonRules],
},
externals: {
sharp: 'commonjs sharp',
'tesseract.js': 'commonjs tesseract.js',
'screenshot-desktop': 'commonjs screenshot-desktop',
entry: {
worker: './worker/index.ts',
},
externals,
plugins: [
...commonPlugins,
defineConstantsPlugin,
new WebpackLicensePlugin({
outputFilename: 'worker-licenses.json',
includePackages() {
return externalPackages.map(getAbsolutePathToExternalPackage);
},
}),
new HtmlWebpackPlugin({
filename: 'worker.html',
filename: '[name].html',
}),
],
};
});
Loading

0 comments on commit 9dcfbce

Please sign in to comment.