@fastly/compute-js-static-publish 2.4.2 → 3.0.1

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 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.
@@ -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,iBAoO5F"}
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"}
@@ -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
- for (const [index, file] of files.entries()) {
115
- const relativeFilePath = path.relative('./src', file);
116
- const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
117
- const filePath = file.slice(publicDirRoot.length);
118
- const type = contentDef === null || contentDef === void 0 ? void 0 : contentDef.type;
119
- const isStatic = staticRoots.some(root => file.startsWith(root));
120
- let query;
121
- if (contentDef == null || contentDef.binary) {
122
- query = '?staticBinary';
123
- }
124
- else {
125
- query = '?staticText';
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 += 'import { StaticAssets } from "@fastly/compute-js-static-publish";\n';
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 { contentType: type, isStatic, loadModule } = knownAssets[filePath];
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
- let content;
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
- content = 'fromBase64(file' + index + ')';
152
+ staticFileExt = '.bin';
153
+ staticFileTypeDesc = 'binary';
154
+ isBinary = true;
164
155
  }
165
156
  else {
166
- content = 'String(file' + index + ')';
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 (loadModule) {
169
+ if (loadAsModule[index]) {
170
170
  module = 'fileModule' + index;
171
171
  }
172
172
  else {
package/build/cli.js CHANGED
File without changes
@@ -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,QA0R5D"}
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");
@@ -172,13 +173,13 @@ export function initApp(commandLineValues) {
172
173
  "description": ${JSON.stringify(description)},
173
174
  "author": ${JSON.stringify(author)},
174
175
  "devDependencies": {
175
- "@fastly/compute-js-static-publish": "^2.4.0",
176
- "@fastly/expressly": "^1.0.0-alpha.7",
176
+ "@fastly/compute-js-static-publish": "^3.0.1",
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.5.12"
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": "2.4.2",
4
+ "version": "3.0.1",
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.5.12",
33
+ "@fastly/js-compute": "^1.0.1",
34
34
  "command-line-args": "^5.2.1"
35
35
  },
36
36
  "devDependencies": {
@@ -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: false
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
  };