Skip to content

Commit

Permalink
fix(es-dev-server): don't transform non-modules (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmsns authored and LarsDenBakker committed Dec 5, 2019
1 parent 750322b commit 8ad6d03
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/es-dev-server/demo/syntax/module-features.js
@@ -1,6 +1,6 @@
import { html, render } from 'lit-html';
import module from './module-features-a.js';

window.__importMeta = import.meta.url.endsWith('syntax/module-features.js');
window.__importMeta = import.meta.url.indexOf('syntax/module-features.js') > 0;
window.__staticImports = module === 'moduleFeaturesA';
window.__dynamicImports = (async () => (await import('./module-features-b.js')).default === 'moduleFeaturesB')();
@@ -0,0 +1,14 @@
export default `
<script>
(function() {
// appends a query param to each systemjs request to trigger es5 compilation
var originalResolve = System.constructor.prototype.resolve;
System.constructor.prototype.resolve = function () {
return Promise.resolve(originalResolve.apply(this, arguments))
.then(function (url) {
return url + (url.indexOf('?') > 0 ? '&' : '?') + 'transform-module';
});
};
})()
</script>
`;
Expand Up @@ -5,6 +5,7 @@ import {
getRequestFilePath,
isPolyfill,
RequestCancelledError,
shoudlTransformToModule,
} from '../utils/utils.js';
import { sendMessageToActiveBrowsers } from '../utils/message-channel.js';
import { ResolveSyntaxError } from '../utils/resolve-module-imports.js';
Expand Down Expand Up @@ -60,6 +61,7 @@ export function createCompatibilityTransformMiddleware(cfg) {
return undefined;
}

const transformModule = shoudlTransformToModule(ctx.url);
const filePath = getRequestFilePath(ctx, cfg.rootDir);
// if there is no file path, this file was not served statically
if (!filePath) {
Expand All @@ -76,6 +78,7 @@ export function createCompatibilityTransformMiddleware(cfg) {
uaCompat,
filePath,
code,
transformModule,
});
ctx.body = transformedCode;
ctx.status = 200;
Expand Down
5 changes: 3 additions & 2 deletions packages/es-dev-server/src/utils/compatibility-transform.js
Expand Up @@ -33,6 +33,7 @@ import { logDebug } from './utils.js';
* @property {import('./user-agent-compat.js').UserAgentCompat} uaCompat
* @property {string} filePath
* @property {string} code
* @property {boolean} transformModule
*/

/**
Expand Down Expand Up @@ -148,9 +149,9 @@ export function createCompatibilityTransform(cfg) {

switch (cfg.compatibilityMode) {
case compatibilityModes.AUTO:
return !file.uaCompat.supportsEsm;
return !file.uaCompat.supportsEsm && file.transformModule;
case compatibilityModes.MAX:
return true;
return true && file.transformModule;
default:
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/es-dev-server/src/utils/transform-index-html.js
@@ -1,4 +1,5 @@
import { extractResources, createIndexHTML } from '@open-wc/building-utils/index-html/index.js';
import systemJsLegacyResolveScript from '../browser-scripts/system-js-legacy-resolve.js';
import { polyfillsPresets } from './polyfills-presets.js';
import { addPolyfilledImportMaps } from './import-maps.js';
import { compatibilityModes, polyfillsModes } from '../constants.js';
Expand Down Expand Up @@ -89,6 +90,11 @@ export function getTransformedIndexHTML(cfg) {
indexHTML = addPolyfilledImportMaps(indexHTML, resources);
}

// inject systemjs resolver which appends a query param to trigger es5 compilation
if (polyfillModules) {
indexHTML = indexHTML.replace('</body>', `${systemJsLegacyResolveScript}</body>`);
}

return {
indexHTML,
inlineModules,
Expand Down
7 changes: 7 additions & 0 deletions packages/es-dev-server/src/utils/utils.js
Expand Up @@ -148,6 +148,13 @@ export function isPolyfill(url) {
return url.includes('/polyfills/');
}

/**
* @param {string} url
*/
export function shoudlTransformToModule(url) {
return url.includes('transform-module');
}

/**
* @param {string} url
*/
Expand Down
Expand Up @@ -31,8 +31,8 @@ async function fetchText(url, userAgent) {
}

async function expectCompatibilityTransform(userAgent, features = {}) {
const stage4Features = await fetchText('stage-4-features.js', userAgent);
const esModules = await fetchText('module-features.js', userAgent);
const stage4Features = await fetchText('stage-4-features.js?transform-modules', userAgent);
const esModules = await fetchText('module-features.js?transform-modules', userAgent);

expect(stage4Features).to.include(
features.objectSpread ? '_objectSpread({}, foo);' : 'bar = { ...foo',
Expand All @@ -55,9 +55,7 @@ async function expectCompatibilityTransform(userAgent, features = {}) {
: "import module from './module-features-a.js';",
);
expect(esModules).to.include("import('./module-features-b.js')");
expect(esModules).to.include(
features.esModules ? 'meta.url.endsWith' : 'import.meta.url.endsWith',
);
expect(esModules).to.include(features.esModules ? 'meta.url.indexOf' : 'import.meta.url.indexOf');
}

async function expectSupportStage3(userAgent) {
Expand Down Expand Up @@ -396,7 +394,7 @@ describe('compatibility transform middleware', () => {
);

try {
const response = await fetch(`${host}app.js`);
const response = await fetch(`${host}app.js?transform-modules`);
const responseText = await response.text();
expect(response.status).to.equal(200);
expect(responseText).to.include(
Expand Down Expand Up @@ -460,7 +458,7 @@ describe('compatibility transform middleware', () => {
});

it('transforms properly', async () => {
const response = await fetch(`${host}app.ts`);
const response = await fetch(`${host}app.ts?transform-modules`);
const responseText = await response.text();

expect(response.status).to.equal(200);
Expand Down
Expand Up @@ -47,4 +47,17 @@
}

polyfills.length ? Promise.all(polyfills).then(loadEntries) : loadEntries();
})();</script></body></html>
})();</script>
<script>
(function() {
// appends a query param to each systemjs request to trigger es5 compilation
var originalResolve = System.constructor.prototype.resolve;
System.constructor.prototype.resolve = function () {
return Promise.resolve(originalResolve.apply(this, arguments))
.then(function (url) {
return url + (url.indexOf('?') > 0 ? '&' : '?') + 'transform-module';
});
};
})()
</script>
</body></html>
Expand Up @@ -33,4 +33,17 @@
}

polyfills.length ? Promise.all(polyfills).then(loadEntries) : loadEntries();
})();</script></body></html>
})();</script>
<script>
(function() {
// appends a query param to each systemjs request to trigger es5 compilation
var originalResolve = System.constructor.prototype.resolve;
System.constructor.prototype.resolve = function () {
return Promise.resolve(originalResolve.apply(this, arguments))
.then(function (url) {
return url + (url.indexOf('?') > 0 ? '&' : '?') + 'transform-module';
});
};
})()
</script>
</body></html>
Expand Up @@ -36,4 +36,17 @@
}

polyfills.length ? Promise.all(polyfills).then(loadEntries) : loadEntries();
})();</script></body></html>
})();</script>
<script>
(function() {
// appends a query param to each systemjs request to trigger es5 compilation
var originalResolve = System.constructor.prototype.resolve;
System.constructor.prototype.resolve = function () {
return Promise.resolve(originalResolve.apply(this, arguments))
.then(function (url) {
return url + (url.indexOf('?') > 0 ? '&' : '?') + 'transform-module';
});
};
})()
</script>
</body></html>

0 comments on commit 8ad6d03

Please sign in to comment.