-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeff Escalante
committed
May 31, 2016
0 parents
commit 9a909bc
Showing
20 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file is for unifying the coding style for different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
indent_size = 2 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.DS_Store | ||
.nyc_output |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test | ||
contributing.md | ||
.editorconfig | ||
.travis.yml |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
sudo: false | ||
node_js: | ||
- 6 | ||
after_script: | ||
- npm run coveralls |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
License (MIT) | ||
------------- | ||
|
||
Copyright (c) 2015 Jeff Escalante, Carrot Creative | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Pug Static Loader | ||
|
||
[![npm](http://img.shields.io/npm/v/pug-static-loader.svg?style=flat)](https://badge.fury.io/js/pug-static-loader) [![tests](http://img.shields.io/travis/static-dev/pug-static-loader/master.svg?style=flat)](https://travis-ci.org/static-dev/pug-static-loader) [![dependencies](http://img.shields.io/david/static-dev/pug-static-loader.svg?style=flat)](https://david-dm.org/static-dev/pug-static-loader) | ||
[![coverage](http://img.shields.io/coveralls/static-dev/pug-static-loader.svg?style=flat)](https://coveralls.io/github/static-dev/pug-static-loader) | ||
|
||
Webpack loader that compiles [pug](https://github.com/pugjs/pug) to static html | ||
|
||
> **Note:** This project is in early development, and versioning is a little different. [Read this](http://markup.im/#q4_cRZ1Q) for more details. | ||
### Why should you care? | ||
|
||
This loader is intended to be used in situations where you want webpack to render a static html file rather than a client side template. It's written by the authors of [spike](https://github.com/static-dev/spike), a static website engine with webpack at the core. | ||
|
||
If you are looking for a simple, solid, and well maintained webpack loader that renders out pug as html, you have come to the right place. | ||
|
||
### Installation | ||
|
||
`npm i pug-static-loader -S` | ||
|
||
### Usage | ||
|
||
This loader does not accept any options via `query`, because all `query` options are stringified, which means that functions cannot be passed. However, it's quite a common use-case to pass a function to pug as a local, and unfortunately functions cannot be stringified. Also querystrings are ugly. | ||
|
||
So instead, this loader pulls settings directly from the webpack options, from the `pug` key. If you were to set up a simple webpack project using this loader, it would look something like this: | ||
|
||
```js | ||
// webpack.config.js | ||
module.exports = { | ||
module: { | ||
loaders: [ | ||
{ test: /\.pug$/, loader: 'pug-static' } | ||
] | ||
}, | ||
pug: { | ||
pretty: false, | ||
locals: { foo: 'bar' } | ||
} | ||
} | ||
``` | ||
|
||
The loader simply returns an export of a string containing the compiled html. Now you also probably would want to extract out the resulting code and write it to an html file, rather than letting it chill in your javascript output, but that's not part of what a loader can do, so use some plugins or maybe spike for this instead. | ||
|
||
This loader also exposes the source of the original pug files internally for plugins to access, using the `_src` property of each webpack module object. | ||
|
||
### License & Contributing | ||
|
||
- Details on the license [can be found here](LICENSE.md) | ||
- Details on running tests and contributing [can be found here](contributing.md) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Contributing to pug-static-loader | ||
|
||
Hello there! First of all, thanks for being interested in pug-static-loader and helping out. We all think you are awesome, and by contributing to open source projects, you are making the world a better place. That being said, there are a few ways to make the process of contributing code to pug-static-loader smoother, detailed below: | ||
|
||
### Filing Issues | ||
|
||
If you are opening an issue about a bug, make sure that you include clear steps for how we can reproduce the problem. _If we can't reproduce it, we can't fix it_. If you are suggesting a feature, make sure your explanation is clear and detailed. | ||
|
||
### Getting Set Up | ||
|
||
- Clone the project down | ||
- Make sure [nodejs](http://nodejs.org) has been installed and is above version `5.2.x` | ||
- Run `npm install` | ||
- Put in work | ||
|
||
### Testing | ||
|
||
This project is constantly evolving, and to ensure that things are secure and working for everyone, we need to have tests. If you are adding a new feature, please make sure to add a test for it. The test suite for this project uses [mocha](http://visionmedia.github.io/mocha/) and [should](https://github.com/visionmedia/should.js/)/ | ||
|
||
To run the test suite, make sure you have installed mocha (`npm install mocha -g`), then you can use the `npm test` or simply `mocha` command to run the tests. | ||
|
||
### Code Style | ||
|
||
This project uses ES6, transpiled by babel for tests and on deploy. To keep a consistent coding style in the project, we are using [standard js](http://standardjs.com/). In order for tests to pass, all code must pass standard js linting. This project also uses an [editorconfig](http://editorconfig.org/). It will make life much easier if you have the [editorconfig plugin](http://editorconfig.org/#download) for your text editor. For any inline documentation in the code, we're using [JSDoc](http://usejsdoc.org/). | ||
|
||
### Commit Cleanliness | ||
|
||
It's ok if you start out with a bunch of experimentation and your commit log isn't totally clean, but before any pull requests are accepted, we like to have a nice clean commit log. That means [well-written and clear commit messages](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) and commits that each do something significant, rather than being typo or bug fixes. | ||
|
||
If you submit a pull request that doesn't have a clean commit log, we will ask you to clean it up before we accept. This means being familiar with rebasing - if you are not, [this guide](https://help.github.com/articles/interactive-rebase) by github should help you to get started. And if you are still confused, feel free to ask! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const pug = require('pug') | ||
const Joi = require('joi') | ||
|
||
module.exports = function (source) { | ||
this.cacheable && this.cacheable(true) | ||
|
||
const _opts = (this.options ? this.options.pug : {}) || {} | ||
const schema = Joi.object().keys({ | ||
filename: Joi.string().default(this.resourcePath), | ||
pretty: Joi.boolean().default(true), | ||
doctype: Joi.string(), | ||
self: Joi.boolean(), | ||
debug: Joi.boolean(), | ||
compileDebug: Joi.boolean(), | ||
cache: Joi.boolean(), | ||
compiler: Joi.func(), | ||
parser: Joi.func(), | ||
globals: Joi.array().single(), | ||
locals: Joi.object() | ||
}) | ||
|
||
// make original source available for plugins | ||
this._module._src = source | ||
|
||
// validate options | ||
const validated = Joi.validate(_opts, schema) | ||
if (validated.error) { throw validated.error } | ||
const opts = validated.value | ||
|
||
// compile the template to a function | ||
const tpl = pug.compile(source, opts) | ||
|
||
// add all dependencies to webpack | ||
tpl.dependencies.map(this.addDependency.bind(this)) | ||
|
||
// render template | ||
const rendered = tpl(opts.locals) | ||
|
||
// stringify before returning so it's valid js for webpack | ||
return 'module.exports = ' + JSON.stringify(rendered) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "pug-static-loader", | ||
"description": "webpack loader that compiles pug to static html", | ||
"version": "0.0.0", | ||
"author": "Jeff Escalante", | ||
"ava": { | ||
"verbose": "true" | ||
}, | ||
"dependencies": { | ||
"joi": "^8.0.5", | ||
"pug": "^2.0.0-alpha8" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.15.1", | ||
"coveralls": "^2.11.9", | ||
"nyc": "^6.4.4", | ||
"rimraf": "^2.5.2", | ||
"standard": "7.x", | ||
"webpack": "^1.13.1" | ||
}, | ||
"license": "MIT", | ||
"main": "lib", | ||
"scripts": { | ||
"coverage": "nyc ava", | ||
"coveralls": "nyc --reporter=lcov ava && cat ./coverage/lcov.info | coveralls", | ||
"lint": "standard", | ||
"test": "ava" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./index.pug') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
p hello world |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
p from partial |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./index.pug') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
h1 dep test | ||
include _partial |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./index.pug') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
|
||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
|
||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
|
||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ }; | ||
|
||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
|
||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
|
||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
|
||
|
||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
|
||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
|
||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
|
||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
|
||
__webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module \"./index.pug\""); e.code = 'MODULE_NOT_FOUND'; throw e; }())) | ||
|
||
|
||
/***/ } | ||
/******/ ]); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
p hello world |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./index.pug') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
= foo() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const test = require('ava') | ||
const path = require('path') | ||
const fs = require('fs') | ||
const rimraf = require('rimraf') | ||
const webpack = require('webpack') | ||
|
||
const fixturesPath = path.join(__dirname, 'fixtures') | ||
|
||
test.cb('compiles a pug template', (t) => { | ||
const p = path.join(fixturesPath, 'default') | ||
webpack({ | ||
context: p, | ||
entry: path.join(p, 'app.js'), | ||
output: { path: p }, | ||
resolveLoader: { root: path.resolve('..') }, | ||
module: { loaders: [{ test: /\.pug$/, loader: 'lib' }] } | ||
}, (err, stats) => { | ||
if (err) { t.end(err) } | ||
const src = fs.readFileSync(path.join(p, 'bundle.js'), 'utf8') | ||
t.truthy(src.match('<p>hello world</p>')) | ||
rimraf(path.join(p, 'bundle.js'), t.end) | ||
}) | ||
}) | ||
|
||
test.cb('compiles a pug template + tracks dependencies', (t) => { | ||
const p = path.join(fixturesPath, 'dependencies') | ||
webpack({ | ||
context: p, | ||
entry: path.join(p, 'app.js'), | ||
output: { path: p }, | ||
resolveLoader: { root: path.resolve('..') }, | ||
module: { loaders: [{ test: /\.pug$/, loader: 'lib' }] } | ||
}, (err, stats) => { | ||
if (err) { t.end(err) } | ||
const src = fs.readFileSync(path.join(p, 'bundle.js'), 'utf8') | ||
t.regex(src, /<p>from partial<\/p>/) | ||
const dep = stats.compilation.fileDependencies[0] | ||
t.regex(dep, /_partial.pug/) | ||
rimraf(path.join(p, 'bundle.js'), t.end) | ||
}) | ||
}) | ||
|
||
test.cb('accepts locals through options object', (t) => { | ||
const p = path.join(fixturesPath, 'locals') | ||
webpack({ | ||
context: p, | ||
entry: path.join(p, 'app.js'), | ||
output: { path: p }, | ||
resolveLoader: { root: path.resolve('..') }, | ||
module: { loaders: [{ test: /\.pug$/, loader: 'lib' }] }, | ||
pug: { locals: { foo: () => 'bar' } } | ||
}, (err, stats) => { | ||
if (err) { t.end(err) } | ||
const src = fs.readFileSync(path.join(p, 'bundle.js'), 'utf8') | ||
t.truthy(src.match('bar')) | ||
rimraf(path.join(p, 'bundle.js'), t.end) | ||
}) | ||
}) | ||
|
||
test.cb('throws if options are invalid', (t) => { | ||
const p = path.join(fixturesPath, 'error') | ||
webpack({ | ||
context: p, | ||
entry: path.join(p, 'app.js'), | ||
output: { path: p }, | ||
resolveLoader: { root: path.resolve('..') }, | ||
module: { loaders: [{ test: /\.pug$/, loader: 'lib' }] }, | ||
pug: { locals: 'wow' } | ||
}, (_, stats) => { | ||
if (stats.compilation.errors) { | ||
const err = stats.compilation.errors[0].toString() | ||
t.truthy(err.match('"locals" must be an object')) | ||
t.end() | ||
} else { | ||
t.end('no error present with invalid options') | ||
} | ||
}) | ||
}) |