Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
refactor(build): replace rollup with webpack to make build faster
Browse files Browse the repository at this point in the history
We replace `rollup` with `webpack` to make our builds faster. While
webpack is not faster without a cache it supports persistent caches in
the file system which makes it significantly faster in development.
Webpack is also significantly faster in watch mode.

On my machine a rollup build and a webpack build without a cache take
about 60 seconds. If a cache is present webpack builds the project in
8-9 seconds. When updating a file in watch mode rollup takes 7-8 seconds
to rebuild. Webpack rebuilds the project in under a second.

Another thing to note is that in the past I spent multiple hours
investigating issues with rollup and tweaking the config. None of this
was necessary for webpack. We also gain more functionality, for example
we can now leverage templating in the HTML document.

Some implementation notes:
* We patch `package.json` of `svelte-persistent-store` because of [this
  issue][sps-issue]. This only worked before because rollup itself was
  buggy.
* We add some dependencies that are used in our source code but were not
  explicitly specified.
* We use `html-webpack-plugin` to dynamically build `public/index.html`
  and configure it for different environments.
* We remove `bundle.css`. CSS is now injected during runtime via JS.

Follup-up:
* We still need to leverage the webpack cache on CI

[sps-issue]: andsala/svelte-persistent-store#15

Signed-off-by: Thomas Scholtes <thomas@monadic.xyz>
  • Loading branch information
