From 60e877af179362b0167e30fe21828a9d413542ab Mon Sep 17 00:00:00 2001 From: Akim McMath Date: Wed, 8 Jun 2016 16:46:52 -0700 Subject: [PATCH] v2.0.0 Breaking Changes - EJS is now a peer dependency, and must be installed separately - Options may not be passed via the `ejsHtmlLoader` property; only `ejsHtml` is accepted Development - Source files are in ES2015 and compiled with Babel - Test files are in CoffeeScript - ESLint is used for linting, replacing JSHint and JSCS - Set up automatic deployment to NPM from Travis --- .babelrc | 8 ++ .eslintrc.yml | 7 ++ .gitignore | 16 ++-- .jscsrc | 7 -- .jshintrc | 5 -- .npmignore | 8 ++ .travis.yml | 26 ++++-- CHANGELOG.md | 76 ---------------- LICENSE.txt => LICENSE | 0 README.md | 32 +++---- index.js | 3 - lib/dependencies.js | 28 ------ lib/ejs-html-loader.js | 37 -------- package.json | 49 +++++----- src/cache.js | 24 +++++ src/index.js | 31 +++++++ src/render.js | 14 +++ test/config/paths.coffee | 11 +++ test/config/paths.js | 14 --- .../{bad-locals/index.ejs => bad-locals.ejs} | 4 +- test/fixtures/bad-locals.js | 1 + test/fixtures/bad-locals/entry.js | 1 - test/fixtures/bad-locals/webpack.config.js | 8 -- .../{bad-syntax/index.ejs => bad-syntax.ejs} | 2 +- test/fixtures/bad-syntax.js | 1 + test/fixtures/bad-syntax/entry.js | 1 - test/fixtures/bad-syntax/webpack.config.js | 10 --- test/fixtures/configure.js | 41 --------- .../{context/index.ejs => context.ejs} | 2 +- test/fixtures/context.js | 1 + test/fixtures/context/entry.js | 1 - test/fixtures/context/webpack.config.js | 12 --- .../{delimiter/index.ejs => delimiter.ejs} | 2 +- test/fixtures/delimiter.js | 1 + test/fixtures/delimiter/entry.js | 1 - test/fixtures/delimiter/webpack.config.js | 11 --- test/fixtures/include-partial.ejs | 1 + test/fixtures/include.ejs | 11 +++ test/fixtures/include.js | 1 + test/fixtures/index.js | 11 --- test/fixtures/query-string/entry.js | 1 - test/fixtures/query-string/index.ejs | 10 --- test/fixtures/query-string/webpack.config.js | 8 -- .../fixtures/{simple/index.ejs => simple.ejs} | 0 test/fixtures/simple.js | 1 + test/fixtures/simple/entry.js | 1 - test/fixtures/simple/webpack.config.js | 10 --- test/fixtures/with-include/entry.js | 1 - test/fixtures/with-include/index.ejs | 10 --- test/fixtures/with-include/partial.ejs | 1 - test/fixtures/with-include/webpack.config.js | 10 --- test/helpers/compile.coffee | 17 ++++ test/helpers/config.coffee | 20 +++++ test/helpers/error.coffee | 12 +++ test/index.coffee | 75 ++++++++++++++++ test/index.test.js | 90 ------------------- test/mocha.opts | 3 + 57 files changed, 315 insertions(+), 475 deletions(-) create mode 100644 .babelrc create mode 100644 .eslintrc.yml delete mode 100644 .jscsrc delete mode 100644 .jshintrc create mode 100644 .npmignore delete mode 100644 CHANGELOG.md rename LICENSE.txt => LICENSE (100%) delete mode 100644 index.js delete mode 100644 lib/dependencies.js delete mode 100644 lib/ejs-html-loader.js create mode 100644 src/cache.js create mode 100644 src/index.js create mode 100644 src/render.js create mode 100644 test/config/paths.coffee delete mode 100644 test/config/paths.js rename test/fixtures/{bad-locals/index.ejs => bad-locals.ejs} (60%) create mode 100644 test/fixtures/bad-locals.js delete mode 100644 test/fixtures/bad-locals/entry.js delete mode 100644 test/fixtures/bad-locals/webpack.config.js rename test/fixtures/{bad-syntax/index.ejs => bad-syntax.ejs} (81%) create mode 100644 test/fixtures/bad-syntax.js delete mode 100644 test/fixtures/bad-syntax/entry.js delete mode 100644 test/fixtures/bad-syntax/webpack.config.js delete mode 100644 test/fixtures/configure.js rename test/fixtures/{context/index.ejs => context.ejs} (83%) create mode 100644 test/fixtures/context.js delete mode 100644 test/fixtures/context/entry.js delete mode 100644 test/fixtures/context/webpack.config.js rename test/fixtures/{delimiter/index.ejs => delimiter.ejs} (81%) create mode 100644 test/fixtures/delimiter.js delete mode 100644 test/fixtures/delimiter/entry.js delete mode 100644 test/fixtures/delimiter/webpack.config.js create mode 100644 test/fixtures/include-partial.ejs create mode 100644 test/fixtures/include.ejs create mode 100644 test/fixtures/include.js delete mode 100644 test/fixtures/index.js delete mode 100644 test/fixtures/query-string/entry.js delete mode 100644 test/fixtures/query-string/index.ejs delete mode 100644 test/fixtures/query-string/webpack.config.js rename test/fixtures/{simple/index.ejs => simple.ejs} (100%) create mode 100644 test/fixtures/simple.js delete mode 100644 test/fixtures/simple/entry.js delete mode 100644 test/fixtures/simple/webpack.config.js delete mode 100644 test/fixtures/with-include/entry.js delete mode 100644 test/fixtures/with-include/index.ejs delete mode 100644 test/fixtures/with-include/partial.ejs delete mode 100644 test/fixtures/with-include/webpack.config.js create mode 100644 test/helpers/compile.coffee create mode 100644 test/helpers/config.coffee create mode 100644 test/helpers/error.coffee create mode 100644 test/index.coffee delete mode 100644 test/index.test.js create mode 100644 test/mocha.opts diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..aef4ffa --- /dev/null +++ b/.babelrc @@ -0,0 +1,8 @@ +{ + "presets": [ + "es2015" + ], + "plugins": [ + "add-module-exports" + ] +} diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..dca915d --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,7 @@ +extends: eslint:recommended +parserOptions: + ecmaVersion: 6 + sourceType: module +env: + node: true + mocha: true diff --git a/.gitignore b/.gitignore index 31aedfb..b3e01e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,5 @@ -.DS_Store -logs -*.log -pids -*.pid -*.seed -lib-cov -coverage -.lock-wscript -npm-debug.log* -node_modules +/coverage/ +/lib/ +/node_modules/ +/npm-debug.log +.tmp diff --git a/.jscsrc b/.jscsrc deleted file mode 100644 index e6e46bd..0000000 --- a/.jscsrc +++ /dev/null @@ -1,7 +0,0 @@ -{ - "preset": "google", - "excludeFiles": [ - "coverage", - "node_modules" - ] -} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 75f8600..0000000 --- a/.jshintrc +++ /dev/null @@ -1,5 +0,0 @@ -{ - "node": true, - "mocha": true, - "expr": true -} diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..d80ec38 --- /dev/null +++ b/.npmignore @@ -0,0 +1,8 @@ +/coverage/ +/src/ +/test/ +/.babelrc +/.editorconfig +/.eslintrc.yml +/.gitignore +/.travis.yml diff --git a/.travis.yml b/.travis.yml index 9b902c2..dbe60e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,24 @@ language: node_js node_js: - - "stable" - - "5.0" - - "4.0" + - "6" + - "5" + - "4" - "0.12" - "0.10" -after_script: +after_success: - npm run coveralls -branches: - only: - - master +before_deploy: + - npm run build +deploy: + provider: npm + email: + secure: E1X0Di6u1dhOXAvEsGXTTMZzz0Cx7RqNS9O4U5Tgdqlqaq9maDoHz8rZNHwqcx97m6DkgH4/go+D22DARLPr7Y+5XJNBrFcDKWPbU34lBHsTjwhxnuoKS8h7VZ/lGvQqPp1ircyYvsG5rd97J4z0hI27gF3Sj9tk8W3bn3ZTc8JxIoEw00aiEC7KNNnDpOosWkHv+SiVASo5Fk9Iwdt+k8FRtmrfmjVb/K1qp8e4lkjN8sXtxLoAYqzuOt5TUpTFDzGjlY4KS42apJxq0cKTvPXsU38Wn4QE848E4urJAIHKLR8N45wapD54FxlvS4FKjWUCpscVf8vDAGPXPbFK0uireqlI1RdHZYhEKeyR0vnIC62lQbYd9GKrh26q9TVeGniKXfIpNC/j5gp6dexYkKqNjmWE5K1CgTplgLq5ufmZArkeS+hKrSGcdGYcfib5hGM3gEfisK0Mvm0/rRposChu3UwUh9IlEblKhGbsynrs8Ee8zSerrTPnuaFuJycDO7m+SpxBe5ujf7W4/Dhh8etCsNIG0f9xxlVSfwgeeLONs1z1i94g7gGHR4n1JTW4/xLhyJuE4jsvEm23y4M1nDwEOCiPw1oJvCASXxxKN5/ydQ0wJsV6t7R0RFwDQSbOA4FYlYsWQQzmqgLJ8BES2JgAEuFhCEHpT/K4lVZl9oA= + api_key: + secure: YGkGJVV8Wx86+4fy8k88vLBku/JU7402q+fFS7VFLa1Lt017N9qsIasJgSiUWBsmRbp6L8FqT77yv7BZsyn3swdeFk/Uz8Le3lKQgANFAy7JQj/4MaceZsGxvuOU1+PHsFMqZDlIQpTzkaiPN6bVCj/nrxspPfTEiaDThsNPy/BNXjoPMnwvg8PuRgotJSHEx1vWlemnLql69RnfoWO0dqO1RY8To8wNjPJVQtMsOGgdDbg1Gi6FAdp3YltVbCIl9XSn0ibjo+sEPfHEhToPszlbyExm4GelocK3qCFowTE6YMO/PHNXq12oPI/mOB0VESyNeawnT6MYQYI+ClgWS8KXGaZVUuOJ3s9nkPODMjTBmx7TElPWBmNNtutOI12VxTmXKs+gKynJuEf7T9xIgunIuzDOFZR/OflT1qySQoVGeYBJCjYeG2IA0hIVcnVHmYBybVuw6ut5A1N5fORxVS+yToJRfR8IrU6dmuhd61nffoAPJ81TzOvcGIldKuN0g6wfc40EaJQr+QZgUvBPp72jffajP0DviQ3RBx0y3ktikqvetm0TuXc9FzLEkjBKJY4r2BKahTcqv9YyPI4Ui9vNvjaD2VG4Qa7HtsUNi342/uaVnRMf24/VI/UOkhgI8e2DDKH35RucLK0sxrEEUfTwqNM2jhBuNQHA+BDpyAg= + skip_cleanup: true + on: + repo: akim-mcmath/ejs-html-loader + node: "4" + tags: true +after_deploy: + - npm run unbuild diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 811577d..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,76 +0,0 @@ -# v1.2.2 - -###### March 27, 2016 - -#### Fixes - -* Fix `Error.captureStackTrace()` to correct constructor argument. - -# v1.2.1 - -###### March 27, 2016 - -#### Fixes - -* Make sure included files maintain their dependencies on error by adding - a caching mechanism. - -#### Maintenance - -* Divide loader into multiple files under a new `lib/` directory. - -# v1.2.0 - -###### March 14, 2016 - -#### Features - -* Included files are now added to the loader [dependencies][mark-dependencies], -ensuring automatic recompilation in watch mode - -#### Development Features - -* Update tests in light of the above changes - -#### Maintenance - -* Update [README][readme] with new "Includes" section - -# v1.1.0 - -###### February 12, 2016 - -#### Minor Features - -* Allow loader options to be set with `ejsHtmlLoader` property - -#### Maintenance - -* Update [README.md][readme] to reflect above change - -#### Development Maintenance - -* Disallow multiple line breaks -* Remove multiple line breaks from [index.js][index] and tests - -# v1.0.4 - -###### February 10, 2016 - -#### Maintenance - -* Minor fixes to [README.md][readme]. -* Minor code style fixes in [index.js][index]. - -#### Development Features - -* Add [CHANGELOG.md][changelog]. -* Add [JSHint][jshint] as a development dependency. -* Add [JSCS][jscs] code-style support. - -[changelog]: CHANGELOG.md -[index]: index.js -[readme]: README.md -[jscs]: http://jscs.info/ -[jshint]: http://jshint.com/about/ -[mark-dependencies]: http://webpack.github.io/docs/how-to-write-a-loader.html#mark-dependencies diff --git a/LICENSE.txt b/LICENSE similarity index 100% rename from LICENSE.txt rename to LICENSE diff --git a/README.md b/README.md index d842a0e..5e6f45e 100644 --- a/README.md +++ b/README.md @@ -6,43 +6,38 @@ [![Coverage][coverage-badge]][coveralls] [![Dependencies][dependencies-badge]][gemnasium] -A [Webpack][webpack] loader for rendering plain HTML from [EJS][ejs] -template files. +[Webpack][webpack] loader for rendering plain HTML from [EJS][ejs] +template files ## Install -Install via [npm][npm]: +Install with [npm][npm]. And be sure to install [EJS][ejs] and +[Webpack][webpack], which are peer dependencies. ```sh -npm install --save-dev ejs-html-loader +npm install --save-dev webpack ejs ejs-html-loader ``` ## Usage -Options and locals may be set via query string or query object: +In your Webpack [configuration][webpack-configuration], options and locals can +be passed to EJS in two ways. First, through a query string or query object: ```js -// webpack.config.js - module.exports = { // ... module: { - loaders: [ - { - test: /\.ejs$/, - loader: 'ejs-html?title=The%20Big%20Gatsby&production' - } - ] + loaders: [{ + test: /\.ejs$/, + loader: 'ejs-html?title=The%20Big%20Gatsby&production' + }] } }; ``` -Or they may be set in the `ejsHtml` or `ejsHtmlLoader` property of your -Webpack configuration: +Or second, via an `'ejsHtml'` property: ```js -// webpack.config.js - module.exports = { // ... ejsHtml: { @@ -88,9 +83,10 @@ Copyright © 2016 Akim McMath. Licensed under the [MIT License][license]. [dependencies-badge]: https://img.shields.io/gemnasium/akim-mcmath/ejs-html-loader.svg?style=flat-square [npm]: https://www.npmjs.com/package/ejs-html-loader -[license]: LICENSE.txt +[license]: LICENSE [travis]: https://travis-ci.org/akim-mcmath/ejs-html-loader [coveralls]: https://coveralls.io/github/akim-mcmath/ejs-html-loader?branch=master [gemnasium]: https://gemnasium.com/akim-mcmath/ejs-html-loader [webpack]: https://webpack.github.io/ +[webpack-configuration]: https://webpack.github.io/docs/configuration.html [ejs]: http://ejs.co/ diff --git a/index.js b/index.js deleted file mode 100644 index 7b4954f..0000000 --- a/index.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -module.exports = require('./lib/ejs-html-loader'); diff --git a/lib/dependencies.js b/lib/dependencies.js deleted file mode 100644 index e2bd6ce..0000000 --- a/lib/dependencies.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -var cache = {}; - -// Add dependencies from cache in case of error -exports.addFromCache = function(context) { - add(context, get(context)); -}; - -// Add dependencies and from compiled template and save to cache -exports.addAndSave = function(context, deps) { - add(context, save(context, deps)); -}; - -function get(context) { - return cache[context.resourcePath] || []; -} - -function save(context, deps) { - cache[context.resourcePath] = deps; - return get(context); -} - -function add(context, deps) { - deps.forEach(function(dep) { - context.addDependency(dep); - }); -} diff --git a/lib/ejs-html-loader.js b/lib/ejs-html-loader.js deleted file mode 100644 index 195e6a2..0000000 --- a/lib/ejs-html-loader.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; - -var parseQuery = require('loader-utils').parseQuery; -var compile = require('ejs').compile; -var dependencies = require('./dependencies'); - -module.exports = function ejsHtmlLoader(source) { - this.cacheable(); - - var options = this.options; - var query = parseQuery(this.query); - var data = options.ejsHtmlLoader || options.ejsHtml || query; - - var template; - var rendered; - - try { - template = compile(source, { - filename: this.resourcePath, - delimiter: data.delimiter, - context: data.context - }); - rendered = template(data); - } catch (e) { - dependencies.addFromCache(this); - throwError(e.message); - } - - dependencies.addAndSave(this, template.dependencies); - return rendered; -}; - -function throwError(message) { - var err = new Error('ejs-html-loader\n' + message); - Error.captureStackTrace(err, module.exports); - throw err; -} diff --git a/package.json b/package.json index f3decd3..9b82567 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,16 @@ { "name": "ejs-html-loader", - "version": "1.2.2", + "version": "2.0.0", "description": "Webpack loader for rendering HTML from EJS templates", - "main": "index.js", - "files": [ - "lib", - "index.js", - "README.md", - "LICENSE.txt" - ], + "main": "lib/index.js", "scripts": { - "test": "npm run jshint && npm run jscs && npm run mocha", - "jshint": "jshint . --exclude-path .gitignore", - "jscs": "jscs .", - "mocha": "istanbul cover _mocha", - "clean": "rm -rf ./node_modules ./coverage", - "coverage": "npm test && open ./coverage/lcov-report/index.html", - "coveralls": "cat ./coverage/lcov.info | coveralls", - "preversion": "npm run test" + "build": "npm run unbuild && babel src -d lib", + "unbuild": "rimraf lib", + "test:lint": "eslint lib test", + "test:unit": "istanbul cover _mocha -- test/index.coffee", + "test:report": "npm run test:unit && open coverage/lcov-report/index.html", + "test": "npm run test:lint && npm run test:unit", + "coveralls": "cat coverage/lcov.info | coveralls" }, "engines": { "node": ">=0.10" @@ -42,21 +35,27 @@ }, "homepage": "https://github.com/akim-mcmath/ejs-html-loader#readme", "peerDependencies": { - "webpack": "1.x - 2.x" + "webpack": "1.x - 2.x", + "ejs": "2.x" }, "dependencies": { - "ejs": "^2.0.2", - "loader-utils": "^0.2.0" + "loader-utils": "^0.2.15" }, "devDependencies": { + "babel-plugin-add-module-exports": "^0.2.1", + "babel-preset-es2015": "^6.9.0", + "babel-register": "^6.9.0", + "bluebird": "^3.4.0", "chai": "^3.5.0", - "coveralls": "^2.11.6", + "coffee-script": "^1.10.0", + "coveralls": "^2.11.9", + "ejs": "^2.4.2", + "eslint": "^2.11.1", "file-loader": "^0.8.5", - "istanbul": "^0.4.2", - "jscs": "^2.9.0", - "jshint": "^2.9.1", - "mocha": "^2.4.5", - "rimraf": "^2.5.1", + "istanbul": "1.0.0-alpha.2", + "mocha": "^2.5.3", + "relative-path-map": "^1.0.1", + "rimraf": "^2.5.2", "webpack": "^1.12.13" } } diff --git a/src/cache.js b/src/cache.js new file mode 100644 index 0000000..82845af --- /dev/null +++ b/src/cache.js @@ -0,0 +1,24 @@ +const cache = {}; + +export function addDependencies(ctx) { + add(ctx, get(ctx)); +} + +export function saveDependencies(ctx, deps) { + add(ctx, save(ctx, deps)); +} + +function get(ctx) { + return cache[ctx.resourcePath] || []; +} + +function save(ctx, deps=[]) { + cache[ctx.resourcePath] = deps; + return get(ctx); +} + +function add(ctx, deps) { + deps.forEach(name => { + ctx.addDependency(name); + }); +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..fbcab5e --- /dev/null +++ b/src/index.js @@ -0,0 +1,31 @@ +import {getLoaderConfig} from 'loader-utils'; +import {addDependencies, saveDependencies} from './cache'; +import {render} from './render'; + +export default function ejsHtmlLoader(src) { + this.cacheable(); + + let rendered = ''; + + try { + rendered = renderTemplate(this, src); + } catch (e) { + emitError(this, e.message); + } + + return rendered; +} + +function renderTemplate(ctx, src) { + let data = getLoaderConfig(ctx, 'ejsHtml'); + let {rendered, deps} = render(ctx, src, data); + + saveDependencies(ctx, deps); + + return rendered; +} + +function emitError(ctx, msg) { + addDependencies(ctx); + ctx.emitError(`ejs-html-loader\n${msg}`); +} diff --git a/src/render.js b/src/render.js new file mode 100644 index 0000000..4817c77 --- /dev/null +++ b/src/render.js @@ -0,0 +1,14 @@ +import {compile} from 'ejs'; + +export function render(ctx, src, data) { + let template = compile(src, { + filename: ctx.resourcePath, + delimiter: data.delimiter, + context: data.context + }); + + return { + rendered: template(data), + deps: template.dependencies + }; +} diff --git a/test/config/paths.coffee b/test/config/paths.coffee new file mode 100644 index 0000000..8dbd921 --- /dev/null +++ b/test/config/paths.coffee @@ -0,0 +1,11 @@ +path = require 'path' +pathMap = require 'relative-path-map' + +module.exports = pathMap( + root: path.resolve __dirname, '../..' + src: '[root]/src/index.js' + test: '[root]/test' + fixt: '[test]/fixtures' + out: '[test]/.tmp' + outFile: '[out]/index.html' +) diff --git a/test/config/paths.js b/test/config/paths.js deleted file mode 100644 index f5620af..0000000 --- a/test/config/paths.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -var resolve = require('path').resolve; - -var ROOT = resolve(__dirname, '../..'); -var TEST = resolve(ROOT, 'test'); -var OUTPUT = resolve(TEST, 'output'); - -module.exports = { - ROOT: ROOT, - OUTPUT: OUTPUT, - LOADER: resolve(ROOT, 'index.js'), - RENDERED: resolve(OUTPUT, 'index.html') -}; diff --git a/test/fixtures/bad-locals/index.ejs b/test/fixtures/bad-locals.ejs similarity index 60% rename from test/fixtures/bad-locals/index.ejs rename to test/fixtures/bad-locals.ejs index de19bd0..d43c114 100644 --- a/test/fixtures/bad-locals/index.ejs +++ b/test/fixtures/bad-locals.ejs @@ -2,9 +2,9 @@ - Bad Locals + Simple -

