GithubHelp home page GithubHelp logo

feflow / builder-webpack4 Goto Github PK

View Code? Open in Web Editor NEW
67.0 8.0 19.0 168 KB

腾讯IVWEB团队使用的基于Webpack4的业务开发构建器

License: MIT License

JavaScript 16.67% CSS 4.16% HTML 8.87% TypeScript 70.30%

builder-webpack4's Introduction

builder-webpack

GitHub license npm package NPM downloads PRs Welcome developing with feflow

Webpack 构建器, 适用于NOW直播业务和活动类型的项目构建

特性

  • 使用webpack4 + babel7 最新的构建解决方案
  • 对H5开发友好,默认集成 Rem 方案,解决适配问题
  • 支持多页面打包的开发方式
  • 支持less和typescript的文件打包
  • 支持CSS Modules

安装

确保feflow的版本在 v0.12.0 以上, 可以通过如下命令安装最新feflow版本

$ npm install feflow-cli -g

快速使用

添加feflow.json配置文件

在项目根目录添加 feflow.json 配置文件

{
    "builderType": "builder-webpack3",
    "builderOptions": {
        "product": "now",                                    // 产品,此处可以是 now 或者 shangfen
        "domain": "now.qq.com",                              // 域名,离线包的域名需要使用
        "cdn": "11.url.cn",                                  // 资源发布到的cdn名称
        "moduleName": "mobile",                              // 部署的模块
        "bizName": "category",                               // 业务名称
        "minifyHTML": true,                                  // 是否压缩 html
        "minifyCSS": true,                                   // 是否压缩 js
        "minifyJS": true,                                    // 是否压缩 css
        "inlineCSS": true,                                   // 生成的 css 是否内联到首屏
        "usePx2rem": true,                                   // 是否使用 Rem
        "useReact": true,                                    // 是否是 React,如果为false,则不会在 html 中引用 React 框架 
        "remUnit": 37.5,                                     // Rem 单位,对于 375 视觉稿,此处填写 37.5,750视觉稿需要改成 75 
        "remPrecision": 8,                                   // Rem 的精度,即 px 转换成了 rem 后的小数点后位数
        "inject": true,                                      // 打包生成的 js 文件是否自动注入到 html 文件 body 之后
        "port": 8001,                                        // 本地开发的 webpack 构建服务进程端口号
        "babelrcPath": ""                                    // 指定.babelrc文件相对根目录的路径,默认加载根目录的.babelrc
        "externals": [                                       // 基础框架不打入到 bundle 里面
            {
                "module": "react",
                "entry": "//11.url.cn/now/lib/16.2.0/react.min.js?_bid=3123",
                "global": "React"
            },
            {
                "module": "react-dom",
                "entry": "//11.url.cn/now/lib/16.2.0/react-dom.min.js?_bid=3123",
                "global": "ReactDOM"
            }
        ]
    }
}

命令

$ feflow dev      # 本地开发时的命令
$ feflow build    # 发布时的打包命令, 打出的包在工程的public目录, 包含 cdn, webserver 和 offline 三个文件夹

文档

内联

同时支持Fis3项目的inline语法糖写法和ejs的写法

  • 内联 html:
<!--inline[/assets/inline/meta.html]-->
  • 内联 javascript
<script src="@tencent/report-whitelist?__inline"></script>

备注:如果希望内联某个 JS 文件,需要使用相对路径的写法。

代理设置

  • 执行 feflow dev 命令后会在本地的 8001 端口开启一个 WDS 服务,所有的静态资源(html, css, js, img) 都会在内存里面。可以通过 http://127.0.0.1:8001/webpack-dev-server 查看

  • Fiddler配置把之前的本地绝对路径改成 本地server 路径即可:

热更新支持

  • 如果要支持热更新,需要再增加一条代理_webpack_hmr的配置,如:

/^https?://now\.qq\.com/(__webpack_hmr)$/ http://127.0.0.1:8001/$1

  • 在项目中,用react-hot-loaderpageComponent变为可接受热更新的组件
