Skip to content

Commit

Permalink
fix gulp integration and server so that it can work with mjs files (#…
Browse files Browse the repository at this point in the history
…30292)

* make it so that src transformation only happens strictly in scripts/script-transform.ts

each module should do 1 thing and 1 thing only. currently the module
transformer also did a src/url transformation. this caused weird
mismatches when they are ran together.

- added looseScriptSrcCheck to allow for non cdn domains. This will
  allow us to transition from the bad fixture files to having all html
  files be valid AMP HTML by default.

* temp

* temp

* fix imports

* add transform back to integration

* fix html

* fix integration

* temp

* temp

* make transformers have a loose mode to be more forgiving on script src

* add more tests and fix bug for extention retention

* allow for mjs files to be served by the test server

* add glob to load mjs files in karma server

* lint fixes

* lint html

* break up this PR

* fix bad error formatting

* apply recs

* Update build-system/server/new-server/transforms/transform.ts

Co-authored-by: Caroline Liu <10456171+caroqliu@users.noreply.github.com>

* Update build-system/server/new-server/transforms/utilities/option-set.ts

Co-authored-by: Caroline Liu <10456171+caroqliu@users.noreply.github.com>

* fix other locations of "FORTESTING"

* fix esm and non esm tests to be green

* use writeFileSync instead of async method. seems to be a race

* guarantee directory exists first

* fix windows issues

* skip failing IE test temporarily

* applied recs

* apply more recs

* temporarily read from testing version.txt

* temp "only"

* dont reroute to max json files

* change error to warning

* fix max builds and integration tests against max files

* revert testing changes. we'll separete it into its own PR

Co-authored-by: Caroline Liu <10456171+caroqliu@users.noreply.github.com>
  • Loading branch information
erwinmombay and caroqliu committed Oct 14, 2020
1 parent b025f30 commit 73b3f0e
Show file tree
Hide file tree
Showing 49 changed files with 306 additions and 112 deletions.
17 changes: 14 additions & 3 deletions build-system/server/new-server/transforms/css/css-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const cwd = process.cwd();
const cssPath = isTestMode
? `${cwd}/${testDir}/css.txt`
: `${cwd}/build/css/v0.css`;
const versionPath = isTestMode
? `${cwd}/${testDir}/version.txt`
: `${cwd}/dist/version.txt`;
const versionPath = `${cwd}/${testDir}/version.txt`

const css = readFileSync(cssPath, 'utf8').toString().trim();
const version = readFileSync(versionPath, 'utf8').toString().trim();
Expand All @@ -45,8 +43,21 @@ interface StyleNode extends PostHTML.Node {
content: string[]
}

function isStyleNode(node: PostHTML.Node | string): node is StyleNode {
return node !== undefined && typeof node !== 'string' &&
(node as StyleNode).tag === 'style';
}