<%- heading %>

+

<%= headingheadingheading %>

diff --git a/test/fixtures/bad-locals.js b/test/fixtures/bad-locals.js new file mode 100644 index 0000000..b6f24a8 --- /dev/null +++ b/test/fixtures/bad-locals.js @@ -0,0 +1 @@ +require('./bad-locals.ejs') diff --git a/test/fixtures/bad-locals/entry.js b/test/fixtures/bad-locals/entry.js deleted file mode 100644 index 7f9a7ed..0000000 --- a/test/fixtures/bad-locals/entry.js +++ /dev/null @@ -1 +0,0 @@ -require('./index.ejs'); diff --git a/test/fixtures/bad-locals/webpack.config.js b/test/fixtures/bad-locals/webpack.config.js deleted file mode 100644 index 5b1106d..0000000 --- a/test/fixtures/bad-locals/webpack.config.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; - -var configure = require('../configure.js'); - -module.exports = configure(__dirname, { - error: /ejs-html-loader/, - options: {} -}); diff --git a/test/fixtures/bad-syntax/index.ejs b/test/fixtures/bad-syntax.ejs similarity index 81% rename from test/fixtures/bad-syntax/index.ejs rename to test/fixtures/bad-syntax.ejs index 98f60a6..46addf8 100644 --- a/test/fixtures/bad-syntax/index.ejs +++ b/test/fixtures/bad-syntax.ejs @@ -2,7 +2,7 @@ - Bad Syntax + Document

