@modern-js/builder 2.33.1 → 2.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import type { BuilderInstance } from '..';
2
+ export declare function setupProgram(builder: BuilderInstance): void;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "setupProgram", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return setupProgram;
9
+ }
10
+ });
11
+ const _path = require("path");
12
+ const _utils = require("@modern-js/utils");
13
+ const _commander = require("@modern-js/utils/commander");
14
+ function setupProgram(builder) {
15
+ const pkgJson = (0, _path.join)(__dirname, "../../package.json");
16
+ const { version } = _utils.fs.readJSONSync(pkgJson);
17
+ _commander.program.name("builder").usage("<command> [options]").version(version);
18
+ _commander.program.command("dev").description("starting the dev server").action(async () => {
19
+ await builder.startDevServer();
20
+ });
21
+ _commander.program.command("build").description("build the app for production").action(async () => {
22
+ await builder.build();
23
+ });
24
+ _commander.program.command("serve").description("preview the production build locally").action(async () => {
25
+ await builder.serve();
26
+ });
27
+ _commander.program.parse();
28
+ }
@@ -0,0 +1,14 @@
1
+ import type { BuilderEntry, BuilderPlugin } from '@modern-js/builder-shared';
2
+ import type { BuilderConfig as WebpackBuilderConfig } from '@modern-js/builder-webpack-provider';
3
+ import type { BuilderConfig as RspackBuilderConfig } from '@modern-js/builder-rspack-provider';
4
+ export type BuilderConfig<Bundler extends 'rspack' | 'webpack' = 'webpack'> = (Bundler extends 'webpack' ? WebpackBuilderConfig : RspackBuilderConfig) & {
5
+ source?: {
6
+ entries?: BuilderEntry;
7
+ };
8
+ builderPlugins?: BuilderPlugin[];
9
+ };
10
+ export declare const defineConfig: <Bundler extends "webpack" | "rspack" = "webpack">(config: BuilderConfig<Bundler>) => BuilderConfig<Bundler>;
11
+ export declare function loadConfig(): Promise<BuilderConfig>;
12
+ export declare function getDefaultEntries(): {
13
+ index: string;
14
+ };
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for (var name in all)
7
+ Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ defineConfig: function() {
14
+ return defineConfig;
15
+ },
16
+ loadConfig: function() {
17
+ return loadConfig;
18
+ },
19
+ getDefaultEntries: function() {
20
+ return getDefaultEntries;
21
+ }
22
+ });
23
+ const _path = require("path");
24
+ const _utils = require("@modern-js/utils");
25
+ const _fsextra = require("@modern-js/utils/fs-extra");
26
+ const _nodebundlerequire = require("@modern-js/node-bundle-require");
27
+ const defineConfig = (config) => config;
28
+ async function loadConfig() {
29
+ const configFile = (0, _path.join)(process.cwd(), "builder.config.ts");
30
+ if ((0, _fsextra.existsSync)(configFile)) {
31
+ const mod = await (0, _nodebundlerequire.bundleRequire)(configFile);
32
+ return mod.default || mod;
33
+ }
34
+ return {};
35
+ }
36
+ function getDefaultEntries() {
37
+ const cwd = process.cwd();
38
+ const files = [
39
+ "ts",
40
+ "tsx",
41
+ "js",
42
+ "jsx"
43
+ ].map((ext) => (0, _path.join)(cwd, `src/index.${ext}`));
44
+ const entryFile = (0, _utils.findExists)(files);
45
+ if (entryFile) {
46
+ return {
47
+ index: entryFile
48
+ };
49
+ }
50
+ throw new Error("Could not find the entry file, please make sure that `src/index.(js|ts|tsx|jsx)` exists, or customize entry through the `source.entries` configuration.");
51
+ }
@@ -0,0 +1,6 @@
1
+ import { BuilderPlugin } from '..';
2
+ export { defineConfig } from './config';
3
+ type RunCliOptions = {
4
+ defaultPlugins?: BuilderPlugin[];
5
+ };
6
+ export declare function runCli(options?: RunCliOptions): Promise<void>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for (var name in all)
7
+ Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ defineConfig: function() {
14
+ return _config.defineConfig;
15
+ },
16
+ runCli: function() {
17
+ return runCli;
18
+ }
19
+ });
20
+ const _ = require("..");
21
+ const _commands = require("./commands");
22
+ const _provider = require("./provider");
23
+ const _config = require("./config");
24
+ async function runCli(options = {}) {
25
+ var _config_source;
26
+ const provider = await (0, _provider.loadProvider)();
27
+ const config = await (0, _config.loadConfig)();
28
+ const builder = await (0, _.createBuilder)(provider({
29
+ builderConfig: config
30
+ }), {
31
+ entry: ((_config_source = config.source) === null || _config_source === void 0 ? void 0 : _config_source.entries) || (0, _config.getDefaultEntries)()
32
+ });
33
+ if (options.defaultPlugins) {
34
+ builder.addPlugins(options.defaultPlugins);
35
+ }
36
+ if (config.builderPlugins) {
37
+ builder.addPlugins(config.builderPlugins);
38
+ }
39
+ (0, _commands.setupProgram)(builder);
40
+ }
@@ -0,0 +1,2 @@
1
+ export declare function getProviderType(): "webpack" | "rspack";
2
+ export declare function loadProvider(): Promise<typeof import("@modern-js/builder-rspack-provider").builderRspackProvider | typeof import("@modern-js/builder-webpack-provider").builderWebpackProvider>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for (var name in all)
7
+ Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ getProviderType: function() {
14
+ return getProviderType;
15
+ },
16
+ loadProvider: function() {
17
+ return loadProvider;
18
+ }
19
+ });
20
+ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
21
+ const _buildershared = require("@modern-js/builder-shared");
22
+ const _utils = require("@modern-js/utils");
23
+ function getProviderType() {
24
+ const root = process.cwd();
25
+ if ((0, _utils.isPackageInstalled)(_buildershared.RSPACK_PROVIDER, root)) {
26
+ return "rspack";
27
+ }
28
+ if ((0, _utils.isPackageInstalled)(_buildershared.WEBPACK_PROVIDER, root)) {
29
+ return "webpack";
30
+ }
31
+ throw new Error(`Failed to load builder provider, please check if you have "${_buildershared.RSPACK_PROVIDER}" or "${_buildershared.WEBPACK_PROVIDER}" installed`);
32
+ }
33
+ async function loadProvider() {
34
+ const providerType = getProviderType();
35
+ if (providerType === "rspack") {
36
+ const { builderRspackProvider } = await Promise.resolve().then(() => /* @__PURE__ */ _interop_require_wildcard._(require("@modern-js/builder-rspack-provider")));
37
+ return builderRspackProvider;
38
+ }
39
+ const { builderWebpackProvider } = await Promise.resolve().then(() => /* @__PURE__ */ _interop_require_wildcard._(require("@modern-js/builder-webpack-provider")));
40
+ return builderWebpackProvider;
41
+ }
@@ -8,7 +8,9 @@ Object.defineProperty(exports, "builderPluginCache", {
8
8
  return builderPluginCache;
9
9
  }
10
10
  });
