Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/exclude dirs for webpack #1551

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/src/schema/configFiles/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ export const zodConfigFileEngine = z
plugins: z.record(z.string(), z.string()).describe('List of required plugins for this engine to work properly'),
npm: zodEngineNpm,
platforms: z.record(zodPlatformsKeys, zodEnginePlatform),
webpackExcludedDirs: z.optional(
z
.array(z.string())
.describe(
'Allows to specify files or directories in the src folder that webpack should ignore when bundling code.'
)
),
})
.partial();
7 changes: 7 additions & 0 deletions packages/core/src/schema/platforms/fragments/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ export const zodPlatformBaseFragment = z
.describe('If set to `true` dedicated source map file will be generated alongside of compiled js bundle'),
bundleIsDev: z.boolean().describe('If set to `true` debug build will be generated'),
getJsBundleFile: z.string(),
webpackExcludedDirs: z.optional(
z
.array(z.string())
.describe(
'Allows to specify files or directories in the src folder that webpack should ignore when bundling code. By default, the "pages" folder is excluded for web platforms that do not use next.js.'
)
),
})
.partial();
3 changes: 2 additions & 1 deletion packages/engine-rn-electron/renative.engine.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"devDependencies": {}
}
}
}
},
"webpackExcludedDirs": ["pages"]
}
6 changes: 5 additions & 1 deletion packages/engine-rn-electron/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BabelConfig, withBabelPluginModuleResolver } from '@rnv/adapter';
import { withRNVWebpack } from '@rnv/sdk-webpack';

