From fd9ace41c19a9d1a994852432d1d2f5681df17cc Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 22 Jul 2021 18:22:19 +0800 Subject: [PATCH] fix(h5): hot update --- .eslintrc.js | 2 +- packages/shims-node.d.ts | 1 + packages/uni-app-vite/src/index.ts | 7 +- packages/uni-cli-nvue/LICENSE | 202 ++++++++++++++++++ packages/uni-cli-nvue/dist/index.js | 13 ++ .../uni-cli-nvue/dist/webpack/config/index.js | 22 ++ .../dist/webpack/config/optimization.js | 18 ++ .../dist/webpack/config/output.js | 7 + .../dist/webpack/config/plugins/banner.js | 9 + .../dist/webpack/config/plugins/define.js | 13 ++ .../dist/webpack/config/plugins/index.js | 7 + .../dist/webpack/config/plugins/provide.js | 21 ++ .../dist/webpack/config/resolve.js | 32 +++ packages/uni-cli-nvue/dist/webpack/index.js | 37 ++++ .../uni-cli-nvue/lib/get-current-sub-nvue.js | 3 + .../uni-cli-nvue/lib/require-native-plugin.js | 3 + packages/uni-cli-nvue/package.json | 29 +++ packages/uni-cli-nvue/src/index.ts | 1 + .../uni-cli-nvue/src/webpack/config/index.ts | 21 ++ .../src/webpack/config/optimization.ts | 14 ++ .../uni-cli-nvue/src/webpack/config/output.ts | 6 + .../src/webpack/config/plugins/banner.ts | 7 + .../src/webpack/config/plugins/define.ts | 13 ++ .../src/webpack/config/plugins/index.ts | 5 + .../src/webpack/config/plugins/provide.ts | 22 ++ .../src/webpack/config/resolve.ts | 29 +++ packages/uni-cli-nvue/src/webpack/index.ts | 35 +++ packages/uni-cli-nvue/tsconfig.json | 12 ++ packages/uni-cli-shared/src/hbx/alias.ts | 51 +++++ packages/uni-cli-shared/src/hbx/env.ts | 20 ++ packages/uni-cli-shared/src/hbx/index.ts | 24 +-- packages/uni-cli-shared/src/index.ts | 1 + packages/uni-cli-shared/src/logs/format.ts | 21 ++ packages/uni-cli-shared/src/logs/index.ts | 1 + .../uni-h5-vite/dist/handleHotUpdate/index.js | 9 +- .../uni-h5-vite/dist/plugins/cssScoped.js | 3 + .../uni-h5-vite/src/handleHotUpdate/index.ts | 9 +- packages/uni-h5-vite/src/plugins/cssScoped.ts | 3 + packages/vite-plugin-uni/src/cli/action.ts | 25 ++- packages/vite-plugin-uni/src/cli/utils.ts | 7 +- .../src/configResolved/index.ts | 12 +- packages/vite-plugin-uni/src/index.ts | 4 + scripts/utils.js | 1 + 43 files changed, 742 insertions(+), 40 deletions(-) create mode 100755 packages/uni-cli-nvue/LICENSE create mode 100644 packages/uni-cli-nvue/dist/index.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/index.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/optimization.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/output.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/plugins/banner.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/plugins/define.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/plugins/index.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/plugins/provide.js create mode 100644 packages/uni-cli-nvue/dist/webpack/config/resolve.js create mode 100644 packages/uni-cli-nvue/dist/webpack/index.js create mode 100644 packages/uni-cli-nvue/lib/get-current-sub-nvue.js create mode 100644 packages/uni-cli-nvue/lib/require-native-plugin.js create mode 100644 packages/uni-cli-nvue/package.json create mode 100644 packages/uni-cli-nvue/src/index.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/index.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/optimization.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/output.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/plugins/banner.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/plugins/define.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/plugins/index.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/plugins/provide.ts create mode 100644 packages/uni-cli-nvue/src/webpack/config/resolve.ts create mode 100644 packages/uni-cli-nvue/src/webpack/index.ts create mode 100644 packages/uni-cli-nvue/tsconfig.json create mode 100644 packages/uni-cli-shared/src/hbx/alias.ts create mode 100644 packages/uni-cli-shared/src/hbx/env.ts create mode 100644 packages/uni-cli-shared/src/logs/format.ts create mode 100644 packages/uni-cli-shared/src/logs/index.ts diff --git a/.eslintrc.js b/.eslintrc.js index 884dc6c3a..173143c09 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -44,7 +44,7 @@ module.exports = { // Packages targeting Node { files: [ - 'packages/{uni-cli-shared,uni-app-vite,uni-h5-vite,vite-plugin-uni}/**', + 'packages/{uni-cli-shared,uni-cli-nvue,uni-app-vite,uni-h5-vite,vite-plugin-uni}/**', 'packages/*/vite.config.ts', ], rules: { diff --git a/packages/shims-node.d.ts b/packages/shims-node.d.ts index f2c6c8db2..e2cc46ddc 100644 --- a/packages/shims-node.d.ts +++ b/packages/shims-node.d.ts @@ -5,5 +5,6 @@ declare namespace NodeJS { UNI_OUTPUT_DIR: string UNI_CLI_CONTEXT: string UNI_COMPILER_VERSION: string + UNI_HBUILDERX_PLUGINS: string } } diff --git a/packages/uni-app-vite/src/index.ts b/packages/uni-app-vite/src/index.ts index 220b3cec8..a8a3fad6b 100644 --- a/packages/uni-app-vite/src/index.ts +++ b/packages/uni-app-vite/src/index.ts @@ -1,5 +1,9 @@ import fs from 'fs' -import { uniCssPlugin } from '@dcloudio/uni-cli-shared' +import { + initProvide, + uniCssPlugin, + uniViteInjectPlugin, +} from '@dcloudio/uni-cli-shared' import { UniAppPlugin } from './plugin' import { uniCopyPlugin } from './plugins/copy' import { uniMainJsPlugin } from './plugins/mainJs' @@ -13,6 +17,7 @@ const plugins = [ uniMainJsPlugin(), uniManifestJsonPlugin(), uniPagesJsonPlugin(), + uniViteInjectPlugin(initProvide()), UniAppPlugin, ] if (!process.env.UNI_APP_CODE_SPLITING) { diff --git a/packages/uni-cli-nvue/LICENSE b/packages/uni-cli-nvue/LICENSE new file mode 100755 index 000000000..7a4a3ea24 --- /dev/null +++ b/packages/uni-cli-nvue/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/packages/uni-cli-nvue/dist/index.js b/packages/uni-cli-nvue/dist/index.js new file mode 100644 index 000000000..6034624f0 --- /dev/null +++ b/packages/uni-cli-nvue/dist/index.js @@ -0,0 +1,13 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./webpack"), exports); diff --git a/packages/uni-cli-nvue/dist/webpack/config/index.js b/packages/uni-cli-nvue/dist/webpack/config/index.js new file mode 100644 index 000000000..8c4569951 --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/index.js @@ -0,0 +1,22 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.createConfig = void 0; +const optimization_1 = require("./optimization"); +const output_1 = require("./output"); +function createConfig(mode) { + return { + target: 'node', + mode: mode, + devtool: false, + watch: mode === 'development', + entry() { + return {}; + }, + externals: { + vue: 'Vue', + }, + optimization: optimization_1.optimization, + output: output_1.output, + }; +} +exports.createConfig = createConfig; diff --git a/packages/uni-cli-nvue/dist/webpack/config/optimization.js b/packages/uni-cli-nvue/dist/webpack/config/optimization.js new file mode 100644 index 000000000..de90c8f26 --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/optimization.js @@ -0,0 +1,18 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.optimization = void 0; +const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin")); +exports.optimization = { + minimizer: [ + new terser_webpack_plugin_1.default({ + terserOptions: { + output: { + ascii_only: true, + }, + }, + }), + ], +}; diff --git a/packages/uni-cli-nvue/dist/webpack/config/output.js b/packages/uni-cli-nvue/dist/webpack/config/output.js new file mode 100644 index 000000000..d78d48bbe --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/output.js @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.output = void 0; +exports.output = { + path: process.env.UNI_OUTPUT_DIR, + filename: '[name].js', +}; diff --git a/packages/uni-cli-nvue/dist/webpack/config/plugins/banner.js b/packages/uni-cli-nvue/dist/webpack/config/plugins/banner.js new file mode 100644 index 000000000..45ca00c47 --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/plugins/banner.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.banner = void 0; +const webpack_1 = require("webpack"); +exports.banner = new webpack_1.BannerPlugin({ + banner: '"use weex:vue";', + raw: true, + exclude: 'Vue', +}); diff --git a/packages/uni-cli-nvue/dist/webpack/config/plugins/define.js b/packages/uni-cli-nvue/dist/webpack/config/plugins/define.js new file mode 100644 index 000000000..2432402a0 --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/plugins/define.js @@ -0,0 +1,13 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.define = void 0; +const webpack_1 = require("webpack"); +exports.define = new webpack_1.DefinePlugin({ + 'process.env': { + NODE_ENV: JSON.stringify(process.env.NODE_ENV), + VUE_APP_PLATFORM: JSON.stringify(process.env.UNI_PLATFORM), + UNI_CLOUD_PROVIDER: process.env.UNI_CLOUD_PROVIDER, + HBX_USER_TOKEN: JSON.stringify(process.env.HBX_USER_TOKEN || ''), + UNI_AUTOMATOR_WS_ENDPOINT: JSON.stringify(process.env.UNI_AUTOMATOR_WS_ENDPOINT), + }, +}); diff --git a/packages/uni-cli-nvue/dist/webpack/config/plugins/index.js b/packages/uni-cli-nvue/dist/webpack/config/plugins/index.js new file mode 100644 index 000000000..ca2fac762 --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/plugins/index.js @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.plugins = void 0; +const define_1 = require("./define"); +const banner_1 = require("./banner"); +const provide_1 = require("./provide"); +exports.plugins = [define_1.define, banner_1.banner, provide_1.provide]; diff --git a/packages/uni-cli-nvue/dist/webpack/config/plugins/provide.js b/packages/uni-cli-nvue/dist/webpack/config/plugins/provide.js new file mode 100644 index 000000000..c9889748f --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/plugins/provide.js @@ -0,0 +1,21 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.provide = void 0; +const path_1 = __importDefault(require("path")); +const webpack_1 = require("webpack"); +const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared"); +const libDir = path_1.default.resolve(__dirname, '../../../../lib'); +const definitions = Object.assign({ uniCloud: [ + require.resolve('@dcloudio/uni-cloud/dist/uni-cloud.es.js'), + 'default', + ], 'uni.getCurrentSubNVue': [ + path_1.default.join(libDir, 'get-current-sub-nvue.js'), + 'default', + ], 'uni.requireNativePlugin': [ + path_1.default.join(libDir, 'require-native-plugin.js'), + 'default', + ] }, uni_cli_shared_1.initProvide()); +exports.provide = new webpack_1.ProvidePlugin(definitions); diff --git a/packages/uni-cli-nvue/dist/webpack/config/resolve.js b/packages/uni-cli-nvue/dist/webpack/config/resolve.js new file mode 100644 index 000000000..d6db08bbc --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/config/resolve.js @@ -0,0 +1,32 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.resolve = void 0; +const path_1 = __importDefault(require("path")); +const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared"); +const inputDir = process.env.UNI_INPUT_DIR; +exports.resolve = { + extensions: ['.js', '.nvue', '.vue', '.json'], + alias: { + '@': inputDir, + 'uni-pages': path_1.default.resolve(inputDir, 'pages.json'), + '@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat'), + 'uni-app-style': uni_cli_shared_1.resolveMainPathOnce(inputDir) + + '?' + + JSON.stringify({ + type: 'appStyle', + }), + 'uni-stat-config': path_1.default.resolve(inputDir, 'pages.json') + + '?' + + JSON.stringify({ + type: 'stat', + }), + }, + modules: [ + 'node_modules', + path_1.default.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'), + path_1.default.resolve(inputDir, 'node_modules'), + ], +}; diff --git a/packages/uni-cli-nvue/dist/webpack/index.js b/packages/uni-cli-nvue/dist/webpack/index.js new file mode 100644 index 000000000..5827755d4 --- /dev/null +++ b/packages/uni-cli-nvue/dist/webpack/index.js @@ -0,0 +1,37 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.runWebpackDev = exports.runWebpackBuild = void 0; +const webpack_1 = __importDefault(require("webpack")); +const config_1 = require("./config"); +function runWebpack(mode) { + return new Promise((resolve, reject) => { + webpack_1.default(config_1.createConfig(mode), (err, stats) => { + if (err) { + return reject(err.stack || err); + } + if (stats.hasErrors()) { + return reject(stats.toString()); + } + const info = stats.toJson(); + if (stats.hasWarnings()) { + console.warn(info.warnings); + } + console.log(stats.toString({ + chunks: true, + colors: true, // 在控制台展示颜色 + })); + resolve(void 0); + }); + }); +} +function runWebpackBuild() { + return runWebpack('production'); +} +exports.runWebpackBuild = runWebpackBuild; +function runWebpackDev() { + return runWebpack('development'); +} +exports.runWebpackDev = runWebpackDev; diff --git a/packages/uni-cli-nvue/lib/get-current-sub-nvue.js b/packages/uni-cli-nvue/lib/get-current-sub-nvue.js new file mode 100644 index 000000000..a3f1369ed --- /dev/null +++ b/packages/uni-cli-nvue/lib/get-current-sub-nvue.js @@ -0,0 +1,3 @@ +export default function getCurrentSubNVue() { + return uni.getSubNVueById(plus.webview.currentWebview().id) +} diff --git a/packages/uni-cli-nvue/lib/require-native-plugin.js b/packages/uni-cli-nvue/lib/require-native-plugin.js new file mode 100644 index 000000000..12d28f8af --- /dev/null +++ b/packages/uni-cli-nvue/lib/require-native-plugin.js @@ -0,0 +1,3 @@ +export default function requireNativePlugin(name) { + return weex.requireModule(name) +} diff --git a/packages/uni-cli-nvue/package.json b/packages/uni-cli-nvue/package.json new file mode 100644 index 000000000..5389079ec --- /dev/null +++ b/packages/uni-cli-nvue/package.json @@ -0,0 +1,29 @@ +{ + "name": "@dcloudio/uni-cli-nvue", + "version": "3.0.0-alpha-3000020210720001", + "description": "uni-cli-nvue", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "files": [ + "dist/**/*.js", + "dist/**/*.d.ts", + "lib" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/dcloudio/uni-app.git", + "directory": "packages/uni-cli-nvue" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "license": "Apache-2.0", + "gitHead": "56deaeb47d42e924d10282d7af418ccee6b139bf", + "dependencies": { + "terser-webpack-plugin": "^5.1.4", + "webpack": "^5.45.1" + }, + "devDependencies": { + "@types/terser-webpack-plugin": "^5.0.4" + } +} diff --git a/packages/uni-cli-nvue/src/index.ts b/packages/uni-cli-nvue/src/index.ts new file mode 100644 index 000000000..85ec0d839 --- /dev/null +++ b/packages/uni-cli-nvue/src/index.ts @@ -0,0 +1 @@ +export * from './webpack' diff --git a/packages/uni-cli-nvue/src/webpack/config/index.ts b/packages/uni-cli-nvue/src/webpack/config/index.ts new file mode 100644 index 000000000..5473b5afd --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/index.ts @@ -0,0 +1,21 @@ +import { Configuration } from 'webpack' +import { optimization } from './optimization' +import { output } from './output' +export function createConfig( + mode: 'production' | 'development' +): Configuration { + return { + target: 'node', + mode: mode, + devtool: false, + watch: mode === 'development', + entry() { + return {} + }, + externals: { + vue: 'Vue', + }, + optimization, + output, + } +} diff --git a/packages/uni-cli-nvue/src/webpack/config/optimization.ts b/packages/uni-cli-nvue/src/webpack/config/optimization.ts new file mode 100644 index 000000000..3071e98a6 --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/optimization.ts @@ -0,0 +1,14 @@ +import TerserPlugin from 'terser-webpack-plugin' +import { Configuration } from 'webpack' + +export const optimization: Configuration['optimization'] = { + minimizer: [ + new TerserPlugin({ + terserOptions: { + output: { + ascii_only: true, + }, + }, + }), + ], +} diff --git a/packages/uni-cli-nvue/src/webpack/config/output.ts b/packages/uni-cli-nvue/src/webpack/config/output.ts new file mode 100644 index 000000000..ffc352f46 --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/output.ts @@ -0,0 +1,6 @@ +import { Configuration } from 'webpack' + +export const output: Configuration['output'] = { + path: process.env.UNI_OUTPUT_DIR, + filename: '[name].js', +} diff --git a/packages/uni-cli-nvue/src/webpack/config/plugins/banner.ts b/packages/uni-cli-nvue/src/webpack/config/plugins/banner.ts new file mode 100644 index 000000000..ecfeed1cb --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/plugins/banner.ts @@ -0,0 +1,7 @@ +import { BannerPlugin } from 'webpack' + +export const banner = new BannerPlugin({ + banner: '"use weex:vue";', + raw: true, + exclude: 'Vue', +}) diff --git a/packages/uni-cli-nvue/src/webpack/config/plugins/define.ts b/packages/uni-cli-nvue/src/webpack/config/plugins/define.ts new file mode 100644 index 000000000..b1b2054c6 --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/plugins/define.ts @@ -0,0 +1,13 @@ +import { DefinePlugin } from 'webpack' + +export const define = new DefinePlugin({ + 'process.env': { + NODE_ENV: JSON.stringify(process.env.NODE_ENV), + VUE_APP_PLATFORM: JSON.stringify(process.env.UNI_PLATFORM), + UNI_CLOUD_PROVIDER: process.env.UNI_CLOUD_PROVIDER, + HBX_USER_TOKEN: JSON.stringify(process.env.HBX_USER_TOKEN || ''), + UNI_AUTOMATOR_WS_ENDPOINT: JSON.stringify( + process.env.UNI_AUTOMATOR_WS_ENDPOINT + ), + }, +}) diff --git a/packages/uni-cli-nvue/src/webpack/config/plugins/index.ts b/packages/uni-cli-nvue/src/webpack/config/plugins/index.ts new file mode 100644 index 000000000..e62872cb4 --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/plugins/index.ts @@ -0,0 +1,5 @@ +import { Configuration } from 'webpack' +import { define } from './define' +import { banner } from './banner' +import { provide } from './provide' +export const plugins: Configuration['plugins'] = [define, banner, provide] diff --git a/packages/uni-cli-nvue/src/webpack/config/plugins/provide.ts b/packages/uni-cli-nvue/src/webpack/config/plugins/provide.ts new file mode 100644 index 000000000..cca2441ca --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/plugins/provide.ts @@ -0,0 +1,22 @@ +import path from 'path' +import { ProvidePlugin } from 'webpack' +import { initProvide } from '@dcloudio/uni-cli-shared' + +const libDir = path.resolve(__dirname, '../../../../lib') +const definitions: Record = { + uniCloud: [ + require.resolve('@dcloudio/uni-cloud/dist/uni-cloud.es.js'), + 'default', + ], + 'uni.getCurrentSubNVue': [ + path.join(libDir, 'get-current-sub-nvue.js'), + 'default', + ], + 'uni.requireNativePlugin': [ + path.join(libDir, 'require-native-plugin.js'), + 'default', + ], + ...initProvide(), +} + +export const provide = new ProvidePlugin(definitions) diff --git a/packages/uni-cli-nvue/src/webpack/config/resolve.ts b/packages/uni-cli-nvue/src/webpack/config/resolve.ts new file mode 100644 index 000000000..509642e69 --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/config/resolve.ts @@ -0,0 +1,29 @@ +import path from 'path' +import { Configuration } from 'webpack' +import { resolveMainPathOnce } from '@dcloudio/uni-cli-shared' +const inputDir = process.env.UNI_INPUT_DIR +export const resolve: Configuration['resolve'] = { + extensions: ['.js', '.nvue', '.vue', '.json'], + alias: { + '@': inputDir, + 'uni-pages': path.resolve(inputDir, 'pages.json'), + '@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat'), + 'uni-app-style': + resolveMainPathOnce(inputDir) + + '?' + + JSON.stringify({ + type: 'appStyle', + }), + 'uni-stat-config': + path.resolve(inputDir, 'pages.json') + + '?' + + JSON.stringify({ + type: 'stat', + }), + }, + modules: [ + 'node_modules', + path.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'), + path.resolve(inputDir, 'node_modules'), + ], +} diff --git a/packages/uni-cli-nvue/src/webpack/index.ts b/packages/uni-cli-nvue/src/webpack/index.ts new file mode 100644 index 000000000..152403db0 --- /dev/null +++ b/packages/uni-cli-nvue/src/webpack/index.ts @@ -0,0 +1,35 @@ +import webpack from 'webpack' +import { createConfig } from './config' + +function runWebpack(mode: 'production' | 'development') { + return new Promise((resolve, reject) => { + webpack(createConfig(mode), (err, stats) => { + if (err) { + return reject(err.stack || err) + } + + if (stats!.hasErrors()) { + return reject(stats!.toString()) + } + const info = stats!.toJson() + if (stats!.hasWarnings()) { + console.warn(info.warnings) + } + console.log( + stats!.toString({ + chunks: true, // 使构建过程更静默无输出 + colors: true, // 在控制台展示颜色 + }) + ) + resolve(void 0) + }) + }) +} + +export function runWebpackBuild() { + return runWebpack('production') +} + +export function runWebpackDev() { + return runWebpack('development') +} diff --git a/packages/uni-cli-nvue/tsconfig.json b/packages/uni-cli-nvue/tsconfig.json new file mode 100644 index 000000000..98dd51629 --- /dev/null +++ b/packages/uni-cli-nvue/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.node.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", + "types/shims.d.ts", + "../shims-node.d.ts", + "../shims-uni-app.d.ts" + ] +} diff --git a/packages/uni-cli-shared/src/hbx/alias.ts b/packages/uni-cli-shared/src/hbx/alias.ts new file mode 100644 index 000000000..0f754257e --- /dev/null +++ b/packages/uni-cli-shared/src/hbx/alias.ts @@ -0,0 +1,51 @@ +import path from 'path' +import moduleAlias from 'module-alias' +import { isInHBuilderX } from './env' +import type { Formatter } from '../logs/format' + +const hbxPlugins = { + // typescript: 'compile-typescript/node_modules/typescript', + less: 'compile-less/node_modules/less', + sass: 'compile-dart-sass/node_modules/sass', + stylus: 'compile-stylus/node_modules/stylus', + // pug: 'compile-pug-cli/node_modules/pug', +} as const + +export function initModuleAlias() { + if (isInHBuilderX()) { + Object.keys(hbxPlugins).forEach((name) => { + moduleAlias.addAlias( + name, + path.resolve( + process.env.UNI_HBUILDERX_PLUGINS, + hbxPlugins[name as keyof typeof hbxPlugins] + ) + ) + }) + } +} + +export const moduleAliasFormatter: Formatter = { + test(msg) { + return msg.includes('Preprocessor dependency') + }, + format(msg) { + let lang = '' + let preprocessor = '' + if (msg.includes(`"sass"`)) { + lang = 'sass' + preprocessor = 'compile-dart-sass' + } else if (msg.includes(`"less"`)) { + lang = 'less' + preprocessor = 'compile-less' + } else if (msg.includes('"stylus"')) { + lang = 'stylus' + preprocessor = 'compile-stylus' + } + if (lang) { + return `预编译器错误:代码使用了${lang}语言,但未安装相应的编译器插件,请前往插件市场安装该插件: + https://ext.dcloud.net.cn/plugin?name=${preprocessor}` + } + return msg + }, +} diff --git a/packages/uni-cli-shared/src/hbx/env.ts b/packages/uni-cli-shared/src/hbx/env.ts new file mode 100644 index 000000000..4d287d9c0 --- /dev/null +++ b/packages/uni-cli-shared/src/hbx/env.ts @@ -0,0 +1,20 @@ +import path from 'path' +import { once } from '@dcloudio/uni-shared' + +export const isInHBuilderX = once(() => { + const { UNI_HBUILDERX_PLUGINS } = process.env + try { + const { name } = require(path.resolve( + UNI_HBUILDERX_PLUGINS, + 'about/package.json' + )) + return name === 'about' + } catch (e) { + // console.error(e) + } + return false +}) + +export const runByHBuilderX = once(() => { + return !!process.env.UNI_HBUILDERX_PLUGINS +}) diff --git a/packages/uni-cli-shared/src/hbx/index.ts b/packages/uni-cli-shared/src/hbx/index.ts index 48d0819ad..9e4246533 100644 --- a/packages/uni-cli-shared/src/hbx/index.ts +++ b/packages/uni-cli-shared/src/hbx/index.ts @@ -1,28 +1,8 @@ import path from 'path' -import { once } from '@dcloudio/uni-shared' - process.env.UNI_HBUILDERX_PLUGINS = process.env.UNI_HBUILDERX_PLUGINS || path.resolve(__dirname, '../../../../../../') -export const isInHBuilderX = once(() => { - const { UNI_HBUILDERX_PLUGINS } = process.env - if (!UNI_HBUILDERX_PLUGINS) { - return false - } - try { - const { name } = require(path.resolve( - UNI_HBUILDERX_PLUGINS, - 'about/package.json' - )) - return name === 'about' - } catch (e) { - // console.error(e) - } - return false -}) - -export const runByHBuilderX = once(() => { - return !!process.env.UNI_HBUILDERX_PLUGINS -}) +export * from './env' +export { initModuleAlias } from './alias' diff --git a/packages/uni-cli-shared/src/index.ts b/packages/uni-cli-shared/src/index.ts index 23374a1fd..491bf23cf 100644 --- a/packages/uni-cli-shared/src/index.ts +++ b/packages/uni-cli-shared/src/index.ts @@ -1,4 +1,5 @@ export * from './hbx' +export * from './logs' export * from './ssr' export * from './deps' export * from './json' diff --git a/packages/uni-cli-shared/src/logs/format.ts b/packages/uni-cli-shared/src/logs/format.ts new file mode 100644 index 000000000..2f389b636 --- /dev/null +++ b/packages/uni-cli-shared/src/logs/format.ts @@ -0,0 +1,21 @@ +import { isInHBuilderX } from '../hbx/env' +import { moduleAliasFormatter } from '../hbx/alias' + +export interface Formatter { + test: (msg: string) => boolean + format: (msg: string) => string +} + +const formatters: Formatter[] = [] + +if (isInHBuilderX()) { + formatters.push(moduleAliasFormatter) +} + +export function formatMsg(msg: string) { + const formatter = formatters.find(({ test }) => test(msg)) + if (formatter) { + return formatter.format(msg) + } + return msg +} diff --git a/packages/uni-cli-shared/src/logs/index.ts b/packages/uni-cli-shared/src/logs/index.ts new file mode 100644 index 000000000..ab7892f3d --- /dev/null +++ b/packages/uni-cli-shared/src/logs/index.ts @@ -0,0 +1 @@ +export { formatMsg } from './format' diff --git a/packages/uni-h5-vite/dist/handleHotUpdate/index.js b/packages/uni-h5-vite/dist/handleHotUpdate/index.js index aa5716d52..606765a7f 100644 --- a/packages/uni-h5-vite/dist/handleHotUpdate/index.js +++ b/packages/uni-h5-vite/dist/handleHotUpdate/index.js @@ -56,11 +56,6 @@ function createHandleHotUpdate() { return; } debugHmr(file); - server.ws.send({ - type: 'custom', - event: 'invalidate', - data: {}, - }); const pagesJson = uni_cli_shared_1.parsePagesJson(inputDir, platform); // 更新define const { define, server: { middlewareMode }, } = server.config; @@ -84,6 +79,10 @@ function createHandleHotUpdate() { for (const file of invalidateFiles) { yield invalidate(file, server.moduleGraph); } + server.ws.send({ + type: 'full-reload', + path: '*', + }); return []; }); }; diff --git a/packages/uni-h5-vite/dist/plugins/cssScoped.js b/packages/uni-h5-vite/dist/plugins/cssScoped.js index f71eecaf5..f3a7f34a4 100644 --- a/packages/uni-h5-vite/dist/plugins/cssScoped.js +++ b/packages/uni-h5-vite/dist/plugins/cssScoped.js @@ -48,6 +48,9 @@ function uniCssScopedPlugin() { if (!uni_cli_shared_1.EXTNAME_VUE.includes(path_1.default.extname(ctx.file))) { return; } + if (ctx.file.endsWith('App.vue')) { + return; + } debugScoped('hmr', ctx.file); const oldRead = ctx.read; ctx.read = () => __awaiter(this, void 0, void 0, function* () { diff --git a/packages/uni-h5-vite/src/handleHotUpdate/index.ts b/packages/uni-h5-vite/src/handleHotUpdate/index.ts index 569872e12..87ffb8d79 100644 --- a/packages/uni-h5-vite/src/handleHotUpdate/index.ts +++ b/packages/uni-h5-vite/src/handleHotUpdate/index.ts @@ -44,11 +44,6 @@ export function createHandleHotUpdate(): Plugin['handleHotUpdate'] { return } debugHmr(file) - server.ws.send({ - type: 'custom', - event: 'invalidate', - data: {}, - }) const pagesJson = parsePagesJson(inputDir, platform) // 更新define const { @@ -83,6 +78,10 @@ export function createHandleHotUpdate(): Plugin['handleHotUpdate'] { for (const file of invalidateFiles) { await invalidate(file, server.moduleGraph) } + server.ws.send({ + type: 'full-reload', + path: '*', + }) return [] } } diff --git a/packages/uni-h5-vite/src/plugins/cssScoped.ts b/packages/uni-h5-vite/src/plugins/cssScoped.ts index ad1f362bf..4bbc09f1e 100644 --- a/packages/uni-h5-vite/src/plugins/cssScoped.ts +++ b/packages/uni-h5-vite/src/plugins/cssScoped.ts @@ -39,6 +39,9 @@ export function uniCssScopedPlugin(): Plugin { if (!EXTNAME_VUE.includes(path.extname(ctx.file))) { return } + if (ctx.file.endsWith('App.vue')) { + return + } debugScoped('hmr', ctx.file) const oldRead = ctx.read ctx.read = async () => { diff --git a/packages/vite-plugin-uni/src/cli/action.ts b/packages/vite-plugin-uni/src/cli/action.ts index 57f3bb9d6..c3ca4e7ce 100644 --- a/packages/vite-plugin-uni/src/cli/action.ts +++ b/packages/vite-plugin-uni/src/cli/action.ts @@ -5,6 +5,21 @@ import { build, buildSSR } from './build' import { createServer, createSSRServer } from './server' import { initEnv } from './utils' +async function runNVue(mode: 'prod' | 'dev') { + let nvue + try { + nvue = require('@dcloudio/uni-cli-nvue') + } catch (e) {} + if (!nvue) { + return + } + if (mode === 'prod') { + await nvue.runWebpackBuild() + } else { + await nvue.runWebpackDev() + } +} + export async function runDev(options: CliOptions & ServerOptions) { initEnv('dev', options) try { @@ -13,8 +28,11 @@ export async function runDev(options: CliOptions & ServerOptions) { } else { await build(extend(options, { watch: true })) } + if (options.platform === 'app') { + await runNVue('prod') + } } catch (e) { - console.error(`error when starting dev server:\n${e.stack}`) + console.error(`error when starting dev server:\n${e.stack || e}`) process.exit(1) } } @@ -25,9 +43,12 @@ export async function runBuild(options: CliOptions & BuildOptions) { await (options.ssr && options.platform === 'h5' ? buildSSR(options) : build(options)) + if (options.platform === 'app') { + await runNVue('dev') + } console.log(` DONE Build complete.`) } catch (e) { - console.error(`error during build:\n${e.stack}`) + console.error(`error during build:\n${e.stack || e}`) process.exit(1) } } diff --git a/packages/vite-plugin-uni/src/cli/utils.ts b/packages/vite-plugin-uni/src/cli/utils.ts index 57a26fb89..596229663 100644 --- a/packages/vite-plugin-uni/src/cli/utils.ts +++ b/packages/vite-plugin-uni/src/cli/utils.ts @@ -64,7 +64,8 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) { ;(options as BuildOptions).outDir = process.env.UNI_OUTPUT_DIR } else { if (!(options as BuildOptions).outDir) { - ;(options as BuildOptions).outDir = path.join( + ;(options as BuildOptions).outDir = path.resolve( + process.cwd(), 'dist', process.env.NODE_ENV === 'production' ? 'build' : 'dev', process.env.UNI_PLATFORM @@ -75,7 +76,9 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) { // tips if (isInHBuilderX() && options.platform === 'app') { return ( - console.error(`Vue3 目前暂不支持编译至 App 端,近期将升级支持。`), + console.error( + `当前项目 Vue 版本为3,暂不支持编译至 App 端,近期将升级支持。` + ), process.exit(1) ) } diff --git a/packages/vite-plugin-uni/src/configResolved/index.ts b/packages/vite-plugin-uni/src/configResolved/index.ts index 90ae23d5e..cf83f5c80 100644 --- a/packages/vite-plugin-uni/src/configResolved/index.ts +++ b/packages/vite-plugin-uni/src/configResolved/index.ts @@ -1,5 +1,5 @@ -import { Plugin } from 'vite' -import { checkUpdate } from '@dcloudio/uni-cli-shared' +import { Plugin, ResolvedConfig } from 'vite' +import { formatMsg, checkUpdate } from '@dcloudio/uni-cli-shared' import { VitePluginUniResolvedOptions } from '..' import { initEnv } from './env' @@ -9,6 +9,7 @@ import { initPlugins } from './plugins' export function createConfigResolved(options: VitePluginUniResolvedOptions) { return ((config) => { initEnv(config) + initLogger(config) initOptions(options, config) initPlugins(config, options) initCheckUpdate() @@ -24,3 +25,10 @@ function initCheckUpdate() { versionType: pkg.version.includes('alpha') ? 'a' : 'r', }) } + +function initLogger({ logger }: ResolvedConfig) { + const { error } = logger + logger.error = (msg, opts) => { + return error(formatMsg(msg), opts) + } +} diff --git a/packages/vite-plugin-uni/src/index.ts b/packages/vite-plugin-uni/src/index.ts index 9202df190..557de52b7 100644 --- a/packages/vite-plugin-uni/src/index.ts +++ b/packages/vite-plugin-uni/src/index.ts @@ -6,6 +6,8 @@ import { VueJSXPluginOptions } from '@vue/babel-plugin-jsx' import VueJsxPlugin from '@vitejs/plugin-vue-jsx' import ViteLegacyPlugin from '@vitejs/plugin-legacy' +import { initModuleAlias } from '@dcloudio/uni-cli-shared' + import { createConfig } from './config' import { createConfigResolved } from './configResolved' import { createConfigureServer } from './configureServer' @@ -15,6 +17,8 @@ const debugUni = debug('vite:uni:plugin') const pkg = require('@dcloudio/vite-plugin-uni/package.json') +initModuleAlias() + process.env.UNI_COMPILER_VERSION = pkg['uni-app']?.['compilerVersion'] || '' export interface VitePluginUniOptions { diff --git a/scripts/utils.js b/scripts/utils.js index 12786ccfd..60ac719ac 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -14,6 +14,7 @@ const priority = { 'uni-mp-weixin': 70, 'uni-quickapp-webview': 70, 'uni-cli-shared': 60, + 'uni-cli-nvue': 55, 'uni-h5': 50, 'uni-h5-vite': 40, 'uni-app-vue': 35, -- GitLab