Files
notesnook/apps/mobile/native/webpack.config.js

310 lines
12 KiB
JavaScript
Raw Normal View History

2022-07-06 12:17:08 +05:00
/* eslint-disable @typescript-eslint/no-var-requires */
2022-08-16 16:48:10 +05:00
const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
const Repack = require("@callstack/repack");
2022-07-06 12:17:08 +05:00
/**
* More documentation, installation, usage, motivation and differences with Metro is available at:
* https://github.com/callstack/repack/blob/main/README.md
*
* The API documentation for the functions and plugins used in this file is available at:
* https://re-pack.netlify.app/
*/
/**
* Webpack configuration.
* You can also export a static object or a function returning a Promise.
*
* @param env Environment options passed from either Webpack CLI or React Native CLI
* when running with `react-native start/bundle`.
*/
2022-08-16 16:48:10 +05:00
module.exports = (env) => {
2022-07-06 12:17:08 +05:00
const {
2022-08-16 16:48:10 +05:00
mode = "development",
2022-07-06 12:17:08 +05:00
context = __dirname,
2022-08-29 16:19:17 +05:00
entry = "./index.js",
2022-07-06 12:17:08 +05:00
platform,
2022-08-16 16:48:10 +05:00
minimize = mode === "production",
2022-07-06 12:17:08 +05:00
devServer = undefined,
bundleFilename = undefined,
sourceMapFilename = undefined,
assetsPath = undefined,
reactNativePath = path.join(__dirname, "../node_modules/react-native"),
2022-07-06 12:17:08 +05:00
} = env;
if (!platform) {
2022-08-16 16:48:10 +05:00
throw new Error("Missing platform");
2022-07-06 12:17:08 +05:00
}
/**
* Depending on your Babel configuration you might want to keep it.
* If you don't use `env` in your Babel config, you can remove it.
*
* Keep in mind that if you remove it you should set `BABEL_ENV` or `NODE_ENV`
* to `development` or `production`. Otherwise your production code might be compiled with
* in development mode by Babel.
*/
process.env.BABEL_ENV = mode;
return {
mode,
/**
* This should be always `false`, since the Source Map configuration is done
* by `SourceMapDevToolPlugin`.
*/
devtool: false,
context,
/**
* `getInitializationEntries` will return necessary entries with setup and initialization code.
* If you don't want to use Hot Module Replacement, set `hmr` option to `false`. By default,
* HMR will be enabled in development mode.
*/
entry: [
...Repack.getInitializationEntries(reactNativePath, {
2022-08-16 16:48:10 +05:00
hmr: devServer && devServer.hmr,
2022-07-06 12:17:08 +05:00
}),
2022-08-16 16:48:10 +05:00
entry,
2022-07-06 12:17:08 +05:00
],
resolve: {
/**
* `getResolveOptions` returns additional resolution configuration for React Native.
* If it's removed, you won't be able to use `<file>.<platform>.<ext>` (eg: `file.ios.js`)
* convention and some 3rd-party libraries that specify `react-native` field
* in their `package.json` might not work correctly.
*/
2022-08-16 16:48:10 +05:00
...Repack.getResolveOptions(platform),
2022-07-06 12:17:08 +05:00
/**
* Uncomment this to ensure all `react-native*` imports will resolve to the same React Native
* dependency. You might need it when using workspaces/monorepos or unconventional project
* structure. For simple/typical project you won't need it.
*/
2022-08-29 16:19:17 +05:00
alias: {
'react-native': reactNativePath,
2022-08-29 16:19:17 +05:00
"react": path.join(__dirname, "../node_modules/react"),
"react-dom": path.join(__dirname, "../node_modules/react-dom"),
"@notesnook": path.join(__dirname, "../../../packages"),
2022-10-19 11:40:38 +05:00
"@streetwriters/showdown": path.join(__dirname, "../node_modules/@streetwriters/showdown"),
"qclone": path.join(__dirname, "../node_modules/qclone"),
2023-04-17 22:37:29 +05:00
"@notifee/react-native": path.join(__dirname, "../node_modules/@ammarahmed/notifee-react-native"),
"html-to-text": path.join(__dirname, "../node_modules/html-to-text"),
"leac": path.join(__dirname, "../node_modules/leac"),
"parseley": path.join(__dirname, "../node_modules/parseley"),
"htmlparser2": path.join(__dirname, "../node_modules/htmlparser2"),
"selderee": path.join(__dirname, "../node_modules/selderee"),
"minimist": path.join(__dirname, "../node_modules/minimist"),
"entities": path.join(__dirname, "../node_modules/entities"),
"deepmerge": path.join(__dirname, "../node_modules/deepmerge"),
"@selderee/plugin-htmlparser2": path.join(__dirname, "../node_modules/@selderee/plugin-htmlparser2"),
"peberminta": path.join(__dirname, "../node_modules/peberminta"),
"react-native-blob-util": path.join(__dirname, "../node_modules/react-native-blob-util"),
"@mdi/js": path.join(__dirname, "../node_modules/@mdi/js/mdi.js")
2022-08-29 16:19:17 +05:00
},
2022-07-06 12:17:08 +05:00
},
/**
* Configures output.
* It's recommended to leave it as it is unless you know what you're doing.
* By default Webpack will emit files into the directory specified under `path`. In order for the
* React Native app use them when bundling the `.ipa`/`.apk`, they need to be copied over with
* `Repack.OutputPlugin`, which is configured by default inside `Repack.RepackPlugin`.
*/
output: {
clean: true,
2022-08-16 16:48:10 +05:00
path: path.join(__dirname, "build/generated", platform),
filename: "index.bundle",
chunkFilename: "[name].chunk.bundle",
publicPath: Repack.getPublicPath({ platform, devServer }),
2022-07-06 12:17:08 +05:00
},
/**
* Configures optimization of the built bundle.
*/
optimization: {
/** Enables minification based on values passed from React Native CLI or from fallback. */
minimize,
/** Configure minimizer to process the bundle. */
minimizer: [
new TerserPlugin({
test: /\.(js)?bundle(\?.*)?$/i,
/**
* Prevents emitting text file with comments, licenses etc.
* If you want to gather in-file licenses, feel free to remove this line or configure it
* differently.
*/
extractComments: false,
terserOptions: {
format: {
2022-08-16 16:48:10 +05:00
comments: false,
},
},
}),
2022-07-06 12:17:08 +05:00
],
2022-08-16 16:48:10 +05:00
chunkIds: "named",
2022-07-06 12:17:08 +05:00
},
module: {
/**
* This rule will process all React Native related dependencies with Babel.
* If you have a 3rd-party dependency that you need to transpile, you can add it to the
* `include` list.
*
* You can also enable persistent caching with `cacheDirectory` - please refer to:
* https://github.com/babel/babel-loader#options
*/
rules: [
{
2022-07-08 18:31:27 +05:00
test: /\.mjs$|cjs$|js$|jsx$|ts$|tsx$/,
2022-07-06 12:17:08 +05:00
include: [
/node_modules(.*[/\\])+react/,
/node_modules(.*[/\\])+@react-native/,
/node_modules(.*[/\\])+@react-navigation/,
/node_modules(.*[/\\])+@react-native-community/,
/node_modules(.*[/\\])+@expo/,
/node_modules(.*[/\\])+pretty-format/,
/node_modules(.*[/\\])+metro/,
/node_modules(.*[/\\])+abort-controller/,
/node_modules(.*[/\\])+@callstack[/\\]repack/,
/node_modules(.*[/\\])+pretty-format/,
/node_modules(.*[/\\])+@react-native-masked-view\/masked-view/,
/node_modules(.*[/\\])+toggle-switch-react-native/,
/node_modules(.*[/\\])+rn-fetch-blob/,
2022-10-19 11:40:38 +05:00
/node_modules(.*[/\\])+@notesnook[/\\]core/,
2022-07-06 12:17:08 +05:00
/node_modules(.*[/\\])+@microsoft/,
/node_modules(.*[/\\])+@msgpack/,
2022-07-08 18:31:27 +05:00
/node_modules(.*[/\\])+liqe/,
2023-04-26 22:29:42 +05:00
/node_modules(.*[/\\])+leac/,
2022-07-08 18:31:27 +05:00
/node_modules(.*[/\\])+selderee/,
/node_modules(.*[/\\])+html-to-text/,
/node_modules(.*[/\\])+buffer/,
2022-07-15 19:14:40 +05:00
/node_modules(.*[/\\])+readable-stream/,
2022-07-20 12:54:02 +05:00
/node_modules(.*[/\\])+react-native-fingerprint-scanner/,
2022-10-19 11:40:38 +05:00
/node_modules(.*[/\\])+@notesnook[/\\]logger/,
2023-01-14 11:18:43 +05:00
/node_modules(.*[/\\])+@ammarahmed[/\\]notifee-react-native/,
global: implement the new theme engine (#2196) * mobile: theme * theme: add theme engine * mobile: migrate app colors to new theme engine * mobile: fixed some colors * mobile: fix colors * mobile: store theme info in store * theme: `ColorsType` -> `Variants` * theme: use explicit return type for `useThemeColors` * theme: add `backdrop` color * mobile: `const colors` -> `const {colors} * theme: add default pitch-black theme * mobile: manage theme state via theme-engine * mobile: add theme scopes * mobile: commit * mobile: fix button width on applock screen * mobile: fix typings * mobile: fix theme definition * web: add partial support for custom themes only context menus & popups are left. * theme: add dialog & sheet scopes * global: sync with master branch and make everything work again * mobile: fix theme-engine usage in editor & app * mobile: fix colors * mobile: fix colors * mobile: cleanup * mobile: fix status bar color incorrect on entering foreground * mobile: fix dark color scheme * web: move emotion theme provider to @notesnook/theme * editor: add support for theme enging * web: adjust hover & focus colors on list item * mobile: migrate share ext to theme engine * mobile: fix editor theme provider * clipper: add support for the new theme engine * mobile: fix statusbar color on switch from bg * misc: fix build * mobile: fix build * misc: fix colors * mobile: fix theme colors * mobile: fix bottom padding * server: add theme server * theme: add previewColors * server: support themes query pagination * mobile: add client from theme server * server: reset cache on sync repo * server: fix types * server: show ip & port on start server * server: theme updates * web: finalize new theme engine on web * editor: fix build * global: fix @emotion/react version to 11.11.1 * editor: update katex patch * web: fix imports * global: fix @trpc/* versions * global: a huge set of changes 1. get rid of ThemeVariant. All variants can now be accessed anywhere. 2. remove unnecessary button variants 3. make buttons more responsive 4. implement themes server * web: add support for theme search and theme switching * global: update lockfiles * mobile: fix error * theme: use vite-plugin-react to start theme server * web: add support for auto updating themes * mobile: update theme selector * mobile: update theme if new verison available * theme: add `isomorphic-fetch` package * global: update lockfiles * web: add theme details dialog * setup: add scope for themes server in bootstrap script * web: add production server url * web: update lockfile * web: update lockfile * mobile: remove `react-native-blob-util` * web: add support for endless scrolling in themes * web: bring back dark/light mode option in settings * web: fix colors in places * theme: add selected variant * global: use single typescript version across the projects * web: fix sort & group options not having submenus * web: apply selected variant where appropriate * ui: use unique id for all menu items * config: add ui scope for commits * theme: export button variant creation fn * web: fix only 1 theme showing in theme selector * web: fix navigation item hover & other colors * mobile: update theme * editor: fix toolbar group alignments * editor: set theme provider at app level * theme: use scope name to get current scope * mobile: fix color usage in message card * theme: remove caching * editor: bring back icons in table menus * theme: use zustand to manage theme engine state * web: fix login/signup theming * mobile: fix webpack build * misc: remove ThemeProvider usage * editor: adjust theming and styling of editor toolbar * mobile: refactor * editor: fix toolbar group padding everywhere * web: fix settings sidebar is not scrollable * web: add loading indicator for themes loading * mobile: fix warning * mobile: fix ui issues * web: fix Loader errors on build * theme: add getPreviewColors & validateTheme * theme: fix theme validation * mobile: load theme from file * mobile: fix share extension crash * mobile: rename state * theme: add sourceURL property * theme: refactor theme-engine * web: add support for loading theme from file * web: improve button hover interaction * mobile: fix floating button color * mobile: update theme * mobile: fix border radius of context menu * mobile: set sheet overlay color to theme backdrop * mobile: set sidemenu backdrop to theme backdrop --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2023-08-01 12:07:21 +05:00
/node_modules(.*[/\\])+@trpc[/\\]client/,
/node_modules(.*[/\\])+@trpc[/\\]server/,
/node_modules(.*[/\\])+@tanstack[/\\]query-core/,
/node_modules(.*[/\\])+@tanstack[/\\]react-query/,
/node_modules(.*[/\\])+@trpc[/\\]react-query/,
2022-07-06 12:17:08 +05:00
],
use: {
2022-08-16 16:48:10 +05:00
loader: "babel-loader",
2022-07-06 12:17:08 +05:00
options: {
configFile: false,
2022-08-16 16:48:10 +05:00
cacheDirectory: path.join(
__dirname,
"node_modules/.webpack-cache"
),
2022-07-06 12:17:08 +05:00
babelrc: false,
2022-08-16 16:48:10 +05:00
presets: ["module:metro-react-native-babel-preset"],
2022-07-06 12:17:08 +05:00
plugins: [
2022-08-16 16:48:10 +05:00
"react-native-reanimated/plugin",
"@babel/plugin-transform-named-capturing-groups-regex",
],
},
},
2022-07-06 12:17:08 +05:00
},
/**
* Here you can adjust loader that will process your files.
*
* You can also enable persistent caching with `cacheDirectory` - please refer to:
* https://github.com/babel/babel-loader#options
*/
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: {
2022-08-16 16:48:10 +05:00
loader: "babel-loader",
2022-07-06 12:17:08 +05:00
options: {
/** Add React Refresh transform only when HMR is enabled. */
configFile: false,
2022-08-16 16:48:10 +05:00
cacheDirectory: path.join(
__dirname,
"node_modules/.webpack-cache"
),
2022-07-06 12:17:08 +05:00
babelrc: false,
2022-08-16 16:48:10 +05:00
presets: [["module:metro-react-native-babel-preset"]],
2022-07-06 12:17:08 +05:00
plugins:
devServer && devServer.hmr
2022-08-16 16:48:10 +05:00
? [
"module:react-refresh/babel",
"react-native-reanimated/plugin",
]
2022-07-06 12:17:08 +05:00
: [
2022-08-16 16:48:10 +05:00
"react-native-reanimated/plugin",
2022-07-06 16:26:33 +05:00
`@babel/plugin-transform-named-capturing-groups-regex`,
2023-07-13 09:55:02 +05:00
//"transform-remove-console",
2022-08-16 16:48:10 +05:00
],
},
},
2022-07-06 12:17:08 +05:00
},
/**
* This loader handles all static assets (images, video, audio and others), so that you can
* use (reference) them inside your application.
*
* If you wan to handle specific asset type manually, filter out the extension
* from `ASSET_EXTENSIONS`, for example:
* ```
* Repack.ASSET_EXTENSIONS.filter((ext) => ext !== 'svg')
* ```
*/
{
test: Repack.getAssetExtensionsRegExp(
2022-08-16 16:48:10 +05:00
Repack.ASSET_EXTENSIONS.filter((ext) => ext !== "svg")
2022-07-06 12:17:08 +05:00
),
use: {
2022-08-16 16:48:10 +05:00
loader: "@callstack/repack/assets-loader",
2022-07-06 12:17:08 +05:00
options: {
platform,
devServerEnabled: Boolean(devServer),
/**
* Defines which assets are scalable - which assets can have
* scale suffixes: `@1x`, `@2x` and so on.
* By default all images are scalable.
*/
2022-08-16 16:48:10 +05:00
scalableAssetExtensions: Repack.SCALABLE_ASSETS,
},
},
2022-07-06 12:17:08 +05:00
},
{
test: /\.svg$/,
use: [
{
2022-08-16 16:48:10 +05:00
loader: "@svgr/webpack",
2022-07-06 12:17:08 +05:00
options: {
native: true,
2022-08-16 16:48:10 +05:00
dimensions: false,
},
},
],
},
],
2022-07-06 12:17:08 +05:00
},
plugins: [
/**
* Configure other required and additional plugins to make the bundle
* work in React Native and provide good development experience with
* sensible defaults.
*
* `Repack.RepackPlugin` provides some degree of customization, but if you
* need more control, you can replace `Repack.RepackPlugin` with plugins
* from `Repack.plugins`.
*/
new Repack.RepackPlugin({
context,
mode,
platform,
devServer,
output: {
bundleFilename,
sourceMapFilename,
2022-08-16 16:48:10 +05:00
assetsPath,
2022-07-06 12:17:08 +05:00
},
extraChunks: [
{
2022-08-16 16:48:10 +05:00
type: "local",
include: /.*/,
},
],
}),
],
2022-07-06 12:17:08 +05:00
};
};