<%= heading =>

diff --git a/test/fixtures/bad-syntax.js b/test/fixtures/bad-syntax.js new file mode 100644 index 0000000..65e8818 --- /dev/null +++ b/test/fixtures/bad-syntax.js @@ -0,0 +1 @@ +require('./bad-syntax.ejs') diff --git a/test/fixtures/bad-syntax/entry.js b/test/fixtures/bad-syntax/entry.js deleted file mode 100644 index 7f9a7ed..0000000 --- a/test/fixtures/bad-syntax/entry.js +++ /dev/null @@ -1 +0,0 @@ -require('./index.ejs'); diff --git a/test/fixtures/bad-syntax/webpack.config.js b/test/fixtures/bad-syntax/webpack.config.js deleted file mode 100644 index f82f7ae..0000000 --- a/test/fixtures/bad-syntax/webpack.config.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -var configure = require('../configure.js'); - -module.exports = configure(__dirname, { - error: /ejs-html-loader/, - options: { - heading: 'Some heading' - } -}); diff --git a/test/fixtures/configure.js b/test/fixtures/configure.js deleted file mode 100644 index 3a7adb0..0000000 --- a/test/fixtures/configure.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - -var paths = require('../config/paths'); - -var ejsTest = /\.ejs$/; - -module.exports = function configure(dirname, opts) { - - var loaders = [ - { - test: ejsTest, - loader: 'file?name=index.html' - }, - { - test: ejsTest, - loader: paths.LOADER + (opts.query || '') - } - ]; - - if (opts.initialLoader) { - loaders.push({ - test: ejsTest, - loader: opts.initialLoader - }); - } - - return { - context: dirname, - entry: './entry.js', - output: { - path: paths.OUTPUT, - filename: 'bundle.js' - }, - module: { - loaders: loaders - }, - match: opts.match, - ejsHtml: opts.options, - error: opts.error - }; -}; diff --git a/test/fixtures/context/index.ejs b/test/fixtures/context.ejs similarity index 83% rename from test/fixtures/context/index.ejs rename to test/fixtures/context.ejs index 131c6c3..688d361 100644 --- a/test/fixtures/context/index.ejs +++ b/test/fixtures/context.ejs @@ -2,7 +2,7 @@ - Context + Simple

