Skip to content

Commit

Permalink
feat: change expected module API
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This commit introduces a major refactoring of the loader.

- Remove node 0.10 and node 0.12 support
- The loaded module must now export a function
- This function will be called with the loader options
- This function must return an object with this structure

Property | Type | Description
:--------|:-----|:-----------
`code`   | `string|Buffer` | **Required**. The code that is passed to the next loader or to webpack.
`sourceMap` | [`SourceMap`](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit) | **Optional**. Will be pased to the next loader or to webpack.
`ast` | `any` | **Optional**. An [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) that will be passed to the next loader. Useful to speed up the build time if the next loader uses the same AST.
`dependencies` | `Array<string>` | **Default: `[]`**. An array of absolute, native paths to file dependencies that need to be watched for changes. 
`cacheable` | `boolean` | **Default: `false`**. Flag whether the code can be re-used in watch mode if none of the `dependencies` have changed.

- The function may also return a promise for async results
- Switch tooling to webpack-defaults

#11
  • Loading branch information
jhnns committed Mar 16, 2017
1 parent d873c86 commit caf2aab
Show file tree
Hide file tree
Showing 37 changed files with 6,043 additions and 71 deletions.
25 changes: 25 additions & 0 deletions .babelrc
@@ -0,0 +1,25 @@
{
"presets": [
[
"env",
{
"modules": false,
"useBuiltIns": true,
"targets": {
"node": 4.3
},
"exclude": [
"transform-async-to-generator",
"transform-regenerator"
]
}
]
],
"env": {
"test": {
"presets": [
"env"
]
}
}
}
17 changes: 17 additions & 0 deletions .editorconfig
@@ -0,0 +1,17 @@
# editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[package.json]
indent_size = 2