import { hot } from 'react-hot-loader'
class pageComponent extends Component {
    ...
}
export default hot(module)(pageComponent)

使用CSS Modules

本构建器默认启用CSS Modules,可生成全局唯一的类名/id名,避免样式污染。只需将样式文件命名为[name].module.(css|less),那么定义在里面的类名和id就会经过CSS Modules转化,不按此规则命名的样式文件,其内容不会经过CSS Modules处理。推荐结合babel-plugin-react-css-modules使用,可简化语法,在项目中安装配置

npm i -S babel-plugin-react-css-modules postcss-less

然后在项目根目录下添加一个babel.config.js文件,内容如下:

const path = require('path');
const loaderUtils = require('loader-utils');

/**
 * 用于css-loader转换类名,与构建器内置的一致:
 * 1.去除样式文件名的'.module'前缀;
 * 2.遇到以'index.module.xxx'命名的样式文件使用文件夹名代替文件名来组成转换后的类名。
 * 此方法基于'react-dev-utils/getCSSModuleLocalIdent',增加less正则匹配(https://www.npmjs.com/package/react-dev-utils)
 * @param context webpack传给css-loader的context对象
 * @param localIdentName css-loader的options.localIdentName,没传默认是'[hash:base64]',这里用不到
 * @param localName 原始css类名
 * @param options css-loader中三个配置项的组合,长这样:
   {
      regExp: options.localIdentRegExp,
      hashPrefix: options.hashPrefix || '',
      context: options.context,
   }
 */
function getCSSModulesLocalIdent(
    context,
    localIdentName,
    localName,
    options
) {
    // Use the filename or folder name, based on some uses the index.js / index.module.(css|scss|sass) project style
    const fileNameOrFolder = context.resourcePath.match(
        /index\.module\.(css|scss|sass|less)$/
    )
        ? '[folder]'
        : '[name]';
    // Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
    const hash = loaderUtils.getHashDigest(
        path.posix.relative(context.rootContext, context.resourcePath) + localName,
        'md5',
        'base64',
        5
    );
    // Use loaderUtils to find the file or folder name
    const className = loaderUtils.interpolateName(
        context,
        fileNameOrFolder + '_' + localName + '__' + hash,
        options
    );

    // remove the .module that appears in every classname when based on the file.
    return className.replace('.module_', '_');
}

/**
 * 由于入参不一致,这里包装一层调用getCSSModulesLocalIdent
 * @param name 原始类名
 * @param filename 样式文件路径
 */
function generateScopedName(name, filename) {
    const loaderContext = {
        rootContext: process.cwd(), // 保持与webpack loader context的rootContext一致(默认是项目根目录)
        resourcePath: filename
    };
    return getCSSModulesLocalIdent(
        loaderContext,
        undefined,
        name,
        {}
    );
}

module.exports = function (api) {
    api.cache(true);

  	const presets = [];
    const plugins = [
        [
            'react-css-modules',
            {
                context: process.cwd(), // 保持与webpack loader context的rootContext一致(默认是项目根目录)
                filetypes: {
                    '.less': {
                        syntax: 'postcss-less'
                    }
                },
                generateScopedName,
                webpackHotModuleReloading: true,
              	autoResolveMultipleImports: true
            }
        ]
    ];

    return {
      	presets,
        plugins
    };
};

然后启动项目,那么在[name].module.(css|less)中定义的类名就可以在React组件中通过styleName prop引用。

如果你同时需要使用feflow.json的babelrcPath配置,那么请同样以js的形式定义babel配置,并把上述内容整合进去,然后再指定给babelrcPath(因为指定了babelrcPath就不能同时读取根目录的babel.confis.js)。

相关资料:

测试

  1. git clone这个用于烟雾测试的模板项目
  2. 配置.travis.yml,可以参考模板项目README
  3. Travis-ci中打开此项目的自动构建

版本日志

版本日志

