@expo/webpack-config
Version:
The default Webpack configuration used to build Expo apps targeting the web.
76 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("@expo/config");
const webpack_1 = require("webpack");
const env_1 = require("../env");
/**
* Create the global environment variables to surface in the project. Also creates the `__DEV__` boolean to provide better parity with Metro bundler.
*
* @param mode defines the Metro bundler `global.__DEV__` value.
* @param publicPath passed as `process.env.PUBLIC_URL` to the app.
* @param nativeAppManifest public values to be used in `expo-constants`.
* @internal
*/
function createClientEnvironment(mode, publicPath, nativeAppManifest) {
const environment = env_1.getMode({ mode });
const __DEV__ = environment !== 'production';
const ENV_VAR_REGEX = /^(EXPO_|REACT_NATIVE_|CI$)/i;
const SECRET_REGEX = /(PASSWORD|SECRET|TOKEN)/i;
const processEnv = Object.keys(process.env)
.filter(key => ENV_VAR_REGEX.test(key) && !SECRET_REGEX.test(key))
.reduce((env, key) => {
env[key] = JSON.stringify(process.env[key]);
return env;
}, {
/**
* Useful for determining whether we’re running in production mode.
* Most importantly, it switches React into the correct mode.
*/
NODE_ENV: JSON.stringify(environment),
/**
* Useful for resolving the correct path to static assets in `public`.
* For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
* This should only be used as an escape hatch. Normally you would put
* images into the root folder and `import` them in code to get their paths.
*/
PUBLIC_URL: JSON.stringify(publicPath),
/**
* Surfaces the `app.json` (config) as an environment variable which is then parsed by
* `expo-constants` https://docs.expo.io/versions/latest/sdk/constants/
*/
APP_MANIFEST: JSON.stringify(nativeAppManifest),
});
return {
'process.env': processEnv,
__DEV__,
};
}
exports.createClientEnvironment = createClientEnvironment;
/**
* Required for `expo-constants` https://docs.expo.io/versions/latest/sdk/constants/.
* This surfaces the `app.json` (config) as an environment variable which is then parsed by `expo-constants`.
* @category plugins
*/
class DefinePlugin extends webpack_1.DefinePlugin {
constructor({ mode, publicUrl, productionManifestPath, config, }) {
const publicAppManifest = config_1.createEnvironmentConstants(config, productionManifestPath);
const environmentVariables = createClientEnvironment(mode, publicUrl, publicAppManifest);
super(environmentVariables);
}
}
exports.default = DefinePlugin;
DefinePlugin.createClientEnvironment = createClientEnvironment;
DefinePlugin.fromEnv = (env) => {
const mode = env_1.getMode(env);
const { publicUrl } = env_1.getPublicPaths(env);
const config = env.config || env_1.getConfig(env);
const locations = env.locations || env_1.getPaths(env.projectRoot);
const productionManifestPath = locations.production.manifest;
return new DefinePlugin({
mode,
publicUrl,
config,
productionManifestPath,
});
};
//# sourceMappingURL=ExpoDefinePlugin.js.map