Skip to content

Commit 0c70326

Browse files
author
Walker Leite
committed
feat(gulp-tasks): add support to es6 modules, fixes #7
1 parent ea23c48 commit 0c70326

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

template/gulp-tasks/build.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import path from 'path';
66
import browserify from 'browserify';
77
import vueify from 'vueify';
88
import babelify from 'babelify';
9+
import tfilter from 'tfilter';
910
import modulesify from 'css-modulesify';
1011
import aliasify from 'aliasify';
1112
import source from 'vinyl-source-stream';
1213
import buffer from 'vinyl-buffer';
1314
import {argv} from 'yargs';
14-
import {dirs} from './config';
15+
import {dirs, extended} from './config';
1516
import {customSass} from './compilers';
1617

1718
gulp.task('build:test', () => gulp.src([
@@ -24,6 +25,12 @@ gulp.task('build:test', () => gulp.src([
2425
.pipe(gulp.dest(dirs.buildTest)));
2526

2627
gulp.task('build:client', ['copy:client', 'copy:config:server'], () => {
28+
// Node modules to be included in babel transpilation
29+
// Use this with ES6 modules for example
30+
const bModules = [];
31+
32+
if (extended) bModules.push('bootstrap-vue/es');
33+
2734
vueify.compiler.applyConfig({
2835
sass: {
2936
includePaths: [
@@ -36,13 +43,49 @@ gulp.task('build:client', ['copy:client', 'copy:config:server'], () => {
3643
});
3744

3845
const b = browserify({
39-
entries: path.resolve(dirs.srcClient, 'main.js'),
46+
entries: [
47+
path.resolve(dirs.srcClient, 'main.js'),
48+
...bModules.map(mod => path.resolve(dirs.modules, mod)),
49+
],
4050
debug: true,
4151
});
4252

53+
// Transpile .vue
4354
b.transform(vueify);
4455

45-
b.transform(babelify, {plugins: ['transform-runtime']});
56+
// Babel Settings with module filter
57+
const bSettings = {
58+
plugins: ['transform-runtime'],
59+
};
60+
61+
b.transform(tfilter(
62+
babelify,
63+
{
64+
filter: (filename) => {
65+
const exception = bModules.some(
66+
mod => filename.includes(
67+
path.resolve(dirs.modules, mod)
68+
)
69+
);
70+
if (exception) {
71+
// node module
72+
// console.log('Transpiling module', filename);
73+
return true;
74+
} else if (!filename.includes(dirs.modules)) {
75+
// filter not in node_modules
76+
// console.log('Transpiling src', filename);
77+
return true;
78+
}
79+
// console.log('NOT transpiling', filename);
80+
return false;
81+
},
82+
},
83+
{
84+
...bSettings,
85+
global: true,
86+
presets: ['env'],
87+
}
88+
));
4689

4790
b.plugin(modulesify, {
4891
output: path.resolve(

template/gulp-tasks/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable import/prefer-default-export */
22
import path from 'path';
3+
import pkg from '../package.json';
4+
5+
// true if this template was generated with extended option
6+
export const extended = pkg.extended || false;
37

48
export const dirs = {};
59
dirs.root = path.resolve(__dirname, '../');

template/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"loopback-chai": "^2.3.0",
7979
"mocha": "^3.4.2",
8080
"sinon": "^2.3.8",
81+
"tfilter": "^1.0.1",
8182
"through2": "^2.0.3",
8283
"tmp": "0.0.33",
8384
"vinyl-buffer": "^1.0.0",
@@ -96,5 +97,7 @@
9697
"url": ""
9798
},
9899
"license": "UNLICENSED",
99-
"description": "{{ description }}"
100+
"description": "{{ description }}"{{#extended}},
101+
"extended": true
102+
{{/extended}}
100103
}

0 commit comments

Comments
 (0)