@ray-js/builder-mp 0.20.0-beta.2 → 0.20.0-beta.4

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.
package/lib/build.js CHANGED
@@ -17,8 +17,9 @@ const shared_1 = require("@ray-js/shared");
17
17
  const legacyExport_1 = require("@ray-core/cli/lib/legacyExport");
18
18
  const package_json_1 = __importDefault(require("@ray-core/cli/package.json"));
19
19
  const builder_1 = require("./builder");
20
- const builder_rjs_1 = __importDefault(require("./builder.rjs"));
21
20
  const ray_core_1 = require("./ray-core");
21
+ const build_rjs_1 = __importDefault(require("./build.rjs"));
22
+ const build_worker_1 = __importDefault(require("./build.worker"));
22
23
  colors_1.default.enable();
23
24
  const LOG_PREFIX = 'builder-mp';
24
25
  const defaultCompileOptions = {
@@ -57,9 +58,15 @@ function build(options, context) {
57
58
  }
58
59
  const rayCoreOpts = (0, ray_core_1.getRayCoreOptions)();
59
60
  const rayApi = new ray_core_1.RayAPI(rayCoreOpts);
60
- // 编译除rjs模块外的小程序模块
61
61
  const compiler = (0, legacyExport_1.buildMini)(rayApi, rayCoreOpts);
62
- builder_rjs_1.default.init(compiler, compileOptions); // TODO rjs 可用动态虚拟模块实现单webpack编译
62
+ const workerBuild = new build_worker_1.default(compileOptions, compiler);
63
+ workerBuild.run();
64
+ if (target === 'thing') {
65
+ const rjsBuild = new build_rjs_1.default(compileOptions, compiler);
66
+ compiler.hooks.afterCompile.tap(LOG_PREFIX, () => {
67
+ rjsBuild.run();
68
+ });
69
+ }
63
70
  return compiler;
64
71
  });
65
72
  }
