mirror of
https://github.com/embarklabs/embark.git
synced 2025-01-11 14:24:24 +00:00
7a93e4b6a7
TL;DR ===== `yarn install` in a fresh clone of the repo. `yarn reboot` when switching branches. When pulling in these changes, there may be untracked files at the root in all/some of: ``` .embark/ .nyc_output/ coverage/ dist/ embark-ui/ test_apps/ ``` They can be safely deleted since those paths are no longer in use at the root. Many of the scripts in the top-level `package.json` support Lerna's [filter options]. For example: `yarn build --scope embark` build only `packages/embark`. `yarn build --ignore embark-ui` build everything except `packages/embark-ui`. Scoping scripts will be more useful when there are more packages in the monorepo and, for example, `yarn start` doesn't need to be invoked for all of them while working on just a few of them simultaneously, e.g `embark` and `embarkjs`. It's also possible to `cd` into a particular package and run its scripts directly: ``` cd packages/embark && yarn watch ``` Hot Topics & Questions ====================== What should be done about the [README][embark-readme] for `packages/embark`? Should the top-level README be duplicated in that package? Lerna is setup to use [Fixed/Locked mode][fixed-locked], and accordingly `packages/embark-ui` is set to `4.0.0-beta.0`. The same will be true when adding embarkjs, swarm-api, etc. to the monorepo. Is this acceptable or do we want to use [Independent mode][independent]? Scripts ======= If a package doesn't have a matching script, `lerna run` skips it automatically. For example, `packages/embark-ui` doesn't have a `typecheck` script. `yarn build` ------------ Runs babel, webpack, etc. according to a package's `build` script. `yarn build:no-ui` is a shortcut for `yarn build --ignore embark-ui`. `yarn ci` --------- Runs a series of scripts relevant in a CI context according to a package's `ci` script. For `packages/embark` that's `lint typecheck build test package`. Also runs the `ci` script of the embedded `test_dapps` monorepo. `yarn clean` ------------ Runs rimraf, etc. according to a package's `clean` script. `yarn globalize` ---------------- Makes the development embark available on the global PATH, either via symlink (Linux, macOS) or a shim script (Windows). `yarn lint` ----------- Runs eslint, etc. according to a package's `lint` script. `yarn package` -------------- Invokes `npm pack` according to a package's `package` script. `yarn qa` --------- Very similar to `ci`, runs a series of scripts according to a package's `qa` script. The big difference between `ci` and `qa` is that at the top-level `qa` first kicks off `reboot:full`. There is a `preqa` script ([invoked automatically][npm-scripts]), which is a bit of a wart. It makes sure that `embark reset` can be run successfully in `packages/embark/templates/*` when the `reboot` script invokes the `reset` script. The `qa` script is invoked by `yarn release` before the latter proceeds to invoke `lerna publish`. `yarn reboot` ------------- Invokes the `reset` script and then does `yarn install`. The `reboot:full` variant invokes `reset:full` and then does `yarn install`. `yarn release` -------------- Works in concert with [lerna publish], which will prompt to verify the version before proceeding. Use `n` to cancel instead of `ctrl-c` as `lerna publish` has been seen to occasionally misbehave when not exited cleanly (e.g. creating a tag when it shouldn't have). ``` yarn release [bump] [--options] ``` * `[bump]` see [`publish` positionals][pub-pos] and [`version` positionals][ver-pos]; an exact version can also be specified. * `--preid` prerelease identifier, e.g. `beta`; when doing a prerelease bump will default to whatever identifier is currently in use. * `--dist-tag` registry distribution tag, defaults to `latest`. * `--message` commit message format, defaults to `chore(release): %v`. * `--sign` indicates that the git commit and tag should be signed; not signed by default. * `--release-branch` default is `master`; must match the current branch. * `--git-remote` default is `origin`. * `--registry` default is `https://registry.npmjs.org/` per the top-level [`lerna.json`][lerna-json]. To release `4.0.0-beta.1` as `embark@next` (assuming version is currently at `4.0.0-beta.0`) could do: ``` yarn release prerelease --dist-tag next ``` For *test releases* (there is no longer a `--dry-run` option) [verdaccio] and a filesystem git remote can be used. Condensend instructions: ``` mkdir -p ~/temp/clones && cd ~/temp/clones git clone git@github.com:embark-framework/embark.git cd ~/repos/embark git remote add FAKEembark ~/temp/clones/embark ``` in another terminal: ``` npm i -g verdaccio && verdaccio ``` in the first terminal: ``` yarn release --git-remote FAKEembark --registry http://localhost:4873/ ``` `yarn reset` ------------ Invokes cleaning and resetting steps according to a package's `reset` script. The big difference between `clean` and `reset` is that `reset` is intended to delete a package's `node_modules`. The `reset:full` variant deletes the monorepo's top-level `node_modules` at the end. That shouldn't be necessary too often, e.g. in day-to-day work when switching branches, which is why there is `reboot` / `reset` vs. `reboot:full` / `reset:full`. Errors may be seen related to invocation of `embark reset` if embark is not built, but `reset` will still complete successfully. `yarn start` ------------ Runs babel, webpack, tsc, etc. (in parallel, in watch mode) according to a package's `start` script. `yarn test` ----------- Run mocha, etc. according to a package's `test` script. The `test:full` variant runs a series of scripts: `lint typecheck test test_dapps`. `yarn test_dapps` ----------------- Runs the `test` script of the embedded `test_dapps` monorepo. The `test_dapps:ci` and `test_dapps:qa` variants run the `ci` and `qa` scripts of the embedded `test_dapps` monorepo, respectively. `yarn typecheck` ---------------- Runs tsc, etc. according to a package's `typecheck` script. Notes ===== `npx` is used in some of the top-level and package scripts to ensure the scripts can run even if `node_modules` is missing. [`"nohoist"`][nohoist] specifies a couple of embark packages because [`restrictPath`][restrictpath] is interfering with access to modules that are located in a higher-up `node_modules`. All dependencies in `packages/embark-ui` have been made `devDependencies` since its production build is self-contained. `packages/embark`'s existing CHANGELOG's formatting has been slightly adjusted to match the formatting that Lerna will use going forward (entries in the log haven't been modified). Lerna will generate a CHANGELOG at the top-level and in each package. Since we're transitioning to a monorepo, things may look a little wonky with respect to old entries in `packages/embark/CHANGELOG.md` and going forward we need to consider how scoping our commits corresponds to member-packages of the monorepo. In `packages/embark`, `test` invokes `scripts/test`, which starts a child process wherein `process.env.DAPP_PATH` is a temporary path that has all of `packages/embark/dist/test` copied into it, so that paths to test helpers/fixtures don't need to be prefixed with `dist/test/` and so that a `.embark` directory doesn't get written into `packages/embark`. The `"engines"` specified in top-level and packages' `package.json` reflect a node and npm pair that match (a source of confusion in the past). The pair was chosen according to the first post v5 npm that's bundled with node. A `"runtime"` key/object has been introduced in `packages/embark/package.json` which is used as the basis for specifying the minimum version of node that can be used to run embark, and that's what is checked by `bin/embark`. Some changes have been introduced, e.g. in `lib/core/config` and `lib/utils/solidity/remapImports` so that it's *not* implicitly assumed that `process.env.DAPP_PATH` / `fs.dappPath()` are the same as `process.cwd()`. There are probably several++ places where that assumption is still in effect, and we should work to identify and correct them. `embark reset` now deletes `embarkArtifacts/` within a dapp root, and `embarkArtifacts/` is git-ignored. `lib/core/env` adds all `node_modules` relative to `process.env.EMBARK_PATH` to `NODE_PATH` so that embark's modules can be resolved as expected whether embark's `node_modules` have been deduped or are installed in npm's flat "global style". `checkDependencies` has been inlined (see `lib/utils/checkDependencies`) and slightly modified to support dependencies that have been hoisted into a higher-up `node_modules`, e.g. as part of a yarn workspace. eslint has been disabled for that script to avoid more involved changes to it. `test_apps` is not in `packages/embark`; rather, there is `test_dapps` at the top-level of the monorepo. `test_dapps` is an embedded monorepo, and its `ci` / `qa` scripts `npm install` embark from freshly built tarballs of the packages in the outer monorepo and then use that installation to run `embark test` in the dapps. This should allow us to rapidly detect breakage related to auto-bumps in transitive dependencies. [filter options]: https://github.com/lerna/lerna/tree/master/core/filter-options [embark-readme]: https://github.com/embark-framework/embark/blob/build/lerna/packages/embark/README.md [fixed-locked]: https://github.com/lerna/lerna#fixedlocked-mode-default [independent]: https://github.com/lerna/lerna#independent-mode [npm-scripts]: https://docs.npmjs.com/misc/scripts [lerna publish]: https://github.com/lerna/lerna/tree/master/commands/publish [pub-pos]: https://github.com/lerna/lerna/tree/master/commands/publish#positionals [ver-pos]: https://github.com/lerna/lerna/tree/master/commands/version#positionals [lerna-json]: https://github.com/embark-framework/embark/blob/build/lerna/lerna.json#L11 [verdaccio]: https://www.npmjs.com/package/verdaccio [nohoist]: https://github.com/embark-framework/embark/blob/build/lerna/package.json#L52-L55 [restrictpath]: https://github.com/embark-framework/embark/blob/build/lerna/packages/embark/src/lib/core/fs.js#L9
541 lines
22 KiB
JavaScript
541 lines
22 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const webpack = require('webpack');
|
|
const resolve = require('resolve');
|
|
const PnpWebpackPlugin = require('pnp-webpack-plugin');
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
|
const safePostCssParser = require('postcss-safe-parser');
|
|
const ManifestPlugin = require('webpack-manifest-plugin');
|
|
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
|
|
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
|
|
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
|
|
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
|
|
const paths = require('./paths');
|
|
const getClientEnvironment = require('./env');
|
|
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
|
|
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin-alt');
|
|
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
|
|
|
|
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
|
|
|
|
// Webpack uses `publicPath` to determine where the app is being served from.
|
|
// It requires a trailing slash, or the file assets will get an incorrect path.
|
|
const publicPath = paths.servedPath;
|
|
// Some apps do not use client-side routing with pushState.
|
|
// For these, "homepage" can be set to "." to enable relative asset paths.
|
|
const shouldUseRelativeAssetPaths = publicPath === './';
|
|
// Source maps are resource heavy and can cause out of memory issue for large source files.
|
|
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
|
|
// Some apps do not need the benefits of saving a web request, so not inlining the chunk
|
|
// makes for a smoother build process.
|
|
const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
|
|
// `publicUrl` is just like `publicPath`, but we will provide it to our app
|
|
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
|
|
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
|
|
const publicUrl = publicPath.slice(0, -1);
|
|
// Get environment variables to inject into our app.
|
|
const env = getClientEnvironment(publicUrl);
|
|
|
|
// Assert this just to be safe.
|
|
// Development builds of React are slow and not intended for production.
|
|
if (env.stringified['process.env'].NODE_ENV !== '"production"') {
|
|
throw new Error('Production builds must have NODE_ENV=production.');
|
|
}
|
|
|
|
// Check if TypeScript is setup
|
|
const useTypeScript = fs.existsSync(paths.appTsConfig);
|
|
|
|
// style files regexes
|
|
const cssRegex = /\.css$/;
|
|
const cssModuleRegex = /\.module\.css$/;
|
|
const sassRegex = /\.(scss|sass)$/;
|
|
const sassModuleRegex = /\.module\.(scss|sass)$/;
|
|
|
|
// common function to get style loaders
|
|
const getStyleLoaders = (cssOptions, preProcessor) => {
|
|
const loaders = [
|
|
{
|
|
loader: MiniCssExtractPlugin.loader,
|
|
options: Object.assign(
|
|
{},
|
|
shouldUseRelativeAssetPaths ? { publicPath: '../../' } : undefined
|
|
),
|
|
},
|
|
{
|
|
loader: require.resolve('css-loader'),
|
|
options: cssOptions,
|
|
},
|
|
{
|
|
// Options for PostCSS as we reference these options twice
|
|
// Adds vendor prefixing based on your specified browser support in
|
|
// package.json
|
|
loader: require.resolve('postcss-loader'),
|
|
options: {
|
|
// Necessary for external CSS imports to work
|
|
// https://github.com/facebook/create-react-app/issues/2677
|
|
ident: 'postcss',
|
|
plugins: () => [
|
|
require('postcss-flexbugs-fixes'),
|
|
require('postcss-preset-env')({
|
|
autoprefixer: {
|
|
flexbox: 'no-2009',
|
|
},
|
|
stage: 3,
|
|
}),
|
|
],
|
|
sourceMap: shouldUseSourceMap,
|
|
},
|
|
},
|
|
];
|
|
if (preProcessor) {
|
|
loaders.push({
|
|
loader: require.resolve(preProcessor),
|
|
options: {
|
|
sourceMap: shouldUseSourceMap,
|
|
},
|
|
});
|
|
}
|
|
return loaders;
|
|
};
|
|
|
|
// This is the production configuration.
|
|
// It compiles slowly and is focused on producing a fast and minimal bundle.
|
|
// The development configuration is different and lives in a separate file.
|
|
module.exports = {
|
|
mode: 'production',
|
|
// Don't attempt to continue if there are any errors.
|
|
bail: true,
|
|
// We generate sourcemaps in production. This is slow but gives good results.
|
|
// You can exclude the *.map files from the build during deployment.
|
|
devtool: shouldUseSourceMap ? 'source-map' : false,
|
|
// In production, we only want to load the app code.
|
|
entry: [paths.appIndexJs],
|
|
output: {
|
|
// The build folder.
|
|
path: paths.appBuild,
|
|
// Generated JS file names (with nested folders).
|
|
// There will be one main bundle, and one file per asynchronous chunk.
|
|
// We don't currently advertise code splitting but Webpack supports it.
|
|
filename: 'static/js/[name].[chunkhash:8].js',
|
|
chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
|
|
// We inferred the "public path" (such as / or /my-project) from homepage.
|
|
publicPath: publicPath,
|
|
// Point sourcemap entries to original disk location (format as URL on Windows)
|
|
devtoolModuleFilenameTemplate: info =>
|
|
path
|
|
.relative(paths.appSrc, info.absoluteResourcePath)
|
|
.replace(/\\/g, '/'),
|
|
},
|
|
optimization: {
|
|
minimizer: [
|
|
new TerserPlugin({
|
|
terserOptions: {
|
|
parse: {
|
|
// we want terser to parse ecma 8 code. However, we don't want it
|
|
// to apply any minfication steps that turns valid ecma 5 code
|
|
// into invalid ecma 5 code. This is why the 'compress' and 'output'
|
|
// sections only apply transformations that are ecma 5 safe
|
|
// https://github.com/facebook/create-react-app/pull/4234
|
|
ecma: 8,
|
|
},
|
|
compress: {
|
|
ecma: 5,
|
|
warnings: false,
|
|
// Disabled because of an issue with Uglify breaking seemingly valid code:
|
|
// https://github.com/facebook/create-react-app/issues/2376
|
|
// Pending further investigation:
|
|
// https://github.com/mishoo/UglifyJS2/issues/2011
|
|
comparisons: false,
|
|
// Disabled because of an issue with Terser breaking valid code:
|
|
// https://github.com/facebook/create-react-app/issues/5250
|
|
// Pending futher investigation:
|
|
// https://github.com/terser-js/terser/issues/120
|
|
inline: 2,
|
|
},
|
|
mangle: {
|
|
safari10: true,
|
|
},
|
|
output: {
|
|
ecma: 5,
|
|
comments: false,
|
|
// Turned on because emoji and regex is not minified properly using default
|
|
// https://github.com/facebook/create-react-app/issues/2488
|
|
ascii_only: true,
|
|
},
|
|
},
|
|
// Use multi-process parallel running to improve the build speed
|
|
// Default number of concurrent runs: os.cpus().length - 1
|
|
parallel: true,
|
|
// Enable file caching
|
|
cache: true,
|
|
sourceMap: shouldUseSourceMap,
|
|
}),
|
|
new OptimizeCSSAssetsPlugin({
|
|
cssProcessorOptions: {
|
|
parser: safePostCssParser,
|
|
map: shouldUseSourceMap
|
|
? {
|
|
// `inline: false` forces the sourcemap to be output into a
|
|
// separate file
|
|
inline: false,
|
|
// `annotation: true` appends the sourceMappingURL to the end of
|
|
// the css file, helping the browser find the sourcemap
|
|
annotation: true,
|
|
}
|
|
: false,
|
|
},
|
|
}),
|
|
],
|
|
// Automatically split vendor and commons
|
|
// https://twitter.com/wSokra/status/969633336732905474
|
|
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
|
|
splitChunks: {
|
|
chunks: 'all',
|
|
name: false,
|
|
},
|
|
// Keep the runtime chunk seperated to enable long term caching
|
|
// https://twitter.com/wSokra/status/969679223278505985
|
|
runtimeChunk: true,
|
|
},
|
|
resolve: {
|
|
// This allows you to set a fallback for where Webpack should look for modules.
|
|
// We placed these paths second because we want `node_modules` to "win"
|
|
// if there are any conflicts. This matches Node resolution mechanism.
|
|
// https://github.com/facebook/create-react-app/issues/253
|
|
modules: ['node_modules'].concat(
|
|
// It is guaranteed to exist because we tweak it in `env.js`
|
|
process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
|
|
),
|
|
// These are the reasonable defaults supported by the Node ecosystem.
|
|
// We also include JSX as a common component filename extension to support
|
|
// some tools, although we do not recommend using it, see:
|
|
// https://github.com/facebook/create-react-app/issues/290
|
|
// `web` extension prefixes have been added for better support
|
|
// for React Native Web.
|
|
extensions: paths.moduleFileExtensions
|
|
.map(ext => `.${ext}`)
|
|
.filter(ext => useTypeScript || !ext.includes('ts')),
|
|
alias: {
|
|
// Support React Native Web
|
|
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
|
|
'react-native': 'react-native-web',
|
|
},
|
|
plugins: [
|
|
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
|
|
// guards against forgotten dependencies and such.
|
|
PnpWebpackPlugin,
|
|
// Prevents users from importing files from outside of src/ (or node_modules/).
|
|
// This often causes confusion because we only process files within src/ with babel.
|
|
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
|
|
// please link the files into your node_modules/ and let module-resolution kick in.
|
|
// Make sure your source files are compiled, as they will not be processed in any way.
|
|
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
|
|
],
|
|
},
|
|
resolveLoader: {
|
|
plugins: [
|
|
// Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
|
|
// from the current package.
|
|
PnpWebpackPlugin.moduleLoader(module),
|
|
],
|
|
},
|
|
module: {
|
|
strictExportPresence: true,
|
|
rules: [
|
|
// Disable require.ensure as it's not a standard language feature.
|
|
{ parser: { requireEnsure: false } },
|
|
|
|
// First, run the linter.
|
|
// It's important to do this before Babel processes the JS.
|
|
{
|
|
test: /\.(js|mjs|jsx)$/,
|
|
enforce: 'pre',
|
|
use: [
|
|
{
|
|
options: {
|
|
formatter: require.resolve('react-dev-utils/eslintFormatter'),
|
|
eslintPath: require.resolve('eslint'),
|
|
|
|
},
|
|
loader: require.resolve('eslint-loader'),
|
|
},
|
|
],
|
|
include: paths.appSrc,
|
|
},
|
|
{
|
|
// "oneOf" will traverse all following loaders until one will
|
|
// match the requirements. When no loader matches it will fall
|
|
// back to the "file" loader at the end of the loader list.
|
|
oneOf: [
|
|
// "url" loader works just like "file" loader but it also embeds
|
|
// assets smaller than specified size as data URLs to avoid requests.
|
|
{
|
|
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
|
|
loader: require.resolve('url-loader'),
|
|
options: {
|
|
limit: 10000,
|
|
name: 'static/media/[name].[hash:8].[ext]',
|
|
},
|
|
},
|
|
// Process application JS with Babel.
|
|
// The preset includes JSX, Flow, TypeScript and some ESnext features.
|
|
{
|
|
test: /\.(js|mjs|jsx|ts|tsx)$/,
|
|
include: paths.appSrc,
|
|
|
|
loader: require.resolve('babel-loader'),
|
|
options: {
|
|
customize: require.resolve(
|
|
'babel-preset-react-app/webpack-overrides'
|
|
),
|
|
|
|
plugins: [
|
|
[
|
|
require.resolve('babel-plugin-named-asset-import'),
|
|
{
|
|
loaderMap: {
|
|
svg: {
|
|
ReactComponent: '@svgr/webpack?-prettier,-svgo![path]',
|
|
},
|
|
},
|
|
},
|
|
],
|
|
],
|
|
cacheDirectory: true,
|
|
// Save disk space when time isn't as important
|
|
cacheCompression: true,
|
|
compact: true,
|
|
},
|
|
},
|
|
// Process any JS outside of the app with Babel.
|
|
// Unlike the application JS, we only compile the standard ES features.
|
|
{
|
|
test: /\.(js|mjs)$/,
|
|
exclude: /@babel(?:\/|\\{1,2})runtime/,
|
|
loader: require.resolve('babel-loader'),
|
|
options: {
|
|
babelrc: false,
|
|
configFile: false,
|
|
compact: false,
|
|
presets: [
|
|
[
|
|
require.resolve('babel-preset-react-app/dependencies'),
|
|
{ helpers: true },
|
|
],
|
|
],
|
|
cacheDirectory: true,
|
|
// Save disk space when time isn't as important
|
|
cacheCompression: true,
|
|
|
|
// If an error happens in a package, it's possible to be
|
|
// because it was compiled. Thus, we don't want the browser
|
|
// debugger to show the original code. Instead, the code
|
|
// being evaluated would be much more helpful.
|
|
sourceMaps: false,
|
|
},
|
|
},
|
|
// "postcss" loader applies autoprefixer to our CSS.
|
|
// "css" loader resolves paths in CSS and adds assets as dependencies.
|
|
// `MiniCSSExtractPlugin` extracts styles into CSS
|
|
// files. If you use code splitting, async bundles will have their own separate CSS chunk file.
|
|
// By default we support CSS Modules with the extension .module.css
|
|
{
|
|
test: cssRegex,
|
|
exclude: cssModuleRegex,
|
|
loader: getStyleLoaders({
|
|
importLoaders: 1,
|
|
sourceMap: shouldUseSourceMap,
|
|
}),
|
|
// Don't consider CSS imports dead code even if the
|
|
// containing package claims to have no side effects.
|
|
// Remove this when webpack adds a warning or an error for this.
|
|
// See https://github.com/webpack/webpack/issues/6571
|
|
sideEffects: true,
|
|
},
|
|
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
|
|
// using the extension .module.css
|
|
{
|
|
test: cssModuleRegex,
|
|
loader: getStyleLoaders({
|
|
importLoaders: 1,
|
|
sourceMap: shouldUseSourceMap,
|
|
modules: true,
|
|
getLocalIdent: getCSSModuleLocalIdent,
|
|
}),
|
|
},
|
|
// Opt-in support for SASS. The logic here is somewhat similar
|
|
// as in the CSS routine, except that "sass-loader" runs first
|
|
// to compile SASS files into CSS.
|
|
// By default we support SASS Modules with the
|
|
// extensions .module.scss or .module.sass
|
|
{
|
|
test: sassRegex,
|
|
exclude: sassModuleRegex,
|
|
loader: getStyleLoaders(
|
|
{
|
|
importLoaders: 2,
|
|
sourceMap: shouldUseSourceMap,
|
|
},
|
|
'sass-loader'
|
|
),
|
|
// Don't consider CSS imports dead code even if the
|
|
// containing package claims to have no side effects.
|
|
// Remove this when webpack adds a warning or an error for this.
|
|
// See https://github.com/webpack/webpack/issues/6571
|
|
sideEffects: true,
|
|
},
|
|
// Adds support for CSS Modules, but using SASS
|
|
// using the extension .module.scss or .module.sass
|
|
{
|
|
test: sassModuleRegex,
|
|
loader: getStyleLoaders(
|
|
{
|
|
importLoaders: 2,
|
|
sourceMap: shouldUseSourceMap,
|
|
modules: true,
|
|
getLocalIdent: getCSSModuleLocalIdent,
|
|
},
|
|
'sass-loader'
|
|
),
|
|
},
|
|
// "file" loader makes sure assets end up in the `build` folder.
|
|
// When you `import` an asset, you get its filename.
|
|
// This loader doesn't use a "test" so it will catch all modules
|
|
// that fall through the other loaders.
|
|
{
|
|
loader: require.resolve('file-loader'),
|
|
// Exclude `js` files to keep "css" loader working as it injects
|
|
// it's runtime that would otherwise be processed through "file" loader.
|
|
// Also exclude `html` and `json` extensions so they get processed
|
|
// by webpacks internal loaders.
|
|
exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
|
|
options: {
|
|
name: 'static/media/[name].[hash:8].[ext]',
|
|
},
|
|
},
|
|
// ** STOP ** Are you adding a new loader?
|
|
// Make sure to add the new loader(s) before the "file" loader.
|
|
],
|
|
},
|
|
],
|
|
},
|
|
plugins: [
|
|
// Generates an `index.html` file with the <script> injected.
|
|
new HtmlWebpackPlugin({
|
|
inject: true,
|
|
template: paths.appHtml,
|
|
minify: {
|
|
removeComments: true,
|
|
collapseWhitespace: true,
|
|
removeRedundantAttributes: true,
|
|
useShortDoctype: true,
|
|
removeEmptyAttributes: true,
|
|
removeStyleLinkTypeAttributes: true,
|
|
keepClosingSlash: true,
|
|
minifyJS: true,
|
|
minifyCSS: true,
|
|
minifyURLs: true,
|
|
},
|
|
}),
|
|
// Inlines the webpack runtime script. This script is too small to warrant
|
|
// a network request.
|
|
shouldInlineRuntimeChunk &&
|
|
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/]),
|
|
// Makes some environment variables available in index.html.
|
|
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
|
|
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
|
// In production, it will be an empty string unless you specify "homepage"
|
|
// in `package.json`, in which case it will be the pathname of that URL.
|
|
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
|
|
// This gives some necessary context to module not found errors, such as
|
|
// the requesting resource.
|
|
new ModuleNotFoundPlugin(paths.appPath),
|
|
// Makes some environment variables available to the JS code, for example:
|
|
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
|
|
// It is absolutely essential that NODE_ENV was set to production here.
|
|
// Otherwise React will be compiled in the very slow development mode.
|
|
new webpack.DefinePlugin(env.stringified),
|
|
new MiniCssExtractPlugin({
|
|
// Options similar to the same options in webpackOptions.output
|
|
// both options are optional
|
|
filename: 'static/css/[name].[contenthash:8].css',
|
|
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
|
|
}),
|
|
// Generate a manifest file which contains a mapping of all asset filenames
|
|
// to their corresponding output file so that tools can pick it up without
|
|
// having to parse `index.html`.
|
|
new ManifestPlugin({
|
|
fileName: 'asset-manifest.json',
|
|
publicPath: publicPath,
|
|
}),
|
|
// Moment.js is an extremely popular library that bundles large locale files
|
|
// by default due to how Webpack interprets its code. This is a practical
|
|
// solution that requires the user to opt into importing specific locales.
|
|
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
|
|
// You can remove this if you don't use Moment.js:
|
|
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
|
// Generate a service worker script that will precache, and keep up to date,
|
|
// the HTML & assets that are part of the Webpack build.
|
|
new WorkboxWebpackPlugin.GenerateSW({
|
|
clientsClaim: true,
|
|
exclude: [/\.map$/, /asset-manifest\.json$/],
|
|
importWorkboxFrom: 'cdn',
|
|
navigateFallback: publicUrl + '/index.html',
|
|
navigateFallbackBlacklist: [
|
|
// Exclude URLs starting with /_, as they're likely an API call
|
|
new RegExp('^/_'),
|
|
// Exclude URLs containing a dot, as they're likely a resource in
|
|
// public/ and not a SPA route
|
|
new RegExp('/[^/]+\\.[^/]+$'),
|
|
],
|
|
}),
|
|
// TypeScript type checking
|
|
fs.existsSync(paths.appTsConfig) &&
|
|
new ForkTsCheckerWebpackPlugin({
|
|
typescript: resolve.sync('typescript', {
|
|
basedir: paths.appNodeModules,
|
|
}),
|
|
async: false,
|
|
checkSyntacticErrors: true,
|
|
tsconfig: paths.appTsConfig,
|
|
compilerOptions: {
|
|
module: 'esnext',
|
|
moduleResolution: 'node',
|
|
resolveJsonModule: true,
|
|
isolatedModules: true,
|
|
noEmit: true,
|
|
jsx: 'preserve',
|
|
},
|
|
reportFiles: [
|
|
'**',
|
|
'!**/*.json',
|
|
'!**/__tests__/**',
|
|
'!**/?(*.)(spec|test).*',
|
|
'!src/setupProxy.js',
|
|
'!src/setupTests.*',
|
|
],
|
|
watch: paths.appSrc,
|
|
silent: true,
|
|
formatter: typescriptFormatter,
|
|
}),
|
|
|
|
new MonacoWebpackPlugin(),
|
|
].filter(Boolean),
|
|
// Some libraries import Node modules but don't use them in the browser.
|
|
// Tell Webpack to provide empty mocks for them so importing them works.
|
|
node: {
|
|
dgram: 'empty',
|
|
fs: 'empty',
|
|
net: 'empty',
|
|
tls: 'empty',
|
|
child_process: 'empty',
|
|
},
|
|
// Turn off performance processing because we utilize
|
|
// our own hints via the FileSizeReporter
|
|
performance: false,
|
|
};
|