<%= this.heading %>

diff --git a/test/fixtures/context.js b/test/fixtures/context.js new file mode 100644 index 0000000..d6b7c58 --- /dev/null +++ b/test/fixtures/context.js @@ -0,0 +1 @@ +require('./context.ejs') diff --git a/test/fixtures/context/entry.js b/test/fixtures/context/entry.js deleted file mode 100644 index 7f9a7ed..0000000 --- a/test/fixtures/context/entry.js +++ /dev/null @@ -1 +0,0 @@ -require('./index.ejs'); diff --git a/test/fixtures/context/webpack.config.js b/test/fixtures/context/webpack.config.js deleted file mode 100644 index 1bb7cf9..0000000 --- a/test/fixtures/context/webpack.config.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -var configure = require('../configure.js'); - -module.exports = configure(__dirname, { - match: /This example has a context/, - options: { - context: { - heading: 'This example has a context' - } - } -}); diff --git a/test/fixtures/delimiter/index.ejs b/test/fixtures/delimiter.ejs similarity index 81% rename from test/fixtures/delimiter/index.ejs rename to test/fixtures/delimiter.ejs index 5ad92fb..5f1d293 100644 --- a/test/fixtures/delimiter/index.ejs +++ b/test/fixtures/delimiter.ejs @@ -2,7 +2,7 @@ - Delimeter + Simple

