|
|
@@ -1,132 +1,58 @@
|
|
|
const del = require('del');
|
|
|
const gulp = require('gulp');
|
|
|
+const gutil = require('gulp-util');
|
|
|
const concat = require('gulp-concat');
|
|
|
const merge2 = require('merge2');
|
|
|
const postcss = require('gulp-postcss');
|
|
|
const gulpFilter = require('gulp-filter');
|
|
|
-const eslint = require('gulp-eslint');
|
|
|
const uglify = require('gulp-uglify');
|
|
|
const svgSprite = require('gulp-svg-sprite');
|
|
|
-const definePack = require('define-commonjs/pack/gulp');
|
|
|
-const templateCache = require('./scripts/templateCache');
|
|
|
+const webpack = require('webpack');
|
|
|
const i18n = require('./scripts/i18n');
|
|
|
-const wrap = require('./scripts/wrap');
|
|
|
const json = require('./scripts/json');
|
|
|
const pkg = require('./package.json');
|
|
|
const isProd = process.env.NODE_ENV === 'production';
|
|
|
+const webpackConfig = require('./scripts/webpack.conf');
|
|
|
|
|
|
const paths = {
|
|
|
- cache: 'src/cache.js',
|
|
|
manifest: 'src/manifest.json',
|
|
|
- templates: [
|
|
|
- 'src/**/*.html',
|
|
|
- '!src/*/index.html',
|
|
|
- ],
|
|
|
- jsCollect: [
|
|
|
- 'src/**/*.js',
|
|
|
- '!src/public/**',
|
|
|
- '!src/injected.js',
|
|
|
+ copy: [
|
|
|
+ 'src/public/**',
|
|
|
],
|
|
|
- jsCommon: 'src/common.js',
|
|
|
- jsBg: 'src/background/**/*.js',
|
|
|
- jsOptions: 'src/options/**/*.js',
|
|
|
- jsPopup: 'src/popup/**/*.js',
|
|
|
locales: [
|
|
|
- 'src/**/*.js',
|
|
|
- 'src/**/*.html',
|
|
|
- 'src/**/*.json',
|
|
|
- 'src/**/*.yml',
|
|
|
+ 'src/_locales/**',
|
|
|
],
|
|
|
- copy: [
|
|
|
- 'src/injected.js',
|
|
|
- 'src/public/**',
|
|
|
- 'src/*/*.html',
|
|
|
- 'src/*/*.css',
|
|
|
+ templates: [
|
|
|
+ 'src/**/*.@(js|html|json|yml|vue)',
|
|
|
],
|
|
|
};
|
|
|
|
|
|
-gulp.task('clean', () => del(['dist']));
|
|
|
-
|
|
|
-gulp.task('watch', ['build'], () => {
|
|
|
- gulp.watch([].concat(paths.cache, paths.templates), ['templates']);
|
|
|
- gulp.watch(paths.jsCollect, ['js']);
|
|
|
- gulp.watch(paths.copy, ['copy-files']);
|
|
|
- gulp.watch(paths.locales, ['copy-i18n']);
|
|
|
-});
|
|
|
-
|
|
|
-gulp.task('lint', () => (
|
|
|
- gulp.src([
|
|
|
- 'src/**/*.js',
|
|
|
- '!src/public/**',
|
|
|
- ])
|
|
|
- .pipe(eslint())
|
|
|
- .pipe(eslint.format())
|
|
|
-));
|
|
|
-
|
|
|
-var cacheObj;
|
|
|
-var collect;
|
|
|
+function webpackCallback(err, stats) {
|
|
|
+ if (err) {
|
|
|
+ gutil.log('[ERROR]', err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ stats.stats.forEach(stat => {
|
|
|
+ const timeCost = (stat.endTime - stat.startTime) / 1000;
|
|
|
+ const chunks = Object.keys(stat.compilation.namedChunks).join(' ');
|
|
|
+ gutil.log(`Webpack built: [${timeCost.toFixed(3)}s] ${chunks}`);
|
|
|
+ });
|
|
|
+}
|
|
|
|
|
|
-gulp.task('collect-js', () => {
|
|
|
- collect = definePack();
|
|
|
- return gulp.src(paths.jsCollect)
|
|
|
- .pipe(collect);
|
|
|
-});
|
|
|
-
|
|
|
-gulp.task('templates', ['collect-js'], () => {
|
|
|
- cacheObj = templateCache('cache');
|
|
|
- var stream = merge2([
|
|
|
- gulp.src(paths.cache),
|
|
|
- gulp.src(paths.templates).pipe(cacheObj),
|
|
|
- ])
|
|
|
- .pipe(concat('cache.js'))
|
|
|
- .pipe(collect.pack({
|
|
|
- getPath(file) {
|
|
|
- return 'src/cache.js';
|
|
|
- },
|
|
|
- }));
|
|
|
- if (isProd) stream = stream.pipe(uglify());
|
|
|
- return stream.pipe(gulp.dest('dist'));
|
|
|
-});
|
|
|
+gulp.task('clean', () => del(['dist']));
|
|
|
|
|
|
-gulp.task('js-common', ['collect-js'], () => {
|
|
|
- var stream = gulp.src(paths.jsCommon)
|
|
|
- .pipe(collect.pack());
|
|
|
- if (isProd) stream = stream.pipe(uglify());
|
|
|
- return stream.pipe(gulp.dest('dist'));
|
|
|
-});
|
|
|
+gulp.task('pack', ['manifest', 'copy-files', 'copy-i18n']);
|
|
|
|
|
|
-gulp.task('js-bg', ['collect-js'], () => {
|
|
|
- var stream = gulp.src(paths.jsBg)
|
|
|
- .pipe(collect.pack({main: 'src/background/app.js'}))
|
|
|
- .pipe(concat('background/app.js'));
|
|
|
- if (isProd) stream = stream.pipe(uglify());
|
|
|
- return stream.pipe(gulp.dest('dist'));
|
|
|
-});
|
|
|
-
|
|
|
-gulp.task('js-options', ['templates', 'collect-js'], () => {
|
|
|
- var stream = gulp.src(paths.jsOptions)
|
|
|
- .pipe(cacheObj.replace())
|
|
|
- .pipe(collect.pack({main: 'src/options/app.js'}))
|
|
|
- .pipe(concat('options/app.js'));
|
|
|
- if (isProd) stream = stream.pipe(uglify());
|
|
|
- return stream.pipe(gulp.dest('dist'));
|
|
|
+gulp.task('watch', ['pack', 'js-dev'], () => {
|
|
|
+ gulp.watch(paths.manifest, ['manifest']);
|
|
|
+ gulp.watch(paths.copy, ['copy-files']);
|
|
|
+ gulp.watch(paths.locales.concat(paths.templates), ['copy-i18n']);
|
|
|
});
|
|
|
|
|
|
-gulp.task('js-popup', ['templates', 'collect-js'], () => {
|
|
|
- var stream = gulp.src(paths.jsPopup)
|
|
|
- .pipe(cacheObj.replace())
|
|
|
- .pipe(collect.pack({main: 'src/popup/app.js'}))
|
|
|
- .pipe(concat('popup/app.js'));
|
|
|
- if (isProd) stream = stream.pipe(uglify());
|
|
|
- return stream.pipe(gulp.dest('dist'))
|
|
|
-});
|
|
|
+gulp.task('build', ['pack', 'js-prd']);
|
|
|
|
|
|
-gulp.task('js', [
|
|
|
- 'js-common',
|
|
|
- 'js-bg',
|
|
|
- 'js-options',
|
|
|
- 'js-popup',
|
|
|
-]);
|
|
|
+gulp.task('js-dev', () => webpack(webpackConfig).watch({}, webpackCallback));
|
|
|
+gulp.task('js-prd', () => webpack(webpackConfig, webpackCallback));
|
|
|
|
|
|
gulp.task('manifest', () => (
|
|
|
gulp.src(paths.manifest, {base: 'src'})
|
|
|
@@ -138,16 +64,9 @@ gulp.task('manifest', () => (
|
|
|
));
|
|
|
|
|
|
gulp.task('copy-files', () => {
|
|
|
- const injectedFilter = gulpFilter(['**/injected.js'], {restore: true});
|
|
|
const jsFilter = gulpFilter(['**/*.js'], {restore: true});
|
|
|
const cssFilter = gulpFilter(['**/*.css'], {restore: true});
|
|
|
- var stream = gulp.src(paths.copy, {base: 'src'})
|
|
|
- .pipe(injectedFilter)
|
|
|
- .pipe(wrap({
|
|
|
- header: '!function(){\n',
|
|
|
- footer: '\n}();',
|
|
|
- }))
|
|
|
- .pipe(injectedFilter.restore);
|
|
|
+ let stream = gulp.src(paths.copy, {base: 'src'});
|
|
|
if (isProd) stream = stream
|
|
|
.pipe(jsFilter)
|
|
|
.pipe(uglify())
|
|
|
@@ -156,14 +75,16 @@ gulp.task('copy-files', () => {
|
|
|
.pipe(cssFilter)
|
|
|
.pipe(postcss([
|
|
|
require('precss')(),
|
|
|
- isProd && require('cssnano')({zindex: false}),
|
|
|
+ isProd && require('cssnano')({
|
|
|
+ // zindex: false,
|
|
|
+ }),
|
|
|
].filter(Boolean)))
|
|
|
.pipe(cssFilter.restore)
|
|
|
.pipe(gulp.dest('dist/'));
|
|
|
});
|
|
|
|
|
|
gulp.task('copy-i18n', () => (
|
|
|
- gulp.src(paths.locales)
|
|
|
+ gulp.src(paths.templates)
|
|
|
.pipe(i18n.extract({
|
|
|
base: 'src',
|
|
|
prefix: '_locales',
|
|
|
@@ -188,16 +109,8 @@ gulp.task('svg', () => (
|
|
|
.pipe(gulp.dest('dist/public'))
|
|
|
));
|
|
|
|
|
|
-gulp.task('build', [
|
|
|
- 'js',
|
|
|
- 'manifest',
|
|
|
- 'copy-files',
|
|
|
- 'copy-i18n',
|
|
|
- 'svg',
|
|
|
-]);
|
|
|
-
|
|
|
gulp.task('i18n', () => (
|
|
|
- gulp.src(paths.locales)
|
|
|
+ gulp.src(paths.templates)
|
|
|
.pipe(i18n.extract({
|
|
|
base: 'src',
|
|
|
prefix: '_locales',
|
|
|
@@ -208,5 +121,3 @@ gulp.task('i18n', () => (
|
|
|
}))
|
|
|
.pipe(gulp.dest('src'))
|
|
|
));
|
|
|
-
|
|
|
-gulp.task('default', ['build']);
|