Skip to content

Commit 8a023ca

Browse files
authored
Merge pull request #243 from givanse/add-build-step
Add build step and TS support
2 parents 2b1dafe + eb06984 commit 8a023ca

File tree

10 files changed

+1031
-472
lines changed

10 files changed

+1031
-472
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
bower_components
22
node_modules
3-
coverage*
3+
coverage*
4+
dist
5+
src/pretender.es.js

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
src/index.ts
2+
test
3+
rollup.config.js
4+
karma.conf.js
5+
*.md
6+
yarn.lock

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ const server = new Pretender(function() {
4040
$.get('/photos/12', {success() => { ... }})
4141
```
4242
43+
## Usage
44+
45+
```
46+
yard add -D pretender
47+
# or
48+
npm install --save-dev pretender
49+
```
50+
51+
You can load Pretender directly in the browser.
52+
53+
```javascript
54+
<script src="pretender.js"></script>
55+
```
56+
57+
Or as a module.
58+
59+
```javascript
60+
import Pretender from 'pretender';
61+
const server = new Pretender(function() {});
62+
```
63+
4364
## The Server DSL
4465
The server DSL is inspired by express/sinatra. Pass a function to the Pretender constructor
4566
that will be invoked with the Pretender instance as its context. Available methods are

karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function(config) {
2222
'node_modules/es6-promise/dist/es6-promise.auto.js',
2323
'node_modules/abortcontroller-polyfill/dist/abortcontroller-polyfill-only.js',
2424
'node_modules/whatwg-fetch/dist/fetch.umd.js',
25-
'pretender.js',
25+
'dist/pretender.js',
2626
'test/**/*.js'
2727
],
2828

@@ -35,7 +35,7 @@ module.exports = function(config) {
3535
// preprocess matching files before serving them to the browser
3636
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
3737
preprocessors: {
38-
'pretender.js': ['coverage']
38+
'dist/pretender.js': ['coverage']
3939
},
4040

4141
coverageReporter: {

package.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
{
22
"name": "pretender",
33
"version": "2.1.0",
4-
"main": "./pretender.js",
4+
"main": "./dist/pretender.js",
5+
"module": "./src/pretender.es.js",
6+
"types": "index.d.ts",
57
"description": "Pretender is a mock server library for XMLHttpRequest and Fetch, that comes with an express/sinatra style syntax for defining routes and their handlers.",
68
"license": "MIT",
79
"engines": {
810
"node": "6.* || 8.* || 10.* || >= 11.*"
911
},
1012
"scripts": {
13+
"prepublishOnly": "npm run build && npm run tests-only",
1114
"pretest": "bower install",
15+
"build": "rollup --config",
1216
"test": "npm run lint && npm run jscs && npm run tests-only",
13-
"test-ci": "npm run pretest && npm run lint && npm run jscs && npm run tests-only-ci",
17+
"test-ci": "npm run pretest && npm run build && npm run lint && npm run jscs && npm run tests-only-ci",
1418
"tests-only": "karma start --single-run",
1519
"tests-only-ci": "karma start --single-run --browsers PhantomJS",
16-
"lint": "jshint pretender.js test",
17-
"jscs": "jscs pretender.js test",
20+
"lint": "jshint test",
21+
"jscs": "jscs test",
1822
"test:server": "karma start --no-single-run"
1923
},
2024
"repository": {
@@ -36,7 +40,14 @@
3640
"karma-sinon": "^1.0.5",
3741
"phantomjs": "^2.1.7",
3842
"qunit": "^2.6.1",
39-
"sinon": "^3.2.1"
43+
"rollup": "0.68.2",
44+
"rollup-plugin-commonjs": "^9.2.0",
45+
"rollup-plugin-multi-entry": "^2.1.0",
46+
"rollup-plugin-node-resolve": "^4.0.0",
47+
"rollup-plugin-typescript": "^1.0.0",
48+
"sinon": "^3.2.1",
49+
"tslib": "^1.9.3",
50+
"typescript": "~3.1.1"
4051
},
4152
"dependencies": {
4253
"whatwg-fetch": "^3.0.0",

rollup.config.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const commonjs = require('rollup-plugin-commonjs');
2+
const path = require('path');
3+
const resolve = require('rollup-plugin-node-resolve');
4+
const typescript = require('rollup-plugin-typescript');
5+
const pkg = require('./package.json');
6+
7+
const selfId = path.resolve(__dirname, 'src/iife-self-placeholder.js');
8+
9+
module.exports = {
10+
input: 'src/index.ts',
11+
external: [
12+
selfId,
13+
'whatwg-fetch',
14+
'fake-xml-http-request',
15+
'route-recognizer',
16+
],
17+
output: [
18+
{
19+
name: 'Pretender',
20+
file: pkg.main,
21+
format: 'iife',
22+
globals: {
23+
[selfId]: 'self',
24+
'whatwg-fetch': 'FakeFetch',
25+
'fake-xml-http-request': 'FakeXMLHttpRequest',
26+
'route-recognizer': 'RouteRecognizer',
27+
},
28+
banner: 'var FakeFetch = self.WHATWGFetch;\n' +
29+
'var FakeXMLHttpRequest = self.FakeXMLHttpRequest;\n' +
30+
'var RouteRecognizer = self.RouteRecognizer;\n',
31+
},
32+
{
33+
file: pkg.module,
34+
format: 'es'
35+
},
36+
],
37+
plugins: [
38+
commonjs(),
39+
resolve(),
40+
typescript()
41+
],
42+
};

src/iife-self-placeholder.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is just a placeholder for the build step
2+
// See the IIFE output in the Rollup config
3+
export default window;

pretender.js renamed to src/index.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
(function(self) {
2-
'use strict';
3-
4-
function getModuleDefault(module) {
5-
return module.default || module;
6-
}
7-
8-
var appearsBrowserified = typeof self !== 'undefined' &&
9-
typeof process !== 'undefined' &&
10-
(Object.prototype.toString.call(process) === '[object Object]' ||
11-
Object.prototype.toString.call(process) === '[object process]');
12-
13-
var RouteRecognizer = appearsBrowserified ? getModuleDefault(require('route-recognizer')) : self.RouteRecognizer;
14-
var FakeXMLHttpRequest = appearsBrowserified ? getModuleDefault(require('fake-xml-http-request')) :
15-
self.FakeXMLHttpRequest;
16-
17-
// fetch related ponyfills
18-
var FakeFetch = appearsBrowserified ? getModuleDefault(require('whatwg-fetch')) : self.WHATWGFetch;
1+
import self from './iife-self-placeholder';
2+
import RouteRecognizer from 'route-recognizer';
3+
import FakeXMLHttpRequest from 'fake-xml-http-request';
4+
import * as FakeFetch from 'whatwg-fetch';
195

206
/**
217
* parseURL - decompose a URL into its parts
@@ -495,12 +481,4 @@ Pretender.parseURL = parseURL;
495481
Pretender.Hosts = Hosts;
496482
Pretender.Registry = Registry;
497483

498-
if (typeof module === 'object') {
499-
module.exports = Pretender;
500-
} else if (typeof define !== 'undefined') {
501-
define('pretender', [], function() {
502-
return Pretender;
503-
});
504-
}
505-
self.Pretender = Pretender;
506-
}(self));
484+
export default Pretender;

tsconfig.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
// "lib": [], /* Specify library files to be included in the compilation. */
7+
// "allowJs": true, /* Allow javascript files to be compiled. */
8+
// "checkJs": true, /* Report errors in .js files. */
9+
"declaration": false, /* Generates corresponding '.d.ts' file. */
10+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
11+
// "sourceMap": true, /* Generates corresponding '.map' file. */
12+
// "outFile": "./", /* Concatenate and emit output to single file. */
13+
// "outDir": "./", /* Redirect output structure to the directory. */
14+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
15+
// "composite": true, /* Enable project compilation */
16+
// "removeComments": true, /* Do not emit comments to output. */
17+
"noEmit": true, /* Do not emit outputs. */
18+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
19+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
20+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
21+
22+
"newLine": "LF",
23+
24+
/* Strict Type-Checking Options */
25+
"strict": true, /* Enable all strict type-checking options. */
26+
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
27+
// "strictNullChecks": true, /* Enable strict null checks. */
28+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
29+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
30+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
31+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
32+
33+
/* Additional Checks */
34+
"noUnusedLocals": true, /* Report errors on unused locals. */
35+
"noUnusedParameters": true, /* Report errors on unused parameters. */
36+
"noImplicitReturns": false, /* Report error when not all code paths in function return a value. */
37+
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
38+
39+
/* Module Resolution Options */
40+
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
41+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
42+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
43+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
44+
// "typeRoots": [], /* List of folders to include type definitions from. */
45+
// "types": [], /* Type declaration files to be included in compilation. */
46+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
47+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
48+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
49+
50+
/* Source Map Options */
51+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
52+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
53+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
54+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
55+
56+
/* Experimental Options */
57+
// "experimentalDecorators": false, /* Enables experimental support for ES7 decorators. */
58+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
59+
}
60+
}

0 commit comments

Comments
 (0)