sorrycc / blog

💡
4.48k stars 325 forks source link

roadhog 2.0 发布 #55

Open sorrycc opened 6 years ago

sorrycc commented 6 years ago

趁着元旦,roadhog 2.0 发布了,我们一起来看看有啥变化吧。

👏 👏 👏  

一、

首先,我们把底层重写,基于 af-webpack

为啥要基于 af-webpack?单看 roadhog,这意义不大,但如果有数个工具都基于 af-webpack 做上层封装,那么就意味着 webpack 工具层的配置统一。 尤其对我们内部来说,有 4-5 个工具,每个工具有各自的配置方法,对于开发者来说无疑是个灾难。对外部的同学来说,umi 和 roadhog 都基于 af-webpack,那么之后在这两个工具之间切换,webpack 的配置层是无需变动的。

另外,大家会发现 roadhog 的 issue 维护不太积极,是因为我的工作重心目前不在这,而是在 umi 上。所以切换到 af-webpack,af-webpack 是 umi 的底层共建,这样就可以变相地保证我在这个项目上的投入。

同时,为了更通用,配置文件从 .roadhogrc 更名为 .webpackrc

 

二、

然后,在体验上,保持了和 create-react-app 最新版一致。像是 redbox 显示出错信息、HMR、出错点击后跳转到 IDE、ESLint 出错提示等等。

并在此基础上,增加了:

 

三、

roadhog 从命令的维度看,包含 dev (mock),build,test。

变化是:

 

四、

配置项即功能,这里有比较大的变化。

不再支持的有:

功能变化的有:

新的有:

 

五、

此外,还有一些变化:

 

六、

最后,我们对照 ant-design-pro 的升级 PR 来看下如何升级到 raodhog@2 。

对于大部分的应用来说,只要:

然后,由于内置了 babel@7

再然后,如果有 src/index.ejs,需要额外配置 html 属性,

"html": { "template": "./src/index.ejs" }

最后,有些配置不再支持,需考虑替代方案或使用 webpack.config.js 进行配置。

 

(完)

afc163 commented 6 years ago

之前有用 src/index.ejs,需移到 public/index.html,并手动引入 CSS 和 JS

如果开启了 hash,这个 html 岂不是要手动维护。

sorrycc commented 6 years ago

这个 public/index.html dev 用(dev 时不会开 hash 的),build 后通常不会用到静态的 html。之前的 html 生成没处理好,很多反馈,就先下掉了,后面后需求再加上。

afc163 commented 6 years ago

这样会让人误解的,public/index.html 引用的 index.js 在 dist 里根本不存在。

sorrycc commented 6 years ago

那开个 html 的配置项,然后透传给 html-webpack-plugin 吧。

e.g.

"html": {
  "template": "./src/index.ejs"
}
sorrycc commented 6 years ago

https://github.com/umijs/umi/pull/39

elivoa commented 6 years ago

“出错点击后跳转到 IDE” 这个 IDE如何设置?

sorrycc commented 6 years ago

“出错点击后跳转到 IDE” 这个 IDE如何设置?

不用设置,自动判断的。

elivoa commented 6 years ago

不用设置,自动判断的。

我点击后打开的是sublime text,但是我想让他用web storm打开。是和操作系统的设置有关?比如js文件的默认打开程序?但是我.js默认使用VSCode打开的。

sorrycc commented 6 years ago

你把 sublime 关了,然后开着 webstorm,看下是用什么打开的。

elivoa commented 6 years ago

明白了,要设置REACT_EDITOR环境变量

Could not open search.js in the editor. The editor process exited with an error: spawn code ENOENT.

To set up the editor integration, add something like REACT_EDITOR=atom to the .env.local file in your project folder and restart the development server. Learn more: https://goo.gl/MMTaZt

henry-fun commented 6 years ago

由于 babel@7 无法使用 babel-plugin-add-module-exports,所有的 require('./file') 需改为 require('./file').default 感觉这个对原项目的改动会很多,暂时还是用1.3.0版本,等以后解决这个问题了再考虑升版本。

elivoa commented 6 years ago

dllPlugin (学习成本太高,并且有其他方法可以提升 dev server 启动速度)

有什么办法可以提高dev server速度的么?现在没了dll plugin,启动速度和重新编译速度都很慢。我的项目要半分钟了。原来使用dllplugin的时候是重编速度6-7秒。

sorrycc commented 6 years ago

有什么办法可以提高dev server速度的么?现在没了dll plugin,启动速度和重新编译速度都很慢。我的项目要半分钟了。原来使用dllplugin的时候是重编速度6-7秒。

按需编译,umi 里有实现。

sorrycc commented 6 years ago

感觉这个对原项目的改动会很多,暂时还是用1.3.0版本,等以后解决这个问题了再考虑升版本。

require 用的地方应该不多吧,大部分应该是 import 才对。而且 require().default 是标准的写法,应该不会再回去了,拥抱变化吧,🤗。

HeskeyBaozi commented 6 years ago

👍 2.0 普天同庆

iceberg211 commented 6 years ago

