gulp-inline-source
gulp-inline-source copied to clipboard
Write inline js in gulpfile.js instead of external file
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>
Yes we want that! ;-P I will use that for my BasketJS unique id
I think to resolve it you can use:
-
gulp-replace
to simply replace script with whatever you want - Use 'handlers' option to modify source in build time as you like (babel or any custom transformation)
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));
}