2024年Kbone基础 Kbone + Vue 项目手工搭建流程(1),前端码农是如何进入腾讯的

其实前端开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

这里再分享一个复习的路线:(以下体系的复习资料是我从各路大佬收集整理好的)

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

《前端开发四大模块核心知识笔记》

最后,说个题外话,我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在IT学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。

const OptimizeCSSAssetsPlugin = require(‘optimize-css-assets-webpack-plugin’);
const TerserPlugin = require(‘terser-webpack-plugin’)
const MpPlugin = require(‘mp-webpack-plugin’) // 用于构建小程序代码的 webpack 插件

const isOptimize = false // 是否压缩业务代码,开发者工具可能无法完美支持业务代码使用到的 es 特性,建议自己做代码压缩

module.exports = {
mode: ‘production’,

entry: {
index: path.resolve(__dirname, ‘…/src/index/main.mp.js’),
},

output: {
path: path.resolve(__dirname, ‘…/dist/mp/common’), // 放到小程序代码目录中的 common 目录下
filename: ‘[name].js’, // 必需字段,不能修改
library: ‘createApp’, // 必需字段,不能修改
libraryExport: ‘default’, // 必需字段,不能修改
libraryTarget: ‘window’, // 必需字段,不能修改
},

target: ‘web’, // 必需字段,不能修改

optimization: {
runtimeChunk: false, // 必需字段,不能修改
splitChunks: { // 代码分隔配置,不建议修改
chunks: ‘all’,
minSize: 1000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 100,
maxInitialRequests: 100,
automaticNameDelimiter: ‘~’,
name: true,
cacheGroups: {
vendors: {
test: /[\/]node_modules[\/]/,
priority: -10
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
}
},

minimizer: isOptimize ? [
  // 压缩CSS
  new OptimizeCSSAssetsPlugin({
    assetNameRegExp: /\.(css|wxss)$/g,
    cssProcessor: require('cssnano'),
    cssProcessorPluginOptions: {
      preset: ['default', {
        discardComments: {
          removeAll: true,
        },
        minifySelectors: false, // 因为 wxss 编译器不支持 .some>:first-child 这样格式的代码,所以暂时禁掉这个
      }],
    },
    canPrint: false
  }),
  // 压缩 js
  new TerserPlugin({
    test: /\.js(\?.*)?$/i,
    parallel: true,
  })
] : [],

},

module: {
rules: [
{
test: /.cssKaTeX parse error: Expected 'EOF', got '}' at position 98: … ], }̲, { …/,
loader: ‘vue-loader’,
},
{
test: /.jsKaTeX parse error: Expected 'EOF', got '}' at position 93: …modules/ }̲, { …/,
loader: ‘file-loader’,
options: {
name: ‘[name].[ext]?[hash]’
}
}
]
},

resolve: {
extensions: [‘*’, ‘.js’, ‘.vue’, ‘.json’]
},

plugins: [
new webpack.DefinePlugin({
‘process.env.isMiniprogram’: process.env.isMiniprogram, // 注入环境变量,用于业务代码判断
}),
new MiniCssExtractPlugin({
filename: ‘[name].wxss’,
}),
new VueLoaderPlugin(),
new MpPlugin(require(‘./miniprogram.config.js’)),
],
}


#### 2.2 安装依赖


安装上述配置文件里的 loader 和 plugin 依赖:



npm install babel-loader @babel/core css-loader file-loader mini-css-extract-plugin vue-loader vue-template-compiler optimize-css-assets-webpack-plugin terser-webpack-plugin mp-webpack-plugin --save-dev


#### 2.3 编写 webpack 插件配置


这里的 webpack 插件配置即 MpPlugin 的配置参数文件。在 build 文件夹下创建 miniprogram.config.js 文件,内容如下:



module.exports = {
// 页面 origin,默认是 https://miniprogram.default
origin: ‘https://test.miniprogram.com’,

// 入口页面路由
entry: ‘/test/aaa’,

// 页面路由,用于页面间跳转。其值是一个以页面名称作为 key 的对象,每项的值是该页面可以响应的路由
router: {
index: [
‘/test/aaa’,
‘/test/bbb’,
],
},

// 特殊路由跳转
redirect: {
notFound: ‘index’,
accessDenied: ‘index’,
},

// 构建输出配置
generate: {
/**
* 注入全局变量,每一项为 [key, value] 的结构。构建时会将需要注入的全局变量声明在所有要执行的代码之前,以方便代码里直接使用。
* 如果配置了 [‘TEST_VAR_STRING’, ‘‘miniprogram’’],则会生成类似 var TEST_VAR_STRING = ‘miniprogram’ 的声明语句;
* 不指定 value 的话,则会从 window 下读取,如 [‘CustomEvent’] 则会生成类似 var CustomEvent = window.CustomEvent 的声明语句。
*/
globalVars: [
[‘TEST_VAR_STRING’, ‘‘miniprogram’’],
[‘TEST_VAR_NUMBER’, ‘123’],
[‘TEST_VAR_BOOL’, ‘true’],
[‘TEST_VAR_FUNCTION’, ‘function() {return ‘I am function’}’],
[‘TEST_VAR_OTHERS’, ‘window.document’],
[‘open’],
],

// 构建完成后是否自动安装小程序依赖。'npm':使用 npm 自动安装依赖
    autoBuildNpm: 'npm',

},

// 小程序全局配置,参见 https://developers.weixin.qq.com/miniprogram/dev/reference/configuration/app.html#window
app: {
navigationBarTitleText: ‘kbone-vue-project’,
},

// 所有页面的全局配置
global: {
rem: true, // 是否支持 rem
pageStyle: true, // 是否支持修改页面样式
},

// 项目配置,会被合并到 project.config.json 中
projectConfig: {
appid: ‘’,
projectname: ‘kbone-vue-project’,
},

// 包配置,会被合并到 package.json 中
packageConfig: {
author: ‘wechat-miniprogram’,
},
}


#### 3、新增入口文件


**3.1 在项目根目录下创建** **`src/index`** **目录,在** **`index`** **目录下创建** **`main.mp.js`文件:**



import Vue from ‘vue’
import Router from ‘vue-router’
import App from ‘./App.vue’
import AAA from ‘./AAA.vue’
import BBB from ‘./BBB.vue’

export default function createApp() {
const container = document.createElement(‘div’)
container.id = ‘app’
document.body.appendChild(container)

// rem 和页面样式修改
window.onload = function() {
document.documentElement.style.fontSize = wx.getSystemInfoSync().screenWidth / 16 + ‘px’
document.documentElement.style.backgroundColor = ‘#fffbe7’
}
window.onerror = (message, source, lineno, colno, error) => {
console.log('window.onerror => ', message, source, lineno, colno, error)
};
window.addEventListener(‘error’, evt => console.log(‘window.addEventListener(‘error’) =>’, evt))

Vue.use(Router)

const router = new Router({
mode: ‘history’, // 是否使用 history api
routes: [
{ path: ‘/test/aaa’, component: AAA },
{ path: ‘/test/bbb’, component: BBB }
]
})

return new Vue({
el: ‘#app’,
router,
render: h => h(App)
})
}


**3.2 安装 Vue Vue-router**



npm install vue vue-router


#### 4、构建项目文件


**4.1 创建App.vue**


在 `index` 目录下创建 `App.vue` 文件,实现了:


* 路由组件的展示和路由切换
* 载入子组件
* 全局变量的测试
* cookie的测试
* 抛出异常的测试



  • aaa
  • bbb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值