11
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
11
12
  const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
13
+ const _crypto = /* @__PURE__ */ _interop_require_default._(require("crypto"));
12
14
  const _path = require("path");
13
15
  const _buildershared = require("@modern-js/builder-shared");
14
16
  async function validateCache(cacheDirectory, buildDependencies) {
@@ -23,6 +25,11 @@ async function validateCache(cacheDirectory, buildDependencies) {
23
25
  }
24
26
  await fs.outputJSON(configFile, buildDependencies);
25
27
  }
28
+ function getDigestHash(digest) {
29
+ const fsHash = _crypto.default.createHash("md5");
30
+ const md5 = fsHash.update(JSON.stringify(digest)).digest("hex").slice(0, 8);
31
+ return md5;
32
+ }
26
33
  function getCacheDirectory({ cacheDirectory }, context) {
27
34
  if (cacheDirectory) {
28
35
  return (0, _path.isAbsolute)(cacheDirectory) ? cacheDirectory : (0, _path.join)(context.rootPath, cacheDirectory);
@@ -33,11 +40,12 @@ async function getBuildDependencies(context) {
33
40
  const { findExists } = await Promise.resolve().then(() => /* @__PURE__ */ _interop_require_wildcard._(require("@modern-js/utils")));
34
41
  const rootPackageJson = (0, _path.join)(context.rootPath, "package.json");
35
42
  const browserslistConfig = (0, _path.join)(context.rootPath, ".browserslistrc");
36
- const buildDependencies = {
37
- packageJson: [
43
+ const buildDependencies = {};
44
+ if (await (0, _buildershared.isFileExists)(rootPackageJson)) {
45
+ buildDependencies.packageJson = [
38
46
  rootPackageJson
39
- ]
40
- };
47
+ ];
48
+ }
41
49
  if (context.configPath) {
42
50
  buildDependencies.config = [
43
51
  context.configPath
@@ -82,10 +90,11 @@ const builderPluginCache = () => ({
82
90
  const cacheDirectory = getCacheDirectory(cacheConfig, context);
83
91
  const buildDependencies = await getBuildDependencies(context);
84
92
  await validateCache(cacheDirectory, buildDependencies);
93
+ const useDigest = Array.isArray(cacheConfig.cacheDigest) && cacheConfig.cacheDigest.length;
85
94
  chain.cache({
86
95
  // The default cache name of webpack is '${name}-${env}', and the `name` is `default` by default.
87
96
  // We set cache name to avoid cache conflicts of different targets.
88
- name: `${target}-${env}`,
97
+ name: useDigest ? `${target}-${env}-${getDigestHash(cacheConfig.cacheDigest)}` : `${target}-${env}`,
89
98
  type: "filesystem",
90
99
  cacheDirectory,
91
100
  buildDependencies
@@ -28,6 +28,7 @@ function builderPluginCheckSyntax() {
28
28
  chain.plugin(CheckSyntaxPlugin.name).use(CheckSyntaxPlugin, [
29
29
  {
30
30
  targets,
31
+ rootPath: api.context.rootPath,
31
32
  ...typeof checkSyntax === "object" ? checkSyntax : {}
32
33
  }
33
34
  ]);
@@ -25,15 +25,16 @@ const builderPluginInlineChunk = () => ({
25
25
  // todo: not support enableInlineScripts in Rspack yet, which will take unknown build error
26
26
  enableInlineScripts
27
27
  } = config.output;
28
- const tests = [];
28
+ const scriptTests = [];
29
+ const styleTests = [];
29
30
  if (enableInlineScripts) {
30
- tests.push(enableInlineScripts === true ? /\.js$/ : enableInlineScripts);
31
+ scriptTests.push(enableInlineScripts === true ? _buildershared.JS_REGEX : enableInlineScripts);
31
32
  }
32
33
  if (enableInlineStyles) {
33
- tests.push(enableInlineStyles === true ? /\.css$/ : enableInlineStyles);
34
+ styleTests.push(enableInlineStyles === true ? _buildershared.CSS_REGEX : enableInlineStyles);
34
35
  }
35
36
  if (!disableInlineRuntimeChunk) {
36
- tests.push(
37
+ scriptTests.push(
37
38
  // RegExp like /builder-runtime([.].+)?\.js$/
38
39
  // matches builder-runtime.js and builder-runtime.123456.js
39
40
  new RegExp(`${_buildershared.RUNTIME_CHUNK_NAME}([.].+)?\\.js$`)
@@ -42,7 +43,8 @@ const builderPluginInlineChunk = () => ({
42
43
  chain.plugin(CHAIN_ID.PLUGIN.INLINE_HTML).use(InlineChunkHtmlPlugin, [
43
44
  HtmlPlugin,
44
45
  {
45
- tests,
46
+ styleTests,
47
+ scriptTests,
46
48
  distPath: (0, _buildershared.pick)(config.output.distPath, [
47
49
  "js",
48
50
  "css"
@@ -1,13 +1,11 @@
1
- import type { BuilderPlugin } from '@modern-js/builder-shared';
1
+ import { type BuilderPlugin } from '@modern-js/builder-shared';
2
2
  import type { BuilderPluginAPI as WebpackBuilderPluginAPI } from '@modern-js/builder-webpack-provider';
3
3
  import type { BuilderPluginAPI as RspackBuilderPluginAPI } from '@modern-js/builder-rspack-provider';
4
- import { type ExtraMonorepoStrategies } from '@modern-js/monorepo-utils';
4
+ import { type Project, type ExtraMonorepoStrategies } from '@modern-js/monorepo-utils';
5
5
  export declare const pluginName = "builder-plugin-source-build";
6
6
  export declare const getSourceInclude: (options: {
7
- projectNameOrRootPath: string;
8
- findMonorepoStartPath: string;
7
+ projects: Project[];
9
8
  sourceField: string;
10
- extraMonorepoStrategies?: ExtraMonorepoStrategies;
11
9
  }) => Promise<string[]>;
12
10
  export interface PluginSourceBuildOptions {
13
11
  projectName?: string;
@@ -20,18 +20,15 @@ _export(exports, {
20
20
  return builderPluginSourceBuild;
21
21
  }
22
22
  });
23
+ const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
24
+ const _buildershared = require("@modern-js/builder-shared");
23
25
  const _monorepoutils = require("@modern-js/monorepo-utils");
24
26
  const _utils = require("@modern-js/utils");
27
+ const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
25
28
  const log = (0, _utils.debug)("BUILDER_PLUGIN_SOURCE_BUILD");
26
29
  const pluginName = "builder-plugin-source-build";
27
30
  const getSourceInclude = async (options) => {
28
- const { projectNameOrRootPath, sourceField, extraMonorepoStrategies, findMonorepoStartPath } = options;
29
- const projects = await (0, _monorepoutils.getDependentProjects)(projectNameOrRootPath, {
30
- cwd: findMonorepoStartPath,
31
- recursive: true,
32
- filter: (0, _monorepoutils.filterByField)(sourceField),
33
- extraMonorepoStrategies
34
- });
31
+ const { projects, sourceField } = options;
35
32
  const includes = [];
36
33
  for (const project of projects) {
37
34
  includes.push(...project.getSourceEntryPaths({
@@ -47,19 +44,24 @@ function builderPluginSourceBuild(options) {
47
44
  name: pluginName,
48
45
  async setup(api) {
49
46
  const projectRootPath = api.context.rootPath;
47
+ let projects = [];
50
48
  if (api.context.bundlerType === "webpack") {
51
49
  api.modifyBuilderConfig(async (config) => {
52
50
  var _config_experiments;
53
- const { sourceBuild = true } = (_config_experiments = config.experiments) !== null && _config_experiments !== void 0 ? _config_experiments : {};
51
+ const { sourceBuild } = (_config_experiments = config.experiments) !== null && _config_experiments !== void 0 ? _config_experiments : {};
54
52
  if (!sourceBuild) {
55
53
  return;
56
54
  }
57
- const includes = await getSourceInclude({
58
- projectNameOrRootPath: projectName || projectRootPath,
59
- sourceField,
60
- findMonorepoStartPath: projectRootPath,
55
+ projects = await (0, _monorepoutils.getDependentProjects)(projectName || projectRootPath, {
56
+ cwd: projectRootPath,
57
+ recursive: true,
58
+ filter: (0, _monorepoutils.filterByField)(sourceField),
61
59
  extraMonorepoStrategies
62
60
  });
61
+ const includes = await getSourceInclude({
62
+ projects,
63
+ sourceField
64
+ });
63
65
  var _config_source;
64
66
  config.source = (_config_source = config.source) !== null && _config_source !== void 0 ? _config_source : {};
65
67
  var _config_source_include;
@@ -68,7 +70,7 @@ function builderPluginSourceBuild(options) {
68
70
  ...includes
69
71
  ];
70
72
  });
71
- api.modifyBundlerChain((chain, { CHAIN_ID }) => {
73
+ api.modifyWebpackChain((chain, { CHAIN_ID }) => {
72
74
  const { experiments: { sourceBuild }, tools: { tsLoader } } = api.getNormalizedConfig();
73
75
  if (!sourceBuild) {
74
76
  return;
@@ -84,6 +86,16 @@ function builderPluginSourceBuild(options) {
84
86
  sourceField
85
87
  ]
86
88
  });
89
+ const { TS_CONFIG_PATHS } = CHAIN_ID.RESOLVE_PLUGIN;
90
+ if (chain.resolve.plugins.has(TS_CONFIG_PATHS)) {
91
+ chain.resolve.plugin(TS_CONFIG_PATHS).tap((options2) => {
92
+ const references = projects.map((project) => _path.default.join(project.dir, _buildershared.TS_CONFIG_FILE)).filter((filePath) => _utils.fs.existsSync(filePath)).map((filePath) => _path.default.relative(projectRootPath, filePath));
93
+ return options2.map((option) => ({
94
+ ...option,
95
+ references
96
+ }));
97
+ });
98
+ }
87
99
  });
88
100
  }
89
101
  }
@@ -49,7 +49,7 @@ async function splitByExperience(ctx) {
49
49
  const { override, polyfill, rootPath, defaultConfig, userDefinedCacheGroups } = ctx;
50
50
  const experienceCacheGroup = {};
51
51
  const packageRegExps = {
52
- react: createDependenciesRegExp("react", "react-dom"),
52
+ react: createDependenciesRegExp("react", "react-dom", "scheduler"),
53
53
  router: createDependenciesRegExp("react-router", "react-router-dom", "@remix-run/router", "history"),
54
54
  lodash: createDependenciesRegExp("lodash", "lodash-es"),
55
55
  axios: createDependenciesRegExp("axios", /axios-.+/)
package/package.json CHANGED
@@ -18,32 +18,46 @@
18
18
  "engines": {
19
19
  "node": ">=14.0.0"
20
20
  },
21
- "version": "2.33.1",
21
+ "version": "2.35.0",
22
22
  "jsnext:source": "./src/index.ts",
23
23
  "types": "./dist/index.d.ts",
24
24
  "main": "./dist/index.js",
25
25
  "module": "./dist/index.js",
26
26
  "exports": {
27
27
  ".": {
28
+ "types": "./dist/index.d.ts",
28
29
  "jsnext:source": "./src/index.ts",
29
30
  "default": "./dist/index.js"
31
+ },
32
+ "./cli": {
33
+ "types": "./dist/cli/index.d.ts",
34
+ "jsnext:source": "./src/cli/index.ts",
35
+ "default": "./dist/cli/index.js"
36
+ }
37
+ },
38
+ "typesVersions": {
39
+ "*": {
40
+ "cli": [
41
+ "./dist/cli/index.d.ts"
42
+ ]
30
43
  }
31
44
  },
32
45
  "dependencies": {
33
46
  "@svgr/webpack": "8.0.1",
34
47
  "@swc/helpers": "0.5.1",
35
- "@modern-js/builder-shared": "2.33.1",
36
- "@modern-js/utils": "2.33.1",
37
- "@modern-js/monorepo-utils": "2.33.1"
48
+ "@modern-js/builder-shared": "2.35.0",
49
+ "@modern-js/monorepo-utils": "2.35.0",
50
+ "@modern-js/node-bundle-require": "2.35.0",
51
+ "@modern-js/utils": "2.35.0"
38
52
  },
39
53
  "devDependencies": {
40
54
  "@types/babel__core": "^7.20.0",
41
55
  "@types/node": "^14",
42
56
  "typescript": "^5",
43
- "@modern-js/builder-webpack-provider": "2.33.1",
44
- "@modern-js/builder-rspack-provider": "2.33.1",
45
- "@scripts/build": "2.33.1",
46
- "@scripts/vitest-config": "2.33.1"
57
+ "@modern-js/builder-webpack-provider": "2.35.0",
58
+ "@modern-js/builder-rspack-provider": "2.35.0",
59
+ "@scripts/build": "2.35.0",
60
+ "@scripts/vitest-config": "2.35.0"
47
61
  },
48
62
  "publishConfig": {
49
63
  "registry": "https://registry.npmjs.org/",