Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base Option #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var uglify = require('gulp-uglify');
gulp.task('copy-js', function() {
gulp.src('./src/*.html')
.pipe(ghtmlSrc())
// From this point, it's as if you'd used gulp.src() listing each of your
// From this point, it's as if you'd used gulp.src() listing each of your
// javascript files that are in your html as <script src="..."></script>
.pipe(uglify())
.pipe(gulp.dest('./build/'));
Expand Down Expand Up @@ -55,7 +55,7 @@ It will skip any script or link tags with data-remove="true" or data-ignore="tru

Type: `bool`
Default: `false`


By default the original HTML file (that probably came from gulp.src()) is swallowed by `gulp-html-src`, and it only emits the matching script or css files. However, if you want to keep the HTML in the stream, then set this option to true. One use of this could be to blindly copy the HTML and all references to a destination directory.

Expand All @@ -68,7 +68,7 @@ The following options are not used for the "normal" cases, but could be useful i

Type: `String`
Default: For presets == `script`, `script:not([data-ignore=true], [data-remove=true])`, for presets == `css`, `link[type="text/css"][rel=stylesheet]:not([data-ignore=true], [data-remove=true])`


This is the [cheerio](https://github.com/cheeriojs/cheerio) selector (basically a jQuery selector) for the elements to select. See `options.getFileName` for how this element is converted to a filename.

Expand All @@ -86,5 +86,8 @@ function getFileName(node) {
}
```

### options.base

Type: `String`

If `options.base` is set, the base for the created file is set to match the given `base` rather than the html-file base. This helps when the js and css files are stored in different folders for development and production.
41 changes: 29 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var fs = require('fs'),
path = require('path'),
url = require('url'),
File = require('vinyl'),
File = require('vinyl'),
cheerio = require('cheerio'),
through = require('through2'),
extend = require('extend'),
Expand All @@ -11,7 +11,8 @@ module.exports = function(options) {
var defaults = {
presets: 'script',
includeHtmlInOutput: false,
createReadStream : fs.createReadStream
createReadStream : fs.createReadStream,
base: null
};

var presets = {
Expand All @@ -28,9 +29,9 @@ module.exports = function(options) {
var selectedPresets = (options && options.presets && presets[options.presets]) ||
presets[defaults.presets];


options = extend({}, defaults, selectedPresets, options);

var makeAbsoluteFileName = function makeAbsoluteFileName(file, fileName) {
//return file.base + fileName; // path.join(file.base, fileName);
return path.join(path.dirname(file.path), fileName);
Expand Down Expand Up @@ -84,9 +85,9 @@ module.exports = function(options) {
var transform = function(file, enc, callback) {
var stream = this;
var bufferReadPromises = [];
var fileNames;
var files = [];
var fileNames;
var files = [];

if (file.isNull()) {
// No contents - do nothing
stream.push(file);
Expand All @@ -103,10 +104,18 @@ module.exports = function(options) {
// Iterate over found file names.
fileNames.forEach(function (fileName) {
if (isRelative(fileName)) {
var absoluteFileName = makeAbsoluteFileName(file, fileName);
var absoluteFileName, fileBase;
if (options.base) {
absoluteFileName = path.join(file.cwd, options.base, fileName);
fileBase = options.base;
}
else {
absoluteFileName = makeAbsoluteFileName(file, fileName);
fileBase = file.base;
}
stream.push(new File({
cwd: file.cwd,
base: file.base,
base: fileBase,
path: absoluteFileName,
contents: options.createReadStream(absoluteFileName)
}));
Expand All @@ -133,12 +142,20 @@ module.exports = function(options) {
fileNames.forEach(function (fileName, index) {
if (isRelative(fileName)) {
try {
var absoluteFileName = makeAbsoluteFileName(file, fileName);
var absoluteFileName, fileBase;
if (options.base) {
absoluteFileName = path.join(file.cwd, options.base, fileName);
fileBase = options.base;
}
else {
absoluteFileName = makeAbsoluteFileName(file, fileName);
fileBase = file.base;
}
var readPromise = streamToBuffer(options.createReadStream(absoluteFileName))
.then(function(contents) {
files[index] = new File({
cwd: file.cwd,
base: file.base,
base: fileBase,
path: absoluteFileName,
contents: contents
});
Expand Down Expand Up @@ -169,6 +186,6 @@ module.exports = function(options) {
});
}
};

return through.obj(transform);
}
Loading