@modern-js/builder 2.0.0-beta.6 → 2.0.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 { DefaultBuilderPlugin } from '@modern-js/builder-shared';
2
+ export declare const PluginCache: () => DefaultBuilderPlugin;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.PluginCache = void 0;
27
+ const path_1 = require("path");
28
+ const builder_shared_1 = require("@modern-js/builder-shared");
29
+ async function validateCache(cacheDirectory, buildDependencies) {
30
+ const { fs } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils')));
31
+ const configFile = (0, path_1.join)(cacheDirectory, 'buildDependencies.json');
32
+ if (await (0, builder_shared_1.isFileExists)(configFile)) {
33
+ const prevBuildDependencies = await fs.readJSON(configFile);
34
+ if (JSON.stringify(prevBuildDependencies) ===
35
+ JSON.stringify(buildDependencies)) {
36
+ return;
37
+ }
38
+ /**
39
+ * If the filenames in the buildDependencies are changed, webpack will not invalidate the previous cache.
40
+ * So we need to remove the cache directory to make sure the cache is invalidated.
41
+ */
42
+ await fs.remove(cacheDirectory);
43
+ }
44
+ await fs.outputJSON(configFile, buildDependencies);
45
+ }
46
+ function getCacheDirectory({ cacheDirectory }, context) {
47
+ if (cacheDirectory) {
48
+ return (0, path_1.isAbsolute)(cacheDirectory)
49
+ ? cacheDirectory
50
+ : (0, path_1.join)(context.rootPath, cacheDirectory);
51
+ }
52
+ return (0, path_1.join)(context.cachePath, context.bundlerType);
53
+ }
54
+ const PluginCache = () => ({
55
+ name: 'builder-plugin-cache',
56
+ setup(api) {
57
+ api.modifyBundlerChain(async (chain, { target, env }) => {
58
+ const { buildCache } = api.getNormalizedConfig().performance;
59
+ if (buildCache === false) {
60
+ chain.cache(false);
61
+ return;
62
+ }
63
+ const { context } = api;
64
+ const cacheConfig = typeof buildCache === 'boolean' ? {} : buildCache;
65
+ const cacheDirectory = getCacheDirectory(cacheConfig, context);
66
+ const rootPackageJson = (0, path_1.join)(context.rootPath, 'package.json');
67
+ const browserslistConfig = (0, path_1.join)(context.rootPath, '.browserslistrc');
68
+ /**
69
+ * webpack can't detect the changes of framework config, tsconfig and browserslist config.
70
+ * but they will affect the compilation result, so they need to be added to buildDependencies.
71
+ */
72
+ const buildDependencies = {
73
+ packageJson: [rootPackageJson],
74
+ };
75
+ if (context.configPath) {
76
+ buildDependencies.config = [context.configPath];
77
+ }
78
+ if (context.tsconfigPath) {
79
+ buildDependencies.tsconfig = [context.tsconfigPath];
80
+ }
81
+ if (await (0, builder_shared_1.isFileExists)(browserslistConfig)) {
82
+ buildDependencies.browserslistrc = [browserslistConfig];
83
+ }
84
+ await validateCache(cacheDirectory, buildDependencies);
85
+ chain.cache({
86
+ // The default cache name of webpack is '${name}-${env}', and the `name` is `default` by default.
87
+ // We set cache name to avoid cache conflicts of different targets.
88
+ name: `${target}-${env}`,
89
+ type: 'filesystem',
90
+ cacheDirectory,
91
+ buildDependencies,
92
+ });
93
+ });
94
+ },
95
+ });
96
+ exports.PluginCache = PluginCache;
@@ -0,0 +1,2 @@
1
+ import { DefaultBuilderPlugin } from '@modern-js/builder-shared';
2
+ export declare const PluginEntry: () => DefaultBuilderPlugin;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PluginEntry = void 0;
7
+ const lodash_1 = __importDefault(require("@modern-js/utils/lodash"));
8
+ const PluginEntry = () => ({
9
+ name: 'builder-plugin-entry',
10
+ setup(api) {
11
+ api.modifyBundlerChain(async (chain) => {
12
+ const { entry } = api.context;
13
+ const { preEntry } = api.getNormalizedConfig().source;
14
+ Object.keys(entry).forEach(entryName => {
15
+ const appendEntry = (file) => chain.entry(entryName).add(file);
16
+ preEntry.forEach(appendEntry);
17
+ lodash_1.default.castArray(entry[entryName]).forEach(appendEntry);
18
+ });
19
+ });
20
+ },
21
+ });
22
+ exports.PluginEntry = PluginEntry;
@@ -4,4 +4,6 @@ export declare const plugins: {
4
4
  fileSize: () => Promise<import("@modern-js/builder-shared").DefaultBuilderPlugin>;
5
5
  devtool: () => Promise<import("@modern-js/builder-shared").DefaultBuilderPlugin>;
6
6
  target: () => Promise<import("@modern-js/builder-shared").DefaultBuilderPlugin>;
7
+ entry: () => Promise<import("@modern-js/builder-shared").DefaultBuilderPlugin>;
8
+ cache: () => Promise<import("@modern-js/builder-shared").DefaultBuilderPlugin>;
7
9
  };
@@ -30,4 +30,6 @@ exports.plugins = {
30
30
  fileSize: () => Promise.resolve().then(() => __importStar(require('./fileSize'))).then(m => m.PluginFileSize()),
31
31
  devtool: () => Promise.resolve().then(() => __importStar(require('./devtool'))).then(m => m.PluginDevtool()),
32
32
  target: () => Promise.resolve().then(() => __importStar(require('./target'))).then(m => m.PluginTarget()),
33
+ entry: () => Promise.resolve().then(() => __importStar(require('./entry'))).then(m => m.PluginEntry()),
34
+ cache: () => Promise.resolve().then(() => __importStar(require('./cache'))).then(m => m.PluginCache()),
33
35
  };
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "engines": {
15
15
  "node": ">=14.0.0"
16
16
  },
17
- "version": "2.0.0-beta.6",
17
+ "version": "2.0.0",
18
18
  "jsnext:source": "./src/index.ts",
19
19
  "types": "./dist/index.d.ts",
20
20
  "main": "./dist/index.js",
@@ -27,8 +27,8 @@
27
27
  }
28
28
  },
29
29
  "dependencies": {
30
- "@modern-js/builder-shared": "2.0.0-beta.6",
31
- "@modern-js/utils": "2.0.0-beta.6"
30
+ "@modern-js/builder-shared": "2.0.0",
31
+ "@modern-js/utils": "2.0.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@babel/core": "7.18.0",
@@ -36,7 +36,7 @@
36
36
  "@types/babel__preset-env": "^7.9.2",
37
37
  "@types/node": "^14",
38
38
  "typescript": "^4",
39
- "@scripts/vitest-config": "2.0.0-beta.6"
39
+ "@scripts/vitest-config": "2.0.0"
40
40
  },
41
41
  "publishConfig": {
42
42
  "registry": "https://registry.npmjs.org/",
package/vitest.config.ts CHANGED
@@ -12,6 +12,7 @@ const config = defineConfig({
12
12
  test: {
13
13
  root: __dirname,
14
14
  environment: 'node',
15
+ setupFiles: ['./tests/setup.ts'],
15
16
  },
16
17
  });
17
18