Skip to content

Commit 43cc205

Browse files
Merge branch 'master' of https://github.com/adobe/helix-cli
2 parents 876cbd5 + 2278f99 commit 43cc205

13 files changed

+230
-94
lines changed

.circleci/config.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ jobs:
157157
working_directory: build
158158
- store_artifacts:
159159
path: build/hlx_install.sh
160-
160+
161161
workflows:
162162
version: 2
163163
build:
@@ -170,4 +170,7 @@ workflows:
170170
branches:
171171
only: master
172172
- build
173-
- installer
173+
- installer:
174+
filters:
175+
branches:
176+
only: master

.vscode/launch.json

+28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@
1010
"name": "Attach",
1111
"port": 9229
1212
},
13+
{
14+
"type": "node",
15+
"request": "launch",
16+
"name": "Mocha All",
17+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
18+
"args": [
19+
"--timeout",
20+
"999999",
21+
"--colors",
22+
"${workspaceFolder}/test"
23+
],
24+
"console": "integratedTerminal",
25+
"internalConsoleOptions": "neverOpen"
26+
},
27+
{
28+
"type": "node",
29+
"request": "launch",
30+
"name": "Mocha Current File",
31+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
32+
"args": [
33+
"--timeout",
34+
"999999",
35+
"--colors",
36+
"${file}"
37+
],
38+
"console": "integratedTerminal",
39+
"internalConsoleOptions": "neverOpen"
40+
},
1341
{
1442
"type": "node",
1543
"request": "launch",

layouts/fastly/helix.vcl

+36-2
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ sub vcl_recv {
318318
set req.http.X-Action-Root = "/api/v1/web/" + table.lookup(secrets, "OPENWHISK_NAMESPACE") + "/default/hlx--static";
319319
set req.url = req.http.X-Action-Root + "?owner=" + var.owner + "&repo=" + var.repo + "&strain=" + var.strain + "&ref=" + var.ref + "&entry=" + var.entry + "&path=" + var.path + "&plain=true" + "&allow=" urlencode(req.http.X-Allow) + "&deny=" urlencode(req.http.X-Deny) + "&root=" + req.http.X-Github-Static-Root;
320320

321+
} elseif (req.http.Fastly-SSL && (req.http.X-Static == "Redirect")) {
322+
# Handle a redirect from static.js by
323+
# - fetching the resource from GitHub
324+
# - don't forget to override the Content-Type header
325+
set req.backend = F_GitHub;
321326

322327
} elsif (!req.http.Fastly-FF && req.http.Fastly-SSL && (req.url.basename ~ "(^[^\.]+)(\.?(.+))?(\.[^\.]*$)" || req.url.basename == "")) {
323328
# This is a dynamic request.
@@ -390,7 +395,8 @@ sub vcl_recv {
390395
}
391396

392397
# enable IO for image file-types
393-
if (req.url.ext ~ "(?i)(?:gif|png|jpe?g|webp)") {
398+
# but not for static images or redirected images
399+
if (req.url.ext ~ "(?i)(?:gif|png|jpe?g|webp)" && (req.http.X-Static != "Static") && (req.http.X-Static == "Redirect")) {
394400
set req.http.X-Fastly-Imageopto-Api = "fastly";
395401
}
396402

@@ -488,7 +494,31 @@ sub vcl_fetch {
488494
esi;
489495
}
490496

491-
if (beresp.status == 404 || beresp.status == 204) {
497+
if (beresp.http.X-Static == "Raw/Static") {
498+
if (beresp.status == 307) {
499+
// static.js returns a 307 if the response is greater than OpenWhisk's
500+
// delivery limit. We restart the request and deliver directly from
501+
// GitHub instead, carring over the Content-Type header that static.js guessed
502+
set req.http.X-Static = "Redirect";
503+
set req.url = beresp.http.Location;
504+
set req.http.X-Content-Type = beresp.http.X-Content-Type;
505+
restart;
506+
} else {
507+
return(deliver);
508+
}
509+
} elseif (req.http.X-Static == "Redirect") {
510+
// and this is where we fix the headers of the GitHub static response
511+
// so that they become digestible by a browser.
512+
// - recover Content-Type from X-Content-Type
513+
// - filter out GitHub-headers
514+
515+
set beresp.http.Content-Type = req.http.X-Content-Type;
516+
unset beresp.http.X-Content-Type-Options;
517+
unset beresp.http.X-Frame-Options;
518+
unset beresp.http.X-XSS-Protection;
519+
unset beresp.http.Content-Security-Policy;
520+
521+
} elseif (beresp.status == 404 || beresp.status == 204) {
492522
# That was a miss. Let's try to restart.
493523
set beresp.http.X-Status = beresp.status + "-Restart " + req.restarts;
494524
set beresp.status = 404;
@@ -566,6 +596,10 @@ sub vcl_deliver {
566596
unset resp.http.X-GW-Cache;
567597
unset resp.http.X-Static;
568598
unset resp.http.X-URL;
599+
unset resp.http.X-Content-Type;
600+
unset resp.http.X-GitHub-Request-Id;
601+
unset resp.http.X-Fastly-Request-ID;
602+
unset resp.http.X-Geo-Block-List;
569603
}
570604
return(deliver);
571605
}

package-lock.json

+5-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adobe/helix-cli",
3-
"version": "0.6.1-pre.1",
3+
"version": "0.7.2-pre.1",
44
"description": "Project Helix CLI",
55
"main": "index.js",
66
"bin": {
@@ -30,7 +30,7 @@
3030
"@adobe/hypermedia-pipeline": "^0.5.0",
3131
"@adobe/openwhisk-loggly-wrapper": "^0.3.0",
3232
"@adobe/parcel-plugin-htl": "0.8.0",
33-
"@adobe/petridish": "^1.2.2",
33+
"@adobe/petridish": "^1.3.0",
3434
"archiver": "^3.0.0",
3535
"calibre": "^1.1.1",
3636
"chalk": "^2.4.1",

src/build.cmd.js

+59-22
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ const path = require('path');
2222
const chalk = require('chalk');
2323
const fse = require('fs-extra');
2424
const klawSync = require('klaw-sync');
25-
const { DEFAULT_OPTIONS } = require('./defaults.js');
2625
const md5 = require('./md5.js');
27-
28-
// #190 sourceMappingURL annotation is incorrect
29-
// => override default publicURL
30-
// see: https://github.com/parcel-bundler/parcel/issues/1028#issuecomment-374537098
31-
DEFAULT_OPTIONS.publicURL = '.';
26+
const strainconfig = require('./strain-config-utils');
3227

3328
/**
3429
* Finds the non-htl files from the generated bundle
@@ -46,12 +41,16 @@ function findStaticFiles(bnd) {
4641
}
4742
if (bnd.htl && bnd.type === 'js') {
4843
// strip leading / from sourceMappingURL
44+
// #190 sourceMappingURL annotation is incorrect
45+
// see: https://github.com/parcel-bundler/parcel/issues/1028#issuecomment-374537098
46+
const contents = fse.readFileSync(bnd.name, 'utf8');
47+
fse.writeFileSync(bnd.name, contents.replace(/\/\/# sourceMappingURL=\//, '//# sourceMappingURL='));
4948
}
5049
if (!bnd.htl) {
5150
statics.push(bnd.name);
5251
}
5352
}
54-
bnd.childBundles.forEach((child) => {
53+
bnd.childBundles.forEach(async (child) => {
5554
statics.push(...findStaticFiles(child));
5655
});
5756
return statics;
@@ -65,7 +64,9 @@ class BuildCommand extends EventEmitter {
6564
this._target = null;
6665
this._files = null;
6766
this._distDir = null;
67+
this._webroot = null;
6868
this._cwd = process.cwd();
69+
this._strainFile = null;
6970
}
7071

7172
withDirectory(dir) {
@@ -98,6 +99,16 @@ class BuildCommand extends EventEmitter {
9899
return this;
99100
}
100101

102+
withStrainFile(file) {
103+
this._strainFile = file;
104+
return this;
105+
}
106+
107+
withWebRoot(root) {
108+
this._webroot = root;
109+
return this;
110+
}
111+
101112
async moveStaticFiles(files, report) {
102113
const jobs = files.map((src) => {
103114
const rel = path.relative(this._target, src);
@@ -117,19 +128,54 @@ class BuildCommand extends EventEmitter {
117128
}
118129

119130
async validate() {
131+
if (!this._strainFile) {
132+
this._strainFile = path.resolve(this._cwd, '.hlx', 'strains.yaml');
133+
}
134+
135+
if (!this._webroot) {
136+
const exists = await fse.pathExists(this._strainFile);
137+
if (exists) {
138+
const strains = strainconfig.load(await fse.readFile(this._strainFile, 'utf8'));
139+
const defaultStrain = strains.find(v => v.name === 'default');
140+
if (defaultStrain && defaultStrain.static && defaultStrain.static.root) {
141+
const root = defaultStrain.static.root.replace(/^\/+/, '');
142+
this._webroot = path.resolve(this._cwd, root);
143+
}
144+
}
145+
}
146+
if (!this._webroot) {
147+
this._webroot = this._cwd;
148+
}
120149
if (!this._distDir) {
121-
this._distDir = path.resolve(this._cwd, 'dist');
150+
this._distDir = path.resolve(this._webroot, 'dist');
151+
}
152+
}
153+
154+
async getBundlerOptions() {
155+
const opts = {
156+
cacheDir: path.resolve(this._cwd, '.hlx', 'cache'),
157+
target: 'node',
158+
logLevel: 3,
159+
detailedReport: true,
160+
watch: false,
161+
cache: this._cache,
162+
minify: this._minify,
163+
outDir: this._target,
164+
};
165+
const relRoot = path.relative(this._webroot, this._distDir);
166+
if (relRoot) {
167+
opts.publicUrl = `/${relRoot}`;
122168
}
169+
return opts;
123170
}
124171

125172
/**
126173
* Initializes the parcel bundler.
127174
* @param files entry files
128-
* @param options bundler options
129175
* @return {Bundler} the bundler
130176
*/
131-
// eslint-disable-next-line class-methods-use-this
132-
createBundler(files, options) {
177+
async createBundler(files) {
178+
const options = await this.getBundlerOptions();
133179
const bundler = new Bundler(files, options);
134180
bundler.addAssetType('htl', require.resolve('@adobe/parcel-plugin-htl/src/HTLPreAsset.js'));
135181
bundler.addAssetType('htl-preprocessed', require.resolve('@adobe/parcel-plugin-htl/src/HTLAsset.js'));
@@ -173,21 +219,12 @@ class BuildCommand extends EventEmitter {
173219
}
174220

175221
async run() {
176-
// override default options with command line arguments
177-
const myoptions = {
178-
...DEFAULT_OPTIONS,
179-
watch: false,
180-
cache: this._cache,
181-
minify: this._minify,
182-
outDir: this._target,
183-
};
184-
185-
this.validate();
222+
await this.validate();
186223

187224
// expand patterns from command line arguments
188225
const myfiles = this._files.reduce((a, f) => [...a, ...glob.sync(f)], []);
189226

190-
const bundler = this.createBundler(myfiles, myoptions);
227+
const bundler = await this.createBundler(myfiles);
191228
const bundle = await bundler.bundle();
192229
if (bundle) {
193230
await this.extractStaticFiles(bundle, true);

src/defaults.js

-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
module.exports.DEFAULT_OPTIONS = {
13-
cacheDir: '.hlx/cache',
14-
target: 'node',
15-
logLevel: 3,
16-
detailedReport: true,
17-
};
18-
1912
const DEFAULT_PATTERNS = ['src/**/*.htl'];
2013

2114
module.exports.defaultArgs = yargs => yargs

0 commit comments

Comments
 (0)