不支持装饰器的写法了吗?

sorrycc commented 6 years ago

不支持装饰器的写法了吗?

要额外引 babel 插件的,1.0 的时候也是这样。

sorrycc commented 6 years ago

commons 可以提高开发时修改之后重新编译的时间么?

不能。

iceberg211 commented 6 years ago

要额外引 babel 插件的,1.0 的时候也是这样。 是babel-plugin-transform-decorators-legacy吧,我在.webpackrc这样配的

"extraBabelPlugins": [
"transform-decorators-legacy",
"lodash", ["import", { "libraryName": "antd-mobile", "style": true }]
],

其他的步骤也照做了

bibiehy commented 6 years ago

dva new 创建项目后,手动升级roadhog@2.0.3,严格按照上面修改,但编译失败了。

image .webpackrc 文件配置: image

sorrycc commented 6 years ago

@bibiehy 如果有 babel-runtime 依赖和配置了 babel-plugin-transform-runtime 插件,需删除,因为已内置处理,否则可能会报 this.setDynamic is not a function 的错误

bibiehy commented 6 years ago

@sorrycc 谢谢,2个都删除掉,修改"extraBabelPlugins": ["@babel/transform-runtime"]就好了。

还一个问题,js 校验工具ESLint 的配置在哪里修改或者禁用。

@+babel/transform-runtime不见了。|- _ -

sorrycc commented 6 years ago

可以 DISABLE_ESLINT roadhog dev 禁用,修改的话本地配 .eslintrc 就好了。

Tsury commented 6 years ago

No more --watch for roadhog build? I need that... I'm building for a non-browser app and I need to build on every change. The watch option made it very fast. Now if I set a watch task, it rebuilds everything on every change, which is too slow for me.

sorrycc commented 6 years ago

@Tsury thanks for feedback, it's supported in roadhog@2.0.6 .

Tsury commented 6 years ago

@sorrycc thanks! Another request - is it possible to build in debug? I'm trying to build, but it only builds for production.

I see this line in af-webpack build.js line 32: process.env.NODE_ENV = 'production'; // These sizes are pretty large. We'll warn for bundles exceeding them.

Then in af-webpack's getConfig.js file line 66: const isDev = process.env.NODE_ENV === 'development';

And then everything in the config is optimized for release (uglified, etc...) because isDev === false.

Is there a way to build debug output files (no dev server)? No compression, no optimizations.

sorrycc commented 6 years ago

@Tsury No compression is supported by NO_COMPRESS=1 roadhog build.

Tsury commented 6 years ago

It's still not enough. There are properties in the config that are determined solely by 'isDev', which is false. When I set NO_COMPRESS=1, my binary get's just a little bigger (~1.7mb), but it still very optimized. When I force isDev === true, the binary get's huge (5.5mb), and there's no compression.

Also, another issue - because of: process.env.NODE_ENV = 'production'; // These sizes are pretty large. We'll warn for bundles exceeding them.

This line forces my ENV to be production, and then my 'production' configuration in .webpackrc.js is used, instead of my development configuration.

Something here looks a little awkward to me.

EDIT: I would just like to control the NODE_ENV myself, and build debug, without af-webpack forcing it to be 'production'... EDIT 2: This forcing of NODE_ENV will also make React run in production mode. To summarize - I can't currently use roadhog build and have NODE_ENV = 'development' LAST EDIT: I believe all of this is caused because now roadhog build --debug was removed.

wanghaomayu commented 6 years ago

用roadhog和create-react-app脚手架创建的项目,体验和目录结构都一样,而且您也说过可以理解为roadhog是可配置版的create-react-app,那么请问一下roadhog形式的项目(比如用dva-cli创建的项目)是在create-react-app的代码基础上升级优化的么

sorrycc commented 6 years ago

@wanghaomayu

wanghaomayu commented 6 years ago

@sorrycc 哦哦,基于react-dev-utils,谢谢

handsomeceleste commented 6 years ago

删除了svgSpriteLoaderDirs那如何实现antd-mobile2的自定义图标呢? 按照https://mobile.ant.design/docs/react/upgrade-notes-cn#1.x-=>-2.0提供的CustomIcon的方法还是要用到svg-sprite-loader。 难道要自己写webpack.config.js?建议把这个选项加回来。

goonwzz commented 6 years ago

less还不能用alias?

sorrycc commented 6 years ago

@goonwzz 可以的,你用法不对吧,这里有例子 https://github.com/umijs/umi/tree/master/packages/af-webpack/test/fixtures/alias

paranoidjk commented 6 years ago

@handsomeceleste

考虑的原因主要是

smithyj commented 6 years ago

@sorrycc 使用antd发现无法按需加载,如图 qq20180119-201803 qq20180120-001157 项目地址:https://github.com/smithyj/backend.yangyj.com

sorrycc commented 6 years ago

@smithyj 手动引了 antd 的 css ?

smithyj commented 6 years ago

@sorrycc 没有引用的,https://github.com/smithyj/backend.yangyj.com 这是项目地址

Tsury commented 6 years ago

