Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.0.0 #1

Merged
merged 53 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
ca94201
First commit of working version for review.
usergenic May 10, 2019
f5e3cf9
Added a .travis.yml
usergenic May 10, 2019
2fb5784
Added @babel/types to devDependencies.
usergenic May 10, 2019
b5b2408
Added @types/node to resolve tsc issue on travis/linux.
usergenic May 10, 2019
57397fa
Tell Travis-CI to use the latest version of node.
usergenic May 10, 2019
75fb7f7
Added linting and formatting and depcheck and fixed types and dropped
usergenic May 15, 2019
4947390
Typescript compile target ES5 -> ES2015
usergenic May 15, 2019
c984f93
Turn on Typescript declaration file generation.
usergenic May 15, 2019
2ac77fa
Remove downlevelIteration Typescript feature because newer target ES2015
usergenic May 15, 2019
e29c902
Turn on stricter Typescript checks.
usergenic May 15, 2019
f1fe49c
Added copyright headers to typescript files.
usergenic May 15, 2019
57f7e12
Converted function x() to const x = ()
usergenic May 15, 2019
c326c1c
Removed unnecessary functions after great PR feedback.
usergenic May 15, 2019
52962f1
Removed unnecessary case from getBodyAsString function.
usergenic May 15, 2019
e8a7144
Simplified the getBodyAsString function and added type for Stream.
usergenic May 15, 2019
2b7a3f3
Cleaned up code around path/baseHref...
usergenic May 15, 2019
f6f78af
Renamed variables for clarity.
usergenic May 15, 2019
6adfd46
Renamed fslash -> forwardSlashesOnlyPlease
usergenic May 15, 2019
140fc95
Moved path/url related stuff into a path-utils.ts
usergenic May 16, 2019
e087422
Use the getBasePath function instead of duplicate regexp.
usergenic May 16, 2019
3cd9885
Simplified another function.
usergenic May 16, 2019
2abee46
Switched travis setting to use the xenial distro instead of trusty.
usergenic May 16, 2019
3f92d27
Renamed koa-npm-resolution to koa-node-resolve
usergenic May 16, 2019
826e031
Added more tsconfig checks.
usergenic May 16, 2019
ae307e1
Remove comments from tsconfig.json
usergenic May 16, 2019
9694920
Renamed a test file so it actually gets tested!
usergenic May 16, 2019
ded0f14
koa-route is a devDependency not a dependency.
usergenic May 16, 2019
0af19fc
Added 'files' to package.json
usergenic May 16, 2019
1ffd95a
Updated README per great suggestion by @aomarks
usergenic May 16, 2019
776fd66
Move the bit about only-for-dev to separate paragraph in README.
usergenic May 16, 2019
6e0ab01
Renamed NPM package to Node package.
usergenic May 16, 2019
e9e4954
More README updates.
usergenic May 16, 2019
b86b7ca
Rename resolve-npm-specifier to resolve-node-specifier.
usergenic May 16, 2019
53bc342
Update README.md per @justinfagnani
usergenic May 16, 2019
f6355f0
Removed is-stream and updated build script to do lint+format+depcheck…
usergenic May 16, 2019
fb3df3b
Added else if to eliminate unnecessary iffing.
usergenic May 16, 2019
491f4b5
Merge branch 'version-one' of github.com:polymerlabs/koa-node-resolve…
usergenic May 16, 2019
f336535
Added rimraf to depcheck.
usergenic May 16, 2019
462888f
Removed the sudo false from travis config.
usergenic May 16, 2019
595a39e
Removed , from the LICENSE.
usergenic May 16, 2019
39ac7c8
CHANGELOG 'NPM' -> 'Node'
usergenic May 16, 2019
2ee728f
Fix README nit.
usergenic May 16, 2019
ea46d96
'This' -> 'The following' import.
usergenic May 16, 2019
01a08d6
Removed the baseHref option and simplified the middleware function to…
usergenic May 18, 2019
d713b7b
Removed resolve-from and added resolve, including module,jsnext:main …
usergenic May 20, 2019
21dd991
Renamed koa-esm-specifier-transform to koa-module-specifier-transform
usergenic May 20, 2019
ac7b65e
Simplified koa-node-resolve.ts
usergenic May 20, 2019
8e973e0
Added tests for more resolve-node-specifier scenarios.
usergenic May 20, 2019
df8baca
Updated .gitignore so that test/fixtures would be included.
usergenic May 21, 2019
d06d5b7
Updated README code examples and changed export names from koaSomethi…
usergenic May 21, 2019
5eeeb78
Dont <X>value -- Do value as X
usergenic May 21, 2019
0ce2cde
Added JSDoc for the root parameter of the nodeResolve middleware.
usergenic May 21, 2019
f67c25c
Added error logging to the middleware.
usergenic May 21, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BasedOnStyle: Google
AlignAfterOpenBracket: AlwaysBreak
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BinPackArguments: false
# This breaks async functions sometimes, see
# https://github.com/Polymer/polymer-analyzer/pull/393
# BinPackParameters: false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
lib
*.tgz
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "node"
usergenic marked this conversation as resolved.
Show resolved Hide resolved
dist: xenial
cache:
directories:
- node_modules
script:
- npm run build
- npm test
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

