UNPKG

@modern-js/builder

Version:
122 lines (121 loc) 5.2 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var sourceBuild_exports = {}; __export(sourceBuild_exports, { builderPluginSourceBuild: () => builderPluginSourceBuild, getSourceInclude: () => getSourceInclude, pluginName: () => pluginName }); module.exports = __toCommonJS(sourceBuild_exports); var import_builder_shared = require("@modern-js/builder-shared"); var import_monorepo_utils = require("@rsbuild/monorepo-utils"); var import_utils = require("@modern-js/utils"); var import_path = __toESM(require("path")); const log = (0, import_utils.debug)("BUILDER_PLUGIN_SOURCE_BUILD"); const pluginName = "builder-plugin-source-build"; const getSourceInclude = async (options) => { const { projects, sourceField } = options; const includes = []; for (const project of projects) { includes.push(...project.getSourceEntryPaths({ field: sourceField })); } log(`get include projects: ${includes}`); return includes; }; function builderPluginSourceBuild(options) { const { projectName, sourceField = "source", extraMonorepoStrategies } = options !== null && options !== void 0 ? options : {}; return { name: pluginName, async setup(api) { const projectRootPath = api.context.rootPath; let projects = []; if (api.context.bundlerType === "webpack") { api.modifyBuilderConfig(async (config) => { var _config_experiments; const { sourceBuild } = (_config_experiments = config.experiments) !== null && _config_experiments !== void 0 ? _config_experiments : {}; if (!sourceBuild) { return; } projects = await (0, import_monorepo_utils.getDependentProjects)(projectName || projectRootPath, { cwd: projectRootPath, recursive: true, filter: (0, import_monorepo_utils.filterByField)(sourceField), extraMonorepoStrategies }); const includes = await getSourceInclude({ projects, sourceField }); var _config_source; config.source = (_config_source = config.source) !== null && _config_source !== void 0 ? _config_source : {}; var _config_source_include; config.source.include = [ ...(_config_source_include = config.source.include) !== null && _config_source_include !== void 0 ? _config_source_include : [], ...includes ]; }); api.modifyWebpackChain((chain, { CHAIN_ID }) => { const { experiments: { sourceBuild }, tools: { tsLoader } } = api.getNormalizedConfig(); if (!sourceBuild) { return; } const useTsLoader = Boolean(tsLoader); chain.module.rule(useTsLoader ? CHAIN_ID.RULE.TS : CHAIN_ID.RULE.JS).resolve.mainFields.merge([ sourceField, "..." ]); chain.module.rule(useTsLoader ? CHAIN_ID.RULE.TS : CHAIN_ID.RULE.JS).resolve.merge({ conditionNames: [ "...", sourceField ] }); const { TS_CONFIG_PATHS } = CHAIN_ID.RESOLVE_PLUGIN; if (chain.resolve.plugins.has(TS_CONFIG_PATHS)) { chain.resolve.plugin(TS_CONFIG_PATHS).tap((options2) => { const references = projects.map((project) => import_path.default.join(project.dir, import_builder_shared.TS_CONFIG_FILE)).filter((filePath) => import_utils.fs.existsSync(filePath)).map((filePath) => import_path.default.relative(projectRootPath, filePath)); return options2.map((option) => ({ ...option, references })); }); } }); } } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { builderPluginSourceBuild, getSourceInclude, pluginName });