refactor: angular polyfill loading

This commit is contained in:
Igor Randjelovic
2021-06-02 19:06:51 +02:00
parent ea2f41a0f5
commit e252e97648

View File

@@ -1,14 +1,14 @@
import { extname, resolve } from 'path';
import Config from 'webpack-chain'; import Config from 'webpack-chain';
import { existsSync } from 'fs'; import { existsSync } from 'fs';
import { extname, join } from 'path';
import { getProjectFilePath } from '../helpers/project';
import { env as _env, IWebpackEnv } from '../index';
import { import {
getEntryDirPath, getEntryDirPath,
getEntryPath, getEntryPath,
getPlatformName, getPlatformName,
} from '../helpers/platform'; } from '../helpers/platform';
import { getProjectFilePath } from '../helpers/project';
import { env as _env, IWebpackEnv } from '../index';
import base from './base'; import base from './base';
export default function (config: Config, env: IWebpackEnv = _env): Config { export default function (config: Config, env: IWebpackEnv = _env): Config {
@@ -132,20 +132,27 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
]); ]);
} }
const getPolyfillPath = (platform?: string) => // look for platform specific polyfills first
join(getEntryDirPath(), `polyfills${platform ? '.' + platform : ''}.ts`); // falling back to independent polyfills
const polyfillsPath = getPolyfillPath(); const polyfillsPath = [
const polyfillsPathResolved = [ resolve(getEntryDirPath(), `polyfills.${getPlatformName()}.ts`),
polyfillsPath, resolve(getEntryDirPath(), `polyfills.ts`),
getPolyfillPath('android'), ].find((path) => existsSync(path));
getPolyfillPath('ios'),
]; if (polyfillsPath) {
if (polyfillsPathResolved.find(existsSync)) { const paths = config.entry('bundle').values();
let entries = config.entry('bundle').values();
entries = entries.map((v) => // replace globals with the polyfills file which
v === '@nativescript/core/globals/index.js' ? polyfillsPath : v // should handle loading the correct globals
); // and any additional polyfills required.
config.entry('bundle').clear().merge(entries); if (paths.includes('@nativescript/core/globals/index.js')) {
paths[
paths.indexOf('@nativescript/core/globals/index.js')
] = polyfillsPath;
// replace paths with the update paths
config.entry('bundle').clear().merge(paths);
}
} }
// Filter common undesirable warnings // Filter common undesirable warnings