diff --git a/gulpfile.js b/gulpfile.js index 07c6700..377ac5b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -14,19 +14,32 @@ const gulp = require('gulp'), del = require('del'), argv = require('yargs').argv, theme = process.env.npm_config_theme || 'default', + allTheme = require('./theme.json'), node_env = argv.env || 'development', scss_path = ['src/**/*.scss', '!node_modules'], output_path = 'dist', + output_path_js = `${output_path}/js`, output_path_style = `${output_path}/style`, output_path_style_modules = `${output_path_style}/modules/`, module_ext_name = `${node_env === 'production' ? '.min.css' : '.css'}`, - concat_theme_name = `${theme}${node_env === 'production' ? '.min' : ''}.css`; + concat_theme_name = (param) => `${param}${node_env === 'production' ? '.min' : ''}.css`; -const scssTask = done => { - gulp.src([...scss_path, '!src/theme/*.scss']) +const themeTask = done => { + allTheme.forEach(item => { + scssTask(done, item) + }) + done() +} + +const scssTask = (done, themeType = theme) => { + return bundleScss(themeType) +} + +const bundleScss = (themeType) => { + return gulp.src([...scss_path, '!src/theme/*.scss']) .pipe(sourcemaps.init()) - .on('error', scss.logError)//错误信息 - .pipe(setGlobalScss()) + .on('error', scss.logError) //错误信息 + .pipe(setGlobalScss(themeType)) .pipe(scss()) .pipe(gulpif(node_env === 'production', cleanCss())) // 仅在生产环境时候进行压缩 .pipe(autoprefix()) @@ -34,20 +47,19 @@ const scssTask = done => { path.extname = module_ext_name })) .pipe(sourcemaps.write('./')) - .pipe(gulp.dest(output_path_style_modules)) + .pipe(gulp.dest(`${output_path_style_modules}/${themeType}`)) .pipe(filter(`**/*.css`)) //合并过滤 - .pipe(concat(concat_theme_name)) + .pipe(concat((concat_theme_name(themeType)))) .pipe(sourcemaps.write('./')) .pipe(gulp.dest(output_path_style)) .pipe(bSync.reload({ stream: true })) - done() } // scss 全局变量注入 -const setGlobalScss = () => { - const resDefault = fs.readFileSync(`./src/theme/${theme}.scss`), +const setGlobalScss = (themeType) => { + const resDefault = fs.readFileSync(`./src/theme/${themeType}.scss`), resCommon = fs.readFileSync('./src/theme/common.scss'), scssString = resCommon.toString() + " \n " + resDefault.toString(); return header(scssString); @@ -58,6 +70,7 @@ const watchPipe = done => { watcher.on("change", gulp.series('clean', 'scss')) watcher.on("add", gulp.series('clean', 'scss')) watcher.on("unlink", gulp.series('clean', 'scss')) + gulp.watch([`${output_path}/**/*`, `!${output_path}/**/*.html`], gulp.parallel('html')) done() } @@ -68,7 +81,7 @@ const cleanFiles = () => { const server = (done) => { bSync({ server: { - baseDir: 'dist' + baseDir: [output_path, './'] } }) done() @@ -76,20 +89,33 @@ const server = (done) => { const injectTask = () => { const target = gulp.src('./public/index.html'), - source = gulp.src('./src/index.js', { read: false }); - // .pipe( - // inject( - // gulp.src(`${output_path_style}/${concat_theme_name}`, { read: false }), - // { starttag: '' } - // ) - // ) - return target.pipe(inject(source, { relative: true })) + source = gulp.src([`${output_path}/**/*.js`, `${output_path}/${theme}.css`, `!${output_path_style_modules}/**/*.css`], { read: false }); + + return target.pipe( + inject(source, { + transform: function (filepath) { + if (filepath.includes(`${theme}.css`)) { + return `` + } + // Use the default transform as fallback: + return inject.transform.apply(inject.transform, arguments); + } + }, { relative: true }) + ) .pipe(gulp.dest(output_path)); } +const jsTask = (done) => { + gulp.src(`./src/**/*.js`) + .pipe(gulp.dest(output_path_js)) + done(); +} + gulp.task('clean', cleanFiles) gulp.task('watch', watchPipe) -gulp.task('scss', scssTask) +// gulp.task('scss', scssTask) +gulp.task('scss', themeTask) +gulp.task('js', jsTask) gulp.task('html', injectTask) gulp.task('server', server) -gulp.task('default', gulp.series('clean', gulp.parallel('scss'), 'html', 'server', 'watch')) +gulp.task('default', gulp.series('clean', gulp.parallel('scss', 'js'), 'server', 'watch')) diff --git a/public/index.html b/public/index.html index 039a277..ea8a510 100644 --- a/public/index.html +++ b/public/index.html @@ -1,15 +1,22 @@ +