diff --git a/test/fixtures/delimiter.js b/test/fixtures/delimiter.js new file mode 100644 index 0000000..070b121 --- /dev/null +++ b/test/fixtures/delimiter.js @@ -0,0 +1 @@ +require('./delimiter.ejs') diff --git a/test/fixtures/delimiter/entry.js b/test/fixtures/delimiter/entry.js deleted file mode 100644 index 7f9a7ed..0000000 --- a/test/fixtures/delimiter/entry.js +++ /dev/null @@ -1 +0,0 @@ -require('./index.ejs'); diff --git a/test/fixtures/delimiter/webpack.config.js b/test/fixtures/delimiter/webpack.config.js deleted file mode 100644 index 4b9ecca..0000000 --- a/test/fixtures/delimiter/webpack.config.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -var configure = require('../configure.js'); - -module.exports = configure(__dirname, { - match: /This example uses a delimiter/, - options: { - heading: 'This example uses a delimiter', - delimiter: '?' - } -}); diff --git a/test/fixtures/include-partial.ejs b/test/fixtures/include-partial.ejs new file mode 100644 index 0000000..d6fa285 --- /dev/null +++ b/test/fixtures/include-partial.ejs @@ -0,0 +1 @@ +

<%= two %>

diff --git a/test/fixtures/include.ejs b/test/fixtures/include.ejs new file mode 100644 index 0000000..f78e1c5 --- /dev/null +++ b/test/fixtures/include.ejs @@ -0,0 +1,11 @@ + + + + + Simple + + +