@@ -0,0 +1,14 @@
1
+ import { CliOptions } from '@ray-js/types';
2
+ import { customWebpack as webpack } from '@ray-core/cli/lib/legacyExport';
3
+ declare class RjsBuild {
4
+ private options;
5
+ private compiler;
6
+ private parent;
7
+ private watcher;
8
+ constructor(options: CliOptions, parent: webpack.Compiler);
9
+ private createWebpackConfig;
10
+ private initRjsBuilder;
11
+ private runOrWatch;
12
+ run(): webpack.Compiler;
13
+ }
14
+ export default RjsBuild;
@@ -0,0 +1,140 @@
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
+ const webpack_chain_1 = __importDefault(require("webpack-chain"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const colors_1 = __importDefault(require("colors"));
9
+ const shared_1 = require("@ray-js/shared");
10
+ const legacyExport_1 = require("@ray-core/cli/lib/legacyExport");
11
+ const builder_1 = require("./builder");
12
+ const onBuildEnd_1 = __importDefault(require("./plugins/onBuildEnd"));
13
+ const optimizeRjsEntries_1 = __importDefault(require("./plugins/optimizeRjsEntries"));
14
+ colors_1.default.enable();
15
+ const extensions = ['.mjs', '.js', '.jsx', '.ts', '.tsx'];
16
+ const moduleMatcher = new RegExp(`(${extensions.join('|')})$`);
17
+ const LOG_PREFIX_RJS = 'build-rjs';
18
+ const innerPlugins = [(0, optimizeRjsEntries_1.default)(), (0, onBuildEnd_1.default)(LOG_PREFIX_RJS)];
19
+ class RjsBuild {
20
+ constructor(options, parent) {
21
+ this.options = options;
22
+ this.parent = parent;
23
+ this.initRjsBuilder();
24
+ }
25
+ createWebpackConfig() {
26
+ const { cwd, output, mini, target } = this.options;
27
+ let nodeEnv = process.env.NODE_ENV;
28
+ if (nodeEnv !== 'development' && nodeEnv !== 'production') {
29
+ nodeEnv = 'development';
30
+ }
31
+ const config = new webpack_chain_1.default();
32
+ config
33
+ .context(cwd)
34
+ .cache({
35
+ type: 'filesystem',
36
+ cacheDirectory: path_1.default.join(cwd, 'node_modules', '.rjs_cache'),
37
+ })
38
+ .devtool('cheap-module-source-map')
39
+ .mode(nodeEnv)
40
+ .module.rule('js')
41
+ .test(moduleMatcher)
42
+ .exclude.add(/node_modules/)
43
+ .end()
44
+ .use('babel-loader')
45
+ .loader('babel-loader')
46
+ .options({
47
+ configFile: path_1.default.resolve(__dirname, './babel/configs/babel.config.js'),
48
+ });
49
+ config.output
50
+ .set('filename', '[name].rjs')
51
+ .set('libraryTarget', 'commonjs2')
52
+ .set('clean', false)
53
+ .set('globalObject', 'Render')
54
+ .set('path', output)
55
+ .set('publicPath', '/')
56
+ .end()
57
+ .optimization.set('runtimeChunk', { name: 'runtime-rjs' })
58
+ .set('splitChunks', {
59
+ cacheGroups: {
60
+ vendors: {
61
+ name: 'ray-vendors-rjs',
62
+ test: moduleMatcher,
63
+ chunks: 'initial',
64
+ minChunks: 2,
65
+ minSize: 0,
66
+ priority: 2,
67
+ },
68
+ },
69
+ })
70
+ .set('minimize', mini !== null && mini !== void 0 ? mini : false)
71
+ .end()
72
+ .plugin('defined')
73
+ .use(legacyExport_1.customWebpack.DefinePlugin, [
74
+ {
75
+ 'process.env.NODE_ENV': JSON.stringify(nodeEnv),
76
+ 'process.env.PLATFORM': JSON.stringify(target),
77
+ },
78
+ ])
79
+ .end()
80
+ .watchOptions({ aggregateTimeout: 1000 });
81
+ innerPlugins.forEach((p) => p.configWebpack({ config }));
82
+ config.resolve.merge(this.parent.options.resolve);
83
+ return config.toConfig();
84
+ }
85
+ initRjsBuilder() {
86
+ const cfg = this.createWebpackConfig();
87
+ cfg.entry = () => {
88
+ shared_1.log.verbose(LOG_PREFIX_RJS, 'rjs entries', builder_1.builder.rjsEntry);
89
+ return builder_1.builder.rjsEntry;
90
+ };
91
+ const compiler = (0, legacyExport_1.customWebpack)(cfg);
92
+ this.compiler = compiler;
93
+ }
94
+ runOrWatch() {
95
+ return (error, stats) => {
96
+ setTimeout(() => {
97
+ var _a, _b;
98
+ if (error) {
99
+ shared_1.log.error(LOG_PREFIX_RJS, error.message);
100
+ throw error;
101
+ }
102
+ const info = stats.toJson();
103
+ if (stats.hasErrors()) {
104
+ (_a = info === null || info === void 0 ? void 0 : info.errors) === null || _a === void 0 ? void 0 : _a.forEach((error) => {
105
+ shared_1.log.error(LOG_PREFIX_RJS, error.message);
106
+ });
107
+ if (!this.options.watch) {
108
+ process.exit(1);
109
+ }
110
+ }
111
+ if (stats.hasWarnings()) {
112
+ (_b = info === null || info === void 0 ? void 0 : info.warnings) === null || _b === void 0 ? void 0 : _b.forEach((warning) => {
113
+ shared_1.log.warn(LOG_PREFIX_RJS, warning.message);
114
+ });
115
+ }
116
+ if (this.options.watch) {
117
+ shared_1.log.info('', 'Watching for changes...'.green);
118
+ }
119
+ });
120
+ };
121
+ }
122
+ run() {
123
+ const cb = this.runOrWatch();
124
+ if (this.options.watch) {
125
+ if (!this.watcher) {
126
+ this.watcher = this.compiler.watch({}, cb);
127
+ }
128
+ else {
129
+ this.watcher.close(() => {
130
+ this.watcher = this.compiler.watch({}, cb);
131
+ });
132
+ }
133
+ }
134
+ else {
135
+ this.compiler.run(cb);
136
+ }
137
+ return this.compiler;
138
+ }
139
+ }
140
+ exports.default = RjsBuild;
@@ -0,0 +1,15 @@
1
+ import { CliOptions } from '@ray-js/types';
2
+ import { customWebpack as webpack } from '@ray-core/cli/lib/legacyExport';
3
+ declare class WorkerBuild {
4
+ private options;
5
+ private compiler;
6
+ private parent;
7
+ private watcher;
8
+ private routesConfigFile;
9
+ constructor(options: CliOptions, parent: webpack.Compiler);
10
+ private createWebpackConfig;
11
+ private initWorkerBuilder;
12
+ private runOrWatch;
13
+ run(): webpack.Compiler;
14
+ }
15
+ export default WorkerBuild;
@@ -0,0 +1,174 @@
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
+ const webpack_chain_1 = __importDefault(require("webpack-chain"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const colors_1 = __importDefault(require("colors"));
9
+ const shared_1 = require("@ray-js/shared");
10
+ const legacyExport_1 = require("@ray-core/cli/lib/legacyExport");
11
+ const builder_1 = require("./builder");
12
+ const onBuildEnd_1 = __importDefault(require("./plugins/onBuildEnd"));
13
+ const utils_1 = require("./utils");
14
+ colors_1.default.enable();
15
+ const extensions = ['.mjs', '.js', '.jsx', '.ts', '.tsx'];
16
+ const moduleMatcher = new RegExp(`(${extensions.join('|')})$`);
17
+ const LOG_PREFIX_WORKER = 'build-worker';
18
+ const innerPlugins = [(0, onBuildEnd_1.default)(LOG_PREFIX_WORKER)];
19
+ class WorkerBuild {
20
+ constructor(options, parent) {
21
+ this.options = options;
22
+ this.parent = parent;
23
+ this.initWorkerBuilder();
24
+ }
25
+ createWebpackConfig() {
26
+ const { cwd, output, mini, target } = this.options;
27
+ let nodeEnv = process.env.NODE_ENV;
28
+ if (nodeEnv !== 'development' && nodeEnv !== 'production') {
29
+ nodeEnv = 'development';
30
+ }
31
+ const config = new webpack_chain_1.default();
32
+ config
33
+ .context(cwd)
34
+ .cache({
35
+ type: 'filesystem',
36
+ cacheDirectory: path_1.default.join(cwd, 'node_modules', '.workers_cache'),
37
+ })
38
+ .devtool('cheap-module-source-map')
39
+ .mode(nodeEnv)
40
+ .module.rule('js')
41
+ .test(moduleMatcher)
42
+ .exclude.add(/node_modules/)
43
+ .end()
44
+ .use('babel-loader')
45
+ .loader('babel-loader')
46
+ .options({
47
+ configFile: path_1.default.resolve(__dirname, './babel/configs/babel.config.js'),
48
+ });
49
+ config.output
50
+ .set('filename', '[name].js')
51
+ .set('libraryTarget', 'commonjs2')
52
+ .set('clean', false)
53
+ .set('globalObject', 'worker')
54
+ .set('path', output)
55
+ .set('publicPath', '/')
56
+ .end()
57
+ .optimization.set('runtimeChunk', { name: 'runtime-worker' })
58
+ .set('splitChunks', {
59
+ cacheGroups: {
60
+ vendors: {
61
+ name: 'ray-vendors-worker',
62
+ test: moduleMatcher,
63
+ chunks: 'initial',
64
+ minChunks: 2,
65
+ minSize: 0,
66
+ priority: 2,
67
+ },
68
+ },
69
+ })
70
+ .set('minimize', mini !== null && mini !== void 0 ? mini : false)
71
+ .end()
72
+ .plugin('defined')
73
+ .use(legacyExport_1.customWebpack.DefinePlugin, [
74
+ {
75
+ 'process.env.NODE_ENV': JSON.stringify(nodeEnv),
76
+ 'process.env.PLATFORM': JSON.stringify(target),
77
+ },
78
+ ])
79
+ .end()
80
+ .watchOptions({ aggregateTimeout: 1000 });
81
+ innerPlugins.forEach((p) => p.configWebpack({ config }));
82
+ config.resolve.merge(this.parent.options.resolve);
83
+ return config.toConfig();
84
+ }
85
+ initWorkerBuilder() {
86
+ const cfg = this.createWebpackConfig();
87
+ cfg.entry = () => {
88
+ this.routesConfigFile = builder_1.builder.api.searchJSFile('src/routes.config');
89
+ if (!this.routesConfigFile) {
90
+ shared_1.log.error(LOG_PREFIX_WORKER, `missing configuration file (routes.config.{ts|js})`);
91
+ return {};
92
+ }
93
+ delete require.cache[this.routesConfigFile];
94
+ const { root, workers = [] } = builder_1.builder.api.requireJSFile(this.routesConfigFile).workers || {};
95
+ if (!root) {
96
+ shared_1.log.verbose(LOG_PREFIX_WORKER, `missing prop 'workers' in ${this.routesConfigFile}`);
97
+ return {};
98
+ }
99
+ const entries = workers.reduce((o, k) => {
100
+ const { source: rootDir, target, cwd } = this.options;
101
+ const name = path_1.default.join(root, k);
102
+ const f = path_1.default.join(cwd, rootDir, name);
103
+ const m = (0, utils_1.getModuleOfPlatform)(f, {
104
+ rootDir,
105
+ target,
106
+ cwd,
107
+ extensions: ['ts', 'js', 'tsx', 'jsx'],
108
+ });
109
+ if (m) {
110
+ o[name] = m[1];
111
+ }
112
+ else {
113
+ shared_1.log.warn(LOG_PREFIX_WORKER, `Can't resolve module ${k} in ${root}`);
114
+ }
115
+ return o;
116
+ }, {});
117
+ shared_1.log.verbose(LOG_PREFIX_WORKER, 'worker entries', entries);
118
+ return entries;
119
+ };
120
+ const compiler = (0, legacyExport_1.customWebpack)(cfg);
121
+ this.compiler = compiler;
122
+ }
123
+ runOrWatch() {
124
+ return (error, stats) => {
125
+ setTimeout(() => {
126
+ var _a, _b;
127
+ if (error) {
128
+ shared_1.log.error(LOG_PREFIX_WORKER, error.message);
129
+ throw error;
130
+ }
131
+ const info = stats.toJson();
132
+ if (stats.hasErrors()) {
133
+ (_a = info === null || info === void 0 ? void 0 : info.errors) === null || _a === void 0 ? void 0 : _a.forEach((error) => {
134
+ shared_1.log.error(LOG_PREFIX_WORKER, error.message);
135
+ });
136
+ if (!this.options.watch) {
137
+ process.exit(1);
138
+ }
139
+ }
140
+ if (stats.hasWarnings()) {
141
+ (_b = info === null || info === void 0 ? void 0 : info.warnings) === null || _b === void 0 ? void 0 : _b.forEach((warning) => {
142
+ shared_1.log.warn(LOG_PREFIX_WORKER, warning.message);
143
+ });
144
+ }
145
+ if (this.options.watch) {
146
+ shared_1.log.info('', 'Watching for changes...'.green);
147
+ }
148
+ });
149
+ };
150
+ }
151
+ run() {
152
+ const cb = this.runOrWatch();
153
+ if (this.options.watch) {
154
+ if (!this.watcher) {
155
+ this.watcher = this.compiler.watch({}, cb);
156
+ }
157
+ else {
158
+ this.watcher.close(() => {
159
+ this.watcher = this.compiler.watch({}, cb);
160
+ });
161
+ }
162
+ this.compiler.hooks.afterCompile.tap(LOG_PREFIX_WORKER, (compilation) => {
163
+ if (!compilation.fileDependencies.has(this.routesConfigFile)) {
164
+ compilation.fileDependencies.add(this.routesConfigFile);
165
+ }
166
+ });
167
+ }
168
+ else {
169
+ this.compiler.run(cb);
170
+ }
171
+ return this.compiler;
172
+ }
173
+ }
174
+ exports.default = WorkerBuild;
package/lib/builder.d.ts CHANGED
@@ -4,11 +4,13 @@ export default class Builder {
4
4
  private cliOptions;
5
5
  private entryValue;
6
6
  private _api;
7
+ rjsEntries: Map<string, string>;
7
8
  get options(): CliOptions;
8
9
  set options(v: CliOptions);
9
10
  get entry(): Entry;
10
11
  set entry(v: Entry);
11
12
  get api(): API;
12
13
  set api(api: API);
14
+ get rjsEntry(): {};
13
15
  }
14
16
  export declare const builder: Builder;
package/lib/builder.js CHANGED
@@ -4,6 +4,7 @@ exports.builder = void 0;
4
4
  class Builder {
5
5
  constructor() {
6
6
  this.entryValue = {};
7
+ this.rjsEntries = new Map();
7
8
  }
8
9
  get options() {
9
10
  return this.cliOptions;
@@ -23,6 +24,12 @@ class Builder {
23
24
  set api(api) {
24
25
  this._api = api;
25
26
  }
27
+ get rjsEntry() {
28
+ return Array.from(this.rjsEntries).reduce((o, [k, v]) => {
29
+ o[k] = v;
30
+ return o;
31
+ }, {});
32
+ }
26
33
  }
27
34
  exports.default = Builder;
28
35
  exports.builder = new Builder();
@@ -5,12 +5,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const slash_1 = __importDefault(require("slash"));
7
7
  const shared_1 = require("@ray-js/shared");
8
+ const path_1 = __importDefault(require("path"));
9
+ const utils_1 = require("../../utils");
8
10
  const rjs = '@ray-js/rjs-for-wechat';
9
11
  const RjsLoader = function RjsLoader(source, map) {
10
- let content = `module.exports = __minipack_require__("${(0, slash_1.default)(this._module.resource)}")`;
11
12
  const { builder } = this.getOptions();
12
- const cwd = builder.options.cwd;
13
- if (process.env.PLATFORM !== 'thing') {
13
+ const { cwd, source: rootDir, target } = builder.options;
14
+ let content;
15
+ if (target === 'thing') {
16
+ const rjsModule = getRjsModule(this._compilation, this.resourcePath);
17
+ if (!rjsModule) {
18
+ shared_1.log.error('rjs loader', `Can't resolve rjs module`);
19
+ this.callback(null, `"Can't resolve rjs module"`, map);
20
+ return;
21
+ }
22
+ let resource = rjsModule[1];
23
+ const m = (0, utils_1.getModuleOfPlatform)(resource, { target, cwd, rootDir, extensions: ['rjs'] });
24
+ if (!m) {
25
+ shared_1.log.error('rjs loader', `Can't resolve rjs module: current platform is ${target}, but got ${resource}`);
26
+ this.callback(null, `"Can't resolve rjs module: current platform is ${target}, but got ${resource}"`, map);
27
+ return;
28
+ }
29
+ builder.rjsEntries.set(m[0], m[1]);
30
+ content = `module.exports = __non_webpack_require__("${(0, slash_1.default)(m[2])}")`;
31
+ }
32
+ else {
14
33
  try {
15
34
  const obj = require(rjs);
16
35
  const rjsMethods = Object.keys(obj).join(', ');
@@ -24,3 +43,20 @@ const RjsLoader = function RjsLoader(source, map) {
24
43
  this.callback(null, content, map);
25
44
  };
26
45
  exports.default = RjsLoader;
46
+ function getRjsModule(compilation, p) {
47
+ const { moduleGraph } = compilation;
48
+ const moduleMap = moduleGraph._moduleMap;
49
+ for (const [m, mgm] of moduleMap) {
50
+ const out = mgm.outgoingConnections || [];
51
+ for (const mc of out) {
52
+ const { resource, rawRequest } = mc.module || {};
53
+ if (resource === p) {
54
+ return [
55
+ m.resource,
56
+ path_1.default.join(path_1.default.dirname(resource), rawRequest),
57
+ rawRequest, // 引用rjs的模块中,import 的具体模块名
58
+ ];
59
+ }
60
+ }
61
+ }
62
+ }
@@ -0,0 +1,8 @@
1
+ import Config from 'webpack-chain';
2
+ declare const _default: (name: any) => {
3
+ name: string;
4
+ configWebpack({ config }: {
5
+ config: Config;
6
+ }): void;
7
+ };
8
+ export default _default;
@@ -0,0 +1,42 @@
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
+ const shared_1 = require("@ray-js/shared");
7
+ const pretty_bytes_1 = __importDefault(require("pretty-bytes"));
8
+ class OnBuildEnd {
9
+ constructor(opts) {
10
+ this.options = opts;
11
+ }
12
+ apply(compiler) {
13
+ const { name } = this.options;
14
+ compiler.hooks.done.tap(name, (stats) => {
15
+ setTimeout(() => {
16
+ var _a;
17
+ const assets = ((_a = stats.toJson()) === null || _a === void 0 ? void 0 : _a.assets) || [];
18
+ const size = assets.reduce((acc, asset) => acc + asset.size, 0);
19
+ console.log(name);
20
+ console.log(stats.toString({
21
+ colors: true,
22
+ entrypoints: false,
23
+ assets: false,
24
+ children: false,
25
+ modules: false,
26
+ }));
27
+ console.log('');
28
+ shared_1.log.info(name, `Output size ${size} bytes`.yellow, (0, pretty_bytes_1.default)(size).bgMagenta.white);
29
+ console.log('');
30
+ });
31
+ });
32
+ }
33
+ }
34
+ exports.default = (name) => {
35
+ const n = `${name}-on-build-end-plugin`;
36
+ return {
37
+ name: n,
38
+ configWebpack({ config }) {
39
+ config.plugin(n).use(OnBuildEnd, [{ name: n }]);
40
+ },
41
+ };
42
+ };
@@ -0,0 +1,8 @@
1
+ import Config from 'webpack-chain';
2
+ declare const _default: () => {
3
+ name: string;
4
+ configWebpack({ config }: {
5
+ config: Config;
6
+ }): void;
7
+ };
8
+ export default _default;
@@ -0,0 +1,57 @@
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
+ const webpack_1 = require("webpack");
7
+ const path_1 = __importDefault(require("path"));
8
+ const slash_1 = __importDefault(require("slash"));
9
+ const { ConcatSource } = webpack_1.sources;
10
+ class RjsBuildPlugin {
11
+ constructor(opts) {
12
+ this.options = opts;
13
+ }
14
+ apply(compiler) {
15
+ const { name } = this.options;
16
+ compiler.hooks.thisCompilation.tap(name, (compilation) => {
17
+ compilation.hooks.processAssets.tapAsync({
18
+ name,
19
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
20
+ }, (_, callback) => {
21
+ compilation.chunkGroups.forEach((group) => {
22
+ group.chunks.reverse().forEach((chunk) => {
23
+ var _a;
24
+ if (chunk.name !== group.name) {
25
+ const requires = [];
26
+ const files = Array.from(chunk.files);
27
+ files.forEach((file) => {
28
+ if (file.endsWith('.rjs')) {
29
+ const relativePath = (0, slash_1.default)(path_1.default.relative(path_1.default.dirname(group.name), file)).replace(/\.rjs$/i, '.js');
30
+ requires.push(`require('./${relativePath}');\n`);
31
+ const newFilename = file.replace(/\.rjs$/i, '.js');
32
+ if (!compilation.assets[newFilename]) {
33
+ compilation.assets[newFilename] = compilation.assets[file];
34
+ delete compilation.assets[file];
35
+ }
36
+ }
37
+ });
38
+ const assetPath = group.name + '.rjs';
39
+ const source = (_a = compilation.assets[assetPath]) !== null && _a !== void 0 ? _a : '';
40
+ compilation.assets[assetPath] = new ConcatSource(...requires, source);
41
+ }
42
+ });
43
+ });
44
+ callback();
45
+ });
46
+ });
47
+ }
48
+ }
49
+ exports.default = () => {
50
+ const name = `rjs-build-optimize-plugin`;
51
+ return {
52
+ name,
53
+ configWebpack({ config }) {
54
+ config.plugin(name).use(RjsBuildPlugin, [{ name }]);
55
+ },
56
+ };
57
+ };
@@ -25,6 +25,11 @@ exports.default = {
25
25
  name: 'mini-app-module-resolve',
26
26
  configWebpack(context) {
27
27
  const { config } = context;
28
+ config.watch(true);
29
+ config.watchOptions({
30
+ ignored: /\.rjs$/i,
31
+ aggregateTimeout: 1000,
32
+ });
28
33
  const { target } = builder_1.builder.options;
29
34
  alias('ray', '@ray-js/ray', config);
30
35
  // 修改 mini-css-extract-plugin 的配置
@@ -33,8 +38,6 @@ exports.default = {
33
38
  // 调整 ray-core 的 babel 配置
34
39
  config.module
35
40
  .rule('js')
36
- .exclude.add((m) => /\.rjs$/i.test(m))
37
- .end()
38
41
  .use('babel')
39
42
  .tap((options) => {
40
43
  // 修复 babel alias 错误问题
package/lib/ray-core.js CHANGED
@@ -20,12 +20,12 @@ const legacyExport_1 = require("@ray-core/cli/lib/legacyExport");
20
20
  const API_1 = __importDefault(require("@ray-core/cli/lib/API"));
21
21
  const build_store_1 = __importDefault(require("@ray-core/build-store"));
22
22
  const BlendedApp_1 = __importDefault(require("@ray-core/cli/lib/plugins/BlendedApp"));
23
+ const builder_1 = require("./builder");
23
24
  const resolve_alias_1 = __importDefault(require("./plugins/resolve-alias"));
24
25
  const app_entry_1 = __importDefault(require("./plugins/app-entry"));
25
26
  const less_1 = __importDefault(require("./plugins/less"));
26
27
  const resolve_legacy_1 = __importDefault(require("./plugins/resolve-legacy"));
27
- const rjs_1 = __importDefault(require("./plugins/rjs"));
28
- const builder_1 = require("./builder");
28
+ const onBuildEnd_1 = __importDefault(require("./plugins/onBuildEnd"));
29
29
  const _a = (0, legacyExport_1.getDefaultOptions)(), { UNSAFE_wechatTemplateDepth } = _a, remaxDefaultOptions = __rest(_a, ["UNSAFE_wechatTemplateDepth"]);
30
30
  const isThing = process.env.PLATFORM === 'thing';
31
31
  const defaultTemplateDepth = isThing
@@ -43,11 +43,11 @@ const defaultTemplateDepth = isThing
43
43
  'navigation-bar': -1,
44
44
  }
45
45
  : {};
46
- const innerPlugins = [resolve_legacy_1.default, app_entry_1.default, less_1.default, resolve_alias_1.default, rjs_1.default];
46
+ const innerPlugins = [resolve_legacy_1.default, app_entry_1.default, less_1.default, resolve_alias_1.default, (0, onBuildEnd_1.default)('ray')];
47
47
  function getRayCoreOptions() {
48
48
  const { cwd, source, watch, target, mini, analyze, output, blended } = builder_1.builder.options;
49
49
  if (blended) {
50
- innerPlugins.push(BlendedApp_1.default);
50
+ innerPlugins.push(Object.assign(Object.assign({}, BlendedApp_1.default), { name: 'blended-app-plugin' }));
51
51
  }
52
52
  const api = builder_1.builder.api;
53
53
  // @ts-ignore
package/lib/utils.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export declare function getModuleOfPlatform(file: string, opts: {
2
+ target: string;
3
+ cwd: string;
4
+ rootDir: string;
5
+ extensions: string[];
6
+ }): string[];
package/lib/utils.js ADDED
@@ -0,0 +1,36 @@
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.getModuleOfPlatform = void 0;
7
+ const paths_1 = require("@ray-core/cli/lib/build/utils/paths");
8
+ const path_1 = __importDefault(require("path"));
9
+ const fs_extra_1 = __importDefault(require("fs-extra"));
10
+ function getModuleOfPlatform(file, opts) {
11
+ const outputName = (0, paths_1.getNativeAssetOutputPath)((0, paths_1.replaceExtension)(file, ''), opts).replace(/(?:\.mini|\.wechat|\.thing|\.tuya|\.web)$/, '');
12
+ const target = opts.target;
13
+ for (const ext of opts.extensions) {
14
+ const exts = [`.${target}.${ext}`];
15
+ if (target !== 'web') {
16
+ if (target === 'thing') {
17
+ exts.push(`.tuya.${ext}`);
18
+ }
19
+ exts.push(`.mini.${ext}`);
20
+ }
21
+ exts.push(`.${ext}`);
22
+ const dirname = path_1.default.dirname(file);
23
+ const reg = RegExp(`\\.${ext}$`);
24
+ const name = path_1.default
25
+ .basename(file)
26
+ .replace(reg, '')
27
+ .replace(/(?:\.mini|\.wechat|\.thing|\.tuya|\.web)$/, '');
28
+ for (const p of exts) {
29
+ const f = path_1.default.join(dirname, `${name}${p}`);
30
+ if (fs_extra_1.default.existsSync(f)) {
31
+ return [outputName, f, `${name}.${ext}`];
32
+ }
33
+ }
34
+ }
35
+ }
36
+ exports.getModuleOfPlatform = getModuleOfPlatform;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/builder-mp",
3
- "version": "0.20.0-beta.2",
3
+ "version": "0.20.0-beta.4",
4
4
  "description": "Ray builder for mini program",
5
5
  "keywords": [
6
6
  "ray"
@@ -24,14 +24,14 @@
24
24
  "@babel/template": "^7.16.0",
25
25
  "@babel/traverse": "^7.16.3",
26
26
  "@babel/types": "^7.16.0",
27
- "@ray-core/babel-preset-remax": "^0.2.0-beta.7",
28
- "@ray-core/build-store": "^0.2.0-beta.7",
29
- "@ray-core/cli": "^0.2.0-beta.7",
30
- "@ray-core/ray": "^0.2.0-beta.7",
31
- "@ray-js/adapter": "^0.20.0-beta.2",
27
+ "@ray-core/babel-preset-remax": "^0.2.0-beta.9",
28
+ "@ray-core/build-store": "^0.2.0-beta.9",
29
+ "@ray-core/cli": "^0.2.0-beta.9",
30
+ "@ray-core/ray": "^0.2.0-beta.9",
31
+ "@ray-js/adapter": "^0.20.0-beta.4",
32
32
  "@ray-js/rjs-for-wechat": "^0.0.3",
33
- "@ray-js/shared": "^0.20.0-beta.2",
34
- "@ray-js/types": "^0.20.0-beta.2",
33
+ "@ray-js/shared": "^0.20.0-beta.4",
34
+ "@ray-js/types": "^0.20.0-beta.4",
35
35
  "babel-loader": "^8.2.3",
36
36
  "babel-plugin-minify-dead-code-elimination": "^0.5.1",
37
37
  "babel-plugin-transform-prune-unused-imports": "^1.0.1",
@@ -48,7 +48,7 @@
48
48
  "webpack-virtual-modules": "^0.4.4"
49
49
  },
50
50
  "devDependencies": {
51
- "@ray-core/types": "^0.2.0-beta.7",
51
+ "@ray-core/types": "^0.2.0-beta.9",
52
52
  "@types/jest": "^27.0.2",
53
53
  "@types/node": "^16.9.1",
54
54
  "babel-plugin-tester": "^10.1.0",
@@ -62,6 +62,6 @@
62
62
  "email": "tuyafe@tuya.com"
63
63
  }
64
64
  ],
65
- "gitHead": "67aa3b9aa0a8113c3965772893bdd76ed3a243bf",
65
+ "gitHead": "826b1049dc292e04661531c7de47cfa38dc45345",
66
66
  "repository": {}
67
67
  }
@@ -1,20 +0,0 @@
1
- import Config from 'webpack-chain';
2
- import { Compiler, Stats } from 'webpack';
3
- export declare class RjsBuilder {
4
- private compiler;
5
- private parentCompiler;
6
- private entries;
7
- private webpackConfig;
8
- private compileOptions;
9
- init: (parentCompiler: Compiler, compileOptions: any) => void;
10
- initWebpack(cfg: Config): Compiler;
11
- run(entries: any): void;
12
- config(config: Config): void;
13
- print: (prefix: string, stats: Stats[]) => void;
14
- parentDoneHook(): void;
15
- rjsDoneHook(): void;
16
- recomposeAssets(): void;
17
- extractRjsModuleHook(): void;
18
- }
19
- declare const rjsBuilder: RjsBuilder;
20
- export default rjsBuilder;
@@ -1,218 +0,0 @@
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.RjsBuilder = void 0;
7
- const pretty_bytes_1 = __importDefault(require("pretty-bytes"));
8
- const webpack_1 = require("webpack");
9
- const path_1 = __importDefault(require("path"));
10
- const slash_1 = __importDefault(require("slash"));
11
- const colors_1 = __importDefault(require("colors"));
12
- const shared_1 = require("@ray-js/shared");
13
- const legacyExport_1 = require("@ray-core/cli/lib/legacyExport");
14
- const { ConcatSource } = webpack_1.sources;
15
- colors_1.default.enable();
16
- // 需保留
17
- const plugins = [
18
- 'webpack-define-plugin',
19
- 'provide-regeneratorRuntime',
20
- // 'webpackbar',
21
- 'defined',
22
- 'mini-css-extract-plugin',
23
- 'remax-coverage-ignore-plugin',
24
- ];
25
- // 需删除
26
- const rules = ['ray-hooks', 'ray-context', 'rjs'];
27
- const getRjsModules = (content) => {
28
- const reg = /__minipack_require__\((?:"|')([^\)]+)(?:"|')\)/g;
29
- const deps = [];
30
- let res;
31
- while ((res = reg.exec(content))) {
32
- let [, dep] = res;
33
- dep = (0, slash_1.default)(dep);
34
- if (dep && !deps.includes(dep)) {
35
- deps.push(dep);
36
- }
37
- }
38
- return deps;
39
- };
40
- const replaceModeContent = (content) => content.replace(/__minipack_require__/g, 'require');
41
- const isSupportMultiRjs = false;
42
- const LOG_PREFIX = 'builder-mp';
43
- const LOG_PREFIX_RJS = 'builder-rjs';
44
- class RjsBuilder {
45
- constructor() {
46
- this.init = (parentCompiler, compileOptions) => {
47
- this.parentCompiler = parentCompiler;
48
- this.compileOptions = compileOptions;
49
- this.parentDoneHook();
50
- if (process.env.PLATFORM === 'thing') {
51
- this.initWebpack(this.webpackConfig);
52
- this.rjsDoneHook();
53
- this.recomposeAssets();
54
- this.extractRjsModuleHook();
55
- }
56
- };
57
- this.print = (prefix, stats) => {
58
- setTimeout(() => {
59
- const assets = stats.reduce((a, s) => { var _a; return ((a = a.concat(((_a = s.toJson()) === null || _a === void 0 ? void 0 : _a.assets) || [])), a); }, []);
60
- const size = assets.reduce((acc, asset) => acc + asset.size, 0);
61
- stats.forEach((s) => {
62
- console.log(s.toString({
63
- colors: true,
64
- entrypoints: false,
65
- assets: this.compileOptions.watch ? false : true,
66
- children: false,
67
- modules: false,
68
- }));
69
- });
70
- console.log('');
71
- shared_1.log.info(prefix, `Output size ${size} bytes`.yellow, (0, pretty_bytes_1.default)(size).bgMagenta.white);
72
- console.log('');
73
- });
74
- };
75
- }
76
- initWebpack(cfg) {
77
- var _a, _b;
78
- cfg.cache({
79
- type: 'filesystem',
80
- cacheDirectory: path_1.default.join(this.compileOptions.cwd, 'node_modules', '.rjs_cache'),
81
- });
82
- shared_1.log.verbose(LOG_PREFIX_RJS, '初始化rjs webpack');
83
- // 部分plugin不需要
84
- (_a = cfg.plugins) === null || _a === void 0 ? void 0 : _a.values().forEach((plugin) => {
85
- if (!plugins.includes(plugin.name)) {
86
- cfg.plugins.delete(plugin.name);
87
- }
88
- });
89
- // 部分rule不需要
90
- (_b = cfg.module) === null || _b === void 0 ? void 0 : _b.rules.values().forEach((rule) => {
91
- if (rules.includes(rule.name)) {
92
- cfg.module.rules.delete(rule.name);
93
- }
94
- });
95
- const cacheGroups = cfg.optimization.get('splitChunks').cacheGroups;
96
- Object.keys(cacheGroups).forEach((k) => {
97
- const v = cacheGroups[k];
98
- v.name += '-rjs';
99
- cacheGroups[k] = v;
100
- });
101
- cfg.output
102
- .filename('[name].rjs')
103
- .globalObject('Render') // 哪来的Render?因视图层没有window等全局对象,但暴露了Render全局函数
104
- .libraryTarget('commonjs2')
105
- .end() // rjs模块需要导出给minipack 使用,蹩脚
106
- .optimization.runtimeChunk({ name: 'runtime-rjs' })
107
- .splitChunks({ cacheGroups });
108
- cfg.output.delete('libraryExport');
109
- cfg.output.set('clean', false);
110
- const config = cfg.toConfig();
111
- config.entry = () => this.entries;
112
- if (this.compiler) {
113
- return;
114
- }
115
- this.compiler = (0, legacyExport_1.customWebpack)(config);
116
- return this.compiler;
117
- }
118
- run(entries) {
119
- shared_1.log.verbose(LOG_PREFIX_RJS, '运行rjs编译');
120
- this.entries = entries;
121
- this.compiler.run(() => { });
122
- }
123
- config(config) {
124
- shared_1.log.verbose(LOG_PREFIX_RJS, '注册rjs编译配置');
125
- this.webpackConfig = config;
126
- }
127
- parentDoneHook() {
128
- this.parentCompiler.hooks.done.tap('tuya-scripts', (stats) => {
129
- this.print(LOG_PREFIX, [stats]);
130
- });
131
- }
132
- rjsDoneHook() {
133
- this.compiler.hooks.done.tap('rjs-builder-done', (stats) => {
134
- this.print(LOG_PREFIX_RJS, [stats]);
135
- });
136
- }
137
- recomposeAssets() {
138
- const PLUGIN_NAME = 'rjs-building';
139
- this.compiler.hooks.thisCompilation.tap('rjs-building', (compilation) => {
140
- compilation.hooks.processAssets.tapAsync({
141
- name: PLUGIN_NAME,
142
- stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
143
- }, (_, callback) => {
144
- compilation.chunkGroups.forEach((group) => {
145
- group.chunks.reverse().forEach((chunk) => {
146
- var _a;
147
- if (chunk.name !== group.name) {
148
- const requires = [];
149
- const files = Array.from(chunk.files);
150
- files.forEach((file) => {
151
- if (file.endsWith('.rjs')) {
152
- const relativePath = (0, slash_1.default)(path_1.default.relative(path_1.default.dirname(group.name), file)).replace(/\.rjs$/i, '.js');
153
- requires.push(`require('./${relativePath}');\n`);
154
- const newFilename = file.replace(/\.rjs$/i, '.js');
155
- if (!compilation.assets[newFilename]) {
156
- compilation.assets[newFilename] = compilation.assets[file];
157
- delete compilation.assets[file];
158
- }
159
- }
160
- });
161
- const assetPath = group.name + '.rjs';
162
- const source = (_a = compilation.assets[assetPath]) !== null && _a !== void 0 ? _a : '';
163
- compilation.assets[assetPath] = new ConcatSource(...requires, source);
164
- }
165
- });
166
- });
167
- callback();
168
- });
169
- });
170
- }
171
- extractRjsModuleHook() {
172
- this.parentCompiler.hooks.emit.tap('rjs-build-plugin', (compilation) => {
173
- shared_1.log.verbose(LOG_PREFIX_RJS, '提取rjs编译入口文件');
174
- const rjsEntry = {};
175
- const entries = compilation.entrypoints;
176
- const assets = compilation.assets;
177
- for (const p of entries.keys()) {
178
- let content = assets[`${p}.js`].source();
179
- if (Buffer.isBuffer(content))
180
- content = content.toString();
181
- const rjsModules = getRjsModules(content);
182
- content = replaceModeContent(content);
183
- rjsModules.forEach((rjs, index) => {
184
- /**
185
- * TODO 支持多页面引用多rjs文件?
186
- * 当前minipack支持一个页面仅能引入单个rjs模块文件,且文件名同页面文件名
187
- **/
188
- // 啥时候支持多rjs?
189
- const sep = (0, slash_1.default)(path_1.default.sep);
190
- if (isSupportMultiRjs) {
191
- const newModName = `${index}.${rjs.slice(rjs.lastIndexOf(sep) + 1)}`;
192
- const chunkName = `${path_1.default.dirname(p)}${sep}${newModName.replace(/\.rjs/i, '')}`;
193
- const filename = `.${sep}${newModName}`;
194
- rjsEntry[chunkName] = rjs;
195
- content = content.replace(rjs, filename);
196
- }
197
- else {
198
- // 当前一个页面仅支持引入一个rjs模块文件
199
- if (rjsEntry[p]) {
200
- rjsEntry[p] = rjs;
201
- shared_1.log.warn('RJS-BUILD-RAY', '一个页面(%s)仅支持引入一个rjs模块: %s', p, rjs);
202
- }
203
- rjsEntry[p] = rjs;
204
- content = content.replace(rjs, (0, slash_1.default)(`.${sep}${p.slice(p.lastIndexOf(sep) + 1)}.rjs`));
205
- }
206
- });
207
- assets[`${p}.js`] = {
208
- source: () => content,
209
- size: () => Buffer.byteLength(content),
210
- };
211
- }
212
- this.run(rjsEntry);
213
- });
214
- }
215
- }
216
- exports.RjsBuilder = RjsBuilder;
217
- const rjsBuilder = new RjsBuilder();
218
- exports.default = rjsBuilder;
@@ -1,6 +0,0 @@
1
- declare const _default: {
2
- configWebpack({ config }: {
3
- config: any;
4
- }): void;
5
- };
6
- export default _default;
@@ -1,11 +0,0 @@
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
- const builder_rjs_1 = __importDefault(require("../builder.rjs"));
7
- exports.default = {
8
- configWebpack({ config }) {
9
- builder_rjs_1.default.config(config);
10
- },
11
- };