https://github.com/gera2ld/define-commonjs
Yet another CommonJS implementation
https://github.com/gera2ld/define-commonjs
Last synced: 2 days ago
JSON representation
Yet another CommonJS implementation
- Host: GitHub
- URL: https://github.com/gera2ld/define-commonjs
- Owner: gera2ld
- Created: 2016-05-09T09:32:05.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-03-11T03:37:50.000Z (over 9 years ago)
- Last Synced: 2026-05-12T00:32:26.694Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
define-commonjs
===



This is yet another CommonJS implementation.
Usage
---
``` html
```
``` js
// bundle.js is like this
define('a', function (require, exports, module) {
module.exports = 'hello';
});
define('b', function (require, exports, module) {
module.exports = 'world';
});
define('app', function (require, exports, module) {
var a = require('a');
var b = require('b');
console.log(a + ', ' + b);
});
// define a dependency asynchronously, needs define-async.js
define.async('async/a', 'http://script/to/a.js');
define.async('async/b', function () {
// should resolve the script code
return getCodeFromSomewhereElse();
});
define.use('app');
```
Packing
---
Assume we have a project with three files:
``` js
// src/a.js
module.exports = 'hello';
// src/b.js
module.exports = 'world';
// src/app.js
var a = require('./a');
var b = require('./b');
console.log(a + ', ' + b);
```
Using gulp:
``` js
const pack = require('define-commonjs/pack/gulp');
const concat = require('gulp-concat');
gulp.task('pack', () => {
const collect = pack();
return gulp.src('src/*.js')
.pipe(collect)
.pipe(collect.pack({main: 'src/app.js'}))
.pipe(concat())
.pipe(gulp.dest('dist'));
});
gulp.task('pack-with-custom-paths', () => {
const collect = pack();
return gulp.src('src/*.js')
.pipe(collect)
.pipe(collect.pack({
main: 'src/app.js',
getPath(file) {
// This is useful if we have changed files before packing, e.g. concat'ed.
return file.relative === 'main.js' ? 'src/app.js' : file.path;
},
}))
.pipe(concat())
.pipe(gulp.dest('dist'));
});
```