许可证

MIT

builder-webpack4's People

Contributors

cpselvis avatar fxy-during avatar leo555 avatar leoeatle avatar stevenzwzhai avatar xqxian avatar zacks223 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

builder-webpack4's Issues

css module

i want use css module,but its not support.

通过绝对路径索引的图片没有被正确打包

feflow支持的绝对路径

/assets/images/xxx/aaa.png === /src/assets/images/xxx/aaa.png

实际按照左侧写法后打包时图片并没有被装进包中

引用的方法:

background: url('/assets/images/xxx/aaa.png');

scss支持

有些老项目迁移(涉及mixin、变量命名等)需要支持scss编译

需要能够支持指定 page 开启 rem

通过配置 usePx2rem 参数,可以开启 rem 支持,但是有些场景下,我们的某些 page 不需要开启,例如在 ipad 场景和pc场景;因此,需要支持区分页面来开启 rem

开发环境使用的16.2.0版react在移动端使用React.Fragment会报错

开发环境使用的cdn版react 16.2.0(//s.url.cn/now/lib/16.2.0/react.js),当项目中有使用<React.Fragment/>时,在移动端访问页面会报错:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

然而此时在pc端并不会报错。如果换成压缩版(//s.url.cn/now/lib/16.2.0/react.min.js?_bid=3234),则在移动端访问也不会报错。

webpack4构建器分工

任务名称 负责人 开发状态 如何升级 完成时间
html-webpack-plugin tickli 完成 平滑升级到4.0.0-beta.1 2018.10.10
extract-text-webpack-plugin erwinliu 完成 mini-css-extract-plugin替代 2018.10.10
clean-webpack-plugin leoytliu 完成 平滑升级到0.1.19 2018.10.10
html-webpack-externals-plugin lewischeng 完成 平滑升级到1.1.0-rc.7 2018.10.10
html-inline-css-webpack-plugin tickli 完成 代替 html-webpack-inline-source-plugin 2018.10.11
replace-bundle-webpack-plugin erwinliu 完成 插件已移除 2018.10.10
html-string-replace-webpack-plugin leoytliu 完成 已移除,原来是为了替换html中的cdn目录问题,现在不用了 2018.10.10
string-replace-webpack-plugin lewischeng 开发中
webpack-subresource-integrity tickli 完成 平滑升级到1.1.0 2018.10.11
offline-webpack-plugin erwinliu 完成 通过改写 API 支持 webpack 4 2018.10.10
Happypack lewischeng 完成 使用thread-loader替代 2018.10.10
webpack-parallel-uglify-plugin tickli 完成 目前的uglifyjs-webpack-plugin可以通过配置parallel=true直接支持
webpack-deep-scope-analysis-plugin leoytliu 开发中
SSR插件开发 lewischeng 开发中
Prerender插件开发 leoytliu 开发中
PWA插件开发 tickli 开发中
支持optional chaining语法糖 lewischeng 开发中
废弃对Sass支持,仅支持Less erwinliu 完成 已经去掉

typescript的支持

  1. 现在还在用happypack连接ts-loader支持ts,需要改为thread-loader,并且加上fork-ts-checker-webpack-plugin
  2. 测试用的项目无论是这个项目里面还是now那个项目,都还没有ts文件,这个测试用例没测到,需要补充
  3. ts-loader需要配置tsconfig,需要脚手架支持

支持prerender

希望支持prerender功能,可以参考prerender-spa-plugin

webpack版本导致mini-css-extract-plugin出现错误

虽然builder-webpack4已经指定版本"webpack": "^4.20.2"
但是在构建中我们的webpack版本为.feflow目录中node_modules的版本,这个版本低于4
这样会导致出现如下错误

image

相关issue已经提到是因为webpack版本问题
#webpack-contrib/mini-css-extract-plugin#69

所以我们需要找到一种方式能够让feflow使用builder中依赖的webpack,否则feflow永远只有一个版本的webpack可以用。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.