@ray-js/builder-mp 1.3.1 → 1.3.3

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.
@@ -8,6 +8,7 @@ declare class WorkerBuild {
8
8
  private routesConfigFile;
9
9
  constructor(options: CliOptions, parent: webpack.Compiler);
10
10
  private createWebpackConfig;
11
+ private readConfig;
11
12
  private initWorkerBuilder;
12
13
  private runOrWatch;
13
14
  run(): webpack.Compiler;
@@ -8,6 +8,7 @@ const path_1 = __importDefault(require("path"));
8
8
  const colors_1 = __importDefault(require("colors"));
9
9
  const shared_1 = require("@ray-js/shared");
10
10
  const legacyExport_1 = require("@ray-core/cli/lib/legacyExport");
11
+ const slash_1 = __importDefault(require("slash"));
11
12
  const builder_1 = require("./builder");
12
13
  const onBuildEnd_1 = __importDefault(require("./plugins/onBuildEnd"));
13
14
  const utils_1 = require("./utils");
@@ -15,6 +16,7 @@ colors_1.default.enable();
15
16
  const extensions = ['.mjs', '.js', '.jsx', '.ts', '.tsx'];
16
17
  const moduleMatcher = new RegExp(`(${extensions.join('|')})$`);
17
18
  const LOG_PREFIX_WORKER = 'build-worker';
19
+ const BUILD_WORKER_PLUGIN = `${LOG_PREFIX_WORKER}-plugin`;
18
20
  const innerPlugins = [(0, onBuildEnd_1.default)(LOG_PREFIX_WORKER)];
19
21
  class WorkerBuild {
20
22
  constructor(options, parent) {
@@ -47,12 +49,13 @@ class WorkerBuild {
47
49
  .options({
48
50
  configFile: path_1.default.resolve(__dirname, './babel/configs/babel.config.js'),
49
51
  });
52
+ const { root } = this.readConfig();
50
53
  config.output
51
54
  .set('filename', '[name].js')
52
55
  .set('libraryTarget', 'commonjs2')
53
56
  .set('clean', false)
54
57
  .set('globalObject', 'worker')
55
- .set('path', output)
58
+ .set('path', root ? path_1.default.join(output, root) : output)
56
59
  .set('publicPath', '/')
57
60
  .end()
58
61
  .optimization.set('runtimeChunk', { name: 'runtime-worker' })
@@ -77,29 +80,40 @@ class WorkerBuild {
77
80
  'process.env.PLATFORM': JSON.stringify(target),
78
81
  },
79
82
  ])
83
+ .end()
84
+ .plugin(BUILD_WORKER_PLUGIN)
85
+ .use(BuildWorkerPlugin)
80
86
  .end()
81
87
  .watchOptions({ aggregateTimeout: 1000 });
82
88
  innerPlugins.forEach((p) => p.configWebpack({ config }));
83
89
  config.resolve.merge(this.parent.options.resolve);
84
90
  return config.toConfig();
85
91
  }
92
+ readConfig() {
93
+ const { source } = this.options;
94
+ this.routesConfigFile = builder_1.builder.api.searchJSFile(`${source}/routes.config`);
95
+ if (!this.routesConfigFile) {
96
+ shared_1.log.error(LOG_PREFIX_WORKER, `missing configuration file (routes.config.{ts|js})`);
97
+ return {};
98
+ }
99
+ delete require.cache[this.routesConfigFile];
100
+ const data = builder_1.builder.api.requireJSFile(this.routesConfigFile).workers || {};
101
+ const { root } = data;
102
+ if (!root) {
103
+ shared_1.log.verbose(LOG_PREFIX_WORKER, `missing prop 'workers' in ${this.routesConfigFile}`);
104
+ return {};
105
+ }
106
+ return data;
107
+ }
86
108
  initWorkerBuilder() {
87
109
  const cfg = this.createWebpackConfig();
88
110
  cfg.entry = () => {
89
- this.routesConfigFile = builder_1.builder.api.searchJSFile('src/routes.config');
90
- if (!this.routesConfigFile) {
91
- shared_1.log.error(LOG_PREFIX_WORKER, `missing configuration file (routes.config.{ts|js})`);
92
- return {};
93
- }
94
- delete require.cache[this.routesConfigFile];
95
- const { root, workers = [] } = builder_1.builder.api.requireJSFile(this.routesConfigFile).workers || {};
96
- if (!root) {
97
- shared_1.log.verbose(LOG_PREFIX_WORKER, `missing prop 'workers' in ${this.routesConfigFile}`);
98
- return {};
99
- }
111
+ const { root, workers = [] } = this.readConfig();
100
112
  const entries = workers.reduce((o, k) => {
101
113
  const { source: rootDir, target, cwd } = this.options;
102
114
  const name = path_1.default.join(root, k);
115
+ const ext = path_1.default.extname(k);
116
+ const outputName = k.replace(new RegExp(`${ext}$`), '');
103
117
  const f = path_1.default.join(cwd, rootDir, name);
104
118
  const m = (0, utils_1.getModuleOfPlatform)(f, {
105
119
  rootDir,
@@ -108,7 +122,7 @@ class WorkerBuild {
108
122
  extensions: ['ts', 'js', 'tsx', 'jsx'],
109
123
  });
110
124
  if (m) {
111
- o[name] = m[1];
125
+ o[outputName] = m[1];
112
126
  }
113
127
  else {
114
128
  shared_1.log.warn(LOG_PREFIX_WORKER, `Can't resolve module ${k} in ${root}`);
@@ -175,3 +189,38 @@ class WorkerBuild {
175
189
  }
176
190
  }
177
191
  exports.default = WorkerBuild;
192
+ const { ConcatSource } = legacyExport_1.customWebpack.sources;
193
+ class BuildWorkerPlugin {
194
+ apply(compiler) {
195
+ compiler.hooks.thisCompilation.tap(BUILD_WORKER_PLUGIN, (compilation) => {
196
+ compilation.hooks.processAssets.tapAsync({
197
+ name: BUILD_WORKER_PLUGIN,
198
+ stage: legacyExport_1.customWebpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
199
+ }, (chunks, callback) => {
200
+ compilation.chunkGroups.forEach((group) => {
201
+ group.chunks.reverse().forEach((chunk) => {
202
+ this.requireChunks(compilation, chunk, group);
203
+ });
204
+ });
205
+ callback();
206
+ });
207
+ });
208
+ }
209
+ requireChunks(compilation, chunk, group) {
210
+ var _a;
211
+ // require 相关的 chunk
212
+ if (chunk.name !== group.name) {
213
+ const requires = [];
214
+ const files = Array.from(chunk.files);
215
+ files.forEach((file) => {
216
+ if (file.endsWith('.js')) {
217
+ const relativePath = (0, slash_1.default)(path_1.default.relative(path_1.default.dirname(group.name), file));
218
+ requires.push(`require('./${relativePath}');\n`);
219
+ }
220
+ });
221
+ const assetPath = group.name + '.js';
222
+ const source = (_a = compilation.assets[assetPath]) !== null && _a !== void 0 ? _a : '';
223
+ compilation.assets[assetPath] = new ConcatSource(...requires, source);
224
+ }
225
+ }
226
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/builder-mp",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Ray builder for mini program",
5
5
  "keywords": [
6
6
  "ray"
@@ -26,14 +26,14 @@
26
26
  "@babel/template": "^7.16.0",
27
27
  "@babel/traverse": "^7.16.3",
28
28
  "@babel/types": "^7.16.0",
29
- "@ray-core/babel-preset-remax": "^0.3.0",
30
- "@ray-core/build-store": "^0.3.0",
31
- "@ray-core/cli": "^0.3.0",
32
- "@ray-core/ray": "^0.3.0",
33
- "@ray-js/adapter": "^1.3.1",
29
+ "@ray-core/babel-preset-remax": "^0.3.5",
30
+ "@ray-core/build-store": "^0.3.5",
31
+ "@ray-core/cli": "^0.3.5",
32
+ "@ray-core/ray": "^0.3.5",
33
+ "@ray-js/adapter": "^1.3.3",
34
34
  "@ray-js/rjs-for-wechat": "^0.0.3",
35
- "@ray-js/shared": "^1.3.1",
36
- "@ray-js/types": "^1.3.1",
35
+ "@ray-js/shared": "^1.3.3",
36
+ "@ray-js/types": "^1.3.3",
37
37
  "babel-loader": "^8.2.3",
38
38
  "babel-plugin-minify-dead-code-elimination": "^0.5.1",
39
39
  "babel-plugin-transform-prune-unused-imports": "^1.0.1",
@@ -50,7 +50,7 @@
50
50
  "webpack-virtual-modules": "^0.4.4"
51
51
  },
52
52
  "devDependencies": {
53
- "@ray-core/types": "^0.3.0",
53
+ "@ray-core/types": "^0.3.5",
54
54
  "@types/jest": "^27.0.2",
55
55
  "@types/node": "^16.9.1",
56
56
  "babel-plugin-tester": "^10.1.0",
@@ -64,6 +64,6 @@
64
64
  "email": "tuyafe@tuya.com"
65
65
  }
66
66
  ],
67
- "gitHead": "4844c0f03bb434999b2e93196dba1cd53b9b6c56",
67
+ "gitHead": "78ca2269ac5efb52e102029258aab74c0a700c68",
68
68
  "repository": {}
69
69
  }