-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added LD+JSON asset and test case (#1936)
- Loading branch information
1 parent
e6c29bc
commit 809925b
Showing
9 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const urlJoin = require('../utils/urlJoin'); | ||
const isURL = require('../utils/is-url'); | ||
const Asset = require('../Asset'); | ||
const logger = require('../Logger'); | ||
|
||
// A list of all attributes in a schema that may produce a dependency | ||
// Based on https://schema.org/ImageObject | ||
// Section "Instances of ImageObject may appear as values for the following properties" | ||
const SCHEMA_ATTRS = [ | ||
'logo', | ||
'photo', | ||
'image', | ||
'thumbnail', | ||
'screenshot', | ||
'primaryImageOfPage', | ||
'embedUrl', | ||
'thumbnailUrl', | ||
'video', | ||
'contentUrl' | ||
]; | ||
|
||
class JSONLDAsset extends Asset { | ||
constructor(name, options) { | ||
super(name, options); | ||
this.type = 'jsonld'; | ||
} | ||
|
||
parse(content) { | ||
return JSON.parse(content.trim()); | ||
} | ||
|
||
collectDependencies() { | ||
if (!this.options.publicURL.startsWith('http')) { | ||
logger.warn( | ||
"Please specify a publicURL using --public-url, otherwise schema assets won't be collected" | ||
); | ||
return; | ||
} | ||
|
||
for (let schemaKey in this.ast) { | ||
if (SCHEMA_ATTRS.includes(schemaKey)) { | ||
this.collectFromKey(this.ast, schemaKey); | ||
this.isAstDirty = true; | ||
} | ||
} | ||
} | ||
|
||
// Auxiliary method for collectDependencies() to use for recursion | ||
collectFromKey(schema, schemaKey) { | ||
if (!schema.hasOwnProperty(schemaKey)) { | ||
return; | ||
} | ||
// values can be strings or objects | ||
// if it's not a string, it should have a url | ||
if (typeof schema[schemaKey] === 'string') { | ||
let assetPath = this.addURLDependency(schema[schemaKey]); | ||
if (!isURL(assetPath)) { | ||
// paths aren't allowed, values must be urls | ||
assetPath = urlJoin(this.options.publicURL, assetPath); | ||
} | ||
schema[schemaKey] = assetPath; | ||
} else { | ||
this.collectFromKey(schema[schemaKey], 'url'); | ||
} | ||
} | ||
|
||
generate() { | ||
if (this.options.production) { | ||
return JSON.stringify(this.ast); | ||
} else { | ||
return JSON.stringify(this.ast, null, 2); | ||
} | ||
} | ||
} | ||
|
||
module.exports = JSONLDAsset; |
Binary file added
BIN
+68.5 KB
packages/core/parcel-bundler/test/integration/schema-jsonld/images/image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+138 KB
packages/core/parcel-bundler/test/integration/schema-jsonld/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions
29
packages/core/parcel-bundler/test/integration/schema-jsonld/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<html> | ||
<head> | ||
<title>hi</title> | ||
<link rel="stylesheet" href="other.css"> | ||
</head> | ||
<body> | ||
|
||
<p>text 123</p> | ||
|
||
<script type="application/ld+json"> | ||
{ | ||
"@context": "http://schema.org", | ||
"@type": "LocalBusiness", | ||
"description": "This is your business description.", | ||
"name": "Parcel's parcel", | ||
"telephone": "555-111-2345", | ||
"openingHours": "Mo,Tu,We,Th,Fr 09:00-17:00", | ||
"logo": { | ||
"@type": "ImageObject", | ||
"url": "images/logo.png", | ||
"width": 180, | ||
"height": 120 | ||
}, | ||
"image": "images/image.jpeg" | ||
} | ||
</script> | ||
|
||
</body> | ||
</html> |
3 changes: 3 additions & 0 deletions
3
packages/core/parcel-bundler/test/integration/schema-jsonld/other.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.other { | ||
color: green; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const {bundle, assertBundleTree} = require('./utils'); | ||
|
||
describe('schema ld+json', function() { | ||
it('Should parse a LD+JSON schema and collect dependencies', async function() { | ||
let b = await bundle(__dirname + '/integration/schema-jsonld/index.html', { | ||
production: true, | ||
publicURL: 'https://place.holder/' | ||
}); | ||
|
||
await assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.html'], | ||
childBundles: [ | ||
{ | ||
type: 'jpeg' | ||
}, | ||
{ | ||
type: 'png' | ||
}, | ||
{ | ||
type: 'css' | ||
} | ||
] | ||
}); | ||
}); | ||
}); |