mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 18:12:09 +08:00
feat: addCopyRule removeCopyRule helpers
This commit is contained in:
@ -8,7 +8,8 @@ module.exports = {
|
|||||||
'<rootDir>/scripts/jest.setup.ts'
|
'<rootDir>/scripts/jest.setup.ts'
|
||||||
],
|
],
|
||||||
setupFilesAfterEnv: [
|
setupFilesAfterEnv: [
|
||||||
'<rootDir>/scripts/jest.mockWarn.ts'
|
'<rootDir>/scripts/jest.mockWarn.ts',
|
||||||
|
'<rootDir>/scripts/jest.copyRules.ts'
|
||||||
],
|
],
|
||||||
globals: {
|
globals: {
|
||||||
__TEST__: true,
|
__TEST__: true,
|
||||||
|
6
packages/webpack5/scripts/jest.copyRules.ts
Normal file
6
packages/webpack5/scripts/jest.copyRules.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { copyRules } from '../src/helpers/copyRules';
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
// Clear copy rules
|
||||||
|
copyRules.clear();
|
||||||
|
});
|
@ -1,15 +1,16 @@
|
|||||||
import { DefinePlugin, HotModuleReplacementPlugin } from 'webpack';
|
import { DefinePlugin, HotModuleReplacementPlugin } from 'webpack';
|
||||||
import Config from 'webpack-chain';
|
import Config from 'webpack-chain';
|
||||||
import path from 'path';
|
|
||||||
|
|
||||||
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
|
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
|
||||||
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
|
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
|
||||||
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
|
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
|
||||||
import CopyWebpackPlugin from 'copy-webpack-plugin';
|
|
||||||
import TerserPlugin from 'terser-webpack-plugin';
|
import TerserPlugin from 'terser-webpack-plugin';
|
||||||
|
|
||||||
// import { WatchStateLoggerPlugin } from '../plugins/WatchStateLoggerPlugin';
|
// import { WatchStateLoggerPlugin } from '../plugins/WatchStateLoggerPlugin';
|
||||||
|
import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin';
|
||||||
|
import { addCopyRule, applyCopyRules } from '../helpers/copyRules';
|
||||||
import { WatchStatePlugin } from '../plugins/WatchStatePlugin';
|
import { WatchStatePlugin } from '../plugins/WatchStatePlugin';
|
||||||
|
import { hasDependency } from '../helpers/dependencies';
|
||||||
import { IWebpackEnv } from '../index';
|
import { IWebpackEnv } from '../index';
|
||||||
import {
|
import {
|
||||||
getAbsoluteDistPath,
|
getAbsoluteDistPath,
|
||||||
@ -17,14 +18,11 @@ import {
|
|||||||
getEntryPath,
|
getEntryPath,
|
||||||
getPlatform,
|
getPlatform,
|
||||||
} from '../helpers/project';
|
} from '../helpers/project';
|
||||||
import { hasDependency } from '../helpers/dependencies';
|
|
||||||
import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin';
|
|
||||||
|
|
||||||
export default function (config: Config, env: IWebpackEnv): Config {
|
export default function (config: Config, env: IWebpackEnv): Config {
|
||||||
const entryPath = getEntryPath();
|
const entryPath = getEntryPath();
|
||||||
const platform = getPlatform();
|
const platform = getPlatform();
|
||||||
const mode = env.production ? 'production' : 'development';
|
const mode = env.production ? 'production' : 'development';
|
||||||
const appPath = path.dirname(entryPath);
|
|
||||||
|
|
||||||
// set mode
|
// set mode
|
||||||
config.mode(mode);
|
config.mode(mode);
|
||||||
@ -232,21 +230,12 @@ export default function (config: Config, env: IWebpackEnv): Config {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const copyPaths = ['assets/**', 'fonts/**', '**/*.+(jpg|png)'];
|
// set up default copy rules
|
||||||
config.plugin('CopyWebpackPlugin').use(CopyWebpackPlugin, [
|
addCopyRule('assets/**');
|
||||||
{
|
addCopyRule('fonts/**');
|
||||||
patterns: copyPaths.map((from) => ({
|
addCopyRule('**/*.+(jpg|png)');
|
||||||
from,
|
|
||||||
context: appPath,
|
applyCopyRules(config);
|
||||||
noErrorOnMissing: true,
|
|
||||||
globOptions: {
|
|
||||||
dot: false,
|
|
||||||
// todo: ignore AppResources if inside app folder!
|
|
||||||
// ignore: [``]
|
|
||||||
},
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
// add the WatchStateLogger plugin used to notify the CLI of build state
|
// add the WatchStateLogger plugin used to notify the CLI of build state
|
||||||
// config.plugin('WatchStateLoggerPlugin').use(WatchStateLoggerPlugin);
|
// config.plugin('WatchStateLoggerPlugin').use(WatchStateLoggerPlugin);
|
||||||
|
36
packages/webpack5/src/helpers/copyRules.ts
Normal file
36
packages/webpack5/src/helpers/copyRules.ts
Normal file
@ -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: [``]
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
@ -1,13 +1,14 @@
|
|||||||
import { merge } from 'webpack-merge';
|
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 {
|
import {
|
||||||
getAllDependencies,
|
getAllDependencies,
|
||||||
hasDependency,
|
hasDependency,
|
||||||
getDependencyPath,
|
getDependencyPath,
|
||||||
} from './dependencies';
|
} from './dependencies';
|
||||||
import { determineProjectFlavor } from './flavor';
|
|
||||||
import { error, info, warn } from './log';
|
|
||||||
import { getValue } from './config';
|
|
||||||
import {
|
import {
|
||||||
getAbsoluteDistPath,
|
getAbsoluteDistPath,
|
||||||
getDistPath,
|
getDistPath,
|
||||||
@ -22,8 +23,11 @@ import {
|
|||||||
// as this generates nicer typings
|
// as this generates nicer typings
|
||||||
// that show all the utils inline
|
// that show all the utils inline
|
||||||
// rather than imports to types
|
// rather than imports to types
|
||||||
|
// todo: maybe use api-extractor instead
|
||||||
export default {
|
export default {
|
||||||
merge,
|
merge,
|
||||||
|
addCopyRule,
|
||||||
|
removeCopyRule,
|
||||||
config: {
|
config: {
|
||||||
getValue,
|
getValue,
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user