function prependAmpStyles(head: PostHTML.Node): PostHTML.Node {
const content = head.content || [];

const firstStyleNode = content.filter(isStyleNode)[0];

// If 'amp-runtime' already exists bail out.
if (firstStyleNode?.attrs && 'amp-runtime' in firstStyleNode.attrs) {
return head;
}

const styleNode: StyleNode = {
walk: head.walk,
match: head.match,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<head>
<style amp-runtime>.amp-runtime{}</style>
<style amp-custom>.my-personal-style {}</style>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<head>
<style amp-runtime="">.amp-runtime{}</style>
<style amp-custom="">.my-personal-style {}</style>
</head>
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,36 @@
*/

import {PostHTML} from 'posthtml';
import {URL} from 'url';
import {isValidScript, ScriptNode} from '../utilities/script';
import {CDNURLToLocalDistURL} from '../utilities/cdn';
import {isJsonScript, isValidScript, toExtension, ScriptNode, tryGetUrl} from '../utilities/script';
import {OptionSet} from '../utilities/option-set';
import minimist from 'minimist';
const argv = minimist(process.argv.slice(2));

/**
* @param head
* @param script
* @param compiled
*/
function appendModuleScript(head: PostHTML.Node, script: ScriptNode, compiled: boolean): void {
let modulePath;
if (argv.compiled || compiled) {
modulePath = CDNURLToLocalDistURL(
new URL(script.attrs.src || ''),
undefined,
'.mjs'
).toString();
script.attrs.src = modulePath.replace('.mjs', '.js');
}
else {
const urlName = script.attrs.src.toString();
modulePath = urlName.replace('.js', '.mjs');
}

const insert: ScriptNode = {
...script,
function appendModuleScript(head: PostHTML.Node, nomoduleScript: ScriptNode, options: OptionSet): void {
const modulePath = toExtension(tryGetUrl(nomoduleScript.attrs.src), '.mjs').toString();
const moduleScript : ScriptNode = {
...nomoduleScript,
attrs: {
...script.attrs,
...nomoduleScript.attrs,
src: modulePath,
type: 'module',
},
};
delete insert.attrs.nomodule;
delete moduleScript.attrs.nomodule;

(head.content || []).push(insert);
const content = head.content || [];
const nomoduleIdx = content.indexOf(nomoduleScript);
// If we are testing and in esm mode, outright replace the nomodule script
// with the module script. This is so that we testing the module script in
// isolation without a fallback.
if (options.fortesting && options.esm) {
content.splice(nomoduleIdx, 1, moduleScript);
} else {
// Add the module script after the nomodule script.
content.splice(nomoduleIdx + 1, 0, '\n', moduleScript);
}
}

/**
Expand All @@ -62,13 +54,20 @@ function appendModuleScript(head: PostHTML.Node, script: ScriptNode, compiled: b
export default function(options: OptionSet = {}): (tree: PostHTML.Node) => void {
return function(tree: PostHTML.Node): void {
let head: PostHTML.Node | undefined = undefined;
let compiled: boolean = options.compiled || false;
const scripts: Array<ScriptNode> = [];
tree.walk(node => {
if (node.tag === 'head') {
head = node;
}
if (!isValidScript(node)) {

// Make sure that isJsonScript is used before `isValidScript`. We bail out
// early if the ScriptNofe is of type="application/json" since it wouldn't
// have any src url to modify.
if (isJsonScript(node)) {
return node;
}

if (!isValidScript(node, options.looseScriptSrcCheck)) {
return node;
}

Expand All @@ -84,7 +83,7 @@ export default function(options: OptionSet = {}): (tree: PostHTML.Node) => void
}

for (const script of scripts) {
appendModuleScript(head, script, compiled);
appendModuleScript(head, script, options);
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<head>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<head>
<script async="" src="https://cdn.ampproject.org/v0.js" nomodule=""></script>
<script async="" src="https://cdn.ampproject.org/v0.mjs" type="module"></script>
<script async="" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js" nomodule=""></script>
<script async="" src="https://cdn.ampproject.org/v0/amp-bind-0.1.mjs" type="module"></script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<head>
<script async src="http://localhost:8000/amp.js"></script>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async src="http://localhost:8000/v0/amp-bind-0.1.js"></script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"looseScriptSrcCheck": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<head>
<script async="" src="http://localhost:8000/amp.js" nomodule=""></script>
<script async="" src="http://localhost:8000/amp.mjs" type="module"></script>
<script async="" src="https://cdn.ampproject.org/v0.js" nomodule=""></script>
<script async="" src="https://cdn.ampproject.org/v0.mjs" type="module"></script>
<script async="" src="http://localhost:8000/v0/amp-bind-0.1.js" nomodule=""></script>
<script async="" src="http://localhost:8000/v0/amp-bind-0.1.mjs" type="module"></script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<head>
<script async src="http://localhost:8000/amp.js"></script>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async src="http://localhost:8000/v0/amp-bind-0.1.js"></script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<head>
<script async="" src="http://localhost:8000/amp.js"></script>
<script async="" src="https://cdn.ampproject.org/v0.js" nomodule=""></script>
<script async="" src="https://cdn.ampproject.org/v0.mjs" type="module"></script>
<script async="" src="http://localhost:8000/v0/amp-bind-0.1.js"></script>
</head>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<head>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"esm": true,
"fortesting": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<head>
<script async="" src="https://cdn.ampproject.org/v0.mjs" type="module"></script>
<script async="" src="https://cdn.ampproject.org/v0/amp-bind-0.1.mjs" type="module"></script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<script type="application/json">{}</script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<script type="application/json">{}</script>
</head>
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@
*/

import {PostHTML} from 'posthtml';
import {URL} from 'url';
import {isValidScript} from '../utilities/script';
import {isJsonScript, isValidScript, tryGetUrl} from '../utilities/script';
import {CDNURLToLocalDistURL} from '../utilities/cdn';
import {OptionSet} from '../utilities/option-set';
import {parse} from 'path';

/**
* For any script, with a valid path to AMP Project CDN, replace it with a local value.
* @param script
*/
function modifySrc(script: PostHTML.Node): PostHTML.Node {
if (!isValidScript(script)) {
function modifySrc(script: PostHTML.Node, options: OptionSet): PostHTML.Node {
// Make sure that isJsonScript is used before `isValidScript`. We bail out
// early if the ScriptNode is of type="application/json" since it wouldn't
// have any src url to modify.
if (isJsonScript(script)) {
return script;
}

const src = CDNURLToLocalDistURL(new URL(script.attrs.src || '')).toString();
if (!isValidScript(script, options.looseScriptSrcCheck)) {
return script;
}

const url = tryGetUrl(script.attrs.src || '');
const parsedPath = parse(url.pathname);
const src = CDNURLToLocalDistURL(url, [null, null], parsedPath.ext, options.port, options.useMaxNames)
.toString();
script.attrs.src = src;
return script;
}
Expand All @@ -39,6 +49,8 @@ function modifySrc(script: PostHTML.Node): PostHTML.Node {
*/
export default function(options: OptionSet = {}): (tree: PostHTML.Node) => void {
return function(tree: PostHTML.Node) {
tree.match({tag: 'script'}, modifySrc);
tree.match({tag: 'script'}, (script) => {
return modifySrc(script, options);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script src="https://cdn.ampproject.org/v0.js" async></script>
<script src="https://cdn.ampproject.org/v0/amp-bind-0.1.js" async></script>
<script src="https://cdn.ampproject.org/v0/amp-bind-0.2.js" async></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"useMaxNames": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script src="http://localhost:8000/dist/amp.js" async=""></script>
<script src="http://localhost:8000/dist/v0/amp-bind-0.1.max.js" async=""></script>
<script src="http://localhost:8000/dist/v0/amp-bind-0.2.max.js" async=""></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script src="https://cdn/v0.js" async></script>
<script src="amp.js" async></script>
<script src="http://localhost:8000/amp.js" async></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"looseScriptSrcCheck": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script src="http://localhost:8000/dist/v0.js" async=""></script>
<script src="http://localhost:8000/dist/amp.js" async=""></script>
<script src="http://localhost:8000/dist/amp.js" async=""></script>
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<script src="https://cdn/v0.js" async></script>
<script src="/dist/amp.js" async></script>
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<script src="https://cdn/v0.js" async=""></script>
<script src="/dist/amp.js" async=""></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script async="" src="http://localhost:8000/amp.js"></script>
<script async="" src="https://cdn.ampproject.org/v0.js" nomodule=""></script>
<script async="" src="https://cdn.ampproject.org/v0.mjs" type="module"></script>
<script async="" src="http://localhost:8000/v0/amp-bind-0.1.js"></script>
<script async="" src="/dist/amp.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"port": 9876,
"looseScriptSrcCheck": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script async="" src="http://localhost:9876/dist/amp.js"></script>
<script async="" src="http://localhost:9876/dist/v0.js" nomodule=""></script>
<script async="" src="http://localhost:9876/dist/v0.mjs" type="module"></script>
<script async="" src="http://localhost:9876/dist/v0/amp-bind-0.1.js"></script>
<script async="" src="http://localhost:9876/dist/dist/amp.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script async="" src="http://localhost:8000/amp.js"></script>
<script async="" src="https://cdn.ampproject.org/v0.js" nomodule=""></script>
<script async="" src="https://cdn.ampproject.org/v0.mjs" type="module"></script>
<script async="" src="http://localhost:8000/v0/amp-bind-0.1.js"></script>
<script async="" src="/dist/amp.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"port": 9876
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script async="" src="http://localhost:8000/amp.js"></script>
<script async="" src="http://localhost:9876/dist/v0.js" nomodule=""></script>
<script async="" src="http://localhost:9876/dist/v0.mjs" type="module"></script>
<script async="" src="http://localhost:8000/v0/amp-bind-0.1.js"></script>
<script async="" src="/dist/amp.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script async="" src="https://cdn.ampproject.org/v0.js"></script>
<script async="" src="https://cdn.ampproject.org/v0.mjs"></script>
<script async="" src="https://cdn.ampproject.org/v0.sxg.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script async="" src="http://localhost:8000/dist/v0.js"></script>
<script async="" src="http://localhost:8000/dist/v0.mjs"></script>
<script async="" src="http://localhost:8000/dist/v0.sxg.js"></script>
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<script src="https://cdn.ampproject.org/v0.js" async></script>
<script src="https://cdn.ampproject.org/v0/amp-bind-0.1.js" async></script>
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<script src="http://localhost:8000/dist/v0.js" async=""></script>
<script src="http://localhost:8000/dist/v0/amp-bind-0.1.js" async=""></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<script type="application/json">{}</script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<script type="application/json">{}</script>
</head>

This file was deleted.

0 comments on commit 73b3f0e

Please sign in to comment.