加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gulpfile-wx-app.js 7.79 KB
一键复制 编辑 原始数据 按行查看 历史
daokes 提交于 2018-12-21 18:21 . ts
const gulp = require('gulp');
const shell = require('gulp-shell');
gulp.task('compile-ts', shell.task('tsc -p ./tsconfig-wx-app.json'));
gulp.task('watch-ts', shell.task('tsc -p ./tsconfig-wx-app.json -w'));
gulp.task('webpack', (cb) => {
const dest = './dist/js/';
const path = require('path');
const webpack = require('webpack');
const named = require('vinyl-named');
const ws = require('webpack-stream');
const insert = require('gulp-insert');
return gulp.src([`./dist/app/app.js`, `./dist/**/p.js`])
.pipe(named((file, enc) => {
return path.basename(path.dirname(file.path));
}))
.pipe(ws({
mode: 'production',
// mode: 'development',
optimization: {
splitChunks: {
name: 'feidao-app',
chunks: 'all'
}
},
externals: [
// "nools" // this is a bad js file
],
output: {
globalObject: 'global',
libraryTarget: "commonjs",
filename: '[name].js'
},
resolve: {
// modules: [path.resolve('src'), 'node_modules'],
// alias: {
// daoke: 'dist',
// dot: 'dot/doT.js',
// mqtt: 'mqtt/dist/mqtt.js'
// }
// },
// module: {
// rules: [{
// test: /\.js$/,
// // exclude: /(node_modules|bower_components)/,
// exclude: /core-js|babel/,
// use: {
// loader: 'babel-loader'
// }
// }]
}
}, webpack))
.pipe(insert.transform(function (contents, file) {
return `const regeneratorRuntime = require('./runtime.js');
${contents}`;
}))
.on('error', (e) => {
cb(e);
})
.pipe(gulp.dest(dest));
});
gulp.task('copy-files', () => {
const copy = require('gulp-copy');
const dist = './dist/';
return gulp.src([
'./.npmrc',
'./src-wx-app/**/*.json',
'./src-wx-app/app.js',
'./src-wx-app/app.wxss',
'./src-wx-app/app.json',
'./src-wx-app/pages/**/*',
'./src-wx-app/images/**/*',
'./src-wx-app/css/**/*'
]).pipe(copy(dist, {
prefix: 1
}));
});
gulp.task('copy-runtime', shell.task('cp ./node_modules/regenerator-runtime/runtime.js ./dist/js/'));
gulp.task('webpack-watch', () => {
const dest = './dist/js/';
const path = require('path');
const webpack = require('webpack');
const named = require('vinyl-named');
const ws = require('webpack-stream');
const insert = require('gulp-insert');
return gulp.src([`./dist/app/app.js`, `./dist/**/p.js`])
.pipe(named((file, enc) => {
return path.basename(path.dirname(file.path));
}))
.pipe(ws({
watch: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},
mode: 'none', // 开发模式下,不分割代码
optimization: {
minimize: false,
splitChunks: {
name: 'feidao-app',
chunks: 'all'
}
},
watch: true,
externals: [
// "nools" // this is a bad js file
],
output: {
globalObject: 'global',
libraryTarget: "commonjs",
filename: '[name].js'
},
resolve: {
// modules: [path.resolve('src'), 'node_modules'],
// alias: {
// daoke: 'dist',
// dot: 'dot/doT.js',
// mqtt: 'mqtt/dist/mqtt.js'
// }
// },
// module: {
// rules: [{
// test: /\.js$/,
// // exclude: /(node_modules|bower_components)/,
// exclude: /core-js|babel/,
// use: {
// loader: 'babel-loader'
// }
// }]
}
}, webpack))
.pipe(insert.transform(function (contents, file) {
return `const regeneratorRuntime = require('./runtime.js');
${contents}`;
}))
.pipe(gulp.dest(dest));
});
gulp.task('watch-files', () => {
const copy = require('gulp-copy');
const dist = './dist/';
return gulp.watch([
'./src-wx-app/*.js',
'./src-wx-app/*.wxss',
'./src-wx-app/pages/*/*',
'./src-wx-app/images/**/*'
]).on('change', (path) => {
// file.type added renamed deleted changed
gulp.src(path)
.pipe(copy(dist, {
prefix: 1
}));
});
});
gulp.task('watch-file-app-debug', () => {
const source = './src-wx-app/app-debug.json';
const dist = './dist/';
const rename = require('gulp-rename');
return gulp.watch(source, () => {
// file.type added renamed deleted changed
return gulp.src(source)
.pipe(rename('app.json'))
.pipe(gulp.dest(dist));
});
});
gulp.task('weapp-pack', gulp.series(
'copy-runtime',
'compile-ts',
'webpack',
'copy-files'
));
gulp.task('copy-app-debug', () => {
const dist = './dist/';
const rename = require('gulp-rename');
return gulp.src('./src-wx-app/app-debug.json')
.pipe(rename('app.json'))
.pipe(gulp.dest(dist));
});
gulp.task('dev', shell.task(`feidao-dev -s ${process.cwd()}/service/dist/`));
gulp.task('service-watch', shell.task(`tsc -w -p ./service/tsconfig.json`));
gulp.task('weapp', gulp.series('copy-runtime', 'copy-files', 'copy-app-debug', 'compile-ts', gulp.parallel('watch-files', 'watch-file-app-debug', 'watch-ts', 'dev', 'webpack-watch', 'service-watch')));
gulp.task('wxapp-add', () => {
const fs = require('fs').promises;
const repl = require('repl');
return repl.start({
prompt: '\nPlease type 1 pagename each time. and stop with [ctrl+d]\n>', async eval(cmd, context, _filename, callback) {
const filename = cmd.trim();
if (cmd) {
try {
// js
const wxdir = `./src-wx-app/pages/${filename}`;
await fs.mkdir(wxdir);
const jsfile = `./src-wx-app/pages/${filename}/${filename}.js`;
const jsfile_content = `require('../../js/feidao-app');
require('../../js/${filename}');
`;
await fs.writeFile(jsfile, jsfile_content, 'utf8');
// json
const jsonfile = `./src-wx-app/pages/${filename}/${filename}.json`;
const jsonfile_content = `{}`;
await fs.writeFile(jsonfile, jsonfile_content, 'utf8');
// wxml
const wxmlfile = `./src-wx-app/pages/${filename}/${filename}.wxml`;
const wxmlfile_content = `\n`;
await fs.writeFile(wxmlfile, wxmlfile_content, 'utf8');
// wxss
const wxssfile = `./src-wx-app/pages/${filename}/${filename}.wxss`;
const wxssfile_content = `\n`;
await fs.writeFile(wxssfile, wxssfile_content, 'utf8');
// ts
const tsdir = `./src-wx-app/${filename}`;
await fs.mkdir(tsdir);
// s.ts
const sfile = `./src-wx-app/${filename}/s.ts`;
const sfile_content = `export default {\n\t'fd-events-init': 'a001'\n};\n`;
await fs.writeFile(sfile, sfile_content, 'utf8');
// a001.ts
const afile = `./src-wx-app/${filename}/a001.ts`;
const afile_content = `import { IFeidaoAiWeappPage } from '@feidao-factory/atom/interfaces';
export default async function a001(fd: IFeidaoAiWeappPage, ...args: any[]) {
// todo
}
`;
await fs.writeFile(afile, afile_content, 'utf8');
// p.ts
const pfile = `./src-wx-app/${filename}/p.ts`;
const pfile_content = `import init from '@feidao/web/framework/wxapp/d/page';
import s from './s';
import a001 from './a001';
(() => {
const actions = {
a001
};
init(s, actions);
})();
`;
await fs.writeFile(pfile, pfile_content, 'utf8');
} catch (error) {
console.error(error);
callback();
}
}
callback();
}
});
});
gulp.task('wxapp-fix', async () => {
const globby = require('globby');
const fs = require('fs').promises;
try {
const paths = await globby('./src-wx-app/*/p.ts');
const filenames = paths.map((filepath) => {
return filepath.match(/.*\/(.*)\/p.ts/)[1];
});
const app = require('./src-wx-app/app.json');
app.pages = filenames.map((filename) => {
return `pages/${filename}/${filename}`;
});
await fs.writeFile('./src-wx-app/app.json', JSON.stringify(app, null, '\t'), 'utf8');
await filenames.map(async (filename) => {
const jsfile = `./src-wx-app/pages/${filename}/${filename}.js`;
try {
await fs.access(jsfile);
} catch (error) {
console.log(`generage file ${jsfile}`);
const file = `require('../../js/feidao-app');
require('../../js/${filename}');
`;
await fs.writeFile(jsfile, file, 'utf8');
}
});
} catch (error) {
console.error(`There is no such dir src-wx-app`);
}
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化