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

Possible to dynamically inject files depends on the name of file to be injected ? #80

Open
liuwenchao opened this issue Jan 12, 2015 · 12 comments

Comments

@liuwenchao
Copy link

I have a huge HTML file, with many files to inject, but it seems not DRY to do this:

var myTransform =  function (filepath, file) {
  return file.contents.toString('utf8');
};
gulp.src('target.html')
  .pipe(inject(gulp.src('file1.html'),  { starttag: '<!-- inject:file1:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file2.html'),  { starttag: '<!-- inject:file2:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file3.html'),  { starttag: '<!-- inject:file3:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file4.html'),  { starttag: '<!-- inject:file4:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file5.html'),  { starttag: '<!-- inject:file5:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file6.html'),  { starttag: '<!-- inject:file6:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file7.html'),  { starttag: '<!-- inject:file7:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file8.html'),  { starttag: '<!-- inject:file8:html} -->'), transform: myTransform))
  .pipe(inject(gulp.src('file9.html'),  { starttag: '<!-- inject:file9:html} -->'), transform: myTransform))
;

Any way to eliminate the duplication? Thanks!

@liuwenchao liuwenchao changed the title Possible to dynamically inject files depends on {{name}} ? Possible to dynamically inject files depends on the name of file to be injected ? Jan 12, 2015
@liuwenchao
Copy link
Author

@joakimbeng what do you think?

@joakimbeng
Copy link
Member

@liuwenchao why don't you just do this:

var myTransform =  function (filepath, file) {
  return file.contents.toString('utf8');
};
gulp.src('target.html')
  .pipe(inject(gulp.src('file*.html'),  { starttag: '<!--inject:html} -->'), transform: myTransform))
;

But please provide an example of how your target.html looks like, it's easier to give a correct solution for your problem then.

N.B. If you're trying to inject all the files in different places (one file per tag) in the target file I think gulp-inject is the wrong tool for the job you should use a template engine for that instead.

@liuwenchao
Copy link
Author

@joakimbeng yes they're injected in different places. The target.html looks like this:

<html>
<head>
    <!-- inject:empty:html -->
    <script src="/bower_components/webcomponentsjs/webcomponents.min.js"></script>
    <link rel="import" href="/bower_components/aha-partial/src/aha-partial.html">
    <!-- endinject -->
</head>
<body>

    <!-- inject:header:html -->
    <aha-partial src="partials/header.html"></aha-partial>
    <!-- endinject -->

    <div class="container">
        <ul class="nav nav-tabs">
            <li class="active">
                <a href="#tab1" data-toggle="tab">Tab 1</a>
            </li>
            <li>
                <a href="#tab2" data-toggle="tab">Tab 2</a>
            </li>
            <li>
                <a href="#tab3" data-toggle="tab">Tab 3</a>
            </li>
            <li>
                <a href="#tab4" data-toggle="tab">Tab 4</a>
            </li>
            <li>
                <a href="#tab5" data-toggle="tab">Tab 5</a>
            </li>
        </ul>

        <div class="tab-content">
            <div class="tab-pane" id="tab1">
                <!-- inject:tab1:html -->
                <aha-partial src="partials/tab1.html"></aha-partial>
                <!-- endinject -->
            </div>
            <div class="tab-pane" id="tab2">
                <!-- inject:tab2:html -->
                <aha-partial src="partials/tab2.html"></aha-partial>
                <!-- endinject -->
            </div>
            <div class="tab-pane" id="tab3">
                <!-- inject:tab3:html -->
                <aha-partial src="partials/tab3.html"></aha-partial>
                <!-- endinject -->
            </div>
            <div class="tab-pane" id="tab4">
                <!-- inject:tab4:html -->
                <aha-partial src="partials/tab4.html"></aha-partial>
                <!-- endinject -->
            </div>
            <div class="tab-pane" id="tab5">
                <!-- inject:tab5:html -->
                <aha-partial src="partials/tab5.html"></aha-partial>
                <!-- endinject -->
            </div>
        </div>
    </div>

    <!-- inject:footer:html -->
    <aha-partial src="partials/footer.html"></aha-partial>
    <!-- endinject -->
