@m2c2kit/build-helpers 0.3.12 → 0.3.14

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +215 -43
  2. package/dist/index.js +16518 -7092
  3. package/package.json +16 -10
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { InputOptions } from 'rollup';
1
+ import { Plugin } from 'rollup';
2
+ import { PackageJSON, AsyncOpts } from 'resolve';
2
3
 
3
4
  /**
4
5
  * rollup plugin to hash m2c2kit assets and enable cache busting.
@@ -6,52 +7,223 @@ import { InputOptions } from 'rollup';
6
7
  * @remarks On repeated deployments, a file with the same name may be
7
8
  * cached by browsers. Thus, the old file will be used, not the updated
8
9
  * one. To enable cache busting, this rollup plugin adds hashes to filenames
9
- * for m2c2kit assets: index.js, canvaskit.wasm, images, and fonts. This is
10
- * done by: 1) parsing index.html and finding the links to css and index.js,
11
- * calculating a hash of these assets, adding a hash to the asset filenames,
12
- * and updating the links in index.html to reflect the hash. 2) parsing
13
- * index.js into an AST, finding string literals that are the urls of fonts,
14
- * images, and canvaskit.wam, calculating a hash of these assets, adding a
15
- * hash to the asset filenames, and updating the urls to reflect the hash.
16
- *
17
- * Note: index.js is transpiled from TypeScript and no longer has types, so
18
- * finding the asset urls in the AST assumes that there will be no
19
- * user code with the same structure as the asset urls. In other words:
20
- * Don't use a define a property named "canvasKitWasmUrl" that refers to
21
- * a string literal, don't define a property named "fonts" that that refers
22
- * to an array expression of object expressions with properties named
23
- * "fontName" and "url", and don't define a property called "images" that
24
- * refers to an array expression of object expressions with properties named
25
- * "imageName", "height", "width", and "url". Otherwise, this plugin may alter
26
- * your code in unexpected ways (although most likely it will simply give
27
- * warnings, because it's unlikely there will be valid file assets that will
28
- * be found and hashed).
10
+ * for files except .html and .htm files, and javascript files that are not
11
+ * index.js (index.js is the main entry point bundle file we have created
12
+ * and is referenced in index.html).
13
+ *
14
+ * In the renderChunk plugin hook, this is done by:
15
+ * 1) Replacing the string __NO_M2C2KIT_MANIFEST_JSON_URL__ with manifest.json
16
+ * in the bundled index.js file. This is done so that the game knows it must
17
+ * look up hashed filenames.
18
+ *
19
+ * In the writeBundle plugin hook, this is done by:
20
+ * 1) Creating an MD5 hash of the file contents of each file and adding the
21
+ * hash of the first 16 digits to the filename.
22
+ *
23
+ * 2) Creating a manifest.json file that maps the original filename to the
24
+ * hashed filename. This manifest will be used by the game to look up and
25
+ * load the hashed filenames.
26
+ *
27
+ * 3) Parsing index.html to find the links to css and index.js files and
28
+ * updating the links with the hashed filenames.
29
29
  *
30
30
  * @param rootDir - root directory of build, usually "dist" because you
31
31
  * usually hash only production builds
32
32
  * @param cwd - current working directory; used only for testing.
33
- * @returns
33
+ * @returns a rollup Plugin
34
34
  */
35
- declare function hashM2c2kitAssets(rootDir: string, cwd?: string): {
36
- name: string;
37
- closeBundle: {
38
- sequential: boolean;
39
- handler(): Promise<void>;
40
- };
41
- };
35
+ declare function hashM2c2kitAssets(rootDir: string, cwd?: string): Plugin;
36
+
37
+ declare function makeM2c2kitServiceWorker(rootDir: string, additionalFiles?: Array<string>): Plugin;
38
+
39
+ /**
40
+ * Restores `import.meta` to the code after the bundler has removed it.
41
+ *
42
+ * @remarks esbuild removes usage of `import.meta` (by substituting an empty
43
+ * object named `import_meta`) if it is not supported in the target environment.
44
+ * However, we will polyfill it in the browser, so we need to restore it.
45
+ * If not using esbuild, this plugin is not needed.
46
+ *
47
+ * @param pattern - the string to replace. Defaults to `import_meta = {};`
48
+ * @param replacement - the string to replace with. Defaults to `import_meta = import.meta;`
49
+ */
50
+ declare function restoreImportMeta(pattern?: string, replacement?: string): Plugin;
42
51
 