<%= one %>

+ <% include include-partial %> + + diff --git a/test/fixtures/include.js b/test/fixtures/include.js new file mode 100644 index 0000000..895daf4 --- /dev/null +++ b/test/fixtures/include.js @@ -0,0 +1 @@ +require('./include.ejs'); diff --git a/test/fixtures/index.js b/test/fixtures/index.js deleted file mode 100644 index d065b21..0000000 --- a/test/fixtures/index.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -module.exports = { - badLocals: require('./bad-locals/webpack.config'), - badSyntax: require('./bad-syntax/webpack.config'), - context: require('./context/webpack.config'), - delimiter: require('./delimiter/webpack.config'), - queryString: require('./query-string/webpack.config'), - simple: require('./simple/webpack.config'), - withInclude: require('./with-include/webpack.config') -}; diff --git a/test/fixtures/query-string/entry.js b/test/fixtures/query-string/entry.js deleted file mode 100644 index 7f9a7ed..0000000 --- a/test/fixtures/query-string/entry.js +++ /dev/null @@ -1 +0,0 @@ -require('./index.ejs'); diff --git a/test/fixtures/query-string/index.ejs b/test/fixtures/query-string/index.ejs deleted file mode 100644 index a51b411..0000000 --- a/test/fixtures/query-string/index.ejs +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Query String - - -