Thomas Scholtes committed Mar 25, 2021
1 parent 6ec277f commit 6f90358
Show file tree
Hide file tree
Showing 9 changed files with 1,191 additions and 511 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist
native/bundle.js
native/bundle.js.map
public/bundle.*
public/index.html
public/twemoji
cypress/videos
cypress/screenshots
Expand Down
36 changes: 17 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@
"main": "./native/bundle.js",
"devDependencies": {
"@ethersproject/cli": "^5.0.9",
"@rollup/plugin-commonjs": "^16.0.0",
"@rollup/plugin-inject": "^4.0.2",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^10.0.0",
"@rollup/plugin-typescript": "^8.2.0",
"@tsconfig/svelte": "^1.0.10",
"@types/jest": "^26.0.20",
"@types/lodash": "^4.14.168",
Expand All @@ -100,6 +95,7 @@
"eslint-svelte3-preprocess": "=0.0.4",
"exit-hook": "^2.2.0",
"ganache-cli": "^6.12.2",
"html-webpack-plugin": "^5.3.1",
"husky": "^4.3.6",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
Expand All @@ -110,23 +106,22 @@
"prettier": "^2.2.1",
"prettier-plugin-svelte": "^1.4.2",
"prompts": "^2.4.0",
"rollup": "^2.39.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-node-externals": "^2.1.6",
"rollup-plugin-svelte": "^7.1.0",
"rollup-plugin-terser": "^7.0.2",
"sinon": "^9.2.4",
"standard-version": "^9.1.1",
"svelte": "^3.32.3",
"svelte-check": "^1.1.35",
"svelte-loader": "^3.0.0",
"svelte-preprocess": "^4.6.9",
"svelte-spa-router": "^3.1.0",
"ts-jest": "^26.5.1",
"ts-loader": "^8.0.18",
"ts-node": "^9.1.1",
"tslib": "^2.1.0",
"typescript": "^4.1.5",
"util": "^0.12.3",
"wait-on": "^5.2.1"
"wait-on": "^5.2.1",
"webpack": "^5.26.0",
"webpack-cli": "^4.5.0"
},
"scripts": {
"start": "RADICLE_UPSTREAM_PROXY_PATH=./target/release/radicle-proxy RADICLE_UPSTREAM_PROXY_ARGS='--default-seed hynewpywqj6x4mxgj7sojhue3erucyexiyhobxx4du9w66hxhbfqbw@seedling.radicle.xyz:12345' yarn _private:start",
Expand All @@ -135,10 +130,10 @@
"ethereum:start": "./scripts/ethereum-dev-node.sh",
"test": "TZ='UTC' yarn test:unit && TZ='UTC' yarn test:integration",
"test:integration": "TZ='UTC' run-p --race _private:proxy:start:test _private:test:integration",
"test:integration:debug": "TZ='UTC' run-p --race _private:rollup:watch _private:proxy:start:test:watch _private:test:integration:debug",
"test:integration:debug": "TZ='UTC' run-p --race _private:webpack:ui:watch _private:proxy:start:test:watch _private:test:integration:debug",
"test:unit": "jest",
"test:unit:watch": "jest --watchAll",
"dist": "yarn _private:dist:clean && yarn _private:rollup:build:release && yarn _private:proxy:build:release && electron-builder --publish never",
"dist": "yarn _private:dist:clean && webpack build --mode production && yarn _private:proxy:build:release && electron-builder --publish never",
"release": "scripts/release.ts",
"typescript:check": "tsc --noEmit && tsc --noEmit --project cypress && svelte-check",
"prettier:check": "yarn _private:prettier --check",
Expand All @@ -147,8 +142,8 @@
"reset:state": "scripts/reset-state.sh",
"_private:start": "yarn _private:proxy:build:release && yarn _private:start:app",
"_private:start:dev": "yarn _private:proxy:build && yarn _private:start:app",
"_private:start:app": "run-p --race _private:rollup:ui:watch _private:electron:start",
"_private:test:integration": "wait-on tcp:17246 && yarn _private:rollup:ui && yarn run cypress run",
"_private:start:app": "run-p --race _private:webpack:ui:watch _private:electron:start",
"_private:test:integration": "wait-on tcp:17246 && yarn run webpack build --config-name ui && yarn run cypress run",
"_private:test:integration:debug": "wait-on ./public/bundle.js tcp:17246 && yarn run cypress open",
"_private:electron:start": "wait-on ./public/bundle.js && NODE_ENV=development electron native/index.js",
"_private:dist:clean": "rm -rf ./dist && mkdir ./dist",
Expand All @@ -157,25 +152,28 @@
"_private:proxy:build:release": "cargo build --release",
"_private:proxy:start:test": "cargo build --release --bin git-remote-rad && cargo run --release -- --test",
"_private:proxy:start:test:watch": "cargo build --release --bin git-remote-rad && cargo watch -x 'run --release -- --test'",
"_private:rollup:build:release": "ROLLUP_RELEASE=true rollup -c --failAfterWarnings",
"_private:rollup:ui": "rollup -c rollup.ui.js",
"_private:rollup:ui:watch": "rollup -c rollup.ui.js -w",
"_private:webpack:ui:watch": "webpack build --watch --config-name ui",
"postinstall": "patch-package && scripts/install-twemoji-assets.sh"
},
"dependencies": {
"@ethersproject/bytes": "^5.0.9",
"@ethersproject/properties": "^5.0.7",
"@types/big.js": "^6.0.2",
"@types/qs": "^6.9.5",
"@types/uuid": "^8.3.0",
"@walletconnect/client": "^1.3.6",
"big.js": "^6.0.3",
"browserify": "^17.0.0",
"crypto-browserify": "^3.12.0",
"ethers": "^5.0.31",
"marked": "^2.0.0",
"mnemonist": "^0.38.1",
"pure-svg-code": "^1.0.6",
"qs": "^6.9.6",
"radicle-contracts": "github:radicle-dev/radicle-contracts#commit=9fb23ac74a403b167a22513fc666779d871d292e",
"rollup-plugin-css-only": "^3.1.0",
"regexparam": "^1.3.0",
"semver": "^7.3.4",
"stream-browserify": "^3.0.0",
"svelte-persistent-store": "^0.1.6",
"timeago.js": "^4.0.2",
"twemoji": "13.0.2",
Expand Down
15 changes: 15 additions & 0 deletions patches/svelte-persistent-store+0.1.6.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/svelte-persistent-store/package.json b/node_modules/svelte-persistent-store/package.json
index 115449e..2d2999d 100644
--- a/node_modules/svelte-persistent-store/package.json
+++ b/node_modules/svelte-persistent-store/package.json
@@ -11,10 +11,6 @@
"homepage": "https://github.com/andsala/svelte-persistent-store",
"main": "dist/index.js",
"module": "dist/index.mjs",
- "exports": {
- ".": "./dist/index.js",
- "./": "./dist/"
- },
"types": "dist/**",
"scripts": {
"build": "tsup src/local.ts src/session.ts src/index.ts --external svelte --format esm,cjs --inlineDynamicImports --dts",
4 changes: 0 additions & 4 deletions rollup.config.js

This file was deleted.

23 changes: 0 additions & 23 deletions rollup.electron.js

This file was deleted.

100 changes: 0 additions & 100 deletions rollup.ui.js

This file was deleted.

4 changes: 0 additions & 4 deletions public/index.html → ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Security-Policy" content="script-src 'self'" />

<title>Radicle Upstream</title>

Expand Down Expand Up @@ -87,9 +86,6 @@
<link rel="stylesheet" href="elevation.css" />
<link rel="stylesheet" href="global.css" />
<link rel="stylesheet" href="typography.css" />
<link rel="stylesheet" href="bundle.css" />

<script defer src="bundle.js"></script>
</head>

<body></body>
Expand Down
112 changes: 112 additions & 0 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import path from "path";
import sveltePreprocess from "svelte-preprocess";
import webpack from "webpack";
import HtmlWebpackPlugin from "html-webpack-plugin";

interface Argv {
mode?: "production" | "development";
}

const tsRule = {
test: /\.ts$/,
exclude: /node_modules/,
use: {
loader: "ts-loader",
options: {
compilerOptions: {
noEmit: false,
module: "es6",
},
},
},
};

function electronMain(_env: unknown, argv: Argv): webpack.Configuration {
const mode = argv.mode || "development";
return {
name: "main",
entry: "./native/index.ts",
mode,
cache: {
type: "filesystem",
},
externals: ["fsevents"],
target: "electron-main",
externalsPresets: { electronMain: true, node: true },
module: {
rules: [tsRule],
},
resolve: {
extensions: [".ts", ".js"],
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "native"),
},
optimization: {
minimize: false,
},
};
}

function ui(_env: unknown, argv: Argv): webpack.Configuration {
const mode = argv.mode || "development";
const isProduction = mode === "production";
return {
name: "ui",
entry: "./ui/index.ts",
mode,
devtool: isProduction ? "source-map" : "eval-source-map",
cache: {
type: "filesystem",
},
performance: {
hints: false,
},
target: "web",
module: {
rules: [
{
test: /\.svelte$/,
use: {
loader: "svelte-loader",
options: {
compilerOptions: { dev: !isProduction },
preprocess: sveltePreprocess(),
},
},
},
tsRule,
],
},
resolve: {
fallback: {
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
},
extensions: [".svelte", ".ts", ".js"],
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "public"),
},
plugins: [
new webpack.ProvidePlugin({
process: "process",
}),
new HtmlWebpackPlugin({
template: "ui/index.html",
meta: {
"Content-Security-Policy": {
"http-equiv": "Content-Security-Policy",
content: isProduction
? "script-src 'self'"
: "script-src 'self' 'unsafe-eval'",
},
},
}),
],
};
}

export default [ui, electronMain];
Loading

0 comments on commit 6f90358

Please sign in to comment.