使用 webpack, babel, scss 开发的微信小程序项目脚手架,git源码地址:https://github.com/webpersonalserver/wx-mini
进入项目文件夹(wx-mini)下,执行命令 npm init,按照提示填写项目基本信息,生成package.json
(1)新建src文件夹(项目开发文件都在其中),然后分别在里面新建components、images、pages、sass、utils、app.js、app.json、app.wxss;
(2)新建webpack.config.babel.js,用于编译打包小程序
/**
* webpack配置文件
*/
import path, {
resolve
} from 'path';
import { DefinePlugin, EnvironmentPlugin } from 'webpack';
import webpack from 'webpack';
import WXAppWebpackPlugin from 'wxapp-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
const { NODE_ENV } = process.env;
const environment = NODE_ENV;
const srcDir = resolve('src');
const relativeFileLoader = (ext = '[ext]') => {
return {
loader: 'file-loader',
options: {
useRelativePath: true,
name: `[name].${ext}`,
context: srcDir
}
};
};
export default {
entry: {
app: [
'./src/app.js',
],
},
output: {
filename: '[name].js',
publicPath: '/',
path: resolve('dist'),
},
resolve: {
modules: [resolve(__dirname, 'src'), 'node_modules'],
alias: {
'src': resolve(__dirname, './src'),
'components': resolve(__dirname, './src/components'),
'images': resolve(__dirname, './src/images'),
'sass': resolve(__dirname, './src/sass')
}
},
module: {
rules: [{
test: /\.js$/,
include: /src/,
use: [
'babel-loader',
].filter(Boolean)
},
{
test: /\.(json|png|jpg|gif)$/,
include: /src/,
use: relativeFileLoader()
},
{
test: /\.(scss|wxss)$/,
include: /src/,
use: [
relativeFileLoader('wxss'),
{
loader: 'sass-loader',
options: {
includePaths: [resolve('src', 'styles'), srcDir]
}
}
]
},
{
test: /\.(wxml|axml|xml)$/,
include: /src/,
use: [
relativeFileLoader('wxml'),
{
loader: 'wxml-loader',
options: {
root: srcDir,
enforceRelativePath: true
}
}
]
}
]
},
plugins: [
new EnvironmentPlugin({
NODE_ENV: 'development'
}),
new DefinePlugin({
ENVIRONMENT: JSON.stringify(environment)
}),
new CopyWebpackPlugin([{
from: __dirname + '/src/images',
to: __dirname + '/dist/images'
},
{
from: __dirname + '/src/components',
to: __dirname + '/dist/components',
ignore: ['*/*.xml']
},
{
from: '**/*.wxml',
to: 'pages',
context: path.join(__dirname, 'src/pages')
}
], {
ignore: [
'**/*.scss',
'**/*.js'
]
}),
new WXAppWebpackPlugin()
],
watchOptions: {
ignored: /dist|manifest/,
aggregateTimeout: 300,
}
};
(3)在package.json文件中加入指令
"dev": "cross-env NODE_ENV=development webpack --watch"(用于开发,开发环境)
"build": "cross-env NODE_ENV=production webpack -p"(用于打包上线,生产环境)
"webpack": "webpack"
{
"presets": ["es2015"]
}
在根目录下新建 .gitignore 文件
# 文件忽略规则
# *.a 忽略所有 .a 结尾的文件
# !lib.a 除lib.a文件
# /TODO 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
# build 忽略 build/ 目录下的所有文件
# doc/*.txt 忽略doc/notes.txt ,但不包括 doc/server/arch.txt
# 忽略依赖包
node_modules
# 忽略编译打包后的文件
dist
# 忽略微信开发者工具生成文件
package-lock.json
# 忽略编辑器生成的文件
.project
(1)安装依赖
npm install --save-dev eslint eslint-config-standard eslint-friendly-formatter eslint-import-resolver-webpack eslint-loader eslint-plugin-flow-vars eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard
(2)在根目录下新建 .eslintrc.js 文件
module.exports = {
//此项是用来告诉eslint找当前配置文件不能往父级查找
root: true,
//此项是用来指定eslint解析器的,解析器必须符合规则,babel-eslint解析器是对babel解析器的包装使其与ESLint解析
parser: 'babel-eslint',
//此项是用来指定javaScript语言类型和风格,sourceType用来指定js导入的方式,默认是script,此处设置为module,指某块导入方式
parserOptions: {
// 设置 script(默认) 或 module,如果代码是在ECMASCRIPT中的模块
sourceType: 'module',
"ecmaVersion": 6,
"ecmaFeatures": {
"jsx": true
}
},
// 此项指定环境的全局变量,下面的配置指定为浏览器环境
env: {
"browser": true,
"node": true,
"commonjs": true,
"es6": true,
"amd": true
},
/*
下面这些rules是用来设置从插件来的规范代码的规则,使用必须去掉前缀eslint-plugin-
主要有如下的设置规则,可以设置字符串也可以设置数字,两者效果一致
"off" -> 0 关闭规则
"warn" -> 1 开启警告规则
"error" -> 2 开启错误规则
*/
rules: {
// 提示错误
"no-unused-vars": 2,
"no-use-before-define": 2,
"block-scoped-var": 2,
"no-var": 2,
// 提示警告
"no-empty": 1,
"no-extra-parens": 1,
"no-extra-semi": 1,
"eqeqeq": 1,
"eol-last": 1,
"no-mixed-spaces-and-tabs": 1,
"no-multiple-empty-lines": 1,
"prefer-arrow-callback": 1
}
}
(3)在项目根目录下新建 .eslintrc 文件,修改webpack.config.babel.js文件,加入:
{
test: /\.js$/,
include: /src/,
loader: 'eslint-loader',
options: {
formatter: require('eslint-friendly-formatter'),
emitWarning: true
}
}
(1)安装依赖
npm install uglifyjs-webpack-plugin --save-dev
(2)配置webpack.config.babel.js文件
(1)引用:import uglifyPlugin from 'uglifyjs-webpack-plugin';
(2)配置:plugins:[
new uglifyPlugin()
]
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_debugger: true,
drop_console: true
}
})
(1)安装依赖
npm install --save-dev gulp gulp-htmlmin gulp-pretty-data
(2)项目根目录下新建 gulpfile.js 文件,内容如下:
const gulp = require('gulp');
const htmlmin = require('gulp-htmlmin');
const prettyData = require('gulp-pretty-data');
gulp.task('minify', function () {
// 压缩wxml
gulp.src('dist/**/*.wxml')
.pipe(htmlmin({
removeComments: true, // 清除wxml注释
collapseWhitespace: true, // 压缩wxml
removeEmptyAttributes: true // 删除所有空格作属性值 ==>
}))
.pipe(gulp.dest('dist'));
// 压缩json、wxss、wxml
gulp.src('dist/**/*.{json,xml,css}')
.pipe(prettyData({
type: 'minify',
preserveComments: true,
extensions: {
'wxml': 'xml',
'wxss': 'css'
}
}))
.pipe(gulp.dest('dist'))
});
(3)执行 gulp minify 压缩文件,可以在package.json文件的scripts中加入 “minify”: “cross-env NODE_ENV=production webpack -p && gulp minify”,这样既可在上生产时,编译压缩打包