{"id":19346522,"url":"https://github.com/canjs/can-compile","last_synced_at":"2025-12-18T00:55:55.864Z","repository":{"id":5705588,"uuid":"6916333","full_name":"canjs/can-compile","owner":"canjs","description":"Compile CanJS Mustache and EJS views for lightning fast production apps","archived":false,"fork":false,"pushed_at":"2020-05-23T12:11:03.000Z","size":732,"stargazers_count":35,"open_issues_count":19,"forks_count":15,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-08-09T22:53:04.291Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://daffl.github.com/can-compile","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/canjs.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-11-29T06:26:11.000Z","updated_at":"2023-08-13T16:10:17.000Z","dependencies_parsed_at":"2022-08-30T10:30:32.336Z","dependency_job_id":null,"html_url":"https://github.com/canjs/can-compile","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-compile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-compile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-compile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-compile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/canjs","download_url":"https://codeload.github.com/canjs/can-compile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223909970,"owners_count":17223592,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T04:10:44.843Z","updated_at":"2025-12-18T00:55:55.796Z","avatar_url":"https://github.com/canjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# can-compile\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/canjs/can-compile.svg)](https://greenkeeper.io/)\n\n[![Build Status](https://travis-ci.org/canjs/can-compile.png?branch=master)](https://travis-ci.org/canjs/can-compile)\n\nNodeJS module that compiles [CanJS](http://canjs.us/) EJS and Mustache views into a single JavaScript file for lightning fast\nproduction apps.\n\nWith NodeJS installed, just run NPM:\n\n\u003e npm install can-compile -g\n\n## Command line\n\nThe `can-compile` command line tool takes a list of files (by default all `*.ejs` and `*.mustache` files in the current folder)\nor a list of [filename patterns](https://github.com/isaacs/minimatch) and writes the compiled views into an `out` file\n(default: `views.production.js`).\n\n__Examples:__\n\nCompile all EJS and Mustache files in the current folder and write them to `views.combined.js` using version 2.1.0:\n\n\u003e can-compile --out views.combined.js --can 2.1.0\n\nCompile `todo.ejs` using CanJS version 1.1.2, write it to `views.production.js`:\n\n\u003e can-compile todo.ejs --can 1.1.2\n\nCompile all EJS files in the current directory and all subdirectories and `mustache/test.mustache`.\nWrite the result to `views.combined.js`:\n\n\u003e can-compile **/*.ejs mustache/test.mustache --out views.combined.js --can 2.0.0\n\n\n## Grunt task\n\ncan-compile also comes with a [Grunt](http://gruntjs.com) task so you can easily make it part of your production build.\nJust `npm install can-compile` in you project folder (or add it as a development dependency).\nThe following example shows a Gruntfile that compiles all Mustache views and then builds a concatenated and minified `production.js`\nof a CanJS application:\n\n```javascript\nmodule.exports = function (grunt) {\n\n  // Project configuration.\n  grunt.initConfig({\n    cancompile: {\n      options: {\n        version: '2.1.1'\n      },\n      dist: {\n        options: {\n          wrapper: '!function() { {{{content}}} }();',\n          tags: ['editor', 'my-component']\n        },\n        src: ['**/*.ejs', '**/*.mustache'],\n        dest: 'production/views.production.js'\n      },\n      legacy: {\n        src: ['**/*.ejs', '**/*.mustache'],\n        dest: 'production/views.production.js',\n        options: {\n          version: '1.1.2'\n        }\n      }\n    },\n    concat: {\n      dist: {\n        src: [\n          '../resources/js/can.jquery.js',\n          '../resources/js/can.view.mustache.js',\n          'js/app.js', // You app\n          '\u003c%= cancompile.dist.dest %\u003e' // The compiled views\n        ],\n        dest: 'production/production.js'\n      }\n    },\n    uglify: {\n      dist: {\n        files: {\n          'production/production.min.js': ['\u003c%= concat.dist.dest %\u003e']\n        }\n      }\n    }\n  });\n\n  // Default task.\n  grunt.registerTask('default', ['cancompile', 'concat', 'uglify']);\n\n  grunt.loadNpmTasks('can-compile');\n  grunt.loadNpmTasks('grunt-contrib-uglify');\n  grunt.loadNpmTasks('grunt-contrib-concat');\n};\n```\n\n## Gulp task\n\nIt is also quite easy to get this up and running with your production build using Gulp.  By placing the following example in your gulpfile.js, all mustache templates in the client/app directory will be compiled into a file at `public/assets/views.production.js`.\n\n```javascript\nvar gulp = require('gulp');\nvar compilerGulp = require('can-compile/gulp.js');\n\nvar options = {\n\tversion: '2.2.7'\n};\n\ngulp.task(\"app-views\", function () {\n\treturn gulp.src('client/app/**/*.mustache')\n\t\t.pipe( compilerGulp('views.production.js', options) )\n\t\t.pipe( gulp.dest('public/assets') );\n});\n```\n**Or you can use available helpers to create your task and watch your files:**\n\n```javascript\nvar gulp = require('gulp');\nvar compilerGulp = require('can-compile/gulp.js');\n\nvar options = {\n  version: '2.2.7',\n  src: ['client/app/**/*.mustache'],\n  out: 'public/assets/views.production.js',\n  tags: ['editor', 'my-component']\n};\n\n// Creates a task called 'app-views'.\n// You must pass in the same gulp instance.\n// You may also pass optional task dependencies (ex. 'clean')\ncompilerGulp.task('app-views', options, gulp, ['clean']);\n\n// Creates a task called 'app-views-watch'. Optional, but convenient.\ncompilerGulp.watch('app-views', options, gulp);\n\n// The default task (called when you run `gulp` from cli)\ngulp.task('default', [\n    'app-views',\n    'app-views-watch'\n]);\n```\n\nYou'll need gulp installed globally and locally:\n\n    sudo npm install gulp -g\n    npm install gulp\n\nAnd a local copy of can-compile\n\n    npm install can-compile\n\nRun `gulp` in the command line to build.\n\n## Programmatically\n\nYou can compile individual files directly like this:\n\n```javascript\nvar compiler = require('can-compile');\nvar options = {\n  filename: 'file.ejs',\n  version: '2.0.1'\n};\n\ncompiler.compile(options, function(error, output) {\n  output // -\u003e compiled `file.ejs`\n});\n```\n\nThe options object allows the following configuration options:\n\n- `filename` {String}: The name of the file to be compiled\n- `version` {String}: The CanJS version to be used\n- `log` {Function}: A logger function (e..g `console.log.bind(console)`)\n- `normalizer` {Function}: A Function that returns the normalized path name\n- `tags` {Array}: A list of all your can.Component tags. They need to be registered in order to pre-compile views properly.\n- `extensions` {Object}: An object to map custom file extensions to the standard extension (e.g. `{ 'mst' : 'mustache' }`)\n- `viewAttributes` {Array}: A list of attribute names (RegExp or String), used for additional behavior for an attribute in a view (can.view.attr)\n- `paths` an object with `ejs`, `mustache` or `stache` and a `jquery` property pointing to files of existing versions or CanJS and jQuery instead of the CDN links.\n\n```javascript\ncompiler.compile({\n  filename: 'file.ejs',\n  log: console.log.bind(console),\n  normalizer: function(filename) {\n    return path.relative(__dirname, filename);\n  },\n  version: '2.0.7'\n}, function(error, output) {\n  output // -\u003e compiled `file.ejs`\n});\n```\n\n## Loading with RequireJS\n\nTo use your pre-compile views with [RequireJS](http://requirejs.org/) just add a custom `wrapper` in the options\nthat uses the AMD definition to load `can/view/mustache` and/or `can/view/ejs` (depending on what you are using).\nIn a Grunt task:\n\nMustache:\n\n```javascript\nmodule.exports = function (grunt) {\n  // Project configuration.\n  grunt.initConfig({\n    options: {\n      wrapper: 'define([\"can/view/mustache\"], function(can) { {{{content}}} });'\n    },\n    cancompile: {\n      dist: {\n        src: ['**/*.mustache', '!node_modules/**/*.mustache'],\n        dest: 'production/views.production.js',\n      }\n    }\n  });\n}\n```\n\nStache:\n\n```javascript\nmodule.exports = function (grunt) {\n  // Project configuration.\n  grunt.initConfig({\n    options: {\n      wrapper: 'define([\"can\", \"can/view/stache\"], function(can) { {{{content}}} });'\n    },\n    cancompile: {\n      dist: {\n        src: ['**/*.stache', '!node_modules/**/*.stache'],\n        dest: 'production/views.production.js',\n      }\n    }\n  });\n}\n```\n\nTo load the generated files only when running the [RequireJS optimizer r.js](http://requirejs.org/docs/optimization.html)\ndefine an empty module in development like:\n\n```js\ndefine('views', function() {});\n```\n\nAnd `require('views');` in your main application file.\n\nWhen running the optimizer map this module to the production build file:\n\n```js\npaths: {\n  views: 'views.production'\n}\n```\n\n## Note\n\nAlways make sure that the output file is in the same folder as the root level for the views that are being loaded.\nSo if your CanJS applications HTML file is in the `app` folder within the current directory use a filename within\nthat folder as the output file:\n\n\u003e can-compile --out app/views.production.js --can 2.0.0\n\n## Changelog\n\n__0.11.0:__\n\n- Point to the rehomed CanJS 1.x and 2.x libraries (on v2.canjs.com)\n- Select correct Mustache path based on 1.x or 2.x (was renamed to can.view.mustache.system.js in 2.x)\n\n__0.10.0:__\n\n- Move to can-simple-dom instead of JSDOM ([#45](https://github.com/canjs/can-compile/pull/45))\n- Make Gulp task pipeable ([#46](https://github.com/canjs/can-compile/pull/46))\n\n__0.9.0:__\n\n- Allowing user to supply paths for jquery and can ([#36](https://github.com/daffl/can-compile/pull/36), [#30](https://github.com/daffl/can-compile/issues/30))\n\n__0.8.0:__\n\n- Fixed path normalize on Windows, Alternative file extension, can.view.attr ([#32](https://github.com/daffl/can-compile/pull/32))\n\n__0.7.1:__\n\n- Gulp task - Error: Cannot find module 'glob' ([#27](https://github.com/daffl/can-compile/issues/27))\n- Also use [glob](https://github.com/isaacs/node-glob) module in CLI\n\n__0.7.0:__\n\n- Added up to date TodoMVC based [example](https://github.com/daffl/can-compile/tree/master/example)\n- Made `version` flag mandatory (caused unexpected behaviour after CanJS updates)\n- Added CanJS 2.1 compatibility ([#20](https://github.com/daffl/can-compile/issues/20), [#21](https://github.com/daffl/can-compile/issues/21), [#22](https://github.com/daffl/can-compile/issues/22))\n- Fixed Grunt options ([#8](https://github.com/daffl/can-compile/pull/8))\n\n__0.6.0:__\n\n- Added Gulp task ([#16](https://github.com/daffl/can-compile/pull/16), [#17](https://github.com/daffl/can-compile/pull/17), [#18](https://github.com/daffl/can-compile/pull/18))\n\n__0.5.0:__\n\n- Merged [#11](https://github.com/daffl/can-compile/pull/11): Implement can.Component/tag-support\n\n__0.4.1:__\n\n- Merged [#10](https://github.com/daffl/can-compile/pull/10): Allow for setting a custom normalizer\n\n__0.4.0:__\n\n- Verify CanJS 2.0.0 compatbility, load can.EJS which isn't in the core anymore\n\n__0.3.2:__\n\n- Custom `wrapper` option uses [Handlebars](http://handlebarsjs.com/) because Underscore templates are useless in Grunt files\n\n__0.3.1:__\n\n- Adds a custom `wrapper` option (uses [_.template](http://underscorejs.org/#template)).\n\n__0.3.0:__\n\n- Allows compilation for different CanJS versions\n\n__0.2.1:__\n\n- Switched to plain JSDom\n- Update to CanJS 1.1.5\n- Verified Node 0.10 compatibility\n\n__0.2.0:__\n\n- Grunt 0.4.0 compatibility\n- Added Travis CI\n\n__0.1.0:__\n\n- Initial release\n\n## License\n\nCopyright (C) 2014 David Luecke daff@neyeon.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanjs%2Fcan-compile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcanjs%2Fcan-compile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanjs%2Fcan-compile/lists"}