0.1.1 • Published 7 years ago

pkg-export v0.1.1

Weekly downloads
7
License
MIT
Repository
github
Last release
7 years ago

The tool is used to export packages in your project.

For Gulp, visit: https://github.com/HaoyCn/gulp-pkg-export

Here is a basic example:

'use strict';

const pkgExport = require('pkg-export');
// `rewriteRequire` accepts a string which is an absolute path to a script file.
// It looks `require()` up in scripts, find the required packages and rewrite the `require()`.
// Return an array which contains all of them.
const rewriteRequire = pkgExport({
	// String.
	// The basic directory of scripts
	base: 'src',
	// String.
	// The directory where node modules will be put
	npm: 'js/npm',
	// Array.
	// A list of package names. If a package cannot be found and it's in the list, the tool won't give a warning
	ignore: ['fs'],
	// Function. Optional.
	// The contents of files those aren't in 'node_modules' will be compiled by this function
	// Here is an example to see how to transform codes with babel
	compiler: function(content) {
		return require('babel-core').transform(content, {
			presets: ['es2015', 'stage-0'],
			plugins: ['transform-runtime']
		}).code;
	},
	// Function. Optional.
	// if a package cannot get resolved, this function will be called with an object which has `src` and `require` properties
	warn: console.warn
});

const arr = rewriteRequire(__dirname + '/src/index.js');

arr.forEach(function(file) {
	// the relative path
	console.log(file.dest);
	// the compiled codes
	console.log(file.content);

	// do something else
});