export const withRNVBabel = (cnf: BabelConfig): BabelConfig => {
const withRNVBabel = (cnf: BabelConfig): BabelConfig => {
const plugins = cnf?.plugins || [];
return {
retainLines: true,
Expand All @@ -11,3 +12,6 @@ export const withRNVBabel = (cnf: BabelConfig): BabelConfig => {
plugins: [withBabelPluginModuleResolver(), ...plugins],
};
};


export { withRNVWebpack, withRNVBabel };
4 changes: 2 additions & 2 deletions packages/engine-rn-electron/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import taskConfigure from './tasks/taskConfigure';
import taskExport from './tasks/taskExport';
import taskRun from './tasks/taskRun';
import taskStart from './tasks/taskStart';
import { withRNVBabel } from './adapter';
import { withRNVBabel, withRNVWebpack } from './adapter';
import { Config } from './config';

const Engine = createRnvEngine({
Expand All @@ -31,6 +31,6 @@ const Engine = createRnvEngine({

export type GetContext = GetContextType<typeof Engine.getContext>;

export { withRNVBabel };
export { withRNVBabel, withRNVWebpack };

export default Engine;
3 changes: 2 additions & 1 deletion packages/engine-rn-web/renative.engine.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"tizenmobile": {},
"chromecast": {},
"kaios": {}
}
},
"webpackExcludedDirs": ["pages"]
}
147 changes: 139 additions & 8 deletions packages/sdk-webpack/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,158 @@
import path from 'path';
import { Configuration } from 'webpack';

import { mergeWithCustomize, unique, merge } from 'webpack-merge';
import paths from './config/paths';
import { mergeWithCustomize } from 'webpack-merge';
import { fsExistsSync, fsReaddirSync, getContext } from '@rnv/core';
import _ from 'lodash';

export const withRNVWebpack = (cnf: Configuration) => {
//TODO: implement further overrides
const rnvConfig: Configuration = {};
const config = merge(rnvConfig, cnf);
return config;
let rnvConfig: Configuration = {};
const c = getContext();
const { platform } = c;
if (platform) {
if (process.env.RNV_ENGINE_PATH) {
const engine = require(process.env.RNV_ENGINE_PATH);
if (engine.withRNVWebpack) {
const excludedDirs =
c.buildConfig?.platforms?.[platform]?.webpackExcludedDirs ||
engine.default.config.webpackExcludedDirs ||
[];
rnvConfig = {
module: {
rules: [],
},
resolve: {},
};
rnvConfig?.module?.rules &&
rnvConfig.module.rules.push({
oneOf: [
{
test: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
include: _getIncludedModules(excludedDirs),
},
],
});
}
}
}

const mergedConfig: Configuration = mergeWithCustomize({
customizeArray(a, b, key) {
if (key === 'module.rules') {
return _getMergedRules(a, b);
}
return undefined;
},
})(rnvConfig, cnf);

return mergedConfig;
};

export const getMergedConfig = (rootConfig: Configuration, appPath: string) => {
// RNV-ADDITION

const projectConfig = require(path.join(appPath, 'webpack.config'));
const rootPlugins = rootConfig.plugins?.map((plugin) => plugin?.constructor.name) as string[];
const projectConfig: Configuration = require(path.join(appPath, 'webpack.config'));

// const rootPlugins = rootConfig.plugins?.map((plugin) => plugin?.constructor.name) as string[];

const mergedConfig: Configuration = mergeWithCustomize({
customizeArray: unique('plugins', rootPlugins, (plugin) => plugin.constructor && plugin.constructor.name),
customizeArray(a, b, key) {
if (key === 'plugins') {
return _.uniq([...a, ...b]);
}
if (key === 'module.rules') {
return _getMergedRules(a, b);
}

return undefined;
},
// customizeArray: unique('plugins', rootPlugins, (plugin) => plugin.constructor && plugin.constructor.name),
})(rootConfig, projectConfig);

// Merge => static config, adapter config , project config
// RNV-ADDITION

return mergedConfig;
};

const _getIncludedModules = (excludedDirs: string[]) => {
const srcDirs: string[] = [];
if (fsExistsSync(paths.appSrc)) {
const resources = fsReaddirSync(paths.appSrc);
resources.forEach((r) => {
if (!excludedDirs.includes(r)) {
srcDirs.push(path.join(paths.appSrc, r));
}
});
}
return process.env.RNV_MODULE_PATHS ? [...srcDirs, ...process.env.RNV_MODULE_PATHS.split(',')] : [...srcDirs];
};

const _getMergedRules = (rnvRules: any[], cnfRules: any[]) => {
const mergedRules: any[] = [];
const copyCnfRules = [...cnfRules];
rnvRules.forEach((rnvRule) => {
const duplicateTestIndex = copyCnfRules.findIndex((cnfRule) => {
if (_.isRegExp(rnvRule.test) && _.isRegExp(cnfRule.test)) {
return rnvRule.test.toString() === cnfRule.test.toString();
}
if (_.isArray(rnvRule.test) && _.isArray(cnfRule.test)) {
return _.isEqualWith(rnvRule.test, cnfRule.test, _regexArrayCustomizer);
}
});
if (duplicateTestIndex !== -1) {
const cnfRule = copyCnfRules[duplicateTestIndex];
const mergedRule = _mergeRule(rnvRule, cnfRule);
mergedRules.push(mergedRule);
copyCnfRules.splice(duplicateTestIndex, 1);
} else {
if (rnvRule?.oneOf) {
const cnfRuleOneOfIndex = copyCnfRules.findIndex((cnfRule) => cnfRule.oneOf);
const mergedOneOf = _getMergedRules(rnvRule.oneOf, copyCnfRules[cnfRuleOneOfIndex]?.oneOf);
mergedRules.push({ oneOf: [...mergedOneOf] });
copyCnfRules.splice(cnfRuleOneOfIndex, 1);
} else {
mergedRules.push(rnvRule);
}
}
});

mergedRules.push(...copyCnfRules);
return mergedRules;
};

const _mergeRule = (rnvRule: any, cnfRule: any) => {
return Object.keys({ ...rnvRule, ...cnfRule }).reduce((merged: any, key: string) => {
const rnvValue = rnvRule[key];
const cnfValue = cnfRule[key];
if (!rnvValue && !cnfValue) {
return merged;
}
if (_.isArray(rnvValue) && _.isArray(cnfValue)) {
if (key === 'include') {
merged[key] = process.env.RNV_MODULE_PATHS
? _.uniq([...cnfValue, ...process.env.RNV_MODULE_PATHS.split(',')])
: [...cnfValue];
} else {
merged[key] = [...cnfValue];
}
} else if (_.isPlainObject(rnvValue) && _.isPlainObject(cnfValue)) {
merged[key] = _mergeRule(rnvValue, cnfValue);
} else {
merged[key] = cnfValue !== undefined ? cnfValue : rnvValue;
}

return merged;
}, {});
};

const _regexArrayCustomizer = (arr1: any, arr2: any) => {
if (arr1.length === arr2.length) {
return _.every(arr1, (value, index) => {
if (_.isRegExp(value) && _.isRegExp(arr2[index])) {
return value.toString() === arr2[index].toString();
}
});
}
};
3 changes: 3 additions & 0 deletions packages/sdk-webpack/src/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;

var tsConfig = fs.readFileSync(paths.appTsConfig, 'utf-8');
var excludeTs = tsConfig.exclude || [];
// const hasJsxRuntime = (() => {
// if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
// return false;
Expand Down Expand Up @@ -676,6 +678,7 @@ module.exports = function (webpackEnv) {
noEmit: true,
incremental: true,
tsBuildInfoFile: paths.appTsBuildInfoFile,
exclude: [...excludeTs, process.env.TS_EXCLUDE_SRC_DIRS],
},
},
context: paths.appPath,
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk-webpack/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export const EnvVars = {
return { WEBPACK_TARGET: ctx.runtime.webpackTarget };
}
},
TS_EXCLUDE_SRC_DIRS: () => {
const ctx = getContext();
if (!ctx.platform) return;
const engine = ctx.runtime.enginesByPlatform[ctx.platform];
const excludedDirs =
ctx.buildConfig?.platforms?.[ctx.platform]?.webpackExcludedDirs ||
engine?.config?.webpackExcludedDirs ||
[];
return { TS_EXCLUDE_SRC_DIRS: excludedDirs.map((dir) => `src/${dir}`).join(',') };
},
RNV_EXTERNAL_PATHS: () => {
const ctx = getContext();
return { RNV_EXTERNAL_PATHS: [ctx.paths.project.assets.dir, ctx.paths.project.dir].join(',') };
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const _runWebDevServer = async (c: RnvContext, enableRemoteDebugger?: boo
...EnvVars.PORT(),
...EnvVars.WEBPACK_TARGET(),
...EnvVars.RNV_EXTERNAL_PATHS(),
...EnvVars.TS_EXCLUDE_SRC_DIRS(),
};

Object.keys(env).forEach((v) => {
Expand Down Expand Up @@ -204,6 +205,7 @@ export const buildCoreWebpackProject = async () => {
...EnvVars.PORT(),
...EnvVars.WEBPACK_TARGET(),
...EnvVars.RNV_EXTERNAL_PATHS(),
...EnvVars.TS_EXCLUDE_SRC_DIRS(),
};
Object.keys(env).forEach((v) => {
process.env[v] = env[v];
Expand Down
Loading