Skip to content

Commit

Permalink
nested paths support
Browse files Browse the repository at this point in the history
  • Loading branch information
pavjacko committed Jul 3, 2024
1 parent 60521bb commit 339ce2c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 60 deletions.
40 changes: 1 addition & 39 deletions packages/core/src/schema/platforms/fragments/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,12 @@ export const zodPlatformWebpackFragment = z
.array(z.string())

.describe('Allows you to inject custom script into html header'),
excludedDirs: z
excludedPaths: z
.array(z.string())
.describe(
'Allows to specify files or directories in the src folder that webpack should ignore when bundling code.'
),
})
.partial(),

// webpackConfig: {
// additionalProperties: true,
// type: 'object',
// properties: {
// publicUrl: {
// type: 'string',
// },
// metaTags: {
// additionalProperties: true,
// type: 'object',
// },
// customScripts: {
// type: 'array',
// },
// extend: {
// additionalProperties: true,
// type: 'object',
// description: 'Allows you to directly extend/override webpack config of your current platform',
// examples: [
// {
// devtool: 'source-map',
// },
// {
// module: {
// rules: [
// {
// test: /\.js$/,
// use: ['source-map-loader'],
// enforce: 'pre',
// },
// ],
// },
// },
// ],
// },
// },
// },
})
.partial();
49 changes: 34 additions & 15 deletions packages/sdk-webpack/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ const env: Env = process?.env;

export const withRNVWebpack = (cnf: Configuration) => {
//TODO: implement further overrides
let rnvConfig: Configuration = {};
rnvConfig = {
const rnvConfig: Configuration = {
module: {
rules: [],
},
resolve: {},
};

rnvConfig?.module?.rules &&
rnvConfig.module.rules.push({
oneOf: [
{
test: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
include: _getIncludedModules((env.WEBPACK_EXCLUDED_DIRS || []).split(',')),
include: _getIncludedModules(env.WEBPACK_EXCLUDED_PATHS),
},
],
});
Expand All @@ -38,9 +38,8 @@ export const withRNVWebpack = (cnf: Configuration) => {
return mergedConfig;
};

// Merge => static config, adapter config , project config
export const getMergedConfig = (rootConfig: Configuration, appPath: string) => {
// RNV-ADDITION

const projectConfig: Configuration = require(path.join(appPath, 'webpack.config'));

const mergedConfig: Configuration = mergeWithCustomize({
Expand All @@ -56,23 +55,43 @@ export const getMergedConfig = (rootConfig: Configuration, appPath: string) => {
},
// 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 _getIncludedPaths = (basePath: string, excludedPaths: 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));
// track excluded sub-paths
const excPathsArrs = excludedPaths.map((p) => p.split('/'));
if (fsExistsSync(basePath)) {
const dirNames = fsReaddirSync(basePath);
dirNames.forEach((dirName) => {
let hasPathExcluded = false;
const subpaths: string[] = [];
excPathsArrs.forEach((pArr) => {
if (pArr[0] === dirName) {
if (pArr.length <= 1) {
hasPathExcluded = true;
} else {
subpaths.push(pArr.slice(1).join('/'));
}
}
});
const incPath = path.join(basePath, dirName);
if (!hasPathExcluded) {
srcDirs.push(incPath);
}
if (subpaths.length) {
// recursively populate included paths
srcDirs.push(..._getIncludedPaths(incPath, subpaths));
}
});
}
return srcDirs;
};

const _getIncludedModules = (excludedPathsStr: string | undefined) => {
const excludedPaths = excludedPathsStr ? excludedPathsStr.split(',') : [];
const srcDirs = _getIncludedPaths(paths.appSrc, excludedPaths);
return env.RNV_MODULE_PATHS ? [...srcDirs, ...env.RNV_MODULE_PATHS.split(',')] : [...srcDirs];
};

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-webpack/src/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ module.exports = function (webpackEnv) {
tsBuildInfoFile: paths.appTsBuildInfoFile,
exclude: [
...excludeTs,
(process.env.TS_EXCLUDE_SRC_DIRS || []).map((dir) => `src/${dir}`).join(','),
(process.env.WEBPACK_EXCLUDED_PATHS || []).map((dir) => `src/${dir}`).join(','),
],
},
},
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk-webpack/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export const EnvVars = {
return { WEBPACK_TARGET: ctx.runtime.webpackTarget };
}
},
WEBPACK_EXCLUDED_DIRS: () => {
WEBPACK_EXCLUDED_PATHS: () => {
const webpackConfig = getConfigProp('webpackConfig');

if (webpackConfig?.excludedDirs) {
return { WEBPACK_EXCLUDED_DIRS: webpackConfig?.excludedDirs };
if (webpackConfig?.excludedPaths) {
return { WEBPACK_EXCLUDED_PATHS: webpackConfig?.excludedPaths };
}
},
RNV_EXTERNAL_PATHS: () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const _runWebDevServer = async (c: RnvContext, enableRemoteDebugger?: boo
...EnvVars.PORT(),
...EnvVars.WEBPACK_TARGET(),
...EnvVars.RNV_EXTERNAL_PATHS(),
...EnvVars.WEBPACK_EXCLUDED_DIRS(),
...EnvVars.WEBPACK_EXCLUDED_PATHS(),
};

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

0 comments on commit 339ce2c

Please sign in to comment.