@sorrycc thanks for version 2.2.0, now I have real debug mode 👍

I have an issue with svg support now. I tried to add support for svg-sprite-loader. I added this to my webpack.config.js:

  webpackConfig.module.rules.push({
    test: /\.svg$/,
    loader: require.resolve('svg-sprite-loader')
  });

  webpackConfig.plugins.push(
    new SpriteLoaderPlugin()
  );

But all of my svg's were base64. I debugged and saw that svg-sprite-loader was working, but url-loader was overriding it.

I look at the code of af-webpack and found this in getConfig.js, line 333:

          exclude: [
            /\.html|ejs$/,
            /\.json$/,
            /\.(js|jsx|ts|tsx)$/,
            /\.(css|less|scss|sass)$/,
          ],
          loader: require.resolve('url-loader'),

When I added /\.svg$/ to the exclude list, svg-sprite-loader worked just fine.

Is it possible to fix this somehow? Maybe add a flag to add svg tourl-loader? Something like svgUrlLoader: true

Thanks!

smithyj commented 6 years ago

@kingsily 这种问题就不用问了大兄弟 ,这是属于eslint验证问题 你ESLINT_DISABLE=true就好了

kingsily commented 6 years ago

@smithyj 如果一定要用这个eslint,那么.eslintrc要怎么修改,或者说有没有什么正确的配置

smithyj commented 6 years ago

如下代码,在 start 中 增加环境变量 cross-env DISABLE_ESLINT=true ,但是得先 npm install --save cross-env

{
  "private": true,
  "scripts": {
    "start": "cross-env DISABLE_ESLINT=true roadhog dev",
    "start:no-proxy": "cross-env NO_PROXY=true DISABLE_ESLINT=true roadhog dev",
    "build": "cross-env DISABLE_ESLINT=true roadhog build",
    "analyze": "cross-env ANALYZE=true roadhog build",
    "lint": "eslint --ext .js src test",
    "precommit": "npm run lint"
  },
  "dependencies": {
    "@babel/polyfill": "^7.0.0-beta.38",
    "antd": "^3.1.3",
    "autobind": "^1.0.3",
    "classnames": "^2.2.5",
    "dva": "^2.1.0",
    "dva-loading": "^1.0.4",
    "enquire-js": "^0.1.1",
    "lodash": "^4.17.4",
    "lodash-decorators": "^4.5.0",
    "moment": "^2.20.1",
    "path-to-regexp": "^2.1.0",
    "prop-types": "^15.6.0",
    "qs": "^6.5.1",
    "rc-drawer-menu": "^0.5.4",
    "react": "^16.2.0",
    "react-container-query": "^0.10.0",
    "react-document-title": "^2.0.3",
    "react-dom": "^16.2.0",
    "url-polyfill": "^1.0.10"
  },
  "devDependencies": {
    "babel-eslint": "^8.2.1",
    "babel-plugin-dva-hmr": "^0.4.1",
    "babel-plugin-import": "^1.6.3",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "cross-env": "^5.1.3",
    "eslint": "^4.14.0",
    "eslint-config-umi": "^0.1.1",
    "eslint-plugin-flowtype": "^2.34.1",
    "eslint-plugin-import": "^2.6.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-react": "^7.1.0",
    "husky": "^0.12.0",
    "mockjs": "^1.0.1-beta3",
    "redbox-react": "^1.4.3",
    "roadhog": "^2.2.0",
    "roadhog-api-doc": "^1.0.0"
  }
}
smithyj commented 6 years ago

这是关闭验证的,如果你要验证的话 那就得好好学学 ESLINT 语法规范

kingsily commented 6 years ago

@smithyj 关闭验证倒不用那么麻烦直接set DISABLE_ESLINT=true&& roadhog dev就好了,不过还是谢谢了。

lehug commented 6 years ago

@sorrycc @afc163 参考了上面的alias的配置,配置的内容如下

{
  "entry": "src/index.js",
  "extraBabelPlugins": [
    "transform-decorators-legacy",
    ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": true }]
  ],
  "alias": {
    "~": "./src/",
    "common": "./src/common/"
  },
....
}

但是不起作用,求助应该怎么修改?另外可否告知下此项目为何不用alias?是项目路径不深还是有其他考虑?

经自测,用绝对路径可以,如

  "alias": {
    "~": "/home/zcs/projects/antd-pro/src/"
  },

但是相对路径的方式不行,是否属于bug?请修复下~

smithyj commented 6 years ago

@lehug 是的,你看看webpack官方文档

kingsily commented 6 years ago

升级2.0之后打包,之前require.ensure动态加载的时候可以配置chunk名字,现在用dynamic,要怎么配置chunk名字?

lehug commented 6 years ago

@smithyj webpack的官方文档,需要path变量,用json方式无法配置吧?纯json方式,该怎么配置呢?

i-smile commented 6 years ago

@lehug 可以使用JS的配置方式,不使用json

i-smile commented 6 years ago

HMR热更新需要怎么配置吗?我的怎么好像都是全页面刷新呀~