【webpack4.0】---base.config.js基本配置(五)
一、创建项目初始化
1、初始化项目
npm init -y2、创建
src(用来存放开发环境的代码)文件夹。config(用来存放webpack的配置项)文件夹3、安装webpack Webpack-cli
二、base.config.js文件
config文件夹下创建base.config.js
1、基本配置
constpath=require("path");
//定义入口文件路径和出口文件路径
constPATH={
app:path.join(__dirname,"../src/main.ts"),
build:path.join(__dirname,"../dist")
}
module.exports={
//入口文件路径
entry:{
app:PATH.app
},
output:{
//导出后文件的名称
filename:process.env.NODE_ENV!='production'?"js/[name].js":"js/[name].[hash:8].js",
//出口文件的路径
path:PATH.build
},
resolve:{
//优先引入的后缀文件
extensions:['.ts','.tsx','.js'],
//配置别名
alias:{}
}
}
三、html-webpack-plugin
1、安装
cnpm install html-webpack-plugin -D
2、使用
const HtmlWebpackPlugin =require("html-webpack-plugin");
module.exports={
plugins:[
newHtmlWebpackPlugin({
template:"../index.html",
filename:"index.html",
title:"vue"
})
]
}
四、loader处理JS文件
1、安装
cnpm install -D @babel/corebabel-loader @babel/preset-env @babel/preset-react @babel/plugin-transform-runtime @babel/polyfill
2、处理JS配置
module:{
rules:[
{
test:/\.js$/,
exclude:path.join(__dirname,"../node_modules"),
loader:"babel-loader"
}
]
},
3、根目录下创建.babelrc文件
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"browsers": ["last 2 versions"]
}
}
]
],
"plugins": ["@babel/plugin-transform-runtime"]
}
五、loader处理图片和字体图标
1、安装
cnpm install file-loader url-loader -D
2、基本使用
module.exports={
module:{
rules:[
{
test:/\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
publicPath: 'assets',
}
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'fonts/[name].[hash:8].[ext]'
}
}//项目设置打包到dist下的fonts文件夹下
]
},
]
}
}
七、loader处理ts文件
1、安装
cnpm install ts-loader -D
2、基本使用
module.exports={
module:{
rules:[
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/]
}
},
]
},
}
3、ts配置文件 根目录创建tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true, //开启装饰器@ (修饰器本质就是编译时执行的函数)
"strict": false, //启用所有严格类型检查选项
// 指定生成哪个模块系统代码
"module": "es2015",
"moduleResolution": "node",
"target": "es5",//编译目标平台
"allowJs":true,//允许编译javascript文件
"noImplicitAny": false, // 在表达式和声明上有隐含的'any'类型时报错。
"allowSyntheticDefaultImports": true,
"lib": [
"es5",
"es2015",
"es2016",
"es2017",
"dom"
]
},
"include": ["./**/*.ts"]
}
4、ts识别vue文件 根目录下创建sfc.d.ts
/**
* 告诉 TypeScript *.vue 后缀的文件可以交给 vue 模块来处理
* 而在代码中导入 *.vue 文件的时候,需要写上 .vue 后缀。
* 原因还是因为 TypeScript 默认只识别 *.ts 文件,不识别 *.vue 文件
*/
declaremodule"*.vue"{
importVuefrom'vue'
exportdefaultVue
}
八、baset.config.js完整代码
constpath=require("path");
constHtmlWebpackPlugin =require("html-webpack-plugin");
constVueLoaderPlugin=require('vue-loader/lib/plugin')
//定义入口文件路径和出口文件路径
constPATH={
app:path.join(__dirname,"../src/main.ts"),
build:path.join(__dirname,"../dist")
}
module.exports={
//入口文件路径
entry:{
app:PATH.app
},
output:{
//导出后文件的名称
filename:process.env.NODE_ENV!='production'?"js/[name].js":"js/[name].[hash:8].js",
//出口文件的路径
path:PATH.build
},
resolve:{
//优先引入的后缀文件
extensions:['.ts','.tsx','.js'],
//配置别名
alias:{}
},
module:{
rules:[
{
test:/\.js$/,
exclude:path.join(__dirname,"../node_modules"),
loader:"babel-loader"
},
{
test:/\.vue$/,
exclude:path.join(__dirname,"../node_modules"),
loader:"vue-loader"
},
{
test:/\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
publicPath: 'assets',
}
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'fonts/[name].[hash:8].[ext]'
}
}//项目设置打包到dist下的fonts文件夹下
]
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/]
}
},
]
},
plugins:[
newHtmlWebpackPlugin({
template:"./index.html",
filename:"index.html",
title:"vue"
}),
newVueLoaderPlugin()
]
}
【webpack4.0】---base.config.js基本配置(五)的更多相关文章
- webpack配置之webpack.config.js文件配置
webpack配置之webpack.config.js文件配置 webpack.config.js webpack resolve 1.总是手动的输入webpack的输入输出文件路径,是一件非常繁琐 ...
- [转]webpack4.0.1安装问题和webpack.config.js的配置变化
本文转自:https://blog.csdn.net/jiang7701037/article/details/79403637 The CLI moved into a separate packa ...
- 【webpack4.0】---dev.config.js基本配置(六)
一.开发环境配置准备 1.创建dev.config.js文件 用来配置开发环境的代码 2.安装webpack-merge cnpm install webpack-merge -D 用来合并webpa ...
- webpack4.0.1安装问题和webpack.config.js的配置变化
The CLI moved into a separate package: webpack-cli. Please install 'webpack-cli' in addition to webp ...
- vue3.0 vue.config.js 配置实战
今天讲述一下vue-config.js配置,我们前面搭建好脚手架会发现,这个对比2.x版本少了很多东西,没有build的配置,也没有webpack的配置,那么问题来了,我们如何去开发我们的项目呢,比如 ...
- vue.config.js常用配置
使用vue-cli3.0搭建项目比之前更简洁,没有了build和config文件夹. vue-cli3的一些服务配置都迁移到CLI Service里面了,对于一些基础配置和一些扩展配置需要在根目录新建 ...
- vue.config.js基础配置
const path = require('path') const UglifyPlugin = require('uglifyjs-webpack-plugin') module.exports ...
- vue-cli3的vue.config.js文件配置,生成dist文件
//vue.config.jsonconst path = require('path'); // const vConsolePlugin = require('vconsole-webpack-p ...
- Vue-Cli 3.0 + vue.config.js
虽然一直没有亲手搭建过vue项目,但是2.0的时候就开始自学(但并没有实践项目).然后公司最近有个人用3.0做了个项目,公司让我参与进去,我就顺便学习了一把3.0.(美滋滋) 因为电脑的环境还是之前自 ...
随机推荐
- 再谈多线程模型之生产者消费者(总结)(c++11实现)
0.关于 为缩短篇幅,本系列记录如下: 再谈多线程模型之生产者消费者(基础概念)(c++11实现) 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现) 再谈多线程模型之生产者消费 ...
- 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- A. Points on Line
A. Points on Line time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- hdu-4561 连续最大积( 水题)
http://acm.hdu.edu.cn/showproblem.php?pid=4561 求连续最大积. 他妈的狗逼思路到底咋说..... 思路是 %&*()*(&--))*)*& ...
- Java 计算加几个月之后的时间
Java 计算加几个月之后的时间 public static Date getAfterMonth(String inputDate,int number) { Calendar c = Calend ...
- JS常用的获取值和设值的方法
1. input 标签<input type="text" name="username" id="name"/> 1) 获取i ...
- <学习opencv>图像、视频和数据文件
/*=========================================================================*/ // openCV中的函数 /*====== ...
- JDK httpClient 详解(源码级分析)——概览及架构篇
1. 前言 2018年9月,伴随着java 11的发布,内置的httpclient正式登上了历史的舞台.此前,JDK内置的http工具URLConnection性能羸弱,操作繁琐,饱受诟病,也因此令如 ...
- 使用 jQuery对象设置页面中 <ul> 元素的标记类型,并使用 DOM 对象设置 <li> 元素的浮动属性和右边距。使用jQuery 对象和 DOM 对象设置页面元素属性
查看本章节 查看作业目录 需求说明: 使用 jQuery对象设置页面中 <ul> 元素的标记类型,并使用 DOM 对象设置 <li> 元素的浮动属性和右边距.使用jQuery ...
- 编写Java程序,演练匿名内部类应用
返回本章节 返回作业目录 需求说明: 定义一个抽象类 Bird,创建使用匿名内部类的操作类Action. 实现思路: 定义抽象类Bird.在其中定义一个String类型的name属性,一个返回类型是i ...