@fastly/compute-js-static-publish 2.4.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -10
- package/build/build-static.d.ts.map +1 -1
- package/build/build-static.js +37 -37
- package/build/cli.js +0 -0
- package/build/init-app.d.ts.map +1 -1
- package/build/init-app.js +3 -2
- package/package.json +2 -2
- package/resources/index.js +14 -9
- package/resources/webpack.config.js +9 -23
package/README.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Using a static site generator to build your website? Do you simply need to serve some static files? With `compute-js-static-publish`, now you can deploy and serve everything from Fastly's blazing-fast [Compute@Edge](https://developer.fastly.com/learning/compute/).
|
|
4
4
|
|
|
5
|
+
## Breaking Changes
|
|
6
|
+
|
|
7
|
+
v3.0 makes some updates that require changes to some files. If you've been using your `compute-js-static-publisher` application without modification, you may simply re-scaffold your application.
|
|
8
|
+
Otherwise, you can make some updates to your files. See [MIGRATING](#migrating) below for details.
|
|
9
|
+
|
|
5
10
|
## How it works
|
|
6
11
|
|
|
7
12
|
You have some HTML files, along with some accompanying CSS, JavaScript, image, and font files in a directory. Perhaps you've used a framework or static site generator to build these files.
|
|
@@ -50,16 +55,16 @@ npx @fastly/compute-js-static-publish \
|
|
|
50
55
|
|
|
51
56
|
Any configuration options will be written to a `static-publish.rc.js` file.
|
|
52
57
|
|
|
53
|
-
| Option | Default | Description
|
|
54
|
-
|
|
55
|
-
| `preset` | (None) | Apply default options from a specified preset. See ["Frameworks and Static Site Generators"](#usage-with-frameworks-and-static-site-generators).
|
|
56
|
-
| `output` | `./compute-js` | The directory in which to create the Compute@Edge application.
|
|
57
|
-
| `public-dir` | (None) | **Required**. The root of the directory that contains your website's public files. Files at this path will be served by the generated Compute@Edge application.
|
|
58
|
-
| `static-dir` | (None) | A subdirectory of `--public-dir` that contains the website's static assets. Files at this path will be cached by the browser for 1 year. Use versioned or hashed filenames to avoid serving stale assets.
|
|
59
|
-
| `auto-index` | `index.html,index.htm` |
|
|
60
|
-
| `auto-ext` | `.html,.htm` | Handle request paths that do not end in `/` by appending these extensions (comma-separated), in the specified order. By default, if `/path/to/a` is requested, attempt to serve `/path/to/a`, `/path/to/a.html`, then `/path/to/a.htm`.
|
|
61
|
-
| `spa` | (None) | Serve this file when the request path does not match known paths – with a `200` status code. Useful for apps that use [client-side routing](https://create-react-app.dev/docs/deployment#serving-apps-with-client-side-routing).
|
|
62
|
-
| `not-found-page` | `<public-dir>/404.html` | Serve this file when the request path does not match known paths – with a `404` status code. Useful for a custom 404 error page.
|
|
58
|
+
| Option | Default | Description |
|
|
59
|
+
|------------------|-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
60
|
+
| `preset` | (None) | Apply default options from a specified preset. See ["Frameworks and Static Site Generators"](#usage-with-frameworks-and-static-site-generators). |
|
|
61
|
+
| `output` | `./compute-js` | The directory in which to create the Compute@Edge application. |
|
|
62
|
+
| `public-dir` | (None) | **Required**. The root of the directory that contains your website's public files. Files at this path will be served by the generated Compute@Edge application. |
|
|
63
|
+
| `static-dir` | (None) | A subdirectory of `--public-dir` that contains the website's static assets. Files at this path will be cached by the browser for 1 year. Use versioned or hashed filenames to avoid serving stale assets. |
|
|
64
|
+
| `auto-index` | `index.html,index.htm` | Attempt to handle request paths by appending these names (comma-separated), in the specified order. If a path does not end in a slash, a slash is added automatically. By default, if `/path/to/a/` is requested, attempt to serve `/path/to/a/index.html`, then `/path/to/a/index.htm`. |
|
|
65
|
+
| `auto-ext` | `.html,.htm` | Handle request paths that do not end in `/` by appending these extensions (comma-separated), in the specified order. By default, if `/path/to/a` is requested, attempt to serve `/path/to/a`, `/path/to/a.html`, then `/path/to/a.htm`. |
|
|
66
|
+
| `spa` | (None) | Serve this file when the request path does not match known paths – with a `200` status code. Useful for apps that use [client-side routing](https://create-react-app.dev/docs/deployment#serving-apps-with-client-side-routing). |
|
|
67
|
+
| `not-found-page` | `<public-dir>/404.html` | Serve this file when the request path does not match known paths – with a `404` status code. Useful for a custom 404 error page. |
|
|
63
68
|
|
|
64
69
|
On subsequent builds of your Compute@Edge application, `compute-js-static-publish` will run with a special flag, `build-static`, reading from stored configuration, then scanning the `--public-dir` directory to recreate `./src/statics.js`.
|
|
65
70
|
|
|
@@ -94,6 +99,63 @@ You may still override any of these options individually.
|
|
|
94
99
|
*1 - For Next.js, consider using `@fastly/next-compute-js`, a Next.js server implementation that allows you to run
|
|
95
100
|
your Next.js application on Compute@Edge.
|
|
96
101
|
|
|
102
|
+
## Migrating
|
|
103
|
+
|
|
104
|
+
If you've updated your project to 3.0.0 or newer of `@fastly/compute-js-static-publish`, and it was scaffolded a version of this tool older than 3.0.0, you'll need to either re-scaffold your project, or make some changes to your application.
|
|
105
|
+
|
|
106
|
+
If you wish to modify your application, make the following changes:
|
|
107
|
+
|
|
108
|
+
* `webpack.config.js`
|
|
109
|
+
|
|
110
|
+
* Add a new `externals` array to the bottom if it doesn't exist already, and add the following entry:
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
module.exports = {
|
|
114
|
+
/* ... other config ... */
|
|
115
|
+
externals: [
|
|
116
|
+
({request,}, callback) => {
|
|
117
|
+
if (/^fastly:.*$/.test(request)) {
|
|
118
|
+
return callback(null, 'commonjs ' + request);
|
|
119
|
+
}
|
|
120
|
+
callback();
|
|
121
|
+
}
|
|
122
|
+
],
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
* Remove the following code from the `module.rules` array.
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
{
|
|
130
|
+
// asset/source exports the source code of the asset.
|
|
131
|
+
resourceQuery: /staticText/,
|
|
132
|
+
type: "asset/source",
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
// asset/inline exports the raw bytes of the asset.
|
|
136
|
+
// We base64 encode them here
|
|
137
|
+
resourceQuery: /staticBinary/,
|
|
138
|
+
type: "asset/inline",
|
|
139
|
+
generator: {
|
|
140
|
+
/**
|
|
141
|
+
* @param {Buffer} content
|
|
142
|
+
* @returns {string}
|
|
143
|
+
*/
|
|
144
|
+
dataUrl: content => {
|
|
145
|
+
return content.toString('base64');
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
* `.gitignore`
|
|
152
|
+
|
|
153
|
+
* Add the following entry:
|
|
154
|
+
|
|
155
|
+
```gitignore
|
|
156
|
+
/src/static-content
|
|
157
|
+
```
|
|
158
|
+
|
|
97
159
|
## Troubleshooting
|
|
98
160
|
|
|
99
161
|
If you're using Fastly CLI 4.0.0 or newer, and your project was scaffolded using a version
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-static.d.ts","sourceRoot":"","sources":["../src/build-static.ts"],"names":[],"mappings":"AAcA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAchD,wBAAsB,iBAAiB,CAAC,iBAAiB,EAAE,eAAe,CAAC,kBAAkB,
|
|
1
|
+
{"version":3,"file":"build-static.d.ts","sourceRoot":"","sources":["../src/build-static.ts"],"names":[],"mappings":"AAcA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAchD,wBAAsB,iBAAiB,CAAC,iBAAiB,EAAE,eAAe,CAAC,kBAAkB,iBAsO5F"}
|
package/build/build-static.js
CHANGED
|
@@ -110,46 +110,34 @@ export async function buildStaticLoader(commandLineValues) {
|
|
|
110
110
|
});
|
|
111
111
|
const moduleTest = config.moduleTest;
|
|
112
112
|
const knownAssets = {};
|
|
113
|
+
// Create "static content" dir that will be used to hold a copy of static files
|
|
114
|
+
// NOTE: this is needed because includeBytes doesn't seem to be able to traverse
|
|
115
|
+
// up to parent dir of Compute project.
|
|
116
|
+
const staticContentDir = './src/static-content';
|
|
117
|
+
fs.rmSync(staticContentDir, { recursive: true, force: true });
|
|
118
|
+
fs.mkdirSync(staticContentDir, { recursive: true });
|
|
113
119
|
let fileContents = '';
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
fileContents += `import file${index} from "${relativeFilePath}${query}";\n`;
|
|
128
|
-
let loadModule = false;
|
|
129
|
-
if (moduleTest != null && moduleTest(filePath)) {
|
|
130
|
-
loadModule = true;
|
|
131
|
-
fileContents += `import * as fileModule${index} from "${relativeFilePath}";\n`;
|
|
120
|
+
fileContents += 'import { StaticAssets } from "@fastly/compute-js-static-publish";\n';
|
|
121
|
+
fileContents += 'import { includeBytes } from "fastly:experimental";\n';
|
|
122
|
+
// If any files need to be loaded as modules, import them now.
|
|
123
|
+
const loadAsModule = [];
|
|
124
|
+
if (moduleTest != null) {
|
|
125
|
+
for (const [index, file] of files.entries()) {
|
|
126
|
+
const relativeFilePath = path.relative('./src', file);
|
|
127
|
+
const filePath = file.slice(publicDirRoot.length);
|
|
128
|
+
if (moduleTest(filePath)) {
|
|
129
|
+
loadAsModule[index] = true;
|
|
130
|
+
fileContents += `import * as fileModule${index} from "${relativeFilePath}";\n`;
|
|
131
|
+
}
|
|
132
132
|
}
|
|
133
|
-
knownAssets[filePath] = { contentType: type, isStatic, loadModule, };
|
|
134
133
|
}
|
|
135
|
-
fileContents += '
|
|
136
|
-
fileContents += `
|
|
137
|
-
function fromBase64(data) {
|
|
138
|
-
const raw = atob(data);
|
|
139
|
-
const rawLength = raw.length;
|
|
140
|
-
const array = new ArrayBuffer(rawLength);
|
|
141
|
-
const view = new Uint8Array(array);
|
|
142
|
-
for (let i = 0; i < rawLength; i++) {
|
|
143
|
-
view[i] = raw.charCodeAt(i);
|
|
144
|
-
}
|
|
145
|
-
return array;
|
|
146
|
-
}
|
|
147
|
-
`;
|
|
134
|
+
fileContents += 'const textDecoder = new TextDecoder();\n';
|
|
148
135
|
fileContents += `\nexport const assets = {\n`;
|
|
149
136
|
for (const [index, file] of files.entries()) {
|
|
150
137
|
const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
|
|
151
138
|
const filePath = file.slice(publicDirRoot.length);
|
|
152
|
-
const
|
|
139
|
+
const type = contentDef === null || contentDef === void 0 ? void 0 : contentDef.type;
|
|
140
|
+
const isStatic = staticRoots.some(root => file.startsWith(root));
|
|
153
141
|
if (contentDef != null) {
|
|
154
142
|
console.log('✔️ ' + filePath + ': ' + JSON.stringify(type) + (isStatic ? ' [STATIC]' : ''));
|
|
155
143
|
}
|
|
@@ -158,15 +146,27 @@ function fromBase64(data) {
|
|
|
158
146
|
console.log('⚠️ Notice: Unknown file type ' + filePath + '. Treating as binary file.');
|
|
159
147
|
}
|
|
160
148
|
}
|
|
161
|
-
|
|
149
|
+
// Serve static files by copying them into the static content dir and using includeBytes()
|
|
150
|
+
let staticFileExt, staticFileTypeDesc, isBinary;
|
|
162
151
|
if (contentDef == null || contentDef.binary) {
|
|
163
|
-
|
|
152
|
+
staticFileExt = '.bin';
|
|
153
|
+
staticFileTypeDesc = 'binary';
|
|
154
|
+
isBinary = true;
|
|
164
155
|
}
|
|
165
156
|
else {
|
|
166
|
-
|
|
157
|
+
staticFileExt = '.txt';
|
|
158
|
+
staticFileTypeDesc = 'text';
|
|
159
|
+
isBinary = false;
|
|
160
|
+
}
|
|
161
|
+
const staticFilePath = `${staticContentDir}/file${index}${staticFileExt}`;
|
|
162
|
+
fs.cpSync(file, staticFilePath);
|
|
163
|
+
console.log(`✔️ Copied static ${staticFileTypeDesc} file "${file}" to "${staticFilePath}".`);
|
|
164
|
+
let content = `includeBytes("${staticFilePath}")`;
|
|
165
|
+
if (!isBinary) {
|
|
166
|
+
content = `textDecoder.decode(${content})`;
|
|
167
167
|
}
|
|
168
168
|
let module;
|
|
169
|
-
if (
|
|
169
|
+
if (loadAsModule[index]) {
|
|
170
170
|
module = 'fileModule' + index;
|
|
171
171
|
}
|
|
172
172
|
else {
|
package/build/cli.js
CHANGED
|
File without changes
|
package/build/init-app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init-app.d.ts","sourceRoot":"","sources":["../src/init-app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAiDvD,wBAAgB,OAAO,CAAC,iBAAiB,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"init-app.d.ts","sourceRoot":"","sources":["../src/init-app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAiDvD,wBAAgB,OAAO,CAAC,iBAAiB,EAAE,kBAAkB,QA2R5D"}
|
package/build/init-app.js
CHANGED
|
@@ -161,6 +161,7 @@ export function initApp(commandLineValues) {
|
|
|
161
161
|
/bin
|
|
162
162
|
/pkg
|
|
163
163
|
/src/statics.js
|
|
164
|
+
/src/static-content
|
|
164
165
|
`;
|
|
165
166
|
const gitIgnorePath = path.resolve(computeJsDir, '.gitignore');
|
|
166
167
|
fs.writeFileSync(gitIgnorePath, gitIgnoreContent, "utf-8");
|
|
@@ -173,12 +174,12 @@ export function initApp(commandLineValues) {
|
|
|
173
174
|
"author": ${JSON.stringify(author)},
|
|
174
175
|
"devDependencies": {
|
|
175
176
|
"@fastly/compute-js-static-publish": "^2.4.0",
|
|
176
|
-
"@fastly/expressly": "^1.0.0-
|
|
177
|
+
"@fastly/expressly": "^1.0.0-beta.2",
|
|
177
178
|
"webpack": "^5.75.0",
|
|
178
179
|
"webpack-cli": "^5.0.0"
|
|
179
180
|
},
|
|
180
181
|
"dependencies": {
|
|
181
|
-
"@fastly/js-compute": "^0.
|
|
182
|
+
"@fastly/js-compute": "^1.0.1"
|
|
182
183
|
},
|
|
183
184
|
"engines": {
|
|
184
185
|
"node": ">=16.0.0"
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastly/compute-js-static-publish",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0",
|
|
5
5
|
"description": "Static Publisher for Compute@Edge JavaScript",
|
|
6
6
|
"main": "./build/index.js",
|
|
7
7
|
"types": "./build/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/fastly/compute-js-static-publish#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@fastly/js-compute": "^0.
|
|
33
|
+
"@fastly/js-compute": "^1.0.1",
|
|
34
34
|
"command-line-args": "^5.2.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
package/resources/index.js
CHANGED
|
@@ -6,15 +6,15 @@ import { staticAssets, spaFile, notFoundPageFile, autoIndex, autoExt } from './s
|
|
|
6
6
|
const router = new Router();
|
|
7
7
|
|
|
8
8
|
function getMatchingRequestPath(path) {
|
|
9
|
-
// If the path being looked up does not end in a slash, it has to
|
|
10
|
-
// match exactly one of the assets
|
|
11
|
-
if(!path.endsWith('/')) {
|
|
12
9
|
|
|
10
|
+
if(!path.endsWith('/')) {
|
|
11
|
+
// A path that does not end in a slash can match an asset directly
|
|
13
12
|
if (staticAssets.getAsset(path) != null) {
|
|
14
13
|
return path;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
// try auto-ext
|
|
16
|
+
// ... or, we can try auto-ext:
|
|
17
|
+
// looks for an asset that has the specified suffix (usually extension, such as .html)
|
|
18
18
|
if(autoExt != null) {
|
|
19
19
|
for (const extEntry of autoExt) {
|
|
20
20
|
let pathWithExt = path + extEntry;
|
|
@@ -23,15 +23,20 @@ function getMatchingRequestPath(path) {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
return null;
|
|
28
|
-
|
|
29
26
|
}
|
|
30
27
|
|
|
31
|
-
// try auto-index
|
|
28
|
+
// try auto-index:
|
|
29
|
+
// treats the path as a directory, and looks for an asset with the specified
|
|
30
|
+
// suffix (usually an index file, such as index.html)
|
|
31
|
+
let dir = path;
|
|
32
|
+
// remove all slashes from end, and add one trailing slash
|
|
33
|
+
while(dir.endsWith('/')) {
|
|
34
|
+
dir = dir.slice(0, -1);
|
|
35
|
+
}
|
|
36
|
+
dir = dir + '/';
|
|
32
37
|
if(autoIndex != null) {
|
|
33
38
|
for (const indexEntry of autoIndex) {
|
|
34
|
-
let indexPath =
|
|
39
|
+
let indexPath = dir + indexEntry;
|
|
35
40
|
if (staticAssets.getAsset(indexPath) != null) {
|
|
36
41
|
return indexPath;
|
|
37
42
|
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
const path = require("path");
|
|
2
|
-
const webpack = require("webpack");
|
|
3
|
-
const { ProvidePlugin } = webpack;
|
|
4
2
|
|
|
5
3
|
module.exports = {
|
|
6
4
|
entry: "./src/index.js",
|
|
7
5
|
optimization: {
|
|
8
|
-
minimize:
|
|
6
|
+
minimize: true
|
|
9
7
|
},
|
|
10
8
|
target: "webworker",
|
|
11
9
|
output: {
|
|
@@ -23,26 +21,6 @@ module.exports = {
|
|
|
23
21
|
test: /\.(txt|html)/,
|
|
24
22
|
type: "asset/source",
|
|
25
23
|
},
|
|
26
|
-
{
|
|
27
|
-
// asset/source exports the source code of the asset.
|
|
28
|
-
resourceQuery: /staticText/,
|
|
29
|
-
type: "asset/source",
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
// asset/inline exports the raw bytes of the asset.
|
|
33
|
-
// We base64 encode them here
|
|
34
|
-
resourceQuery: /staticBinary/,
|
|
35
|
-
type: "asset/inline",
|
|
36
|
-
generator: {
|
|
37
|
-
/**
|
|
38
|
-
* @param {Buffer} content
|
|
39
|
-
* @returns {string}
|
|
40
|
-
*/
|
|
41
|
-
dataUrl: content => {
|
|
42
|
-
return content.toString('base64');
|
|
43
|
-
},
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
24
|
],
|
|
47
25
|
},
|
|
48
26
|
plugins: [
|
|
@@ -50,4 +28,12 @@ module.exports = {
|
|
|
50
28
|
// Used for, e.g., any cross-platform WHATWG,
|
|
51
29
|
// or core nodejs modules needed for your application.
|
|
52
30
|
],
|
|
31
|
+
externals: [
|
|
32
|
+
({request,}, callback) => {
|
|
33
|
+
if (/^fastly:.*$/.test(request)) {
|
|
34
|
+
return callback(null, 'commonjs ' + request);
|
|
35
|
+
}
|
|
36
|
+
callback();
|
|
37
|
+
}
|
|
38
|
+
],
|
|
53
39
|
};
|