diff --git a/lib/webpack/shared.cjs b/lib/webpack/shared.cjs index afc65d7fc..217019bb7 100644 --- a/lib/webpack/shared.cjs +++ b/lib/webpack/shared.cjs @@ -1,258 +1,280 @@ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const TerserPlugin = require('terser-webpack-plugin'); const webpack = require('webpack'); const sharedPlugins = [ new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1, }), ]; const cssLoader = { loader: 'css-loader', options: { modules: { mode: 'local', localIdentName: '[path][name]__[local]--[contenthash:base64:5]', }, }, }; const cssExtractLoader = { loader: MiniCssExtractPlugin.loader, }; const styleLoader = { loader: 'style-loader', }; function getBabelRule(babelConfig) { return { test: /\.js$/, exclude: /node_modules\/(?!lib)/, loader: 'babel-loader', options: babelConfig, }; } function getBrowserBabelRule(babelConfig) { const babelRule = getBabelRule(babelConfig); return { ...babelRule, options: { ...babelRule.options, presets: [ ...babelRule.options.presets, [ '@babel/preset-env', { targets: 'defaults', useBuiltIns: 'usage', corejs: '3.6', }, ], ], }, }; } const imageRule = { test: /\.(png|svg)$/, type: 'asset/inline', }; const typographyRule = { test: /\.(woff2|woff)$/, type: 'asset/inline', }; function createBaseBrowserConfig(baseConfig) { return { ...baseConfig, name: 'browser', optimization: { minimizer: [new TerserPlugin(), new CssMinimizerPlugin()], }, plugins: [ ...(baseConfig.plugins ?? []), ...sharedPlugins, new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'], }), new CleanWebpackPlugin({ cleanOnceBeforeBuildPatterns: [], }), ], node: { global: true, }, }; } const alchemyKey = process.env.COMM_ALCHEMY_KEY; const walletConnectKey = process.env.COMM_WALLETCONNECT_KEY; function createProdBrowserConfig(baseConfig, babelConfig) { const browserConfig = createBaseBrowserConfig(baseConfig); const babelRule = getBrowserBabelRule(babelConfig); return { ...browserConfig, mode: 'production', plugins: [ ...browserConfig.plugins, new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production'), BROWSER: true, COMM_ALCHEMY_KEY: JSON.stringify(alchemyKey), COMM_WALLETCONNECT_KEY: JSON.stringify(walletConnectKey), }, }), new MiniCssExtractPlugin({ filename: 'prod.[contenthash:12].build.css', }), ], module: { rules: [ { ...babelRule, options: { ...babelRule.options, plugins: [ ...babelRule.options.plugins, '@babel/plugin-transform-react-constant-elements', ['transform-remove-console', { exclude: ['error', 'warn'] }], ], }, }, { test: /\.css$/, exclude: /node_modules\/.*\.css$/, use: [ cssExtractLoader, { ...cssLoader, options: { ...cssLoader.options, url: false, }, }, ], }, { test: /node_modules\/.*\.css$/, sideEffects: true, use: [ cssExtractLoader, { ...cssLoader, options: { ...cssLoader.options, url: false, modules: false, }, }, ], }, ], }, }; } function createDevBrowserConfig(baseConfig, babelConfig) { const browserConfig = createBaseBrowserConfig(baseConfig); const babelRule = getBrowserBabelRule(babelConfig); return { ...browserConfig, mode: 'development', plugins: [ ...browserConfig.plugins, new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('development'), BROWSER: true, COMM_ALCHEMY_KEY: JSON.stringify(alchemyKey), COMM_WALLETCONNECT_KEY: JSON.stringify(walletConnectKey), }, }), new ReactRefreshWebpackPlugin(), ], module: { rules: [ { ...babelRule, options: { ...babelRule.options, plugins: [ require.resolve('react-refresh/babel'), ...babelRule.options.plugins, ], }, }, imageRule, typographyRule, { test: /\.css$/, exclude: /node_modules\/.*\.css$/, use: [styleLoader, cssLoader], }, { test: /node_modules\/.*\.css$/, sideEffects: true, use: [ styleLoader, { ...cssLoader, options: { ...cssLoader.options, modules: false, }, }, ], }, ], }, devtool: 'eval-cheap-module-source-map', }; } function createNodeServerRenderingConfig(baseConfig, babelConfig) { return { ...baseConfig, name: 'server', target: 'node', module: { rules: [ getBabelRule(babelConfig), { test: /\.css$/, use: { ...cssLoader, options: { ...cssLoader.options, modules: { ...cssLoader.options.modules, exportOnlyLocals: true, }, }, }, }, ], }, plugins: [ ...sharedPlugins, new webpack.DefinePlugin({ 'process.env': { COMM_ALCHEMY_KEY: JSON.stringify(alchemyKey), COMM_WALLETCONNECT_KEY: JSON.stringify(walletConnectKey), }, }), ], }; } +function createWebWorkersConfig(env, baseConfig, babelConfig) { + return { + ...baseConfig, + name: 'webworkers', + target: 'webworker', + mode: env.prod ? 'production' : 'development', + module: { + rules: [getBrowserBabelRule(babelConfig)], + }, + plugins: [ + ...sharedPlugins, + new webpack.DefinePlugin({ + 'process.env': { + NODE_ENV: JSON.stringify(env.prod ? 'production' : 'development'), + BROWSER: true, + }, + }), + ], + }; +} + module.exports = { createProdBrowserConfig, createDevBrowserConfig, createNodeServerRenderingConfig, + createWebWorkersConfig, }; diff --git a/web/package.json b/web/package.json index 49a93c5b1..150ed84ae 100644 --- a/web/package.json +++ b/web/package.json @@ -1,100 +1,100 @@ { "name": "web", "version": "0.0.1", "type": "module", "private": true, "license": "BSD-3-Clause", "scripts": { "clean": "rm -rf dist/ && rm -rf node_modules/", - "dev": "yarn concurrently --names=\"NODESSR,BROWSER\" -c \"bgBlue.bold,bgMagenta.bold\" \"yarn webpack --config webpack.config.cjs --config-name=server --watch\" \"yarn webpack-dev-server --config webpack.config.cjs --config-name=browser\"", + "dev": "yarn concurrently --names=\"NODESSR,BROWSER,WORKERS\" -c \"bgBlue.bold,bgMagenta.bold,bgCyan.bold\" \"yarn webpack --config webpack.config.cjs --config-name=server --watch\" \"yarn webpack-dev-server --config webpack.config.cjs --config-name=browser\" \"yarn webpack --config webpack.config.cjs --config-name=webworkers --watch\"", "prod": "yarn webpack --config webpack.config.cjs --env prod --progress", "test": "jest" }, "devDependencies": { "@babel/core": "^7.13.14", "@babel/plugin-proposal-class-properties": "^7.13.0", "@babel/plugin-proposal-nullish-coalescing-operator": "^7.13.8", "@babel/plugin-proposal-object-rest-spread": "^7.13.8", "@babel/plugin-proposal-optional-chaining": "^7.13.12", "@babel/plugin-transform-react-constant-elements": "^7.13.13", "@babel/plugin-transform-runtime": "^7.13.10", "@babel/preset-env": "^7.13.12", "@babel/preset-flow": "^7.13.13", "@babel/preset-react": "^7.13.13", "assets-webpack-plugin": "^7.1.1", "babel-jest": "^26.6.3", "babel-plugin-transform-remove-console": "^6.9.4", "concurrently": "^5.3.0", "copy-webpack-plugin": "^11.0.0", "flow-bin": "^0.182.0", "flow-typed": "^3.2.1", "identity-obj-proxy": "^3.0.0", "jest": "^26.6.3", "webpack-cli": "^5.0.1", "webpack-dev-server": "^4.11.1" }, "dependencies": { "@babel/runtime": "^7.13.10", "@emoji-mart/data": "^1.1.2", "@emoji-mart/react": "^1.1.1", "@fortawesome/fontawesome-svg-core": "1.2.25", "@fortawesome/free-regular-svg-icons": "5.11.2", "@fortawesome/free-solid-svg-icons": "5.11.2", "@fortawesome/react-fontawesome": "0.1.5", "@rainbow-me/rainbowkit": "^0.8.1", "basscss": "8.0.2", "classnames": "^2.2.5", "core-js": "^3.6.5", "dateformat": "^3.0.3", "detect-browser": "^4.0.4", "emoji-mart": "^5.5.2", "exif-js": "^2.3.0", "history": "^4.6.3", "ethers": "^5.7.2", "invariant": "^2.2.4", "is-svg": "^4.3.0", "isomorphic-fetch": "^3.0.0", "lib": "0.0.1", "lodash": "^4.17.21", "@matrix-org/olm": "3.2.4", "react": "18.1.0", "react-circular-progressbar": "^2.0.2", "react-color": "^2.13.0", "react-dnd": "^11.1.3", "react-dnd-html5-backend": "^11.1.3", "react-dom": "18.1.0", "react-feather": "^2.0.3", "react-icomoon": "^2.5.7", "react-icons": "^4.4.0", "react-redux": "^7.1.1", "react-router": "^5.2.0", "react-router-dom": "^5.2.0", "react-switch": "^7.0.0", "react-timeago": "^5.2.0", "redux": "^4.0.4", "redux-devtools-extension": "^2.13.2", "redux-persist": "^6.0.0", "redux-thunk": "^2.2.0", "reselect": "^4.0.0", "simple-markdown": "^0.7.2", "siwe": "^1.1.6", "tinycolor2": "^1.4.1", "uuid": "^3.3.3", "visibilityjs": "^2.0.2", "wagmi": "^0.8.10" }, "jest": { "roots": [ "/utils" ], "transform": { "\\.js$": "babel-jest" }, "transformIgnorePatterns": [ "/node_modules/(?!@babel/runtime)" ], "moduleNameMapper": { "\\.(css)$": "identity-obj-proxy" } } } diff --git a/web/push-notif/service-worker.js b/web/push-notif/service-worker.js new file mode 100644 index 000000000..f0aa9b0c2 --- /dev/null +++ b/web/push-notif/service-worker.js @@ -0,0 +1,9 @@ +// @flow + +self.addEventListener('install', () => { + console.log('service worker installed'); +}); + +self.addEventListener('activate', () => { + console.log('service worker activated'); +}); diff --git a/web/webpack.config.cjs b/web/webpack.config.cjs index c5e6c79fa..2276982a3 100644 --- a/web/webpack.config.cjs +++ b/web/webpack.config.cjs @@ -1,100 +1,116 @@ const AssetsPlugin = require('assets-webpack-plugin'); const CopyPlugin = require('copy-webpack-plugin'); const path = require('path'); const { createProdBrowserConfig, createDevBrowserConfig, createNodeServerRenderingConfig, + createWebWorkersConfig, } = require('lib/webpack/shared.cjs'); const babelConfig = require('./babel.config.cjs'); const baseBrowserConfig = { plugins: [ new CopyPlugin({ patterns: [ { from: 'node_modules/@matrix-org/olm/olm.wasm', to: path.join(__dirname, 'dist'), }, ], }), ], entry: { browser: ['./script.js'], }, output: { filename: 'prod.[contenthash:12].build.js', path: path.join(__dirname, 'dist'), }, resolve: { alias: { '../images': path.resolve('../keyserver/images'), }, fallback: { crypto: false, fs: false, path: false, }, }, }; const baseDevBrowserConfig = { ...baseBrowserConfig, output: { ...baseBrowserConfig.output, filename: 'dev.build.js', pathinfo: true, publicPath: 'http://localhost:8080/', }, devServer: { port: 8080, headers: { 'Access-Control-Allow-Origin': '*' }, allowedHosts: ['all'], host: '0.0.0.0', static: { directory: path.join(__dirname, 'dist'), }, }, }; const baseProdBrowserConfig = { ...baseBrowserConfig, plugins: [ ...baseBrowserConfig.plugins, new AssetsPlugin({ filename: 'assets.json', path: path.join(__dirname, 'dist'), removeFullPathAutoPrefix: true, }), ], }; const baseNodeServerRenderingConfig = { externals: ['react', 'react-dom', 'react-redux'], entry: { keyserver: ['./loading.react.js'], }, output: { filename: 'app.build.cjs', library: 'app', libraryTarget: 'commonjs2', path: path.join(__dirname, 'dist'), }, }; +const baseWebWorkersConfig = { + entry: { + pushNotif: './push-notif/service-worker.js', + }, + output: { + filename: '[name].build.js', + path: path.join(__dirname, 'dist', 'webworkers'), + }, +}; + module.exports = function (env) { const browserConfig = env.prod ? createProdBrowserConfig(baseProdBrowserConfig, babelConfig) : createDevBrowserConfig(baseDevBrowserConfig, babelConfig); const nodeConfig = createNodeServerRenderingConfig( baseNodeServerRenderingConfig, babelConfig, ); const nodeServerRenderingConfig = { ...nodeConfig, mode: env.prod ? 'production' : 'development', }; - return [browserConfig, nodeServerRenderingConfig]; + const webWorkersConfig = createWebWorkersConfig( + env, + baseWebWorkersConfig, + babelConfig, + ); + return [browserConfig, nodeServerRenderingConfig, webWorkersConfig]; };