Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

[webpack-config] Remove yup validation #2335

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/webpack-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ The main options used to configure how `@expo/webpack-config` works.
| `https` | `boolean` | `false` | Should the dev server use https protocol. |
| `offline` | `boolean` | `true` | Passing `false` will disable offline support and skip adding a service worker. |
| `mode` | `Mode` | required | The Webpack mode to bundle the project in. |
| `platform` | [`ExpoPlatform`](#ExpoPlatform) | required | The target platform to bundle for. Only `web` and `electron` are supported. |
| `removeUnusedImportExports` | `boolean` | `false` | Enables advanced tree-shaking with deep scope analysis. |
| `platform` | [`ExpoPlatform`](#ExpoPlatform) | required | The target platform to bundle for. |
| `pwa` | `boolean` | `true` | Generate the PWA image assets in production mode. |
| `babel` | [`ExpoBabelOptions`](#ExpoBabelOptions) | `undefined` | Control how the default Babel loader is configured. |
| `removeUnusedImportExports` | `boolean` | `false` | Enables advanced tree-shaking with deep scope analysis. |

### `Environment` internal

Expand All @@ -65,9 +65,9 @@ The main options used to configure how `@expo/webpack-config` works.

### `ExpoPlatform`

| type | description |
| ---------------------------------------- | --------------------------------------------------------------------------- |
| `'ios' | 'android' | 'web' | 'electron'` | The target platform to bundle for. Only `web` and `electron` are supported. |
| type | description |
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| `'ios' | 'android' | 'web' | 'electron'` | The target platform to bundle for. Native platforms are experimental and require a special native runtime. |

### `ExpoBabelOptions`

Expand Down
4 changes: 1 addition & 3 deletions packages/webpack-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"webpack-manifest-plugin": "^2.2.0",
"webpackbar": "^4.0.0",
"workbox-webpack-plugin": "^3.6.3",
"worker-loader": "^2.0.0",
"yup": "^0.27.0"
"worker-loader": "^2.0.0"
},
"devDependencies": {
"@expo/babel-preset-cli": "0.2.16",
Expand All @@ -85,7 +84,6 @@
"@types/webpack-dev-server": "3.10.1",
"@types/webpack-manifest-plugin": "^2.1.0",
"@types/workbox-webpack-plugin": "^4.1.0",
"@types/yup": "^0.26.24",
"jest-puppeteer": "^4.4.0",
"prettier": "^1.16.4",
"rimraf": "^3.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function normalizeConfig(config) {
}

// Make the paths be relative to the project
const normalized = normalizePaths(config, value => value.split('expo-cli/').pop());
const normalized = normalizePaths(config, value => value.split('cli/').pop());

// performance is disabled in CI
delete normalized.performance;
Expand Down
8 changes: 1 addition & 7 deletions packages/webpack-config/src/env/__tests__/validate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ it(`throws when the projectRoot isn't defined`, () => {

it(`throws when an invalid mode is provided`, () => {
expect(() => validateEnvironment({ projectRoot: '', mode: '-invalid-' })).toThrowError(
'mode must be one of the following values:'
);
});

it(`throws when an invalid platform is provided`, () => {
expect(() => validateEnvironment({ projectRoot: '', platform: '-invalid-' })).toThrowError(
'must be one of the following values:'
'requires a valid `mode` string which should be one of'
);
});

Expand Down
52 changes: 28 additions & 24 deletions packages/webpack-config/src/env/validate.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import chalk from 'chalk';
import * as yup from 'yup';

import { Environment, InputEnvironment } from '../types';
import { getPaths } from './paths';
import getConfig from './getConfig';

const environmentSchema = yup.object({
config: yup.object().notRequired(),
locations: yup.object().notRequired(),
https: yup.boolean().default(false),
polyfill: yup.boolean().notRequired(),
removeUnusedImportExports: yup.boolean().default(false),
pwa: yup.boolean().notRequired(),
offline: yup.boolean().notRequired(),
projectRoot: yup.string().required(),
mode: yup
.mixed<'production' | 'development' | 'none'>()
.oneOf(['production', 'development', 'none']),
platform: yup
.mixed<'ios' | 'android' | 'web' | 'electron'>()
.oneOf(['ios', 'android', 'web', 'electron'])
.default('web'),
});

/**
* Validate the environment options and apply default values.
*
* @param env
* @category env
Expand All @@ -36,17 +18,39 @@ export function validateEnvironment(env: InputEnvironment): Environment {
}
warnEnvironmentDeprecation(env, true);

const filledEnv: any = environmentSchema.validateSync(env);
const validModes = ['development', 'production', 'none'];
if (!env.mode || !validModes.includes(env.mode)) {
throw new Error(
`@expo/webpack-config requires a valid \`mode\` string which should be one of: ${validModes.join(
', '
)}`
);
}

// Default to web. Allow any arbitrary platform.
if (typeof env.platform === 'undefined') {
env.platform = 'web';
}
// No https by default since it doesn't work well across different browsers and devices.
if (typeof env.https === 'undefined') {
env.https = false;
}
// This is experimental and might be removed in the future.
if (typeof env.removeUnusedImportExports === 'undefined') {
env.removeUnusedImportExports = false;
}

// Ensure the locations are defined.
if (!env.locations) {
filledEnv.locations = getPaths(env.projectRoot, env);
env.locations = getPaths(env.projectRoot, env);
}

// Ensure the config is evaluated.
if (!env.config) {
filledEnv.config = getConfig(filledEnv);
env.config = getConfig(env as Environment);
}

return filledEnv;
return env as Environment;
}

let warned: { [key: string]: boolean } = {};
Expand All @@ -70,7 +74,7 @@ export function warnEnvironmentDeprecation(env: InputEnvironment, warnOnce: bool
const warnings: { [key: string]: string } = {
production: 'Please use `mode: "production"` instead.',
development: 'Please use `mode: "development"` instead.',
polyfill: '',
polyfill: 'Please include polyfills manually in your project.',
};

for (const warning of Object.keys(warnings)) {
Expand Down
7 changes: 0 additions & 7 deletions packages/webpack-config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export type InputEnvironment = {
development?: boolean;
config?: AnyObject;
locations?: FilePaths;
polyfill?: boolean;
mode?: Mode;
removeUnusedImportExports?: boolean;
pwa?: boolean;
Expand Down Expand Up @@ -84,12 +83,6 @@ export type Environment = {
* Control how the default Babel loader is configured.
*/
babel?: ExpoBabelOptions;
/**
* Includes all Babel polyfills.
*
* @deprecated
*/
polyfill?: boolean;
};

/**
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4117,11 +4117,6 @@
dependencies:
"@types/yargs-parser" "*"

"@types/yup@^0.26.24":
version "0.26.26"
resolved "https://registry.yarnpkg.com/@types/yup/-/yup-0.26.26.tgz#2e7065384ac2b7711271d8790f26ac7d73f6a33d"
integrity sha512-5cLJLd8NIT7OfJLi7ScquRn/NWfmewBqJ92nT/FYfdpgKzyUNcR4n2BKEOQ7mOG8WuJXgomIvNl5P3sn9Akd4A==

"@types/zen-observable@^0.8.0":
version "0.8.0"
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.0.tgz#8b63ab7f1aa5321248aad5ac890a485656dcea4d"
Expand Down