当前位置: 首页 > 编程笔记 >

详解weex默认webpack.config.js改造

章哲彦
2023-03-14
本文向大家介绍详解weex默认webpack.config.js改造,包括了详解weex默认webpack.config.js改造的使用技巧和注意事项,需要的朋友参考一下

本文介绍了weex默认webpack.config.js改造,分享给大家,具体如下:

解决的问题:

由于weex默认的webpack配置,会导致在src文件夹下的每一个.vue在temp文件夹下生成对应的一个.js文件,该js文件有一段这样的代码

contents += 'var App = require(\'' + relativePath + '\')\n';
 contents += 'App.el = \'#root\'\n';
 contents += 'new Vue(App)\n';

会导致多个vue对象挂载同一个id(#root),导致整个页面就只有一个vue对象,无法像写spa项目一样写weex项目,因此在这里对webpack.cofig进行改造(添加一个entry.js入口js文件,和一个最外层的App.vue承载路由渲染)

默认的webpack.config.js文件中,有两个方法

第一个 getEntryFileContent

const getEntryFileContent = (entryPath, vueFilePath) => {
 let relativePath = pathTo.relative(pathTo.join(entryPath, '../'), vueFilePath);
 let contents = '';
 /**
  * The plugin's logic currently only supports the .we version
  * which will be supported later in .vue
  */
 if (hasPluginInstalled) {
  const plugindir = pathTo.resolve('./web/plugin.js');
  contents = 'require(\'' + plugindir + '\') \n';
 }
 if (isWin) {
  relativePath = relativePath.replace(/\\/g, '\\\\');
 }
 contents += 'var App = require(\'' + relativePath + '\')\n';
 contents += 'App.el = \'#root\'\n';
 contents += 'new Vue(App)\n';
 return contents;
 }

第二个 walk

const walk = (dir) => {
 dir = dir || '.';
 const directory = pathTo.join(__dirname, 'src', dir);
 fs.readdirSync(directory).forEach((file) => {
  const fullpath = pathTo.join(directory, file);
  const stat = fs.statSync(fullpath);
  const extname = pathTo.extname(fullpath);
  if (stat.isFile() && extname === '.vue' || extname === '.we') {
  if (!fileType) {
   fileType = extname;
  }
  if (fileType && extname !== fileType) {
   console.log('Error: This is not a good practice when you use ".we" and ".vue" togither!');
  }
  const name = pathTo.join(dir, pathTo.basename(file, extname));
  if (extname === '.vue') {
   const entryFile = pathTo.join(vueWebTemp, dir, pathTo.basename(file, extname) + '.js');
   fs.outputFileSync(pathTo.join(entryFile), getEntryFileContent(entryFile, fullpath));
   entry[name] = pathTo.join(__dirname, entryFile) + '?entry=true';
  }
  weexEntry[name] = fullpath + '?entry=true';
  } else if (stat.isDirectory() && file !== 'build' && file !== 'include') {
  const subdir = pathTo.join(dir, file);
  walk(subdir);
  }
 });
 }

这两个方法,是遍历src中的.vue文件,然后在temp文件夹中生成一个相对应的JS文件

如果我们采用传统的vue开发,需要一个入口.js文件,我们需要对这个配置进行改造

添加入口文件配置

const entry = {index: pathTo.resolve('src', 'entry.js')};
const weexEntry = {index: pathTo.resolve('src', 'entry.js')};

删除或者更改配置(当然,第三种方法还可以把.vue组件不写在src内)

删除

  1. 删除getEntryFileContent函数
  2. 删除walk函数
  3. 删除walk() walk函数的调用

修改(代码来自github上高仿网易严选项目)

注意看最外层的if判断,添加了额外条件 如果是文件且后缀是.vue且不是App.vue的,则进入判断。否则,判断是不是page文件夹,如果不是,则结束。

function walk(dir) {
 dir = dir || '.';
 const directory = pathTo.join(__dirname, 'src', dir);
 fs.readdirSync(directory)
 .forEach((file) => {
  const fullpath = pathTo.join(directory, file);
  const stat = fs.statSync(fullpath);
  const extname = pathTo.extname(fullpath);
  const basename = pathTo.basename(fullpath);
  console.log("配置",file,fullpath,stat,extname,basename,)
  if (stat.isFile() && extname === '.vue' && basename != 'App.vue' ) {
  if (!fileType) {
   fileType = extname;
  }
  if (fileType && extname !== fileType) {
   console.log('Error: This is not a good practice when you use ".we" and ".vue" togither!');
  }
  const name = pathTo.join(dir, pathTo.basename(file, extname));
  if (extname === '.vue') {
   const entryFile = pathTo.join(vueWebTemp, dir, pathTo.basename(file, extname) + '.js');
   fs.outputFileSync(pathTo.join(entryFile), getEntryFileContent(entryFile, fullpath));
   entry[name] = pathTo.join(__dirname, entryFile) + '?entry=true';
  }
   weexEntry[name] = fullpath + '?entry=true';
  } else if (stat.isDirectory() && ['build','include','assets','filters','mixins'].indexOf(file) == -1 ) {
  const subdir = pathTo.join(dir, file);
  walk(subdir);
  }
 });
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍js阻止冒泡和默认事件(默认行为)详解,包括了js阻止冒泡和默认事件(默认行为)详解的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了js阻止冒泡和默认事件方法,供大家参考,具体内容如下 阻止冒泡。冒泡简单的举例来说,儿子知道了一个秘密消息,它告诉了爸爸,爸爸知道了又告诉了爷爷,一级级传递从而引起事件的混乱,而阻止冒泡就是不让儿子告诉爸爸,爸爸自然不会告诉爷爷了。下面的d

  • 本文向大家介绍Eclipse中改变默认的workspace的方法及说明详解,包括了Eclipse中改变默认的workspace的方法及说明详解的使用技巧和注意事项,需要的朋友参考一下 eclipse中改变默然的workspace的方法可以有以下几种: 1.在创建project的时候,手动选择使用新的workspace,如创建一个web project,在向导中的Location选项,取消使用"Us

  • 本文向大家介绍Ubuntu中安装MySQL更改默认密码的步骤详解,包括了Ubuntu中安装MySQL更改默认密码的步骤详解的使用技巧和注意事项,需要的朋友参考一下 第一步:进入目录:cd /etc/mysql,查看debian.cnf文件 第二步:使用上图中的账号密码登录MySQL。 第三步:查看数据库中的库。 第四步:使用mysql库。 第五步:使用一下语句设置账号密码:update user

  • 本文向大家介绍vue vue-Router默认hash模式修改为history需要做的修改详解,包括了vue vue-Router默认hash模式修改为history需要做的修改详解的使用技巧和注意事项,需要的朋友参考一下 主要是因为活动页会存在pc端的时候未登录的用户也需要访问的问题,因为未登录用户在活动页面进行操作的时候会触发到登录事件,然后我们实现的方式是通过接口来判断,该接口标记的是一个u

  • 本文向大家介绍python 默认参数相关知识详解,包括了python 默认参数相关知识详解的使用技巧和注意事项,需要的朋友参考一下 最常见的一种形式是的是为一个或者多个参数指定默认值,这会创建一个可以使用比定义时允许的参数更少的参数调用的函数, 可以用以下三种调用方式: ask_ok('Do you really want to quit?') ask_ok('OK to overwrite th

  • 本文向大家介绍Python进阶-函数默认参数(详解),包括了Python进阶-函数默认参数(详解)的使用技巧和注意事项,需要的朋友参考一下 一、默认参数 python为了简化函数的调用,提供了默认参数机制: 这样在调用pow函数时,就可以省略最后一个参数不写: 在定义有默认参数的函数时,需要注意以下: 必选参数必须在前面,默认参数在后; 设置何种参数为默认参数?一般来说,将参数值变化小的设置为默认