Last active
May 6, 2024 05:27
-
-
Save mshwery/4b7a7cdd0a44babff2ff to your computer and use it in GitHub Desktop.
Gulp + Browserify + requiring .html templates + Knockout web components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ko = require('knockout'); | |
ko.components.register('simple-name', require('./components/simple-name/simple-name.js')); | |
ko.applyBindings({ userName: ko.observable() }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'), | |
browserify = require('browserify'), | |
stringify = require('stringify'), | |
source = require('vinyl-source-stream'); | |
gulp.task('browserify', function() { | |
var bundleMethod = browserify;//global.isWatching ? watchify : browserify; | |
var bundler = bundleMethod({ | |
// Specify the entry point of your app | |
entries: ['./src/app.js'] | |
}); | |
var bundle = function() { | |
return bundler | |
.transform(stringify(['.html'])) | |
// Enable source maps! | |
.bundle({debug: true}) | |
// Use vinyl-source-stream to make the | |
// stream gulp compatible. Specifiy the | |
// desired output filename here. | |
.pipe(source('app.js')) | |
// Specify the output destination | |
.pipe(gulp.dest('./build/')); | |
}; | |
return bundle(); | |
}); | |
gulp.task('default', ['browserify']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head></head> | |
<body> | |
<input data-bind="value: userName"/> | |
<!-- using our custom element --> | |
<simple-name params="name: userName"></simple-name> | |
<script src="knockout.js"></script> | |
<script src="app.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Project Name", | |
"version": "0.0.0", | |
"browserify": { | |
"transform": [ | |
"browserify-global-shim" | |
] | |
}, | |
"browserify-global-shim": { | |
"jQuery": "$", | |
"knockout": "ko" | |
}, | |
"devDependencies": { | |
"gulp": "~3.6.2", | |
"browserify": "^4.2.1", | |
"vinyl-source-stream": "^0.1.1", | |
"stringify": "^2.2.0", | |
"browserify-global-shim": "^1.0.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p data-bind='text: name'></p> | |
<button data-bind='click: something'>Click me</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ko = require('knockout'), | |
template = require('./simple-name.html'); | |
function viewModel(data) { | |
this.name = data.name; | |
} | |
viewModel.prototype.something = function() { | |
console.log('You invoked something() on the viewmodel.'); | |
}; | |
module.exports = { | |
viewModel: viewModel, | |
template: template | |
}; |
thank you!
👍 this is awesome, thanks from me as well
This is amazing, I was about to write my own library to convert this stuff over to a .js file. Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I'm currently trying to get browserify + ko working and this was a giant help.