gulp-inline-source icon indicating copy to clipboard operation
gulp-inline-source copied to clipboard

Write inline js in gulpfile.js instead of external file

Open leocaseiro opened this issue 9 years ago • 3 comments

Hi, thanks for your plugin. It's really good.

That would be nice if we could write our scripts at the gulpfile.js without external file. Very useful for some apps that need domain for ajax.

Eq. gulpfile.js:

gulp.task('inlinesource', function () {
    var options = {
        string: 'var ajaxDomain = "http://localhost";'
    };

    return gulp.src('./src/*.html')
        .pipe(inlinesource(options))
        .pipe(gulp.dest('./out'));
});

Would output:

<html>
  <head>
    <script>var ajaxDomain = "http://localhost";</script>
  </head>
  <body>
  </body>
</html>

leocaseiro avatar Sep 24 '15 05:09 leocaseiro

Yes we want that! ;-P I will use that for my BasketJS unique id

nivida avatar Nov 10 '16 09:11 nivida

I think to resolve it you can use:

  1. gulp-replace to simply replace script with whatever you want
  2. Use 'handlers' option to modify source in build time as you like (babel or any custom transformation)

klimashkin avatar Nov 11 '16 21:11 klimashkin

I just implemented something like this. In my code the pipeline inside tsLoader can be whatever you want as long as it produces a single buffer-type vinyl.

const { src, dest } = require('gulp');
const tsify = require('tsify');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const inlinesource = require('gulp-inline-source');

function tsLoader(sourceFile) {
  return new Promise((resolve, reject) => {
    browserify([sourceFile.filepath])
      .plugin(tsify)
      .bundle()
      .on('error', (error) => {
        console.log(error.toString());
        return reject(error);
      })
      .pipe(source())
      .pipe(buffer())
      .on('error', (error) => {
        content.log(error.toString());
        return reject(error);
      })
      .on('data', (file) => {
        if (file.isBuffer()) {
          sourceFile.content = file.contents;
          return resolve();
        } else {
          return reject('Unsupported record type');
        }
      });
  });
}

function build() {
  return src(htmlFile)
    .pipe(inlinesource({
      attribute: false,
      handlers: [
        (source, context) => {
          if (source.format === 'ts') {
            return tsLoader(source);
          }
        }
      ],
    }))
    .pipe(dest(builddir));
}

bno1 avatar May 28 '20 17:05 bno1