Skip to content

Commit

Permalink
refactor: esm webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Feb 15, 2024
1 parent 81fea0d commit ff81e03
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .dependency-cruiser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('dependency-cruiser').IConfiguration} */
module.exports = {

export default {
extends: 'dependency-cruiser/configs/recommended',

forbidden: [
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions .github/actions/check-version-lock/check-version-lock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const core = require('@actions/core');
const appPackage = require('../../../package.json');
import core from '@actions/core';

import appPackage from '../../../package.json' assert { type: 'json' };

const illegalVersionSymbol = ['~', '^', '>', '<'];

Expand All @@ -15,8 +16,8 @@ function containsIllegalChar(input) {
const allPackages = [...Object.entries(dependencies), ...Object.entries(devDependencies)];

const illegalPackages = allPackages
.filter(([package, version]) => containsIllegalChar(version))
.map(([package, version]) => ({ package, version }));
.filter(([pkg, version]) => containsIllegalChar(version))
.map(([pkg, version]) => ({ package: pkg, version }));

if (illegalPackages.length > 0) {
core.setFailed(`
Expand Down
6 changes: 2 additions & 4 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const defaultConfig = require('@leather-wallet/prettier-config');
import config from '@leather-wallet/prettier-config';

module.exports = {
...defaultConfig,
};
export default config;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": {
"name": "Leather Wallet LLC"
},
"type": "module",
"scripts": {
"dev": "concurrently --raw \"node webpack/dev-server.js\" \"redux-devtools --hostname=localhost --port=8000\"",
"dev:test-app": "webpack serve --config test-app/webpack/webpack.config.dev.js",
Expand Down Expand Up @@ -330,11 +331,11 @@
"vm-browserify": "1.1.2",
"web-ext": "7.8.0",
"web-ext-submit": "7.8.0",
"webpack": "5.89.0",
"webpack": "5.90.1",
"webpack-bundle-analyzer": "4.10.1",
"webpack-cli": "5.1.4",
"webpack-dev-server": "4.15.1",
"webpack-hot-middleware": "2.26.0",
"webpack-hot-middleware": "2.26.1",
"webpack-shell-plugin": "0.5.0"
},
"resolutions": {
Expand Down
9 changes: 2 additions & 7 deletions scripts/generate-manifest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* @typedef {import('@schemastore/web-manifest').JSONSchemaForWebApplicationManifestFiles} Manifest
*/
const deepMerge = require('deepmerge');
import deepMerge from 'deepmerge';

// Manifest can only be prod or dev
const WALLET_ENVIRONMENT =
Expand Down Expand Up @@ -121,7 +118,7 @@ const prodManifest = {
},
};

function generateManifest(packageVersion) {
export default function generateManifest(packageVersion) {
if (!packageVersion)
throw new Error('Version number must be passed to `generateManifest` function');

Expand All @@ -139,5 +136,3 @@ function generateManifest(packageVersion) {
environmentIcons[WALLET_ENVIRONMENT],
]);
}

module.exports = generateManifest;
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"module": "ESNext",
"moduleResolution": "node",
"lib": ["dom", "dom.iterable", "esnext"],
"jsx": "react-jsx",
"sourceMap": true,
Expand All @@ -18,7 +19,6 @@
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"baseUrl": "src",
"paths": {
Expand Down
20 changes: 11 additions & 9 deletions webpack/dev-server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const WebpackDevServer = require('webpack-dev-server');
const webpack = require('webpack');
const path = require('path');
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
import path from 'path';
import * as url from 'url';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';

import config from './webpack.config.dev.js';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const WALLET_ENVIRONMENT = process.env.WALLET_ENVIRONMENT;

const HOST = 'localhost';
const PORT = process.env.PORT || '8080';

const config = require('./webpack.config.dev');

// This is important bc it allows for fast refresh to work
// We don't want to inject our fast refresh helpers into these entry points
const excludeEntriesFromHotModuleReload = ['content-script', 'inpage'];
Expand Down Expand Up @@ -53,9 +55,9 @@ const server = new WebpackDevServer(
compiler
);

if (WALLET_ENVIRONMENT === 'development' && module.hot) {
module.hot.accept();
}
// if (WALLET_ENVIRONMENT === 'development' && module.hot) {
// module.hot.accept();
// }

server.startCallback(() => {
console.log('Starting server on http://localhost:8080');
Expand Down
59 changes: 32 additions & 27 deletions webpack/webpack.config.base.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const webpack = require('webpack');
const { version: _version } = require('../package.json');
const generateManifest = require('../scripts/generate-manifest');
const Dotenv = require('dotenv-webpack');
const GenerateJsonPlugin = require('generate-json-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
import { execSync } from 'node:child_process';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import Dotenv from 'dotenv-webpack';
import GenerateJsonPlugin from 'generate-json-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { createRequire } from 'node:module';
import path from 'node:path';
import ProgressBarPlugin from 'progress-bar-webpack-plugin';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import webpack from 'webpack';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import * as url from 'url';

import packageJson from '../package.json' assert { type: 'json' };
import generateManifest from '../scripts/generate-manifest.js';


const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const SRC_ROOT_PATH = path.join(__dirname, '../', 'src');
const DIST_ROOT_PATH = path.join(__dirname, '../', 'dist');
const { execSync } = require('child_process');

const WALLET_ENVIRONMENT = process.env.WALLET_ENVIRONMENT ?? 'development';
const ANALYZE_BUNDLE = process.env.ANALYZE === 'true';
Expand All @@ -38,10 +43,12 @@ console.log({
locallyExe: executeGitCommand('git rev-parse HEAD'),
});

const require = createRequire(import.meta.url);

// For non main branch builds, add a random number
const getVersionWithRandomSuffix = ref => {
if (ref === MAIN_BRANCH || !ref || IS_PUBLISHING) return _version;
return `${_version}.${Math.floor(Math.floor(Math.random() * 1000))}`;
if (ref === MAIN_BRANCH || !ref || IS_PUBLISHING) return packageJson.version;
return `${packageJson.version}.${Math.floor(Math.floor(Math.random() * 1000))}`;
};
const VERSION = getVersionWithRandomSuffix(BRANCH_NAME);

Expand Down Expand Up @@ -82,7 +89,7 @@ const aliases = {
'leather-styles': path.resolve('leather-styles'),
};

const config = {
export const config = {
entry: {
background: path.join(SRC_ROOT_PATH, 'background', 'background.ts'),
inpage: path.join(SRC_ROOT_PATH, 'inpage', 'inpage.ts'),
Expand Down Expand Up @@ -260,13 +267,11 @@ const config = {
},
};

module.exports = config;

if (IS_PROD) {
module.exports.plugins.push(
new CleanWebpackPlugin({ verbose: true, dry: false, cleanStaleWebpackAssets: false })
);
}
if (ANALYZE_BUNDLE) {
module.exports.plugins.push(new BundleAnalyzerPlugin());
}
// if (IS_PROD) {
// module.exports.plugins.push(
// new CleanWebpackPlugin({ verbose: true, dry: false, cleanStaleWebpackAssets: false })
// );
// }
// if (ANALYZE_BUNDLE) {
// module.exports.plugins.push(new BundleAnalyzerPlugin());
// }
7 changes: 2 additions & 5 deletions webpack/webpack.config.dev.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const baseConfig = require('./webpack.config.base');
import { config as baseConfig } from './webpack.config.base.js';

const config = {
export default {
...baseConfig,
devtool: 'inline-source-map',
mode: 'development',
Expand All @@ -19,5 +18,3 @@ const config = {
splitChunks: false,
},
};

module.exports = config;
14 changes: 8 additions & 6 deletions webpack/webpack.config.prod.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const config = require('./webpack.config.base');
const packageJson = require('../package.json');
const { sentryWebpackPlugin } = require('@sentry/webpack-plugin');
const webpack = require('webpack');
import { sentryWebpackPlugin } from '@sentry/webpack-plugin';
import webpack from 'webpack';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';

import packageJson from '../package.json' assert { type: 'json' };
import { config } from './webpack.config.base.js';

const sentryAuthToken = process.env.SENTRY_AUTH_TOKEN;

Expand All @@ -26,6 +27,7 @@ config.optimization = {

config.plugins = [
...config.plugins,
new CleanWebpackPlugin({ verbose: true, dry: false, cleanStaleWebpackAssets: false }),
new webpack.SourceMapDevToolPlugin({
// These entry points are excuted in an app's context. If we generate source
// maps for them, the browser attempts to load them from the inaccessible
Expand Down Expand Up @@ -58,4 +60,4 @@ config.plugins = [

config.devtool = false;

module.exports = config;
export default config;
Loading

0 comments on commit ff81e03

Please sign in to comment.