gulpfile.js 3.86 KB
Newer Older
Patrick's avatar
neu  
Patrick committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var gulp = require('gulp');
var gutil = require('gulp-util');
var replace = require('gulp-replace');
var gulpif = require('gulp-if');
var rename = require('gulp-rename');
var zip = require('gulp-zip');
var del = require('del');
var vinylPaths = require('vinyl-paths');
var runSequence = require('run-sequence');
var program = require('commander');
var spawn = require('child_process').spawn;
var jeditor = require('gulp-json-editor');
var fs = require('fs');

program
  .option('-p, --plugin <name>', 'the name to use for the plugin, overwrites the default')
  .option('-l, --library <name>', 'to export the plugin as a library')
  .parse(process.argv);
var pluginName = program.plugin;

gulp.task('clean', function() {
  return del([
    'dist/*'
  ]);
});

gulp.task('webpack-modern', function(cb) {
  const child = spawn('node', ['node_modules/webpack/bin/webpack.js', '--config', 'build/webpack.prod.conf.js'], {
    env: Object.assign({
      VUE_CLI_MODERN_BUILD: true,
      LIBRARY_NAME: program.library,
    }, process.env),
  });
  child.stdout.on('data', (data) => {
    gutil.log(data.toString());
  });
  child.stderr.on('data', (data) => {
    gutil.log(data.toString());
  });
  child.on('close', (code) => {
    if (code) {
      throw new gutil.PluginError('webpack modern', code);
    }
    cb();
  });
});

gulp.task('webpack-legacy', function(cb) {
  const child = spawn('node', ['node_modules/webpack/bin/webpack.js', '--config', 'build/webpack.prod.conf.js'], {
    env: Object.assign({
      LIBRARY_NAME: program.library,
    }, process.env),
  });
  child.stdout.on('data', (data) => {
    gutil.log(data.toString());
  });
  child.stderr.on('data', (data) => {
    gutil.log(data.toString());
  });
  child.on('close', (code) => {
    if (code) {
      throw new gutil.PluginError('webpack legacy', code);
    }
    cb();
  });
});

gulp.task('uglify-modern', function(cb) { // TODO replace with terser-webpack-plugin when moving to webpack 4
  const child = spawn('node', ['node_modules/terser/bin/uglifyjs', '--mangle', '--output', 'dist/plugin.es6.js', 'dist/plugin._es6.js']);
  child.stdout.on('data', (data) => {
    gutil.log(data.toString());
  });
  child.stderr.on('data', (data) => {
    gutil.log(data.toString());
  });
  child.on('close', (code) => {
    if (code) {
      throw new gutil.PluginError('webpack modern', code);
    }
    fs.unlink('dist/plugin._es6.js', (err) => {
      if (err) {
        throw new guitl.PluginError('webpack modern', err);
      }
      cb();
    });
  });
});

gulp.task('replace', function() {
  return gulp.src(['dist/*.js'])
    .pipe(gulpif(!pluginName, replace(/registerPlugin\((.*)\)/, function(match) {
      pluginName = match.match(/name:"(\w+)"/)[1];
      return match;
    })))
    .pipe(replace(/\.?\/?(assets|img|fonts|media)\//g, function(match, folder) {
      return `plugins/${pluginName}/`;
    }))
    .pipe(vinylPaths(del))
    .pipe(rename(function(path) {
      path.basename = /\.es6$/.test(path.basename) ? `${pluginName}.es6` : pluginName;
    }))
    .pipe(gulp.dest('dist/'));
});

gulp.task('add-esmodule', function() {
  if (!fs.existsSync('config.json')) {
    fs.writeFileSync('config.json', '{}');
  }
  return gulp.src('config.json')
    .pipe(jeditor({
      _esmodule: true,
    }))
    .pipe(gulp.dest('.'));
});

gulp.task('zip', function() {
  return gulp.src(['dist/*.js', 'assets/**/*', 'config.json', 'package.json', 'img/**/*', 'README.md'], { base: process.cwd() })
    .pipe(rename(function (path) {
      var ext = path.dirname !== 'dist' && path.dirname !== '.' ? path.dirname : '';
      path.dirname = pluginName + '/' + ext;
    }))
    .pipe(zip(pluginName + '.zip'))
    .pipe(gulp.dest('dist'));
});

gulp.task('build', function(cb) {
  runSequence(
    'clean',
    'webpack-modern',
    'webpack-legacy',
    'uglify-modern',
    'replace',
    'add-esmodule',
    'zip',
    cb
  );
});

gulp.task('default', ['build']);