[.md]
insert_final_newline = false
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .eslintignore
@@ -0,0 +1,3 @@
/node_modules
/dist
/test/output
3 changes: 3 additions & 0 deletions .eslintrc
@@ -0,0 +1,3 @@
{
"extends": "webpack"
}
3 changes: 3 additions & 0 deletions .gitattributes
@@ -0,0 +1,3 @@
yarn.lock -diff
* text=auto
bin/* eol=lf
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE.md
@@ -0,0 +1,5 @@
1. Check the version of package you are using. If it's not the newest version, update and try again (see changelog while updating!).
2. If the issue is still there, write a minimal project showing the problem and expected output.
3. Link to the project and mention Node version and OS in your report.

**IMPORTANT! You should use [Stack Overflow](https://stackoverflow.com/) for support related questions.**
5 changes: 5 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,5 @@
<!--
1. [Read and sign the CLA](https://cla.js.foundation/webpack/webpack.js.org). This needs to be done only once. PRs that haven't signed it won't be accepted.
2. Check out the [development guide](https://webpack.js.org/development/) for the API and development guidelines.
3. Read through the PR diff carefully as sometimes this can reveal issues. The work will be reviewed, but this can save some effort.
-->
15 changes: 14 additions & 1 deletion .gitignore
@@ -1 +1,14 @@
node_modules
yarn-debug.log*
.eslintcache
/coverage
/dist
/local
/reports
/node_modules
.DS_Store
Thumbs.db
.idea
.vscode
*.sublime-project
*.sublime-workspace
/test/output
32 changes: 32 additions & 0 deletions .travis.yml
@@ -0,0 +1,32 @@
sudo: false
language: node_js
branches:
only:
- master
matrix:
fast_finish: true
include:
- os: linux
node_js: '7'
env: WEBPACK_VERSION="2.2.0" JOB_PART=lint
- os: linux
node_js: '4.3'
env: WEBPACK_VERSION="2.2.0" JOB_PART=test
- os: linux
node_js: '6'
env: WEBPACK_VERSION="2.2.0" JOB_PART=test
- os: linux
node_js: '7'
env: WEBPACK_VERSION="2.2.0" JOB_PART=coverage
before_install:
- nvm --version
- node --version
before_script:
- |-
if [ "$WEBPACK_VERSION" ]; then
yarn add webpack@^$WEBPACK_VERSION
fi
script:
- 'yarn run travis:$JOB_PART'
after_success:
- 'bash <(curl -s https://codecov.io/bash)'
11 changes: 11 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,11 @@
# Change Log

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

x.x.x / <year>-<month>-<day>
==================

* Bug fix -
* Feature -
* Chore -
* Docs -
1 change: 0 additions & 1 deletion LICENSE
@@ -1,4 +1,3 @@

Copyright JS Foundation and other contributors

Permission is hereby granted, free of charge, to any person obtaining
Expand Down
186 changes: 155 additions & 31 deletions README.md
@@ -1,5 +1,10 @@
[![npm][npm]][npm-url]
[![deps][deps]][deps-url]
[![npm][npm]][npm-url]
[![node][node]][node-url]
[![npm-stats][npm-stats]][npm-url]
[![deps][deps]][deps-url]
[![travis][travis]][travis-url]
[![appveyor][appveyor]][appveyor-url]
[![coverage][cover]][cover-url]
[![chat][chat]][chat-url]

<div align="center">
Expand All @@ -9,33 +14,139 @@
src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
</a>
<h1>Val Loader</h1>
<p>Provides data to another loader<p>
<p>Executes the given module to generate source code on build time.<p>
</div>

<h2 align="center">Install</h2>

```bash
npm i val-loader --save
npm install val-loader --save-dev
```

<h2 align="center">Examples</h2>

If you have a module `findAnswer.js` like this:

```js
function findAnswer() {
return {
code: 'module.exports = 42;'
};
}

module.exports = findAnswer;
```

you can use the **val-loader** to generate source code on build time:

```js
// webpack.config.js
module.exports = {
...
module: {
rules: [{
test: require.resolve('path/to/findAnswer.js'),
use: [{
loader: 'val-loader'
}]
}]
}
};
```

---

A complete example of all available features looks like this:

```js
const ask = require('./ask.js');
const generateResult = require('./generateResult.js');

function findAnswer(options) {
return ask(options.question)
.then(generateResult)
.then(result => ({

code: result.code,
sourceMap: result.sourceMap,
ast: result.abstractSyntaxTree,

// Mark dependencies of findAnswer().
// The function will be re-executed if one of these
// dependencies has changed in watch mode.
dependencies: [
// Array of absolute native paths!
require.resolve('./ask.js'),
require.resolve('./generateResult.js')
],

// Flag the generated code as cacheable.
// If none of the dependencies have changed,
// the function won't be executed again.
cacheable: true

}));
}

module.exports = findAnswer;
```

```js
// webpack.config.js
module.exports = {
...
module: {
rules: [{
test: require.resolve('path/to/findAnswer.js'),
use: [{
loader: 'val-loader',
options: {
question: 'What is the meaning of life?'
}
}]
}]
}
};
```

<h2 align="center">Usage</h2>
<h2 align="center">Usage</h2>

The module that is loaded with this loader must stick to the following interfaces:

### Expected module interface

The loaded module must export a function as `default` export with the following *function interface*.

```js
module.exports = function (...) { ... };
```

Modules transpiled by [Babel](https://babeljs.io/) are also supported.

```js
export default function (...) { ... }
```

### Expected function interface

The function will be called with the loader [`options`](https://webpack.js.org/configuration/module/#useentry) and must either return:

- an object with the following *object interface* or
- a promise that resolves to the following *object interface*

### Expected object interface

Property | Type | Description
:--------|:-----|:-----------
`code` | `string|Buffer` | **Required**. The code that is passed to the next loader or to webpack.
`sourceMap` | [`SourceMap`](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit) | **Optional**. Will be pased to the next loader or to webpack.
`ast``any` | **Optional**. An [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) that will be passed to the next loader. Useful to speed up the build time if the next loader uses the same AST.
`dependencies` | `Array<string>` | **Default: `[]`**. An array of absolute, native paths to file dependencies that need to be watched for changes.
`cacheable` | `boolean` | **Default: `false`**. Flag whether the code can be re-used in watch mode if none of the `dependencies` have changed.

``` javascript
var a = require("val-loader!./file.js");
// => excute file.js while compiling and
// take the result as javascript code for including
```

Don't forget to polyfill `require` if you want to use it in node.
See `webpack` documentation.

The excution of file.js has polyfill already applied.

Provide data for another loader:

``` javascript
require("css-loader!val-loader!./generateCss.js");
```
### Loader Options

The **val-loader** itself has no options. The options are passed as they are (without cloning them) to the exported function.

<h2 align="center">Maintainers</h2>

Expand All @@ -62,20 +173,33 @@ require("css-loader!val-loader!./generateCss.js");
</td>
<td align="center">
<img width="150" height="150"
src="https://avatars3.githubusercontent.com/u/3408176?v=3&s=150">
src="https://avatars3.githubusercontent.com/u/781746?v=3&s=150">
</br>
<a href="https://github.com/TheLarkInn">Sean Larkin</a>
<a href="https://github.com/jhnns">Johannes Ewald</a>
</td>
</tr>
<tbody>
</table>


[npm]: https://img.shields.io/npm/v/val-loader.svg
[npm-url]: https://npmjs.com/package/val-loader

[deps]: https://david-dm.org/webpack-contrib/val-loader.svg
[deps-url]: https://david-dm.org/webpack-contrib/val-loader

[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
[chat-url]: https://gitter.im/webpack/webpack
[npm]: https://img.shields.io/npm/v/val-loader.svg
[npm-stats]: https://img.shields.io/npm/dm/val-loader.svg
[npm-url]: https://npmjs.com/package/val-loader

[node]: https://img.shields.io/node/v/val-loader.svg
[node-url]: https://nodejs.org

[deps]: https://david-dm.org/webpack-contrib/val-loader.svg
[deps-url]: https://david-dm.org/webpack-contrib/val-loader

[travis]: http://img.shields.io/travis/webpack-contrib/val-loader.svg
[travis-url]: https://travis-ci.org/webpack-contrib/val-loader

[appveyor-url]: https://ci.appveyor.com/project/jhnns/val-loader/branch/master
[appveyor]: https://ci.appveyor.com/api/projects/status/github/webpack-contrib/val-loader?svg=true

[cover]: https://coveralls.io/repos/github/webpack-contrib/val-loader/badge.svg
[cover-url]: https://coveralls.io/github/webpack-contrib/val-loader

[chat]: https://badges.gitter.im/webpack-contrib/webpack.svg
[chat-url]: https://gitter.im/webpack-contrib/webpack
19 changes: 19 additions & 0 deletions appveyor.yml
@@ -0,0 +1,19 @@
# appveyor file
# http://www.appveyor.com/docs/appveyor-yml

init:
- git config --global core.autocrlf input

install:
- ps: Install-Product node $env:nodejs_version x64
- npm install

build: off

matrix:
fast_finish: true

test_script:
- node --version
- npm --version
- cmd: npm run test
15 changes: 0 additions & 15 deletions index.js

This file was deleted.

0 comments on commit caf2aab

Please sign in to comment.