From 1086c6f9b3d5d4139c20245305cfbf40be73d173 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Tue, 1 Dec 2020 20:23:56 +0100 Subject: [PATCH] feat: addCopyRule removeCopyRule helpers --- packages/webpack5/jest.config.js | 3 +- packages/webpack5/scripts/jest.copyRules.ts | 6 ++++ packages/webpack5/src/configuration/base.ts | 29 ++++++----------- packages/webpack5/src/helpers/copyRules.ts | 36 +++++++++++++++++++++ packages/webpack5/src/helpers/index.ts | 10 ++++-- 5 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 packages/webpack5/scripts/jest.copyRules.ts create mode 100644 packages/webpack5/src/helpers/copyRules.ts diff --git a/packages/webpack5/jest.config.js b/packages/webpack5/jest.config.js index 6cc6b67ae..ace328d00 100644 --- a/packages/webpack5/jest.config.js +++ b/packages/webpack5/jest.config.js @@ -8,7 +8,8 @@ module.exports = { '/scripts/jest.setup.ts' ], setupFilesAfterEnv: [ - '/scripts/jest.mockWarn.ts' + '/scripts/jest.mockWarn.ts', + '/scripts/jest.copyRules.ts' ], globals: { __TEST__: true, diff --git a/packages/webpack5/scripts/jest.copyRules.ts b/packages/webpack5/scripts/jest.copyRules.ts new file mode 100644 index 000000000..3699686ea --- /dev/null +++ b/packages/webpack5/scripts/jest.copyRules.ts @@ -0,0 +1,6 @@ +import { copyRules } from '../src/helpers/copyRules'; + +afterEach(() => { + // Clear copy rules + copyRules.clear(); +}); diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index f866ac545..dbf8609db 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -1,15 +1,16 @@ import { DefinePlugin, HotModuleReplacementPlugin } from 'webpack'; import Config from 'webpack-chain'; -import path from 'path'; import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'; import { CleanWebpackPlugin } from 'clean-webpack-plugin'; -import CopyWebpackPlugin from 'copy-webpack-plugin'; import TerserPlugin from 'terser-webpack-plugin'; // import { WatchStateLoggerPlugin } from '../plugins/WatchStateLoggerPlugin'; +import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin'; +import { addCopyRule, applyCopyRules } from '../helpers/copyRules'; import { WatchStatePlugin } from '../plugins/WatchStatePlugin'; +import { hasDependency } from '../helpers/dependencies'; import { IWebpackEnv } from '../index'; import { getAbsoluteDistPath, @@ -17,14 +18,11 @@ import { getEntryPath, getPlatform, } from '../helpers/project'; -import { hasDependency } from '../helpers/dependencies'; -import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin'; export default function (config: Config, env: IWebpackEnv): Config { const entryPath = getEntryPath(); const platform = getPlatform(); const mode = env.production ? 'production' : 'development'; - const appPath = path.dirname(entryPath); // set mode config.mode(mode); @@ -232,21 +230,12 @@ export default function (config: Config, env: IWebpackEnv): Config { }, ]); - const copyPaths = ['assets/**', 'fonts/**', '**/*.+(jpg|png)']; - config.plugin('CopyWebpackPlugin').use(CopyWebpackPlugin, [ - { - patterns: copyPaths.map((from) => ({ - from, - context: appPath, - noErrorOnMissing: true, - globOptions: { - dot: false, - // todo: ignore AppResources if inside app folder! - // ignore: [``] - }, - })), - }, - ]); + // set up default copy rules + addCopyRule('assets/**'); + addCopyRule('fonts/**'); + addCopyRule('**/*.+(jpg|png)'); + + applyCopyRules(config); // add the WatchStateLogger plugin used to notify the CLI of build state // config.plugin('WatchStateLoggerPlugin').use(WatchStateLoggerPlugin); diff --git a/packages/webpack5/src/helpers/copyRules.ts b/packages/webpack5/src/helpers/copyRules.ts new file mode 100644 index 000000000..0bf7a9a1b --- /dev/null +++ b/packages/webpack5/src/helpers/copyRules.ts @@ -0,0 +1,36 @@ +import CopyWebpackPlugin from 'copy-webpack-plugin'; + +import { getEntryDirPath } from './project'; + +/** + * @internal + */ +export let copyRules = new Set([]); + +export function addCopyRule(glob: string) { + copyRules.add(glob); +} + +export function removeCopyRule(glob: string) { + copyRules.delete(glob); +} + +/** + * @internal + */ +export function applyCopyRules(config) { + config.plugin('CopyWebpackPlugin').use(CopyWebpackPlugin, [ + { + patterns: Array.from(copyRules).map((glob) => ({ + from: glob, + context: getEntryDirPath(), + noErrorOnMissing: true, + globOptions: { + dot: false, + // todo: ignore AppResources if inside app folder! + // ignore: [``] + }, + })), + }, + ]); +} diff --git a/packages/webpack5/src/helpers/index.ts b/packages/webpack5/src/helpers/index.ts index d34c3a845..876325720 100644 --- a/packages/webpack5/src/helpers/index.ts +++ b/packages/webpack5/src/helpers/index.ts @@ -1,13 +1,14 @@ import { merge } from 'webpack-merge'; +import { addCopyRule, removeCopyRule } from './copyRules'; +import { determineProjectFlavor } from './flavor'; +import { error, info, warn } from './log'; +import { getValue } from './config'; import { getAllDependencies, hasDependency, getDependencyPath, } from './dependencies'; -import { determineProjectFlavor } from './flavor'; -import { error, info, warn } from './log'; -import { getValue } from './config'; import { getAbsoluteDistPath, getDistPath, @@ -22,8 +23,11 @@ import { // as this generates nicer typings // that show all the utils inline // rather than imports to types +// todo: maybe use api-extractor instead export default { merge, + addCopyRule, + removeCopyRule, config: { getValue, },