Weex - Mobile Cross-platform UI Framework
repository·master·Indexed 29 days ago
https://github.com/alibaba/weexA framework for building cross-platform mobile user interfaces for Android, iOS, Web, and OpenHarmony. It provides a JavaScript orchestration layer to enable web-standard APIs within native runtimes and supports integration with frameworks like Vue or Rax.
What's inside weex
- Weex is a framework designed for building cross-platform mobile user interfaces. It supports development for Android, iOS, and Web platforms.
Locate OpenHarmony (OHOS) Weex resources and examples
masterThe
ohosdirectory contains the following key components for OpenHarmony development:- Advanced API Extension Package:
AdvancedAPI_OHOS - Web Advanced API Extension Package:
web_api_ohos - OpenHarmony Integration Module:
web_scenekit_hsp - OpenHarmony Extension Plugin Package:
weex-openharmony - HarmonyOS Migration Guides: Located in the
docsdirectory within theohosfolder. - Example Projects:
ohos/example/ohos-example: DevEco Studio project.ohos/example/weex-example: Weex OpenHarmony project.
- Test Cases:
ohos/Test/commonTest: Weex test case project.
- Advanced API Extension Package:
Understand the Weex HarmonyOS Loader mechanism
masterThe Weex HarmonyOS Loader is a build-time tool (typically a Webpack/Babel plugin) that transforms Vue templates and scripts to enable HarmonyOS compatibility. It performs three main tasks:
- Attribute & Event Parsing: It scans Vue templates for specific attributes and events (e.g.,
@touchstart,:show-scrollbar). It extracts the handler names, parameters, and modifiers (like.stopor.prevent). - Class ID Injection: For elements with tracked attributes or events, the loader injects a unique
classname following the patternweex-harmony-register-[moduleHash]-[id]. This allows the runtime to map native events back to the correct DOM elements. - Script Transformation: It uses Babel to modify the component's JavaScript. It injects an import for
weex_harmongy_registerGestureand automatically injects the registration logic into themountedlifecycle hook to ensure events are registered when the component enters the DOM.
- Attribute & Event Parsing: It scans Vue templates for specific attributes and events (e.g.,
Implement partial refreshes for Weex pages
masterAfter successful preloading, use
history.pushStateto achieve partial page refreshes when navigating with parameters. In the Weex project, you must implement a listener for thepushStateevent.Typically, this involves extracting parameters from the
routerUrlusingUrlUtil.getUrlParams(routerUrl)and then executing a JavaScript command via the Web controller to trigger a reload with the new parameters.// Example of partial refresh implementation let params = UrlUtil.getUrlParams(routerUrl); webManagerInstance.getWebController(webId)?.runJavaScript(`reloadUrl("${params}")`); if (WebManager.getInstance().isPreloadFinish(routerUrl)) { PageModelController.getInstance().loading(false); }Run the Weex Playground App on iOS
masterTo run the Weex Playground App on an iOS device or simulator:
- Ensure CocoaPods is installed on your system.
- Open a terminal, navigate to the
iosdirectory, and executepod installto install dependencies. - Open the
WeexDemo.xcworkspacefile using Xcode. - Click the
Runbutton in Xcode.
Scaffold a Weex project using weex-toolkit
masterTo start a new project using the Webpack template, install the
weex-toolkitglobally and use theweex createcommand. This will scaffold a project structure suitable for large-scale applications usingweex-loaderand Webpack.$ npm install -g weex-toolkit $ weex create my-project # default will create the webpack template $ cd my-project && npm startBuild the legacy Weex Android SDK for backward compatibility
masterIf you are an existing user and need to maintain compatibility with the old
com.taobao.weexpackage name, you can build theweex_sdk_legacyversion.Warning:
weex_sdk_legacyis provided for backward compatibility only and will not be maintained. You should migrate toweex_sdkas soon as possible../gradlew clean assembleRelease -PapachePackageName="false"Register a custom loader for .vue files in Webpack
masterTo support HarmonyOS adaptation, you may need to use a custom loader (e.g.,
transform-loader.js) to parse.vuefiles. When configuring Webpack, ensure your custom loader is placed at the end of theusearray so that it executes after other loaders likevue-loader.Example configuration for
webpack.common.conf.js:module.exports = { module: { rules: [ { test: /\.vue(\?[^?]+)?$/, use: [ { loader: 'vue-loader', options: { postcss: [require('autoprefixer')({ browsers: ['last 10 Chrome versions', 'last 5 Firefox versions', 'Safari >= 6', 'ie > 8'] })], compilerOptions: { modules: [ { postTransformNode: el => { require('weex-vue-precompiler')()(el) } } ] } } }, { loader: path.resolve(__dirname, 'transform-loader.js'), } ], }, ] }, };Configure Android Studio Build Variants for Weex
masterYou can switch between the modern and legacy package structures within Android Studio using the Build Variants window (accessible via
SHIFT+Enter->Build Variants).- To use files under
org.apache.weex: SelectapacheReleasein the Active Build Variants dropdown. - To use files under
com.taobao.weex: SelectlegacyReleaseorreleasein the Active Build Variants dropdown.
- To use files under
Run Apache-Rat to verify release files
masterApache-Rat can be used to verify that release files comply with license requirements. To run it against the Weex project, you must first download the Apache Rat binary (version 0.12 is tested) and decompress it. Then, execute the Ant build command pointing to the project's Rat configuration and the downloaded Rat JAR file.
For specific rule definitions, refer to
scripts/rat-ant-build.xmlin the repository.ant -buildfile scripts/rat-ant-build.xml -lib path_to_the_folder_you_place_rat/apache-rat-0.12.jarUse Sass in Weex templates
masterTo use Sass in your Weex project, installnode-sassandsass-loaderas development dependencies. Once installed, you can use Sass by specifying thelang="sass"attribute on your<style>tags.Install js-base64
masterYou can install the
js-base64package via npm to use it in your projects.$ npm install --save js-base64