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

Commit

Permalink
fix(webpack-config): use custom register-service-worker.js if availab…
Browse files Browse the repository at this point in the history
…le (#1159)

* fix(webpack-config): use custom register-service-worker.js if available

* Prevent register-service-worker from being copied twice
  • Loading branch information
jonathonlui authored and EvanBacon committed Nov 5, 2019
1 parent 8804510 commit feb47b2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/webpack-config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface FilePathsFolder {
serveJson: string;
favicon: string;
serviceWorker: string;
registerServiceWorker: string;
}
export interface FilePaths {
absolute: PathResolver;
Expand Down
2 changes: 2 additions & 0 deletions packages/webpack-config/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function parsePaths(projectRoot: string, nativeAppManifest?: ExpoConfig): FilePa
serveJson: templatePath('serve.json'),
favicon: templatePath('favicon.ico'),
serviceWorker: templatePath('expo-service-worker.js'),
registerServiceWorker: templatePath('register-service-worker.js'),
},
production: {
get: getProductionPath,
Expand All @@ -115,6 +116,7 @@ function parsePaths(projectRoot: string, nativeAppManifest?: ExpoConfig): FilePa
serveJson: getProductionPath('serve.json'),
favicon: getProductionPath('favicon.ico'),
serviceWorker: getProductionPath('expo-service-worker.js'),
registerServiceWorker: getProductionPath('register-service-worker.js'),
},
};
}
Expand Down
56 changes: 33 additions & 23 deletions packages/webpack-config/src/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import WatchMissingNodeModulesPlugin from 'react-dev-utils/WatchMissingNodeModul
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import { boolish } from 'getenv';
import path from 'path';
import webpack from 'webpack';
import { getPathsAsync, getPublicPaths } from './utils/paths';
import createAllLoaders from './loaders/createAllLoaders';
import { ExpoDefinePlugin, ExpoProgressBarPlugin, ExpoHtmlWebpackPlugin } from './plugins';
Expand All @@ -24,16 +26,10 @@ import withOptimizations from './withOptimizations';
import withReporting from './withReporting';
import withCompression from './withCompression';

import path from 'path';
import webpack from 'webpack';

import createDevServerConfigAsync from './createDevServerConfigAsync';
import { Arguments, DevConfiguration, FilePaths, Mode } from './types';

import {
DEFAULT_ALIAS,
overrideWithPropertyOrConfig,
} from './utils/config';
import { DEFAULT_ALIAS, overrideWithPropertyOrConfig } from './utils/config';
import getMode from './utils/getMode';
import getConfig from './utils/getConfig';
import { Environment } from './types';
Expand Down Expand Up @@ -63,21 +59,22 @@ function getDevtool(

function getOutput(locations: FilePaths, mode: Mode, publicPath: string): Output {
const commonOutput: Output = {
sourceMapFilename: '[chunkhash].map',
// We inferred the "public path" (such as / or /my-project) from homepage.
// We use "/" in development.
publicPath,
// Build folder (default `web-build`)
path: locations.production.folder,
}
sourceMapFilename: '[chunkhash].map',
// We inferred the "public path" (such as / or /my-project) from homepage.
// We use "/" in development.
publicPath,
// Build folder (default `web-build`)
path: locations.production.folder,
};

if (mode === 'production') {
commonOutput.filename = 'static/js/[name].[contenthash:8].js';
// There are also additional JS chunk files if you use code splitting.
commonOutput.chunkFilename = 'static/js/[name].[contenthash:8].chunk.js';
// Point sourcemap entries to original disk location (format as URL on Windows)
commonOutput.devtoolModuleFilenameTemplate = (info: webpack.DevtoolModuleFilenameTemplateInfo): string =>
locations.absolute(info.absoluteResourcePath).replace(/\\/g, '/');
commonOutput.devtoolModuleFilenameTemplate = (
info: webpack.DevtoolModuleFilenameTemplateInfo
): string => locations.absolute(info.absoluteResourcePath).replace(/\\/g, '/');
} else {
// Add comments that describe the file import/exports.
// This will make it easier to debug.
Expand All @@ -88,15 +85,18 @@ function getOutput(locations: FilePaths, mode: Mode, publicPath: string): Output
// There are also additional JS chunk files if you use code splitting.
commonOutput.chunkFilename = 'static/js/[name].chunk.js';
// Point sourcemap entries to original disk location (format as URL on Windows)
commonOutput.devtoolModuleFilenameTemplate = (info: webpack.DevtoolModuleFilenameTemplateInfo): string =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, '/');
commonOutput.devtoolModuleFilenameTemplate = (
info: webpack.DevtoolModuleFilenameTemplateInfo
): string => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/');
}

return commonOutput;
}


export default async function(env: Environment, argv: Arguments = {}): Promise<Configuration | DevConfiguration> {
export default async function(
env: Environment,
argv: Arguments = {}
): Promise<Configuration | DevConfiguration> {
const config = getConfig(env);
const mode = getMode(env);
const isDev = mode === 'development';
Expand Down Expand Up @@ -177,7 +177,15 @@ export default async function(env: Environment, argv: Arguments = {}): Promise<C
from: locations.template.folder,
to: locations.production.folder,
// We generate new versions of these based on the templates
ignore: ['expo-service-worker.js', 'favicon.ico', 'serve.json', 'index.html', 'icon.png'],
ignore: [
'expo-service-worker.js',
'favicon.ico',
'serve.json',
'index.html',
'icon.png',
// We copy this over in `withWorkbox` as it must be part of the Webpack `entry` and have templates replaced.
'register-service-worker.js',
],
},
{
from: locations.template.serveJson,
Expand Down Expand Up @@ -303,7 +311,9 @@ export default async function(env: Environment, argv: Arguments = {}): Promise<C

// 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.
function withNodeMocks(webpackConfig: Configuration | DevConfiguration): Configuration | DevConfiguration {
function withNodeMocks(
webpackConfig: Configuration | DevConfiguration
): Configuration | DevConfiguration {
webpackConfig.node = {
...(webpackConfig.node || {}),
module: 'empty',
Expand All @@ -314,6 +324,6 @@ function withNodeMocks(webpackConfig: Configuration | DevConfiguration): Configu
net: 'empty',
tls: 'empty',
child_process: 'empty',
}
};
return webpackConfig;
}
4 changes: 2 additions & 2 deletions packages/webpack-config/src/withWorkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ export default function withWorkbox(
const expoEntry = config.entry;
config.entry = async () => {
const entries = await ensureEntryAsync(expoEntry);
const swPath = join(locations.production.folder, 'register-service-worker.js');
const swPath = join(locations.production.registerServiceWorker);
if (entries.app && !entries.app.includes(swPath) && autoRegister) {
const content = (await readFile(
require.resolve('../web-default/register-service-worker.js'),
require.resolve(locations.template.registerServiceWorker),
'utf8'
))
.replace('SW_PUBLIC_URL', publicUrl)
Expand Down

0 comments on commit feb47b2

Please sign in to comment.