<%= heading %>

- - diff --git a/test/fixtures/query-string/webpack.config.js b/test/fixtures/query-string/webpack.config.js deleted file mode 100644 index f724540..0000000 --- a/test/fixtures/query-string/webpack.config.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; - -var configure = require('../configure.js'); - -module.exports = configure(__dirname, { - match: /This example uses a query string/, - query: '?heading=This%20example%20uses%20a%20query%20string' -}); diff --git a/test/fixtures/simple/index.ejs b/test/fixtures/simple.ejs similarity index 100% rename from test/fixtures/simple/index.ejs rename to test/fixtures/simple.ejs diff --git a/test/fixtures/simple.js b/test/fixtures/simple.js new file mode 100644 index 0000000..6852e1d --- /dev/null +++ b/test/fixtures/simple.js @@ -0,0 +1 @@ +require('./simple.ejs'); diff --git a/test/fixtures/simple/entry.js b/test/fixtures/simple/entry.js deleted file mode 100644 index 7f9a7ed..0000000 --- a/test/fixtures/simple/entry.js +++ /dev/null @@ -1 +0,0 @@ -require('./index.ejs'); diff --git a/test/fixtures/simple/webpack.config.js b/test/fixtures/simple/webpack.config.js deleted file mode 100644 index 73fa8d7..0000000 --- a/test/fixtures/simple/webpack.config.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -var configure = require('../configure.js'); - -module.exports = configure(__dirname, { - match: /This is a simple example/, - options: { - heading: 'This is a simple example' - } -}); diff --git a/test/fixtures/with-include/entry.js b/test/fixtures/with-include/entry.js deleted file mode 100644 index 7f9a7ed..0000000 --- a/test/fixtures/with-include/entry.js +++ /dev/null @@ -1 +0,0 @@ -require('./index.ejs'); diff --git a/test/fixtures/with-include/index.ejs b/test/fixtures/with-include/index.ejs deleted file mode 100644 index 4294acd..0000000 --- a/test/fixtures/with-include/index.ejs +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Include - - -
<%- include partial %>
- - diff --git a/test/fixtures/with-include/partial.ejs b/test/fixtures/with-include/partial.ejs deleted file mode 100644 index c0a6f7d..0000000 --- a/test/fixtures/with-include/partial.ejs +++ /dev/null @@ -1 +0,0 @@ -

<%= subheading %>

