Skip to content

Commit 407c7dc

Browse files
authored
Initial version 1.0.0-pre.1 (#1)
It just works.
1 parent 14d8139 commit 407c7dc

27 files changed

+4883
-1
lines changed

.clang-format

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
BasedOnStyle: Google
2+
AlignAfterOpenBracket: AlwaysBreak
3+
AllowAllParametersOfDeclarationOnNextLine: false
4+
AllowShortBlocksOnASingleLine: false
5+
AllowShortCaseLabelsOnASingleLine: false
6+
AllowShortFunctionsOnASingleLine: None
7+
AllowShortIfStatementsOnASingleLine: false
8+
AllowShortLoopsOnASingleLine: false
9+
BinPackArguments: false
10+
# This breaks async functions sometimes, see
11+
# https://github.com/Polymer/polymer-analyzer/pull/393
12+
# BinPackParameters: false

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules/*
2+
/lib/*
3+
/*.tgz

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
node_js:
3+
- "node"
4+
dist: xenial
5+
cache:
6+
directories:
7+
- node_modules
8+
script:
9+
- npm run build
10+
- npm test

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
<!--
9+
PRs should document their user-visible changes (if any) in the
10+
Unreleased section, uncommenting the header as necessary.
11+
-->
12+
13+
<!-- ## Unreleased -->
14+
<!-- Add new unreleased items here -->

LICENSE

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2019 The Polymer Authors. All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
* Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# koa-esm-transform
2+
3+
Middleware for Koa servers that transforms standard JavaScript modules to earlier versions of JavaScript and/or AMD modules (inlining loader script `@polymer/esm-amd-loader` into the HTML), for use with older browsers.
4+
5+
Consider an HTML file containing the following inline JavaScript module:
6+
7+
```html
8+
<script type="module">
9+
import {someFunction} from './some-module.js';
10+
someFunction();
11+
</script>
12+
```
13+
14+
The koa-esm-to-amd middleware would transform that HTML code to something like the following:
15+
16+
```html
17+
<script>
18+
// An inline loader script from `@polymer/esm-amd-loader` package
19+
// which adds the `define` function used below.
20+
</script>
21+
<script>
22+
define(["./some-module.js"], function (someModule) {
23+
someModule.someFunction();
24+
});
25+
</script>
26+
```
27+
28+
Because this is middleware, you can use it in a simple static file server as well with a proxy server. This makes it possible to use in front of a test server such as the one `karma` starts up. (See [koa-karma-proxy](https://github.com/Polymer/koa-karma-proxy) for examples.)
29+
30+
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.
31+
32+
## How it works
33+
34+
By default, a set of babel transform plugins are chosen based on the known capabilities of the browser/user-agent identified in the Koa Context for the request.
35+
36+
When the downstream server returns HTML content, it is scanned for module script tags (e.g. `<script type="module">`). Any `src` attribute values are appended with the `__esmTransform` query parameter. The query parameter is added because the middleware needs to distinguish between scripts that are being imported as a module vs a traditional script and this can't necessarily be determined by looking at the script itself.
37+
38+
Inline module script content and URLs with the `__esmTransform` query parameter have their `import` specifiers appended with the `__esmTransform` to indicate the requested content should be treated as a module and compiled with the selected babel plugins.
39+
40+
Depending on plugins being used, certain support scripts will also be inlined into the HTML, such as `@polymer/esm-amd-loader` and `regenerator-runtime`.
41+
42+
## Options
43+
44+
- `babelPlugins`: Either an Array of babel plugins (e.g. `[require('@babel/plugin-transform-modules-amd')]`) or a Function that takes a Koa Context and returns an Array of babel plugins `(ctx) => []`. Providing a value for this option will override the default behavior of the middleware's capabilities-based babel plugin selection.
45+
- `exclude`: An array of requested paths or [minimatch](https://www.npmjs.com/package/minimatch) based patterns to match requested paths that should be excluded from any process/rewriting by the middleware.
46+
- `queryParam`: You can redefine the appended query parameter string from `__esmTransform` as something else.
47+
- `logger`: Middleware will call `debug`, `info`, `warn` and `error` methods on `console` to log events. If you use a different logger for your application, provide it here.
48+
- `logLevel`: Set a minimum level for events to be logged to override the default level of `info`.

_

-1
This file was deleted.

custom_typings/babel-7.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// The typings for these Babel 7 modules do not appear to be available, yet.
2+
declare module '@babel/plugin-syntax-dynamic-import';
3+
declare module '@babel/plugin-syntax-import-meta';
4+
declare module '@babel/template';

0 commit comments

Comments
 (0)