<!--
PRs should document their user-visible changes (if any) in the
Unreleased section, uncommenting the header as necessary.
-->

## Unreleased

- Rewrites resolvable Node package specifiers in JavaScript module files.
- Rewrites resolvable Node package specifiers in HTML files in `<script type="module">` elements, honoring `<base href>` where present.
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 3-Clause License

Copyright (c) 2019 The Polymer Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 changes: 73 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,73 @@
# koa-npm-resolution
Koa middleware that transforms NPM package specifiers to relative paths
# koa-node-resolve

Middleware for Koa servers that resolves Node package specifiers in standard JS modules to relative paths for use on the web.

The following import uses a _bare module specifier_, which won't currently load natively in browsers (until [import maps](https://www.chromestatus.com/feature/5315286962012160) are available):

```js
import { foo } from "stuff";
```

`koa-node-resolve` solves this problem by resolving `stuff` using the same rules as [Node `require()`](https://nodejs.org/api/modules.html#modules_all_together), and transforming the import specifier to a path that can be loaded natively by any browser that [supports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Browser_compatibility) standard JS modules:

```js
import { foo } from "./node_modules/stuff/index.js";
```

Because this is middleware, you can use it in a simple static file server as well as a proxy server sitting in front of a test server such as the one `karma` starts up. (See [karma testing setup](#karma-testing-setup) below.)

Note: HTML and JavaScript are parsed on every request for those content-types, it is intended for use in development context to facilitate build-free testing/iteration as opposed to in a high volume production web server.

## Installation

```sh
$ npm install --save koa-node-resolve
```

## Usage

Create your own mini-development server in file `./dev-server.js`. This one depends on `koa` and `koa-static`, so you'll need to `npm install --save-dev koa koa-static` for your project to use it.

```js
const Koa = require("koa");
const server = new Koa()
.use(require("koa-node-resolve").middleware())
usergenic marked this conversation as resolved.
Show resolved Hide resolved
.use(require("koa-static")("."))
usergenic marked this conversation as resolved.
Show resolved Hide resolved
.listen(3000);
```

```sh
$ node dev-server.js
```

Now you can serve up your web assets and Node package specifiers will be transformed on request.

## Karma Testing Setup

In a `karma` setup, your `karma.conf.js` file could create the Koa server before exporting the config. The Koa server uses the `koa-proxy` package (therefore `npm install --save-dev koa-proxy`) in between the browser and the Karma server, transforming all the Node package specifiers encountered in documents located under the `base/` URL namespace, which is a special Karma behavior for partitioning the package resources under test from Karma support resources.

```js
const Koa = require("koa");
const server = new Koa()
.use(require("koa-mount")("/base", require("koa-node-resolve").middleware()))
usergenic marked this conversation as resolved.
Show resolved Hide resolved
.use(require("koa-proxy")({ host: "http://127.0.0.1:9876" }))
.listen(9877);

module.exports = config => {
config.set({
upstreamProxy: {
hostname: "127.0.0.1",
port: 9877,
},
files: [
{ pattern: "test/**/*.js", type: "module" },
{ pattern: "**/*.js", included: false },
{ pattern: "node_modules/**/*", included: false },
],
});
};
```

In this setup, the Koa proxy server that runs the Node resolution middleware will be on port 9877 and the Karma server will be on port 9876, so be sure to open up `http://127.0.0.1:9877` in your browser rather than `http://127.0.0.1:9876`. The `upstreamProxy` configuration block tells Karma, when it launches browsers, to points them to the Koa app instead of directly to the Karma server.

Note also that in this configuration its important to tell Karma that the test files are modules and to serve those up, but to list the other files, like the ones in `node_modules` as available but not "included" (i.e. Karma can serve them by request, but shouldn't add inline dependencies on them when generating its "context" HTML).
Loading