</body>
</html>

The real file is more complicated than this, this is a simplified version.

I'm using a custom element in development environment, it's kind of a template engine, I need to use a HTML solution here; and in production environment, it will be replaced by one complete HTML w/o custom element.

@tuespetre
Copy link

+1 on this. I'm trying to inject minified HTML templates into specific Javascript string variables.

@joakimbeng
Copy link
Member

Actually I would recommend using a real template engine for this... Though I will accept #109 when it has some tests which solves this.

@JustMaier
Copy link

+1 here as well. Would be great if we could do something like:

.pipe(inject(gulp.src(['injects/*.html']), {
    starttag: '<!-- inject:{{filename}}:{{ext}} -->',
    transform: function (filePath, file) {
        return file.contents.toString('utf8')
    }
}))

@khudia
Copy link

khudia commented Aug 30, 2015

i found the way to insert template file i need in shortcode))
the only way i don't like is filenames to be used to work correct.
so if there was file header.html => rename it to html.header
=))

gulp.task('tpl', function () {
gulp.src('dev/index.html')
.pipe(inject(gulp.src(['dev/templates/tpl.*']), {
starttag: '',
transform: function (filePath, file) {
return file.contents.toString('utf8');
}})).pipe(gulp.dest('dev'));
});

But i wish can use as described before

@tuespetre
Copy link

I have found another plugin recently that better suits this need: gulp-file-insert

On Aug 30, 2015, at 2:06 PM, Georgy Khudiakov [email protected] wrote:

i found the way to insert template file i need in shordcode))
the only way i don't like is filenames to be used to work correct.
so if there was file header.html => rename it to html.header
=))
But i wish can use as described before

gulp.task('tpl', function () {
gulp.src('dev/index.html')
.pipe(inject(gulp.src(['dev/templates/tpl.*']), {
starttag: '',
transform: function (filePath, file) {
return file.contents.toString('utf8');
}
}))

.pipe(gulp.dest('dev'));
});


Reply to this email directly or view it on GitHub.

@zhe
Copy link

zhe commented Oct 31, 2015

Same here. I really prefer @JustMaier 's way which includes a {{filename}} option:

.pipe(inject(gulp.src(['injects/*.html']), {
    starttag: '<!-- inject:{{filename}}:{{ext}} -->',
    transform: function (filePath, file) {
        return file.contents.toString('utf8')
    }
}))

@idefy
Copy link

idefy commented Nov 19, 2015

Although @JustMaier solution is simpler, it is limited to a single file. For large application, you may want to manage by folders or subfolders, or some other way even.
In the pull request I made, you get the full tagname as a parameter in the transform method, this gives you all the flexibility you need to apply your business rules as desired: no constraints.

@dsozzi
Copy link

dsozzi commented Jul 4, 2016

+1

@adumat
Copy link

adumat commented Jul 6, 2016

a solution that i've made for my project is this:

gulp.task('inject', () => {
  var stream = gulp.src(config.src)
    .pipe(inject(gulp.src(bowerFiles(), {read: false}), {name: 'bower',relative: true,ignorePath: '../', quiet: true}));
  gulp.src(config.assets, {read: false})
    .pipe(named())
    .pipe(through2.obj(function (file, enc, cb) {
      console.log('creating pipe for ' + file.named + ' with ext ' + path.extname(file.path).replace('.', ''));
      console.log(file.path);
      stream = stream.pipe(inject(gulp.src(file.path, {read: false}), {starttag: '<!-- inject:' + file.named + ':' + path.extname(file.path).replace('.', '') + ' -->',relative: true,ignorePath: '../', quiet: true}));
      this.push(file);
      cb();
    }));
  return stream.pipe(gulp.dest(config.dest));
});

but i'm waiting a better solution without create a through pipe like those provided in this thread....
then 👍 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants