Skip to content

Commit

Permalink
Intracto#172 concat task now also uses Rollup to parse js files befor…
Browse files Browse the repository at this point in the history
…e concatting.
  • Loading branch information
gijsroge committed May 25, 2020
1 parent 0dbf6fd commit 63a91c7
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 31 deletions.
10 changes: 9 additions & 1 deletion lib/gulp/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ function processConfig(config, cwd) {
['css', 'js', 'img', 'js-concat', 'svg-sprite', 'copy'].forEach(key => {
if (config[key]) {
config[key] = config[key].map(value => {
const src = path.join(cwd, config.src_base_path, value.src);
let src = '';
if (Array.isArray(value.src)) {
src = value.src.map(src => path.join(cwd, config.src_base_path, src));
} else {
src = path.join(cwd, config.src_base_path, value.src);
}

console.log(src);

const dest = path.join(cwd, config.dest_base_path, value.dest);
let watch = [src];

Expand Down
41 changes: 19 additions & 22 deletions lib/gulp/js.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const concat = require('gulp-concat');
const gulp = require('gulp');
const {cosmiconfigSync} = require('cosmiconfig');
const plumber = require('gulp-plumber');
const {babel} = require('@rollup/plugin-babel');
const {eslint} = require('rollup-plugin-eslint');
const {rollup} = require('rollup');
const path = require('path');
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const glob = require('util').promisify(require('glob'));
const {terser} = require('rollup-plugin-terser');
const configs = require('./configs.js');
const multi = require('@rollup/plugin-multi-entry');

async function js() {
let jsTasks = [];
Expand All @@ -18,7 +17,7 @@ async function js() {
const configurations = await configs.then(configurations => {
configurations.forEach(config => {
jsTasks = jsTasks.concat(config.js.map(async js => {
const files = await glob(js.src, {absolute: true});
const files = Array.isArray(js.src) ? js.src : await glob(js.src, {absolute: true});
return Promise.all(files.map(async file => jsCompile({src: file, dest: js.dest, cwd: config.cwd})));
}));
});
Expand All @@ -28,16 +27,16 @@ async function js() {
await Promise.all(jsTasks).then(() => {
configurations.forEach(config => {
jsConcatTasks = jsConcatTasks.concat(config['js-concat'].map(js => {
return jsConcat({src: js.src, dest: js.dest, name: js.name});
return jsConcat({src: js.src, dest: js.dest, name: js.name, cwd: config.cwd});
}));
});
});

return Promise.all(jsConcatTasks);
}

async function generateBundle({src, cwd}) {
const rollupPlugins = [];
async function generateBundle({src, cwd, output = {}, plugins = []}) {
const rollupPlugins = plugins;

// Bundle third party imported modules
rollupPlugins.push(resolve.default());
Expand Down Expand Up @@ -65,6 +64,7 @@ async function generateBundle({src, cwd}) {

return rollup({
input: src,
output,
plugins: rollupPlugins
});
}
Expand All @@ -83,23 +83,20 @@ async function jsCompile({src, dest, cwd, browserSync = false}) {
});
}

function jsConcat({src, dest, name, browserSync = false}) {
return new Promise(
(async resolve => { // eslint-disable-line no-async-promise-executor
let stream = gulp.src(src);
async function jsConcat({src, dest, name, browserSync = false}, cwd) {
const output = {file: name};
const plugins = [multi()];
const bundle = await generateBundle({src, cwd, output, plugins});

stream = stream
.pipe(plumber())
.pipe(concat(name))
.pipe(gulp.dest(dest));

if (browserSync !== false) {
stream.pipe(browserSync.stream({match: '**/*.js'}));
}
if (browserSync) {
browserSync.watch(bundle.watchFiles).on('change', browserSync.reload);
}

stream.on('end', resolve);
})
);
return bundle.write({
file: path.join(dest, name),
format: 'iife',
sourcemap: process.env.NODE_ENV !== 'production'
});
}

module.exports = {js, jsCompile, jsConcat, generateBundle};
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"devDependencies": {
"@rollup/plugin-babel": "^5.0.2",
"@rollup/plugin-commonjs": "^12.0.0",
"@rollup/plugin-multi-entry": "^3.0.1",
"@rollup/plugin-node-resolve": "^8.0.0",
"npm-run-all": "^4.1.5",
"rfs": "^9.0.3",
Expand Down
11 changes: 9 additions & 2 deletions test/js/.buildozerrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ src_base_path: ./
dest_base_path: ./
img: []
js:
- src: js/index.js
- src:
- js/index.js
- js/b.js
dest: dist/js
js-concat: []
- src: js/c.js
dest: dist/js/c
js-concat:
- src: js/concat/*.js
name: all.js
dest: dist/js/concat
svg-sprite: []
browsersync:
server: null # Static sites
Expand Down
1 change: 1 addition & 0 deletions test/js/dist/js/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";function o(){console.log("b")}o(),module.exports=o;
1 change: 1 addition & 0 deletions test/js/dist/js/c/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";function o(){console.log("c")}o(),module.exports=o;
1 change: 1 addition & 0 deletions test/js/dist/js/concat/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!function(o){"use strict";o.a=function(){console.log("a")},o.b=function(){console.log("b")}}({});
2 changes: 1 addition & 1 deletion test/js/dist/js/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";console.log((function(){console.log("foo")}));
"use strict";console.log((function(){console.log("a")}));
3 changes: 3 additions & 0 deletions test/js/js/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function a () {
console.log('a')
}
4 changes: 4 additions & 0 deletions test/js/js/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function b() {
console.log('b')
}
b();
4 changes: 4 additions & 0 deletions test/js/js/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function c() {
console.log('c')
}
c();
3 changes: 3 additions & 0 deletions test/js/js/concat/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function a() {
console.log('a')
}
3 changes: 3 additions & 0 deletions test/js/js/concat/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function b() {
console.log('b')
}
3 changes: 3 additions & 0 deletions test/js/js/concat/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function c() {
console.log('c')
}
3 changes: 0 additions & 3 deletions test/js/js/foo.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/js/js/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import foo from "./foo";
console.log(foo);
import a from "./a";
console.log(a);

0 comments on commit 63a91c7

Please sign in to comment.