文章内容输出来源:拉勾教育大前端高薪训练营
1、谈谈你对工程化的初步认识,结合你之前遇到过的问题说出三个以上工程化能够解决问题或者带来的价值。
工程化就是通过一些方法去提升研发效率和研发质量的手段。
2、你认为脚手架除了为我们创建项目结构,还有什么更深的意义?
1、概述脚手架实现的过程,并使用 NodeJS 完成一个自定义的小型脚手架工具
npm init
初始化"bin":"入口文件路径"
npm link
命令连接全局npm publish
发布到npm仓库npm init -y
初始化#! /usr/bin/env node
// index.js初始内容
console.log('toy cli 入口文件');
#! /usr/bin/env node
// console.log('toy cli 入口文件');
const inquirer = require('inquirer')
const path = require('path')
const fs = require('fs')
const ejs = require('ejs')
// 命令行询问配置项
const {selectConfig} = require('./select.config')
inquirer.prompt(selectConfig).then(answer => {
const tmpl = path.join(process.cwd(),'template');
const dist = process.cwd()
fs.readdir(tmpl,(err,files)=>{
if(err) throw err;
files.forEach(file=>{
// console.log(path.join(tmpl,file));
ejs.renderFile(path.join(tmpl,file),answer,(renderErr,res)=>{
if(renderErr) throw renderErr;
let sonDir = path.join(dist,'dist')
fs.mkdir(sonDir, {recursive: true}, (err) => {
if (err) throw err;
fs.writeFileSync(path.join(sonDir,file), res);
});
})
})
})
})
2、尝试使用 Gulp 完成 项目 的自动化构建
主要任务
组合任务
const develop = series(parallel(style,js,html),ref, server)
const build = series(clean,parallel(style,js,html,img,extra,font))
package.json配置脚本
"scripts": {
"dev": "yarn gulp develop",
"build":"yarn gulp build"
},
// 实现这个项目的构建任务
const { src, dest ,watch,series,parallel} = require('gulp')
const del = require('del')
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const imagemin = require('gulp-imagemin')
// const swig = require('gulp-swig')
// const ifg = require('gulp-if')
// const useref = require('gulp-useref')
// const uglify = require('gulp-uglify')
// const cleanCss = require('gulp-clean-css')
const gulpLoadPlugins = require('gulp-load-plugins')
const plugins = gulpLoadPlugins()
//
const browserSync = require('browser-sync')
const bs = browserSync.create()
const data = require('./package.json')
// 编译scss
const style = () => {
return src('src/assets/styles/*.scss')
.pipe(plugins.sass({ outputStyle: 'expanded' }))
.pipe(dest('dist/css'))
}
// 编译JS
const js = () => {
return src('src/assets/scripts/*.js')
.pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
.pipe(dest('dist/js'))
}
// 处理图片
const img = () => {
return src('src/assets/images/**')
.pipe(plugins.imagemin())
.pipe(dest('dist/img'))
}
// 处理字体
const font = () => {
return src('src/assets/fonts/**')
.pipe(plugins.imagemin())
.pipe(dest('dist/font'))
}
// 拷贝静态资源
const extra = () => {
return src('public/**')
.pipe(dest('dist/public'))
}
// 处理HTML文件
const html = ()=>{
return src(['src/*.html','src/layouts/*.html','src/partials/*.html'])
.pipe(plugins.swig({data:{pkg:data}}))
.pipe(dest('dist'))
}
// 清除构建后的目录
const clean = () => {
return del('dist')
}
const server = ()=>{
watch('src/assets/styles/*.scss',style)
watch('src/assets/scripts/*.js',js)
watch(['src/assets/fonts/**','src/assets/images/**'],bs.reload)
bs.init({
notify:false,
files:'dist',
server:{
baseDir:['dist','src','public'],
routes:{
'/node_modules':'node_modules'
}
}
})
}
// 文件引用处理
const ref = ()=>{
return src('dist/*.html')
.pipe(plugins.useref({searchPath:['dist','.']}))
.pipe(plugins.if(/\.js$/,plugins.uglify()))
.pipe(plugins.if(/\.html$/,plugins.htmlmin({collapseWhitespace:true,minifyCSS:true,minifyJS:true})))
.pipe(plugins.if(/\.css$/,plugins.cleanCss()))
.pipe(dest('dist'))
}
const develop = series(parallel(style,js,html),ref, server)
const build = series(clean,parallel(style,js,html,img,extra,font))
module.exports = {
clean,
develop,
build
}
3、使用 Grunt 完成 项目 的自动化构建
grunt.registerTask('develop',['sass','babel','watch'])
grunt.registerTask('default',['clean','copy','sass','babel','uglify'])
"scripts": {
"dev":"yarn grunt develop",
"build":"yarn grunt"
},
参考配置
// grunt 构建任务
const sass = require('sass')
const loadGruntTask = require('load-grunt-tasks')// 加载grunt插件
module.exports = grunt =>{
grunt.initConfig({
babel:{
options:{
presets:['@babel/preset-env']
},
main:{
files:{
'dist/assets/scripts/main.js':'src/assets/scripts/main.js'
}
}
},
// 压缩js
uglify: {
my_target: {
files: {
'dist/assets/scripts/main.min.js': ['dist/assets/scripts/main.js']
}
}
},
// 处理scss文件
sass:{
options:{
sourceMap:true,
implementation:sass//使用sass处理编译
},
main:{//配置目标
files:{
'dist/assets/styles/main.css':'src/assets/styles/main.scss'
}
}
},
// 拷贝静态文件
copy:{
main: {
expand: true,
src: 'public/*',
dest: 'dist/',
}
},
clean:{
dist:'dist/**'
},
// 监视文件变化
watch:{
js:{
files:['src/assets/scripts/*.js'],
task:['babel']
},
css:{
files:['src/assets/styles/*.scss'],
task:['sass']
}
}
})
grunt.loadNpmTasks('grunt-contrib-clean') //删除构建后的文件
// grunt.registerTask('foo',()=>{
// console.log('foo task');
// })
// grunt.loadNpmTasks('grunt-sass')
// grunt.loadNpmTasks('grunt-babel')
loadGruntTask(grunt)//自动加载所有插件
grunt.registerTask('develop',['sass','babel','watch'])
grunt.registerTask('default',['clean','copy','sass','babel','uglify'])
}
答案仅供参考