-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
386 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
example/add-initializing-scripts-into-the-common-bundle/gulpfile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
var gulp = require('gulp') | ||
var reduce = require('../..') | ||
var path = require('path') | ||
var uglify = require('gulp-uglify') | ||
var del = require('del') | ||
|
||
var onerror = function (err) { | ||
console.log(err.message) | ||
} | ||
|
||
var initialScripts = [ | ||
path.join(__dirname, 'src/web_modules/lib/init.js'), | ||
] | ||
|
||
var bundleOpts = { | ||
// Options passed to `factor-vinylify` | ||
// Refer to `https://github.com/zoubin/factor-vinylify#options` for more information. | ||
factor: { | ||
// One bundle for each entry detected from `.src()`. | ||
needFactor: true, | ||
|
||
// If omitted, no common bundle will be created. | ||
common: 'common.js', | ||
|
||
// This is a little complicated. | ||
// We need to make `factor-vinylify` more convenient to handle such cases. | ||
threshold: function (row, groups) { | ||
// Force `modules` specified in `initialScripts` go to the common bundle | ||
if (initialScripts.indexOf(row.file) >= 0) { | ||
return true | ||
} | ||
|
||
// Apply commonify | ||
// Refer to `https://github.com/zoubin/factor-vinylify#commonify` for more information | ||
if (typeof row.common === 'boolean') { | ||
return row.common | ||
} | ||
|
||
return groups.length > 1 || groups.length === 0 | ||
}, | ||
}, | ||
|
||
// And all options passed to `browserify` | ||
// Refer to `https://github.com/substack/node-browserify#methods` for more information | ||
|
||
basedir: path.join(__dirname, 'src'), | ||
|
||
// Now, we can `require('lib/world')` anywhere under the `src` directory. | ||
// Otherwise, we have to write relative paths like `require('../../web_modules/lib/world')` | ||
paths: [path.join(__dirname, 'src', 'web_modules')], | ||
} | ||
|
||
gulp.task('clean', function () { | ||
return del(path.join(__dirname, 'build')) | ||
}) | ||
|
||
// Pack all JS modules into multiple bundles. | ||
gulp.task('build', ['clean'], function () { | ||
reduce.on('log', console.log.bind(console)) | ||
reduce.on('error', onerror) | ||
|
||
// `b` is the browserify instance. | ||
reduce.on('instance', function (b) { | ||
b.add('./web_modules/lib/init.js') | ||
}) | ||
|
||
// The first argument is passed to globby. | ||
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information | ||
return reduce.src('page/**/index.js', bundleOpts) | ||
// `pipe` into gulp-plugins | ||
.pipe(uglify()) | ||
.pipe(gulp.dest('build')) | ||
}) | ||
|
||
// To keep `watch` unfinished, declare `cb` as the first argument of the task callback | ||
gulp.task('watch', ['clean'], function (cb) { | ||
var watcher = reduce.watch() | ||
watcher.on('log', console.log.bind(console)) | ||
watcher.on('error', onerror) | ||
|
||
// `b` is the browserify instance. | ||
watcher.on('instance', function (b) { | ||
b.add('./web_modules/lib/init.js') | ||
}) | ||
|
||
// The first argument is passed to globby. | ||
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information | ||
watcher.src('page/**/index.js', bundleOpts) | ||
// `pipe` into lazy transforms, i.e. functions to create transforms | ||
.pipe(uglify) | ||
.pipe(gulp.dest, 'build') | ||
}) | ||
|
2 changes: 2 additions & 0 deletions
2
example/add-initializing-scripts-into-the-common-bundle/src/node_modules/exclamation/excl.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
...add-initializing-scripts-into-the-common-bundle/src/node_modules/exclamation/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
example/add-initializing-scripts-into-the-common-bundle/src/page/hello/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var world = require('lib/world') | ||
module.exports = 'hello, ' + world | ||
|
3 changes: 3 additions & 0 deletions
3
example/add-initializing-scripts-into-the-common-bundle/src/page/hi/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var world = require('lib/world') | ||
module.exports = 'hi, ' + world | ||
|
2 changes: 2 additions & 0 deletions
2
example/add-initializing-scripts-into-the-common-bundle/src/web_modules/lib/init.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = 'Welcome to ' + require('./website') + require('exclamation') | ||
|
1 change: 1 addition & 0 deletions
1
example/add-initializing-scripts-into-the-common-bundle/src/web_modules/lib/website.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'http://www.example.com' |
1 change: 1 addition & 0 deletions
1
example/add-initializing-scripts-into-the-common-bundle/src/web_modules/lib/world.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'world' + require('exclamation') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var gulp = require('gulp') | ||
var reduce = require('../..') | ||
var path = require('path') | ||
var uglify = require('gulp-uglify') | ||
var del = require('del') | ||
|
||
var onerror = function (err) { | ||
console.log(err.message) | ||
} | ||
|
||
var bundleOpts = { | ||
// Options passed to `factor-vinylify` | ||
// Refer to `https://github.com/zoubin/factor-vinylify#options` for more information. | ||
factor: { | ||
// One bundle for each entry detected from `.src()`. | ||
needFactor: true, | ||
|
||
// If omitted, no common bundle will be created. | ||
common: 'common.js', | ||
|
||
// Passed to multimatch to filter modules should go to the common bundle. | ||
// Refer to `https://github.com/sindresorhus/multimatch` for more information | ||
threshold: '**/lib/*.js', | ||
}, | ||
|
||
// And all options passed to `browserify` | ||
// Refer to `https://github.com/substack/node-browserify#methods` for more information | ||
|
||
basedir: path.join(__dirname, 'src'), | ||
|
||
// Now, we can `require('lib/world')` anywhere under the `src` directory. | ||
// Otherwise, we have to write relative paths like `require('../../web_modules/lib/world')` | ||
paths: [path.join(__dirname, 'src', 'web_modules')], | ||
} | ||
|
||
gulp.task('clean', function () { | ||
return del(path.join(__dirname, 'build')) | ||
}) | ||
|
||
// Pack all JS modules into multiple bundles. | ||
gulp.task('build', ['clean'], function () { | ||
reduce.on('log', console.log.bind(console)) | ||
reduce.on('error', onerror) | ||
|
||
// The first argument is passed to globby. | ||
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information | ||
return reduce.src('page/**/index.js', bundleOpts) | ||
// `pipe` into gulp-plugins | ||
.pipe(uglify()) | ||
.pipe(gulp.dest('build')) | ||
}) | ||
|
||
// To keep `watch` unfinished, declare `cb` as the first argument of the task callback | ||
gulp.task('watch', ['clean'], function (cb) { | ||
var watcher = reduce.watch() | ||
watcher.on('log', console.log.bind(console)) | ||
watcher.on('error', onerror) | ||
|
||
// The first argument is passed to globby. | ||
// Refer to `https://github.com/sindresorhus/globby#globbypatterns-options` for more information | ||
watcher.src('page/**/index.js', bundleOpts) | ||
// `pipe` into lazy transforms, i.e. functions to create transforms | ||
.pipe(uglify) | ||
.pipe(gulp.dest, 'build') | ||
}) | ||
|
2 changes: 2 additions & 0 deletions
2
example/customize-the-common-bundle/src/node_modules/exclamation/excl.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
example/customize-the-common-bundle/src/node_modules/exclamation/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var world = require('lib/world') | ||
module.exports = 'hello, ' + world | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var there = require('./there') | ||
module.exports = 'hi, ' + there | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'there' + require('exclamation') |
1 change: 1 addition & 0 deletions
1
example/customize-the-common-bundle/src/web_modules/lib/world.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'world' + require('exclamation') |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.