43
- declare function makeM2c2kitServiceWorker(rootDir: string, additionalFiles?: Array<string>): {
52
+ interface PackageAndExtras {
53
+ /** Package name */
44
54
  name: string;
45
- buildStart: {
46
- handler(options: InputOptions): void;
47
- };
48
- transform: {
49
- handler(code: string, id: any): string;
50
- };
51
- closeBundle: {
52
- sequential: boolean;
53
- handler(): Promise<void>;
54
- };
55
- };
56
-
57
- export { hashM2c2kitAssets, makeM2c2kitServiceWorker };
55
+ /** Extra files to copy from package */
56
+ extras?: Array<ExtraFile>;
57
+ }
58
+ /**
59
+ * Extra file to copy from package.
60
+ */
61
+ interface ExtraFile {
62
+ /** Source path, within the package folder structure, of extra file to copy. @remarks IMPORTANT: Add a wildcard * to the end of the source (`index.html*`), otherwise it will be interpreted as a folder. */
63
+ source: string;
64
+ /** Destination path, within the build output folder structure, of extra file to copy. Use the empty string (`""`) for the destination root. */
65
+ dest: string;
66
+ }
67
+ interface CopyAssetsOptions {
68
+ /**
69
+ * For assets in an external m2c2kit package, the name of the m2c2kit package
70
+ * or array of names of m2c2kit packages that need assets copied, e.g.,
71
+ * `@m2c2kit/assessment-symbol-search` or
72
+ * `["@m2c2kit/assessment-symbol-search", "@m2c2kit/survey"]`, or a
73
+ * `PackageAndExtras` object, e.g.,
74
+ * `{name: "@m2c2kit/survey", extras: [{ source: "assets/index.html*", dest: "" }]}`
75
+ */
76
+ package?: string | PackageAndExtras | Array<string | PackageAndExtras>;
77
+ /**
78
+ * For assets within this package, the _id_ or _ids_ of the
79
+ * assessment(s) to copy assets for. If the assessment is not within this
80
+ * package, then the assessment _package name_ should be provided in the
81
+ * `package` option.
82
+ */
83
+ id?: string | Array<string>;
84
+ /**
85
+ * Output folder, e.g. `dist` or `build`.
86
+ */
87
+ outputFolder: string;
88
+ /**
89
+ * Whether to log verbose output. Useful for debugging when assets can not
90
+ * be found. Default is false.
91
+ */
92
+ verbose?: boolean;
93
+ }
94
+ /**
95
+ * Copies m2c2kit assets to the bundle output folder.
96
+ *
97
+ * @remarks Assets such as images, fonts, CSS, and WebAssembly files are
98
+ * needed by assessments. This plugin copies those assets from their
99
+ * respective packages to the output folder.
100
+ *
101
+ * Packages and ids are assumed to be assessments (see exception below), and
102
+ * their assets folders **and** the wasm file from the required version of
103
+ * `@m2c2kit/core` are copied.
104
+ *
105
+ * At the end of the copy process, the `index.html` file from the `src` folder
106
+ * is copied to the output folder. This `index.html` file will have been
107
+ * created by the CLI or the user.
108
+ *
109
+ * A typical use case: Here, there is a new assessment with the id
110
+ * `my-new-assessment` being developed in this package. In addition, two
111
+ * existing assessments, `@m2c2kit/assessment-symbol-search` and
112
+ * `@m2c2kit/assessment-grid-memory`, are being used.
113
+ *
114
+ * @example
115
+ * ```
116
+ * copyAssets({
117
+ * id: "my-new-assessment",
118
+ * package: [
119
+ * "@m2c2kit/assessment-symbol-search",
120
+ * "@m2c2kit/assessment-grid-memory"
121
+ * ],
122
+ * outputFolder: "dist",
123
+ * })
124
+ * ```
125
+ *
126
+ * Exception: if the `package` option is specified and it includes
127
+ * `@m2c2kit/db`, `@m2c2kit/survey`, or `@m2c2kit/session`, then the following
128
+ * assets are copied:
129
+ * - `@m2c2kit/db`: `data.js` and `data.html`
130
+ * - `@m2c2kit/survey`: assets css folder contents
131
+ * - `@m2c2kit/session`: assets folder contents, except `index.html`
132
+ *
133
+ * A typical use case: Here, in addition to assessments, a survey will be
134
+ * administered, and thus survey CSS assets are needed.
135
+ *
136
+ * @example
137
+ * ```
138
+ * copyAssets({
139
+ * id: "my-new-assessment",
140
+ * package: [
141
+ * "@m2c2kit/assessment-symbol-search",
142
+ * "@m2c2kit/assessment-grid-memory",
143
+ * "@m2c2kit/survey"
144
+ * ],
145
+ * outputFolder: "dist",
146
+ * })
147
+ * ```
148
+ *
149
+ * If the `index.html` from `@m2c2kit/session` or `@m2c2kit/survey` is needed, it
150
+ * can be added as an extra file to copy, but this will be done only in special
151
+ * cases, such as when creating library demos within this repository.
152
+ *
153
+ * This is a very atypical, unusual use case: Here, the `index.html` file from
154
+ * `@m2c2kit/survey` is copied to the output folder on every build.
155
+ *
156
+ * @example
157
+ * ```
158
+ * copyAssets({
159
+ * package: [
160
+ * "@m2c2kit/assessment-symbol-search",
161
+ * {
162
+ * name: "@m2c2kit/survey",
163
+ * extras: [{
164
+ * source: "assets/index.html*",
165
+ * dest: "",
166
+ * }]
167
+ * }
168
+ * ],
169
+ * outputFolder: "dist",
170
+ * })
171
+ * ```
172
+ *
173
+ * Usually, the `index.html` **should not** be copied from `@m2c2kit/session` or
174
+ * `@m2c2kit/survey` on every build with the `extras` option because the
175
+ * `index.html` file in the `src` folder should be used. Thus, the `extras`
176
+ * option is only for special cases, such as when creating library demos within
177
+ * this repository (see `@m2c2kit/assessments-demo`).
178
+ *
179
+ * @param options - {@link CopyAssetsOptions}
180
+ * @returns the copyAssets rollup plugin
181
+ */
182
+ declare function copyAssets(options: CopyAssetsOptions): Plugin;
183
+
184
+ /**
185
+ * Adds module metadata to the assessment code.
186
+ *
187
+ * @remarks Use this plugin when creating an assessment that will be used
188
+ * as a module and imported into other projects. This plugin reads metadata
189
+ * from `package.json` and embeds it in the assessment code.
190
+ *
191
+ * This module metadata is important so that the m2c2kit library can locate
192
+ * assessment resources that are not bundled. The module metadata contains the
193
+ * assessment name, version, and m2c2kit dependencies.
194
+ *
195
+ * To use: add `moduleMetadata: Constants.MODULE_METADATA_PLACEHOLDER` to the
196
+ * `GameOptions` object. At build time, this plugin will replace this
197
+ * placeholder with the actual assessment metadata.
198
+ */
199
+ declare function addModuleMetadata(): Plugin;
200
+
201
+ /**
202
+ * Replaces the string `__PACKAGE_JSON_VERSION__` with version information.
203
+ *
204
+ * @remarks This plugin gets the version string from the `package.json` file
205
+ * and the short commit hash from git. It finds the string
206
+ * `__PACKAGE_JSON_VERSION__` and replaces it with the version string and the
207
+ * short commit hash in the form of `version (shortCommitHash)`.
208
+ */
209
+ declare const insertVersionString: () => Plugin;
210
+
211
+ interface ResolveAsyncResult {
212
+ /** Absolute path to resolved package */
213
+ path: string;
214
+ /** package.json of resolved package */
215
+ package: PackageJSON;
216
+ }
217
+ /**
218
+ * Resolves a package asynchronously.
219
+ *
220
+ * @remarks This is a wrapper around https://www.npmjs.com/package/resolve
221
+ * that returns a promise, rather than using the callback style.
222
+ *
223
+ * @param id - Identifier to resolve
224
+ * @param options - Options to use for resolving, optional.
225
+ * @returns Promise of {@link ResolveAsyncResult}
226
+ */
227
+ declare function resolveAsync(id: string, options: AsyncOpts): Promise<ResolveAsyncResult>;
228
+
229
+ export { type CopyAssetsOptions, type ResolveAsyncResult, addModuleMetadata, copyAssets, hashM2c2kitAssets, insertVersionString, makeM2c2kitServiceWorker, resolveAsync, restoreImportMeta };