diff --git a/test/fixtures/with-include/webpack.config.js b/test/fixtures/with-include/webpack.config.js deleted file mode 100644 index 7416973..0000000 --- a/test/fixtures/with-include/webpack.config.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -var configure = require('../configure.js'); - -module.exports = configure(__dirname, { - match: /This text is included/, - options: { - subheading: 'This text is included' - } -}); diff --git a/test/helpers/compile.coffee b/test/helpers/compile.coffee new file mode 100644 index 0000000..3a0b2ea --- /dev/null +++ b/test/helpers/compile.coffee @@ -0,0 +1,17 @@ +{readFile} = require 'fs' +{expect} = require 'chai' +Promise = require 'bluebird' +webpack = require 'webpack' +paths = require '../config/paths' +config = require './config' + +module.exports = (match, opts) -> + new Promise (resolve) -> + webpack config(opts), (err) -> + expect(err).not.to.exist + + readFile paths.outFile, 'utf8', (err, content) -> + expect(err).not.to.exist + expect(content).to.match match + + resolve() diff --git a/test/helpers/config.coffee b/test/helpers/config.coffee new file mode 100644 index 0000000..78bdbd4 --- /dev/null +++ b/test/helpers/config.coffee @@ -0,0 +1,20 @@ +{resolve} = require 'path' +paths = require '../config/paths' + +ejsRe = /\.ejs$/ + +module.exports = (opts) -> + context: paths.fixt + entry: resolve paths.fixt, opts.entry + output: + path: paths.out + filename: 'bundle.js' + module: + loaders: [ + test: ejsRe + loader: 'file?name=index.html' + , + test: ejsRe + loader: "#{paths.src}?#{opts.query || ''}" + ] + ejsHtml: opts.ejsHtml diff --git a/test/helpers/error.coffee b/test/helpers/error.coffee new file mode 100644 index 0000000..713d893 --- /dev/null +++ b/test/helpers/error.coffee @@ -0,0 +1,12 @@ +{expect} = require 'chai' +Promise = require 'bluebird' +webpack = require 'webpack' +config = require './config' + +module.exports = (match, opts, done) -> + new Promise (resolve) -> + webpack config(opts), (err, stats) -> + expect(err).not.to.exist + expect(stats.toString()).to.match match + + resolve() diff --git a/test/index.coffee b/test/index.coffee new file mode 100644 index 0000000..e28d407 --- /dev/null +++ b/test/index.coffee @@ -0,0 +1,75 @@ +chai = require 'chai' +rimraf = require 'rimraf' +ejsHtmlLoader = require '../src' +paths = require './config/paths' +compile = require './helpers/compile' +error = require './helpers/error' + +before -> + chai.should() + +describe 'ejs-html-loader', -> + + afterEach (done) -> + rimraf paths.out, done + + it 'exports a function', -> + ejsHtmlLoader.should.be.a 'function' + + context 'options passed as ejsHtml property', -> + + it 'renders template', -> + compile /tuvalu/, + entry: 'simple.js' + ejsHtml: + heading: 'tuvalu' + + context 'options passed as query string', -> + + it 'renders template', -> + compile /togo/, + entry: 'simple.js' + query: 'heading=togo' + + context 'with include statement', -> + + it 'includes partial', -> + compile /trinidad[\s\S]*tobago/, + entry: 'include.js' + ejsHtml: + one: 'trinidad' + two: 'tobago' + + context 'option: delimiter', -> + + it 'sets EJS delimiter option', -> + compile /tajikistan/, + entry: 'delimiter.js' + ejsHtml: + heading: 'tajikistan' + delimiter: '?' + + context 'option: context', -> + + it 'sets EJS context option', -> + compile /tibet/, + entry: 'context.js' + ejsHtml: + context: + heading: 'tibet' + + context 'error: undelcared locals', -> + + it 'emits error', -> + error /ejs-html-loader/, + entry: 'bad-locals.js' + ejsHtml: + heading: 'tunisia' + + context 'error: invalaid syntax', -> + + it 'emits error', -> + error /ejs-html-loader/, + entry: 'bad-syntax.js' + ejsHtml: + heading: 'turkey' diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index 2bf6c73..0000000 --- a/test/index.test.js +++ /dev/null @@ -1,90 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var readFile = require('fs').readFile; -var webpack = require('webpack'); -var fixtures = require('./fixtures'); -var paths = require('./config/paths'); -var rimraf = require('rimraf'); - -describe('loader:ejs-html-loader', function() { - - afterEach('remove output directory', function(done) { - rimraf(paths.OUTPUT, done); - }); - - describe('usage', function() { - - describe('simple usage', function() { - - it('renders template', function(done) { - testMatch(fixtures.simple, done); - }); - }); - - describe('partials included in template', function() { - - it('renders included partial', function(done) { - testMatch(fixtures.withInclude, done); - }); - }); - - describe('options passed as query string', function() { - - it('renders locals from query string', function(done) { - testMatch(fixtures.queryString, done); - }); - }); - }); - - describe('options', function() { - - describe('option:context', function() { - - it('renders templates with `this` context set', function(done) { - testMatch(fixtures.context, done); - }); - }); - - describe('option:delimiter', function() { - - it('renders templates with custom delimiter set', function(done) { - testMatch(fixtures.delimiter, done); - }); - }); - }); - - describe('incorrect EJS syntax', function() { - it('emits a custom Error', function(done) { - testError(fixtures.badSyntax, done); - }); - }); - - describe('missing local variable', function() { - it('emits a custom Error', function(done) { - testError(fixtures.badLocals, done); - }); - }); - -}); - -function testMatch(config, done) { - webpack(config, function(err, stats) { - expect(err).not.to.exist; - - readFile(paths.RENDERED, 'utf8', function(err, content) { - expect(err).not.to.exist; - expect(content).to.match(config.match); - - done(); - }); - }); -} - -function testError(config, done) { - webpack(config, function(err, stats) { - expect(err).not.to.exist; - expect(stats.toString()).to.match(config.error); - done(); - }); -} diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..f57cf01 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,3 @@ +--recursive +--reporter dot +--compilers js:babel-register,coffee:coffee-script/register