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

Write inline js in gulpfile.js instead of external file #21

Open
leocaseiro opened this issue Sep 24, 2015 · 3 comments
Open

Write inline js in gulpfile.js instead of external file #21

leocaseiro opened this issue Sep 24, 2015 · 3 comments

Comments

@leocaseiro
Copy link

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>
@nivida
Copy link

nivida commented Nov 10, 2016

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

@klimashkin
Copy link

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)

@bno1
Copy link

bno1 commented May 28, 2020

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));
}

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

4 participants