ember-cli-ifa
Advanced tools
Comparing version 0.7.0 to 0.8.0
import RSVP from 'rsvp'; | ||
import $ from 'jquery'; | ||
import AssetMap from '../services/asset-map'; | ||
@@ -10,6 +9,3 @@ import { typeOf as getTypeOf } from '@ember/utils'; | ||
// This is split out like this, in order to prevent this from being accidentally replaced | ||
let replacementPath = ['__', 'asset_map_placeholder', '__'].join(''); | ||
if (!assetMapFile || assetMapFile === replacementPath) { | ||
if (!assetMapFile) { | ||
app.register('service:asset-map', AssetMap); | ||
@@ -29,12 +25,3 @@ return; | ||
let { ajax } = $; | ||
const promise = new RSVP.Promise((resolve, reject) => { | ||
let options = { | ||
type: 'GET', | ||
dataType: 'json', | ||
success: resolve, | ||
error: reject | ||
}; | ||
ajax(assetMapFile, options); | ||
}); | ||
const promise = fetch(assetMapFile).then(response => response.json()); | ||
@@ -47,4 +34,10 @@ promise.then((map = {}) => { | ||
}); | ||
}).then(() => { | ||
}) | ||
.then(() => { | ||
app.register('service:asset-map', AssetMap); | ||
}) | ||
.catch((err) => { | ||
console.error('Failed to register service:asset-map', err); | ||
}) | ||
.finally(() => { | ||
app.advanceReadiness(); | ||
@@ -51,0 +44,0 @@ }); |
@@ -9,7 +9,22 @@ import Service from '@ember/service'; | ||
fullMap: computed('map', function() { | ||
const map = get(this, 'map'); | ||
const ret = {}; | ||
Object.keys(map).forEach(k => { | ||
const v = map[k]; | ||
ret[k] = v; | ||
ret[v] = v; | ||
}); | ||
return ret; | ||
}), | ||
resolve(name) { | ||
const map = get(this, 'map') || {}; | ||
const fullMap = get(this, 'fullMap') || {}; | ||
const prepend = get(this, 'prepend'); | ||
const enabled = get(this, 'enabled'); | ||
const assetName = enabled ? map[name] : name; | ||
const assetName = enabled ? | ||
(fullMap[name] || name) : | ||
name; | ||
@@ -16,0 +31,0 @@ return `${prepend}${assetName}`; |
export default function getAssetMapData() { | ||
// This placeholder is replaced in the build step | ||
return '__asset_map_placeholder__'; | ||
const assetMapString = document.querySelector("meta[name='ember-cli-ifa:assetMap']").content; | ||
if (!assetMapString) { | ||
return; | ||
} | ||
return JSON.parse(decodeURIComponent(assetMapString)); | ||
} |
@@ -0,1 +1,25 @@ | ||
## v0.8.0 (2019-10-23) | ||
#### :boom: Breaking Change | ||
* Update minimum supported Node.js version to 10 | ||
#### :rocket: Enhancement | ||
* [#40](https://github.com/RuslanZavacky/ember-cli-ifa/pull/40) Use HTML meta to store asset map information ([@krallin](https://github.com/krallin)) | ||
* [#41](https://github.com/RuslanZavacky/ember-cli-ifa/pull/41) Passthrough fingerprinted paths ([@krallin](https://github.com/krallin)) | ||
* [#43](https://github.com/RuslanZavacky/ember-cli-ifa/pull/43) Remove jQuery ([@NullVoxPopuli](https://github.com/NullVoxPopuli)) | ||
#### :bug: Bug Fix | ||
* [#54](https://github.com/RuslanZavacky/ember-cli-ifa/pull/54) Ensure that we don't write `undefined` into the `<meta>` element value ([@Turbo87](https://github.com/Turbo87)) | ||
* [#51](https://github.com/RuslanZavacky/ember-cli-ifa/pull/51) Fall back to name in resolve ([@mydea](https://github.com/mydea)) | ||
* [#53](https://github.com/RuslanZavacky/ember-cli-ifa/pull/53) Fix broken `test/index.html` mutation ([@Turbo87](https://github.com/Turbo87)) | ||
* [#42](https://github.com/RuslanZavacky/ember-cli-ifa/pull/42) Catch errors if asset-map fails to load ([@headquarters](https://github.com/headquarters)) | ||
#### Committers: 5 | ||
- Francesco Novy ([@mydea](https://github.com/mydea)) | ||
- L. Preston Sego III ([@NullVoxPopuli](https://github.com/NullVoxPopuli)) | ||
- Michael Head ([@headquarters](https://github.com/headquarters)) | ||
- Thomas Orozco ([@krallin](https://github.com/krallin)) | ||
- Tobias Bieniek ([@Turbo87](https://github.com/Turbo87)) | ||
# 0.7.0 | ||
@@ -2,0 +26,0 @@ - [ENHANCEMENT] Upgrade to Ember 3.0 (@mydea) |
69
index.js
'use strict'; | ||
let fs = require('fs'); | ||
let path = require('path'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const MetaPlaceholder = '__ember-cli-ifa__AssetMapPlaceholder__'; | ||
function replacePlaceholder(filePath, assetMap) { | ||
const assetMapString = assetMap ? encodeURIComponent(JSON.stringify(assetMap)) : ''; | ||
const fileBody = fs.readFileSync(filePath, { encoding: 'utf-8' }); | ||
fs.writeFileSync(filePath, fileBody.replace(MetaPlaceholder, assetMapString)); | ||
} | ||
module.exports = { | ||
@@ -13,13 +21,3 @@ name: 'ember-cli-ifa', | ||
included(app) { | ||
this._super.included.apply(this, arguments); | ||
// You could overwrite the path to replace the placeholder in | ||
// By default, it looks for a `vendor.js` or `vendor-XXXXX.js` file | ||
// It looks for that file in the `/assets` folder | ||
let options = app.options['ember-cli-ifa'] || {}; | ||
this._replacePathRegex = options.replacePathRegex || /^vendor(-(\w|\d)*)?\.js$/i; | ||
}, | ||
treeForFastBoot: function(tree) { | ||
treeForFastBoot(tree) { | ||
this._isFastBoot = true; | ||
@@ -29,2 +27,10 @@ return tree; | ||
contentFor(type) { | ||
if (type !== 'head') { | ||
return; | ||
} | ||
return `<meta name="ember-cli-ifa:assetMap" content="${MetaPlaceholder}">`; | ||
}, | ||
postBuild(build) { | ||
@@ -42,4 +48,4 @@ this._super.included.apply(this, arguments); | ||
let files = fs.readdirSync(path.join(build.directory, 'assets')); | ||
let totalFiles = files.length; | ||
const files = fs.readdirSync(path.join(build.directory, 'assets')); | ||
const totalFiles = files.length; | ||
@@ -54,14 +60,2 @@ let assetFileName = null; | ||
let replacePathRegex = this._replacePathRegex; | ||
let vendorJsFileName = null; | ||
for (let i = 0; i < totalFiles; i++) { | ||
if (files[i].match(replacePathRegex)) { | ||
vendorJsFileName = files[i]; | ||
break; | ||
} | ||
} | ||
let vendorJsFilePath = path.join(build.directory, 'assets', vendorJsFileName); | ||
let vendorJsFile = fs.readFileSync(vendorJsFilePath, { encoding: 'utf-8' }); | ||
// Prepend the URL of the assetMap with the location defined in fingerprint | ||
@@ -73,6 +67,4 @@ // options. | ||
let assetFileNamePath = `${build.directory}/assets/${assetFileName}`; | ||
const assetFileNamePath = `${build.directory}/assets/${assetFileName}`; | ||
let assetMapPlaceholder; | ||
// When using fastboot, always use the inline form | ||
@@ -83,15 +75,18 @@ // As ajax is not so easily possible there | ||
} | ||
let inline = ifaConfig.inline || this._isFastBoot; | ||
const inline = ifaConfig.inline || this._isFastBoot; | ||
let assetMap; | ||
if (inline && fs.existsSync(assetFileNamePath)) { | ||
assetMapPlaceholder = fs.readFileSync(assetFileNamePath, { encoding: 'utf-8' }); | ||
assetMapPlaceholder = JSON.stringify(JSON.parse(assetMapPlaceholder)); | ||
assetMap = JSON.parse(fs.readFileSync(assetFileNamePath, { encoding: 'utf-8' })); | ||
} else if (assetFileName) { | ||
assetMapPlaceholder = `"${fingerprintPrepend}assets/${assetFileName}"`; | ||
assetMap = `${fingerprintPrepend}assets/${assetFileName}`; | ||
} | ||
// When minifiying, '__asset_map_placeholder__' may be re-written into "__asset_map_placeholder__" | ||
// So we need to replace both variants | ||
fs.writeFileSync(vendorJsFilePath, vendorJsFile.replace(/('|")(__asset_map_placeholder__)('|")/, assetMapPlaceholder)); | ||
replacePlaceholder(path.join(build.directory, 'index.html'), assetMap); | ||
let testIndexPath = path.join(build.directory, 'tests/index.html'); | ||
if (fs.existsSync(testIndexPath)) { | ||
replacePlaceholder(testIndexPath, assetMap); | ||
} | ||
} | ||
}; |
{ | ||
"name": "ember-cli-ifa", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "Inject fingerprinted assetMap.json file into your app and provide initializer, service, and helper to dynamically reference fingerprinted assets.", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -130,22 +130,4 @@ [![Build Status](https://travis-ci.org/RuslanZavacky/ember-cli-ifa.svg?branch=master)](https://travis-ci.org/RuslanZavacky/ember-cli-ifa) | ||
If `inline: true` is specified in config, contents of assetMap file will be inline into index.html. | ||
If `inline: true` is specified in config, contents of assetMap file will be inline into index.html. | ||
```html | ||
<script> | ||
... | ||
var __assetMapPlaceholder__ = { | ||
"assets": { | ||
"assets/assetMap.json": "assets/assetMap-0a0447ba419421fa257963a718324fa8.json", | ||
"assets/failed.png": "assets/failed-836936cf32381ff14d191d7b10be9a89.png", | ||
"assets/passed.png": "assets/passed-b8506cbc195c8b9db541745aee267c48.png", | ||
"assets/tomster-under-construction.png": "assets/tomster-under-construction-da524c8bc9283f759ae640b68db81f24.png" | ||
}, | ||
"prepend": "" | ||
}; | ||
</script> | ||
... | ||
``` | ||
This might save one request to assetMap.json, but will increase overall size of `index.html` file, so use carefully. |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14728
13
169
133
2