@fastly/compute-js-static-publish 1.0.0 → 2.0.0-alpha.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.
@@ -78,6 +78,7 @@ export async function buildStaticLoader() {
78
78
  console.log(`No include directories defined.`);
79
79
  }
80
80
  const includeRoots = includeDirs.map(dir => path.resolve(config.publicDir, dir));
81
+ const excludeTest = config.excludeTest;
81
82
  const files = results
82
83
  .filter(file => {
83
84
  // Exclude files that come from C@E app dir
@@ -97,20 +98,39 @@ export async function buildStaticLoader() {
97
98
  return false;
98
99
  }
99
100
  return true;
101
+ })
102
+ .filter(file => {
103
+ return excludeTest == null || !excludeTest(file);
100
104
  });
105
+ const moduleTest = config.moduleTest;
106
+ const knownAssets = {};
101
107
  let fileContents = '';
102
108
  for (const [index, file] of files.entries()) {
103
109
  const relativeFilePath = path.relative('./src', file);
104
- fileContents += `import file${index} from "${relativeFilePath}";\n`;
110
+ const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
111
+ const filePath = file.slice(publicDirRoot.length);
112
+ const type = contentDef === null || contentDef === void 0 ? void 0 : contentDef.type;
113
+ const isStatic = staticRoots.some(root => file.startsWith(root));
114
+ let query;
115
+ if (contentDef == null || contentDef.binary) {
116
+ query = '?staticBinary';
117
+ }
118
+ else {
119
+ query = '?staticText';
120
+ }
121
+ fileContents += `import file${index} from "${relativeFilePath}${query}";\n`;
122
+ let loadModule = false;
123
+ if (moduleTest != null && moduleTest(filePath)) {
124
+ loadModule = true;
125
+ fileContents += `import fileModule${index} from "${relativeFilePath}";\n`;
126
+ }
127
+ knownAssets[filePath] = { contentType: type, isStatic, loadModule, };
105
128
  }
106
- const knownAssets = {};
107
129
  fileContents += `\nexport const assets = {\n`;
