From 4f8522f5d4a8cbfc08cd79b4a58a713bc06a953b Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Wed, 14 Apr 2021 15:09:33 +0200 Subject: [PATCH] refactor: simplify App_Resource exclusions --- .../__snapshots__/base.spec.ts.snap | 28 +++++++--- .../__tests__/configuration/base.spec.ts | 53 ++++++++++--------- packages/webpack5/src/helpers/copyRules.ts | 9 ++-- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap index c14a8c09b..d365dc75e 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/base.spec.ts.snap @@ -15,7 +15,7 @@ exports[`base configuration for android 1`] = ` watchOptions: { ignored: [ '__jest__/platforms/**', - '__jest__/App_Resources/**' + '__jest__/custom_app_resources/**' ] }, ignoreWarnings: [ @@ -255,7 +255,9 @@ exports[`base configuration for android 1`] = ` noErrorOnMissing: true, globOptions: { dot: false, - ignore: [] + ignore: [ + '**/custom_app_resources/**' + ] } }, { @@ -264,7 +266,9 @@ exports[`base configuration for android 1`] = ` noErrorOnMissing: true, globOptions: { dot: false, - ignore: [] + ignore: [ + '**/custom_app_resources/**' + ] } }, { @@ -273,7 +277,9 @@ exports[`base configuration for android 1`] = ` noErrorOnMissing: true, globOptions: { dot: false, - ignore: [] + ignore: [ + '**/custom_app_resources/**' + ] } } ] @@ -308,7 +314,7 @@ exports[`base configuration for ios 1`] = ` watchOptions: { ignored: [ '__jest__/platforms/**', - '__jest__/App_Resources/**' + '__jest__/custom_app_resources/**' ] }, ignoreWarnings: [ @@ -548,7 +554,9 @@ exports[`base configuration for ios 1`] = ` noErrorOnMissing: true, globOptions: { dot: false, - ignore: [] + ignore: [ + '**/custom_app_resources/**' + ] } }, { @@ -557,7 +565,9 @@ exports[`base configuration for ios 1`] = ` noErrorOnMissing: true, globOptions: { dot: false, - ignore: [] + ignore: [ + '**/custom_app_resources/**' + ] } }, { @@ -566,7 +576,9 @@ exports[`base configuration for ios 1`] = ` noErrorOnMissing: true, globOptions: { dot: false, - ignore: [] + ignore: [ + '**/custom_app_resources/**' + ] } } ] diff --git a/packages/webpack5/__tests__/configuration/base.spec.ts b/packages/webpack5/__tests__/configuration/base.spec.ts index fd33bb7c2..042b1ee33 100644 --- a/packages/webpack5/__tests__/configuration/base.spec.ts +++ b/packages/webpack5/__tests__/configuration/base.spec.ts @@ -3,8 +3,8 @@ import fs from 'fs'; import base from '../../src/configuration/base'; import { init } from '../../src'; -import { applyFileReplacements } from "../../src/helpers/fileReplacements"; -import { additionalCopyRules } from "../../src/helpers/copyRules"; +import { applyFileReplacements } from '../../src/helpers/fileReplacements'; +import { additionalCopyRules } from '../../src/helpers/copyRules'; describe('base configuration', () => { const platforms = ['ios', 'android']; @@ -13,14 +13,18 @@ describe('base configuration', () => { it(`for ${platform}`, () => { init({ [platform]: true, + + // only test in base config to make sure App_Resources + // are properly excluded from the copy-rules + appResourcesPath: '__jest__/custom_app_resources', }); expect(base(new Config()).toString()).toMatchSnapshot(); }); } it('supports dotenv', () => { - const fsSpy = jest.spyOn(fs, "existsSync") - fsSpy.mockReturnValue(true) + const fsSpy = jest.spyOn(fs, 'existsSync'); + fsSpy.mockReturnValue(true); init({ ios: true, @@ -33,12 +37,12 @@ describe('base configuration', () => { return args; }); - fsSpy.mockRestore() + fsSpy.mockRestore(); }); it('supports env specific dotenv', () => { - const fsSpy = jest.spyOn(fs, "existsSync") - fsSpy.mockReturnValue(true) + const fsSpy = jest.spyOn(fs, 'existsSync'); + fsSpy.mockReturnValue(true); init({ ios: true, @@ -46,22 +50,19 @@ describe('base configuration', () => { }); const config = base(new Config()); - expect(fsSpy).toHaveBeenCalledWith('__jest__/.env.prod') - expect(fsSpy).toHaveBeenCalledTimes(1) + expect(fsSpy).toHaveBeenCalledWith('__jest__/.env.prod'); + expect(fsSpy).toHaveBeenCalledTimes(1); expect(config.plugin('DotEnvPlugin')).toBeDefined(); config.plugin('DotEnvPlugin').tap((args) => { expect(args[0].path).toEqual('__jest__/.env.prod'); return args; }); - fsSpy.mockRestore() + fsSpy.mockRestore(); }); it('falls back to default .env', () => { - const fsSpy = jest.spyOn(fs, "existsSync") - fsSpy - .mockReturnValueOnce(false) - .mockReturnValueOnce(true) - + const fsSpy = jest.spyOn(fs, 'existsSync'); + fsSpy.mockReturnValueOnce(false).mockReturnValueOnce(true); init({ ios: true, @@ -69,15 +70,15 @@ describe('base configuration', () => { }); const config = base(new Config()); - expect(fsSpy).toHaveBeenCalledWith('__jest__/.env.prod') - expect(fsSpy).toHaveBeenCalledWith('__jest__/.env') - expect(fsSpy).toHaveBeenCalledTimes(2) + expect(fsSpy).toHaveBeenCalledWith('__jest__/.env.prod'); + expect(fsSpy).toHaveBeenCalledWith('__jest__/.env'); + expect(fsSpy).toHaveBeenCalledTimes(2); expect(config.plugin('DotEnvPlugin')).toBeDefined(); config.plugin('DotEnvPlugin').tap((args) => { expect(args[0].path).toEqual('__jest__/.env'); return args; }); - fsSpy.mockRestore() + fsSpy.mockRestore(); }); it('applies file replacements', () => { @@ -88,16 +89,16 @@ describe('base configuration', () => { 'bar.js': 'bar.replaced.js', // should apply as a file replacement using the copy plugin - 'foo.json': 'foo.replaced.json' - }) + 'foo.json': 'foo.replaced.json', + }); - expect(config.resolve.alias.get('foo.ts')).toBe('foo.replaced.ts') - expect(config.resolve.alias.get('bar.js')).toBe('bar.replaced.js') - expect(additionalCopyRules.length).toBe(1) + expect(config.resolve.alias.get('foo.ts')).toBe('foo.replaced.ts'); + expect(config.resolve.alias.get('bar.js')).toBe('bar.replaced.js'); + expect(additionalCopyRules.length).toBe(1); expect(additionalCopyRules[0]).toEqual({ from: 'foo.replaced.json', to: 'foo.json', force: true, - }) - }) + }); + }); }); diff --git a/packages/webpack5/src/helpers/copyRules.ts b/packages/webpack5/src/helpers/copyRules.ts index 52e3aff04..fe6dc932c 100644 --- a/packages/webpack5/src/helpers/copyRules.ts +++ b/packages/webpack5/src/helpers/copyRules.ts @@ -1,5 +1,5 @@ import CopyWebpackPlugin from 'copy-webpack-plugin'; -import { relative, resolve } from 'path'; +import { basename, relative, resolve } from 'path'; import Config from 'webpack-chain'; import { getProjectRootPath } from './project'; @@ -61,13 +61,10 @@ export function applyCopyRules(config: Config) { // todo: do we need to handle empty appResourcesPath? // (the CLI should always pass the path - maybe not required) if (env.appResourcesPath) { - const appResourcesFullPath = resolve( - getProjectRootPath(), - env.appResourcesPath - ); + const appResourcesFolderName = basename(env.appResourcesPath); // ignore everything in App_Resources (regardless where they are located) - globOptions.ignore.push(`**/${relative(entryDir, appResourcesFullPath)}/**`); + globOptions.ignore.push(`**/${appResourcesFolderName}/**`); } config.plugin('CopyWebpackPlugin').use(CopyWebpackPlugin, [