当前位置:首页> 社会热点 > 用html table theadk从零开始的html border-radius-cli搭建'生活'

用html table theadk从零开始的html border-radius-cli搭建'生活'

2022-10-03 21:05:51 来源: 网络   编辑: 佚名   浏览(243)人   
0

html table theadk从零开始的html border-radius-cli搭建'生活'    

有了html border-radius-cli的帮助,我们创建html border-radius的项目非常的方便,使用html border-radiuscreate然后选择些需要的配置项就能自动帮我们创建配置好的html table theadk项目脚手架了,实在是‘居家旅行’必备良药。这次借着学习html table theadk的机会,不用html border-radius-cli搭建一个html border-radius项目。

    注:基于html table theadk5,其运行于Node.jsv10.13.0+的版本。

    完整代码:https://html border-radius.com/mashiro-cat/learn_html table theadk

    html table theadk基础

    html table theadk官网:https://html table theadk.js.org/

    html table theadk中文官网:https://html table theadk.docschina.org/

    安装:

    npmihtml table theadkhtml table theadk-cli-D

    运行:

    npxhtml table theadk./src/main.js--mode=development

    #根目录有配置文件

    npxhtml table theadk

    打开文档就能看到五个核心配置点:

    入口(entry)

    输出(output)

    html border-radius

    插件(plugin)

    模式(mode)

    html table theadk本身只提供了对jsESModule和压缩的支持,很多功能都要通过使用html border-radius或者plugin拓展。

    html table theadk.config.js配置文件编写:

    module.exports={

    //入口多入口则配置成对象形式

    entry:"",

    //输出需使用绝对路径

    output:{},

    //html border-radius

    module:{

    rules:[]

    },

    //插件

    plugins:[],

    //development或者production

    //生产模式默认开启jshtml压缩

    mode:"development"

    }

    样式资源处理

    配置资源输出的路径和名称

    输出:

    output:{

    path:path.resolve(__dirname,'dist'),

    filename:'static/js/main.js'//js输出到static/js目录中

    }

    module中:

    generator:{

    //将图片文件输出到static/imgs目录中

    //将图片文件命名[hash:8][ext][query]

    //[hash:8]:hash值取8位直接[hash]则不截取

    //[ext]:使用之前的文件扩展名

    //[name]:会使用之前的名字

    //[query]:添加之前的query参数

    filename:"static/imgs/[hash:8][ext][query]",

    },

    css处理

    安装两个html border-radius,其使用顺序是css-html border-radius会处理css,而将编译的cssstyle-html border-radius后会动态创建style标签。

    css-html border-radius

    #安装

    npmicss-html border-radiusstyle-html border-radius-D

    rules:[

    //两个html border-radius顺序按此它会先使用后面的

    {test:/\.css$/i,use:["style-html border-radius","css-html border-radius"]}

    ]

    提取css到单独文件

    现在是css全部是打包到js中,然后动态插入的。若需要提取到单独文件,则可以借助插件。

    //安装插件

    npmimini-css-extract-plugin-D

    //配置插件

    constMiniCssExtractPlugin=require("mini-css-extract-plugin");

    //styel-html border-radius改为MiniCssExtractPlugin.html border-radius

    {

    //用来匹配.css结尾的文件

    test:/\.css$/,

    //use数组里面html border-radius执行顺序是从右到左

    use:[MiniCssExtractPlugin.html border-radius,"css-html border-radius"],

    },

    plugins:[

    newMiniCssExtractPlugin({

    //定义输出文件名和目录

    filename:"static/css/main.css",

    }),

    ]

    css兼容处理

    //安装

    npmipostcss-html border-radiuspostcsspostcss-preset-env-D

    //配置

    {

    //用来匹配.css结尾的文件

    test:/\.css$/,

    //use数组里面html border-radius执行顺序是从右到左

    use:[

    MiniCssExtractPlugin.html border-radius,

    "css-html border-radius",

    {//css-html border-radius之后,预处理器html border-radius之前

    html border-radius:"postcss-html border-radius",

    options:{

    postcssOptions:{

    plugins:[

    "postcss-preset-env",//能解决大多数样式兼容性问题

    ],

    },

    },

    },

    ],

    },

    控制兼容性:

    package.json文件中添加browserslist来控制样式的兼容性的程度:

    {

    //其他省略

    //"browserslist":["ie>=8"]

    //实际开发中我们一般不考虑旧版本浏览器了,所以我们可以这样设置:

    //所有浏览器的最新两个版本支持市面上99%浏览器还没死的浏览器

    "browserslist":["last2version",">1%","notdead"]

    }

    css压缩

    安装插件:

    npmicss-minimizer-html table theadk-plugin-D

    html table theadk配置:

    constCssMinimizerPlugin=require("css-minimizer-html table theadk-plugin");

    plugins:[

    //css压缩

    newCssMinimizerPlugin(),

    ]

    预处理器

    使用lessscss等预处理都要安装对应的html border-radius进行编译,html table theadk才能识别处理。

    less的使用:

    //安装less-html border-radius

    npmiless-html border-radius-D

    //配置

    //less-html border-radiusless转为css后还是要交给css-html border-radius处理的

    {

    test:/\.less$/,

    use:["style-html border-radius","css-html border-radius","less-html border-radius"]

    }

    scss,sass的使用:

    //安装

    npmisass-html border-radiussass-D

    //配置

    {

    test:/\.s[ac]ss$/,

    use:["style-html border-radius","css-html border-radius","sass-html border-radius"],

    },

    点击查看完整配置

    图片资源处理

    html table theadk4使用file-html border-radiusurl-html border-radius处理图片资源,而html table theadk5将那俩都内置了,直接配置开启就可。

    {

    test:/\.(png|jpe?g|gif|webp)$/,

    type:"asset",

    },

    将小于某个大小的图片转化成Base64可添加此配置:

    {

    test:/\.(png|jpe?g|gif|webp)$/,

    type:"asset",

    parser:{

    dataUrlCondition:{

    maxSize:10*1024//小于10kb的图片会被base64处理

    }

    }

    },

    点击查看完整配置

    其它资源处理

    若项目中引用了字体,视频等资源,则是希望不要处理它,直接输出就好了。配置为type:"asset/resource"它就会原封不动的输出了。

    {

    //处理字体图标或者视频等其它资源

    test:/\.(ttf|woff2?|map4|map3)$/,

    type:"asset/resource",

    generator:{

    filename:"static/media/[hash:8][ext][query]",

    },

    }

    点击查看完整配置

    js资源处理

    代码质量检测Eslint

    安装:

    npmieslint-html table theadk-plugineslint-D

    html table theadk配置中使用eslint插件

    constESLinthtml table theadkPlugin=require("eslint-html table theadk-plugin");

    plugins:[

    newESLinthtml table theadkPlugin({

    //指定检查文件的根目录

    context:path.resolve(__dirname,"src"),

    }),

    ],

    编写配置:

    配置文件由很多种写法:.eslintrc.*:新建文件,位于项目根目录

    .eslintrc

    .eslintrc.js

    .eslintrc.json

    区别在于配置格式不一样package.jsoneslintConfig:不需要创建文件,在原有文件基础上写,ESLint会查找和自动读取它们,所以以上配置文件只需要存在一个即可

    根目录创建.eslintrc.js配置文件

    //.eslintrc.js

    module.exports={

    //解析配置项

    parserOptions:{

    ecmaVersion:6,//ES语法版本

    sourceType:"module",//ES模块化

    },

    env:{

    node:true,//启用node中全局变量

    browser:true,//启用浏览器中全局变量不开启则像consoleMath等全局变量无法使用

    },

    //继承规则

    extends:['eslint:recommended'],

    //检测规则

    //自定义的规则会覆盖继承的规则

    //"off"0-关闭规则

    //"warn"1-开启规则,使用警告级别的错误:warn(不会导致程序退出)

    //"error"2-开启规则,使用错误级别的错误:error(当被触发的时候,程序会退出)

    rules:{

    semi:"off",//禁止使用分号

    'array-callback-return':'warn',//强制数组方法的回调函数中有return语句,否则警告

    'default-case':[

    'warn',//要求switch语句中有default分支,否则警告

    {commentPattern:'^nodefault$'}//允许在最后注释nodefault,就不会有警告了

    ],

    eqeqeq:[

    'warn',//强制使用===!==,否则警告

    'smart'//https://eslint.bootcss.com/docs/rules/eqeqeq#smart除了少数情况下不会有警告

    ],

    },

    }

    babel兼容处理

    安装:

    npmibabel-html border-radius@babel/core@babel/preset-env-D

    babel配置编写:

    配置文件由很多种写法:

    babel.config.*:新建文件,位于项目根目录

    babel.config.js

    babel.config.json

    .babelrc.*:新建文件,位于项目根目录

    .babelrc

    .babelrc.js

    .babelrc.json

    package.jsonbabel:不需要创建文件,在原有文件基础上写

    presets预设:

    简单理解:就是一组Babel插件,扩展Babel功能

    @babel/preset-env:一个智能预设,允许您使用最新的JavaScript

    @babel/preset-react:一个用来编译Reactjsx语法的预设

    @babel/preset-typescript:一个用来编译TypeScript语法的预设

    //创建.babelrc.js

    module.exports={

    presets:["@babel/preset-env"],

    };

    html table theadk增加babel

    {

    test:/\.js$/,

    exclude:/node_modules/,//排除node_modules代码不编译

    html border-radius:"babel-html border-radius",

    },

    html自动导入处理

    安装html-html table theadk-plugin

    npmihtml-html table theadk-plugin-D

    html table theadk配置,配置好后就会自动的引入所需的js了。

    constHtmlhtml table theadkPlugin=require("html-html table theadk-plugin");

    plugins:[

    newHtmlhtml table theadkPlugin({

    //public/index.html为模板创建文件

    //新的html文件有两个特点:1.内容和源文件一致2.自动引入打包生成的js等资源

    template:path.resolve(__dirname,"public/index.html"),

    }),

    ]

    html table theadkSever

    使用html table theadksever后,在开发时就能自动检测文件变化,并实时编译展示出来了。

    //安装

    npmihtml table theadk-dev-server-D

    //配置

    devServer:{

    host:"localhost",//启动服务器域名

    port:"3000",//启动服务器端口号

    open:true,//是否自动打开浏览器

    },

    运行:

    //此时不会打包生成文件,都是在内存中进行编译的

    npxhtml table theadkserve

    点击查看完整配置

    html table theadk进阶

    使用sourcemap

    SourceMap(源代码映射)是一个用来生成源代码与构建后代码一一映射的文件的方案。

    通过查看html table theadkDevTool文档可知,SourceMap的值有很多种情况.

    开发时我们只需要关注两种情况即可:

    开发模式:cheap-module-source-map,优点:打包编译速度快,只包含行映射,缺点:没有列映射

    生产模式:source-map,优点:包含行/列映射,缺点:打包编译速度更慢。

    配置:

    devtool:"cheap-module-source-map",

    提升打包速度

    HotModuleReplacement:它(HMR/热模块替换):在程序运行中,替换、添加或删除模块,而无需重新加载整个页面。

    module.exports={

    //其他省略

    devServer:{

    host:"localhost",//启动服务器域名

    port:"3000",//启动服务器端口号

    open:true,//是否自动打开浏览器

    hot:true,//开启HMR功能(只能用于开发环境,生产环境不需要了)

    },

    };

    此时css样式经过style-html border-radius处理,已经具备HMR功能了。但是js可以使用html border-radius-html border-radius,react-hot-html border-radius实现。

    OneOf配置(开发和正式都能用):

    匹配到一条规则就不继续匹配了

    module:{

    rules:[

    {

    oneOf:[

    {test:/\.css$/,use:["style-html border-radius","css-html border-radius"]},

    .........

    ]

    }

    ]

    }

    Include/Exclude

    如在配置babel排除node_moudels文件夹

    使用缓存Cache

    每次打包时js文件都要经过Eslint检查和Babel编译,速度比较慢。我们可以缓存之前的Eslint检查和Babel编译结果,这样第二次打包时速度就会更快了

    //babel

    {

    test:/\.js$/,

    //exclude:/node_modules/,//排除node_modules代码不编译

    include:path.resolve(__dirname,"../src"),//也可以用包含

    html border-radius:"babel-html border-radius",

    options:{

    cacheDirectory:true,//开启babel编译缓存

    cacheCompression:false,//缓存文件不要压缩

    },

    },

    //Eslint

    newESLinthtml table theadkPlugin({

    //指定检查文件的根目录

    context:path.resolve(__dirname,"../src"),

    exclude:"node_modules",//默认值

    cache:true,//开启缓存

    //缓存目录

    cacheLocation:path.resolve(

    __dirname,

    "../node_modules/.cache/.eslintcache"

    ),

    })

    打包启用多线程:

    开启线程也需要时间,小项目可能提升不明显。

    npmithread-html border-radius-D安装html border-radius,然后配置:

    //nodejs核心模块,直接使用

    constos=require("os");

    constTerserPlugin=require("terser-html table theadk-plugin");//html table theadk自带的js压缩模块

    //cpu核数

    constthreads=os.cpus().length;

  

【版权与免责声明】如发现内容存在版权问题,烦请提供相关信息发邮件至 1439028666@qq.com ,我们将及时沟通进行删除处理。 本站内容除了 98link( http://www.98link.com/ )特别标记的原创外,其它均为网友转载内容,涉及言论、版权与本站无关。