108
130
  for (const [index, file] of files.entries()) {
109
131
  const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
110
132
  const filePath = file.slice(publicDirRoot.length);
111
- const type = contentDef === null || contentDef === void 0 ? void 0 : contentDef.type;
112
- const isStatic = staticRoots.some(root => file.startsWith(root));
113
- knownAssets[filePath] = { contentType: type, isStatic };
133
+ const { contentType: type, isStatic, loadModule } = knownAssets[filePath];
114
134
  if (contentDef != null) {
115
135
  console.log(filePath + ': ' + JSON.stringify(type) + (isStatic ? ' [STATIC]' : ''));
116
136
  }
@@ -124,7 +144,14 @@ export async function buildStaticLoader() {
124
144
  else {
125
145
  content = 'file' + index;
126
146
  }
127
- fileContents += ` ${JSON.stringify(filePath)}: { contentType: ${JSON.stringify(type)}, content: ${content}, isStatic: ${JSON.stringify(isStatic)} },\n`;
147
+ let module;
148
+ if (loadModule) {
149
+ module = 'fileModule' + index;
150
+ }
151
+ else {
152
+ module = 'null';
153
+ }
154
+ fileContents += ` ${JSON.stringify(filePath)}: { contentType: ${JSON.stringify(type)}, content: ${content}, module: ${module}, isStatic: ${JSON.stringify(isStatic)} },\n`;
128
155
  }
129
156
  fileContents += '};\n';
130
157
  let spaFile = (_e = config.spa) !== null && _e !== void 0 ? _e : false;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fastly/compute-js-static-publish",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "2.0.0-alpha.0",
5
5
  "description": "Static Publisher for Compute@Edge JavaScript",
6
6
  "main": "./build/index.js",
7
7
  "scripts": {
@@ -30,12 +30,12 @@
30
30
  },
31
31
  "homepage": "https://github.com/fastly/compute-js-static-publish#readme",
32
32
  "dependencies": {
33
- "command-line-args": "^5.2.1",
34
- "typescript": "^4.7.4"
33
+ "command-line-args": "^5.2.1"
35
34
  },
36
35
  "devDependencies": {
37
36
  "@types/command-line-args": "^5.2.0",
38
- "@types/node": "^18.0.0"
37
+ "@types/node": "^18.0.0",
38
+ "typescript": "^4.7.4"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=14.8"
@@ -2,35 +2,6 @@ const path = require("path");
2
2
  const webpack = require("webpack");
3
3
  const { ProvidePlugin } = webpack;
4
4
 
5
- let config;
6
- try {
7
- config = require("./static-publish.rc.js");
8
- } catch {
9
- console.error('Error loading static-publish.rc.js');
10
- process.exit(1);
11
- }
12
-
13
- let defaultContentTypes;
14
- try {
15
- defaultContentTypes = require("./default-content-types.cjs");
16
- } catch {
17
- console.error('Error loading default-content-types.cjs');
18
- process.exit(1);
19
- }
20
-
21
- const contentTypes = defaultContentTypes.mergeContentTypes(config.contentTypes ?? []);
22
-
23
- const srcDir = path.resolve('./src');
24
- const srcNodeModulesDir = path.resolve('./node_modules');
25
- const publicDir = path.resolve(config.publicDir);
26
-
27
- if (publicDir.startsWith(path.resolve())) {
28
- // If public dir is INSIDE the compute-js app dir, results may be weird
29
- console.warn('⚠️ public files directory is inside of the Compute@Edge app directory.');
30
- console.warn('This is an unsupported scenario and you may experience trouble.');
31
- console.warn('');
32
- }
33
-
34
5
  module.exports = {
35
6
  entry: "./src/index.js",
36
7
  optimization: {
@@ -49,39 +20,18 @@ module.exports = {
49
20
  // asset/source exports the source code of the asset.
50
21
  // Usage: e.g., import notFoundPage from "./page_404.html"
51
22
  {
52
- test: (file) => {
53
- if(!file.startsWith(publicDir + '/')) {
54
- return false;
55
- }
56
- if(file.startsWith(srcDir + '/')) {
57
- return false;
58
- }
59
- if(file.startsWith(srcNodeModulesDir + '/')) {
60
- return false;
61
- }
62
- // If content type is known, and it's known not to be binary.
63
- const result = defaultContentTypes.testFileContentType(contentTypes, file);
64
- return result != null && !result.binary;
65
- },
23
+ test: /\.(txt|html)/,
24
+ type: "asset/source",
25
+ },
26
+ {
27
+ // asset/source exports the source code of the asset.
28
+ resourceQuery: /staticText/,
66
29
  type: "asset/source",
67
30
  },
68
- // asset/inline exports the raw bytes of the asset.
69
- // We base64 encode them here
70
31
  {
71
- test: (file) => {
72
- if(!file.startsWith(publicDir + '/')) {
73
- return false;
74
- }
75
- if(file.startsWith(srcDir + '/')) {
76
- return false;
77
- }
78
- if(file.startsWith(srcNodeModulesDir + '/')) {
79
- return false;
80
- }
81
- // If content type unknown, or it's known to be binary.
82
- const result = defaultContentTypes.testFileContentType(contentTypes, file);
83
- return result == null || result.binary;
84
- },
32
+ // asset/inline exports the raw bytes of the asset.
33
+ // We base64 encode them here
34
+ resourceQuery: /staticBinary/,
85
35
  type: "asset/inline",
86
36
  generator: {
87
37
  /**
@@ -99,6 +99,8 @@ export async function buildStaticLoader() {
99
99
  dir => path.resolve(config.publicDir, dir)
100
100
  );
101
101
 
102
+ const excludeTest: ((path: string) => boolean) | undefined = config.excludeTest;
103
+
102
104
  const files = results
103
105
  .filter(file => {
104
106
  // Exclude files that come from C@E app dir
@@ -118,25 +120,45 @@ export async function buildStaticLoader() {
118
120
  return false;
119
121
  }
120
122
  return true;
123
+ })
124
+ .filter(file => {
125
+ return excludeTest == null || !excludeTest(file);
121
126
  });
122
127
 
128
+ const moduleTest: ((path: string) => boolean) | undefined = config.moduleTest;
129
+
130
+ const knownAssets: Record<string, {contentType: string, isStatic: boolean, loadModule: boolean}> = {};
131
+
123
132
  let fileContents = '';
124
133
 
125
134
  for (const [index, file] of files.entries()) {
126
135
  const relativeFilePath = path.relative('./src', file);
127
- fileContents += `import file${index} from "${relativeFilePath}";\n`;
128
- }
136
+ const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
137
+ const filePath = file.slice(publicDirRoot.length);
138
+ const type = contentDef?.type;
139
+ const isStatic = staticRoots.some(root => file.startsWith(root));
129
140
 
130
- const knownAssets: Record<string, {contentType: string, isStatic:boolean}> = {};
141
+ let query;
142
+ if (contentDef == null || contentDef.binary) {
143
+ query = '?staticBinary';
144
+ } else {
145
+ query = '?staticText';
146
+ }
147
+ fileContents += `import file${index} from "${relativeFilePath}${query}";\n`;
148
+ let loadModule = false;
149
+ if (moduleTest != null && moduleTest(filePath)) {
150
+ loadModule = true;
151
+ fileContents += `import fileModule${index} from "${relativeFilePath}";\n`;
152
+ }
153
+ knownAssets[filePath] = { contentType: type, isStatic, loadModule, };
154
+ }
131
155
 
132
156
  fileContents += `\nexport const assets = {\n`;
133
157
 
134
158
  for (const [index, file] of files.entries()) {
135
159
  const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
136
160
  const filePath = file.slice(publicDirRoot.length);
137
- const type = contentDef?.type;
138
- const isStatic = staticRoots.some(root => file.startsWith(root));
139
- knownAssets[filePath] = { contentType: type, isStatic };
161
+ const { contentType: type, isStatic, loadModule } = knownAssets[filePath];
140
162
 
141
163
  if (contentDef != null) {
142
164
  console.log(filePath + ': ' + JSON.stringify(type) + (isStatic ? ' [STATIC]' : ''));
@@ -151,7 +173,14 @@ export async function buildStaticLoader() {
151
173
  content = 'file' + index;
152
174
  }
153
175
 
154
- fileContents += ` ${JSON.stringify(filePath)}: { contentType: ${JSON.stringify(type)}, content: ${content}, isStatic: ${JSON.stringify(isStatic)} },\n`;
176
+ let module;
177
+ if (loadModule) {
178
+ module = 'fileModule' + index;
179
+ } else {
180
+ module = 'null';
181
+ }
182
+
183
+ fileContents += ` ${JSON.stringify(filePath)}: { contentType: ${JSON.stringify(type)}, content: ${content}, module: ${module}, isStatic: ${JSON.stringify(isStatic)} },\n`;
155
184
  }
156
185
 
157
186
  fileContents += '};\n';