UNPKG

@expo/webpack-config

Version:

The default Webpack configuration used to build Expo apps targeting the web.

175 lines 6.85 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-env node */ const config_1 = require("@expo/config"); const paths_1 = require("@expo/config/paths"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const url_1 = __importDefault(require("url")); const getMode_1 = __importDefault(require("./getMode")); function parsePaths(projectRoot, nativeAppManifest) { const inputProjectRoot = projectRoot || paths_1.getPossibleProjectRoot(); function absolute(...pathComponents) { return paths_1.getAbsolutePathWithProjectRoot(inputProjectRoot, ...pathComponents); } const packageJsonPath = absolute('package.json'); const modulesPath = paths_1.getModulesPath(inputProjectRoot); const productionPath = absolute(config_1.getWebOutputPath(nativeAppManifest)); function templatePath(filename = '') { const overridePath = absolute('web', filename); if (fs_1.default.existsSync(overridePath)) { return overridePath; } return path_1.default.join(__dirname, '../../web-default', filename); } function getProductionPath(...props) { return path_1.default.resolve(productionPath, ...props); } function getIncludeModule(...props) { return path_1.default.resolve(modulesPath, ...props); } let appMain = null; try { appMain = paths_1.getEntryPoint(inputProjectRoot, ['./index', './src/index'], ['web']); } catch (_) { // ignore the error } return { absolute, includeModule: getIncludeModule, packageJson: packageJsonPath, root: path_1.default.resolve(inputProjectRoot), appMain, modules: modulesPath, servedPath: getServedPath(inputProjectRoot), template: { get: templatePath, folder: templatePath(), indexHtml: templatePath('index.html'), manifest: templatePath('manifest.json'), serveJson: templatePath('serve.json'), favicon: templatePath('favicon.ico'), serviceWorker: templatePath('expo-service-worker.js'), registerServiceWorker: templatePath('register-service-worker.js'), }, production: { get: getProductionPath, folder: getProductionPath(), indexHtml: getProductionPath('index.html'), manifest: getProductionPath('manifest.json'), serveJson: getProductionPath('serve.json'), favicon: getProductionPath('favicon.ico'), serviceWorker: getProductionPath('expo-service-worker.js'), registerServiceWorker: getProductionPath('register-service-worker.js'), }, }; } /** * Sync method for getting default paths used throughout the Webpack config. * This is useful for Next.js which doesn't support async Webpack configs. * * @param projectRoot * @category env */ function getPaths(projectRoot) { const { exp } = config_1.getConfig(projectRoot, { skipSDKVersionRequirement: true, }); return parsePaths(projectRoot, exp); } exports.getPaths = getPaths; /** * Async method for getting default paths used throughout the Webpack config. * * @param projectRoot * @category env */ function getPathsAsync(projectRoot) { return __awaiter(this, void 0, void 0, function* () { let exp; try { exp = config_1.getConfig(projectRoot, { skipSDKVersionRequirement: true }).exp; } catch (error) { } return parsePaths(projectRoot, exp); }); } exports.getPathsAsync = getPathsAsync; /** * Get paths dictating where the app is served regardless of the current Webpack mode. * * @param projectRoot * @category env */ function getServedPath(projectRoot) { const { pkg } = config_1.getConfig(projectRoot, { skipSDKVersionRequirement: true, }); const envPublicUrl = process.env.WEB_PUBLIC_URL; // We use `WEB_PUBLIC_URL` environment variable or "homepage" field to infer // "public path" at which the app is served. // Webpack needs to know it to put the right <script> hrefs into HTML even in // single-page apps that may serve index.html for nested URLs like /todos/42. // We can't use a relative path in HTML because we don't want to load something // like /todos/42/static/js/bundle.7289d.js. We have to know the root. const publicUrl = envPublicUrl || pkg.homepage; const servedUrl = envPublicUrl || (publicUrl ? url_1.default.parse(publicUrl).pathname : '/'); return paths_1.ensureSlash(servedUrl, true); } exports.getServedPath = getServedPath; /** * Get paths dictating where the app is served. In development mode this returns default values. * * @param env * @category env */ function getPublicPaths(env) { const parsedMode = getMode_1.default(env); if (parsedMode === 'production') { const publicPath = getServedPath(env.projectRoot); return { publicPath, publicUrl: publicPath.slice(0, -1), }; } return { publicUrl: '', publicPath: '/' }; } exports.getPublicPaths = getPublicPaths; /** * Get the output folder path. Defaults to `web-build`. * * @param projectRoot * @category env */ function getProductionPath(projectRoot) { const { exp } = config_1.getConfig(projectRoot, { skipSDKVersionRequirement: true, }); return paths_1.getAbsolutePathWithProjectRoot(projectRoot, config_1.getWebOutputPath(exp)); } exports.getProductionPath = getProductionPath; /** * Get an absolute path relative to the project root while accounting for remote paths (`https://`). * * @param projectRoot * @category env */ function getAbsolute(projectRoot, ...pathComponents) { const inputProjectRoot = projectRoot || paths_1.getPossibleProjectRoot(); return paths_1.getAbsolutePathWithProjectRoot(inputProjectRoot, ...pathComponents); } exports.getAbsolute = getAbsolute; //# sourceMappingURL=paths.js.map