Skip to content

Commit

Permalink
chore: conditional legacy browser support using custom vite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
langemike committed Jun 3, 2024
1 parent 48a5df0 commit 0e7bb82
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion knip.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ const config: KnipConfig = {
'@babel/core', // Required peer dependency for babel plugins
'@types/luxon', // Used in tests
'babel-plugin-transform-typescript-metadata', // Used to build with decorators for ioc resolution
'core-js', // Conditionally imported at build time
'eslint-plugin-codeceptjs', // Used by apps/web/test-e2e/.eslintrc.cjs
'luxon', // Used in tests
'playwright', // Used in test configs
'sharp', // Requirement for @vite-pwa/assets-generator
'tsconfig-paths', // Used for e2e test setup
'virtual:pwa-register', // Service Worker code is injected at build time,
'virtual:pwa-register', // Service Worker code is injected at build time
],
},
'configs/eslint-config-jwp': {
Expand Down
3 changes: 2 additions & 1 deletion platforms/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@jwp/ott-theme": "*",
"@jwp/ott-ui-react": "*",
"eslint-config-jwp": "*",
"postcss-config-jwp": "*"
"postcss-config-jwp": "*",
"virtual:polyfills": "*"
}
}
23 changes: 23 additions & 0 deletions platforms/web/scripts/build-tools/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Plugin } from 'vite';

export const legacyBrowserPlugin = (enabled: boolean = true): Plugin => {
return {
name: 'legacy-browser',
resolveId(id) {
if (id.includes('virtual:polyfills')) {
return '\0' + id;
}
},
load(id) {
if (id.includes('\0virtual:polyfills')) {
return enabled ? "import './src/polyfills';" : 'export default {};';
}
},
config(config) {
if (enabled) {
config.build = config.build ?? {};
config.build.target = ['es2020', 'edge88', 'firefox78', 'chrome68', 'safari14'];
}
},
};
};
3 changes: 1 addition & 2 deletions platforms/web/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'core-js/es/array/flat-map';
import 'core-js/es/object/from-entries';
import 'virtual:polyfills';
import React from 'react';
import { createRoot } from 'react-dom/client';
import 'wicg-inert';
Expand Down
2 changes: 2 additions & 0 deletions platforms/web/src/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'core-js/es/array/flat-map';
import 'core-js/es/object/from-entries';
3 changes: 2 additions & 1 deletion platforms/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import svgr from 'vite-plugin-svgr';
import { viteStaticCopy } from 'vite-plugin-static-copy';

import { basePath, favIconSizes, appleIconSizes } from './pwa-assets.config';
import { legacyBrowserPlugin } from './scripts/build-tools/plugins';
import {
extractExternalFonts,
getFileCopyTargets,
Expand Down Expand Up @@ -51,6 +52,7 @@ export default ({ mode, command }: ConfigEnv): UserConfigExport => {

return defineConfig({
plugins: [
legacyBrowserPlugin(!!process.env.APP_LEGACY_BUILD),
react({
// This is needed to do decorator transforms for ioc resolution to work for classes
babel: { plugins: ['babel-plugin-transform-typescript-metadata', ['@babel/plugin-proposal-decorators', { legacy: true }]] },
Expand Down Expand Up @@ -114,7 +116,6 @@ export default ({ mode, command }: ConfigEnv): UserConfigExport => {
cssCodeSplit: false,
sourcemap: true,
minify: true,
target: ['es2020', 'edge88', 'firefox78', 'chrome68', 'safari14'],
rollupOptions: {
output: {
manualChunks: (id) => {
Expand Down

0 comments on commit 0e7bb82

Please sign in to comment.