@m2c2kit/build-helpers 0.3.11 → 0.3.13

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 +169 -43
  2. package/dist/index.js +22526 -6825
  3. package/package.json +19 -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,177 @@ 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
+ * Name of m2c2kit package or array of names of m2c2kit packages that need
70
+ * assets copied, e.g., `@m2c2kit/assessment-symbol-search` or
71
+ * `["@m2c2kit/assessment-symbol-search", "@m2c2kit/session"]`, or a
72
+ * PackageAndExtras object, e.g.,
73
+ * {name: "@m2c2kit/session", extras: [{ source: "assets/index.html*", dest: "" }]}
74
+ */
75
+ package: string | PackageAndExtras | Array<string | PackageAndExtras>;
76
+ /**
77
+ * Output folder, e.g. `dist` or `build`.
78
+ */
79
+ outputFolder: string;
80
+ /**
81
+ * Whether to log verbose output. Useful for debugging when assets can not
82
+ * be found. Default is false.
83
+ */
84
+ verbose?: boolean;
85
+ }
86
+ /**
87
+ * Copies assets from an m2c2kit package to the bundle output folder.
88
+ *
89
+ * @remarks Assets such as images, fonts, CSS, and WebAssembly files are
90
+ * needed by assessments. This plugin copies those assets from their
91
+ * respective packages to the output folder. What is copied depends on the
92
+ * package that is specified.
93
+ * - `@m2c2kit/session`: assets folder contents **except** `index.html`
94
+ * - `@m2c2kit/db`: `data.js` and `data.html`
95
+ * - `@m2c2kit/survey`: assets folder contents
96
+ * - All other packages are assumed to be assessments, and their assets folder
97
+ * **and** the wasm file from the required version of `@m2c2kit/core` are
98
+ * copied. Note that the assessment's package name must be used, not its
99
+ * m2c2kit game `id`.
100
+ *
101
+ * At the end of the copy process, the `index.html` file from the `src` folder
102
+ * is copied to the output folder. This `index.html` file will have been created
103
+ * by the CLI or the user.
104
+ *
105
+ * If the `index.html` from `@m2c2kit/session` or `@m2c2kit/survey` is needed, it
106
+ * can be added as an extra file to copy, but this will be done only in special
107
+ * cases, such as when creating library demos within this repository.
108
+ *
109
+ * @example
110
+ * ```
111
+ * copyAssets({
112
+ * package: [
113
+ * "@m2c2kit/session",
114
+ * "@m2c2kit/assessment-symbol-search",
115
+ * {
116
+ * name: "@m2c2kit/survey",
117
+ * extras: [{
118
+ * source: "assets/index.html*",
119
+ * dest: "",
120
+ * }]
121
+ * }
122
+ * ],
123
+ * outputFolder: "dist",
124
+ * })
125
+ * ```
126
+ *
127
+ * Usually, the `index.html` **should not** be copied from `@m2c2kit/session` or
128
+ * `@m2c2kit/survey` on every build with the `extras` options because the
129
+ * `index.html` file in the `src` folder should be used. Thus, the `extras`
130
+ * option is only for special cases, such as when creating library demos within
131
+ * this repository (see `@m2c2kit/assessments-demo`).
132
+ *
133
+ * @param options - {@link CopyAssetsOptions}
134
+ * @returns
135
+ */
136
+ declare function copyAssets(options: CopyAssetsOptions): Plugin;
137
+
138
+ /**
139
+ * Adds module metadata to the assessment code.
140
+ *
141
+ * @remarks Use this plugin when creating an assessment that will be used
142
+ * as a module and imported into other projects. This plugin reads metadata
143
+ * from `package.json` and embeds it in the assessment code.
144
+ *
145
+ * This module metadata is important so that the m2c2kit library can locate
146
+ * assessment resources that are not bundled. The module metadata contains the
147
+ * assessment name, version, and m2c2kit dependencies.
148
+ *
149
+ * To use: add `moduleMetadata: Constants.MODULE_METADATA_PLACEHOLDER` to the
150
+ * `GameOptions` object. At build time, this plugin will replace this
151
+ * placeholder with the actual assessment metadata.
152
+ */
153
+ declare function addModuleMetadata(): Plugin;
154
+
155
+ /**
156
+ * Replaces the string `__PACKAGE_JSON_VERSION__` with version information.
157
+ *
158
+ * @remarks This plugin gets the version string from the `package.json` file
159
+ * and the short commit hash from git. It finds the string
160
+ * `__PACKAGE_JSON_VERSION__` and replaces it with the version string and the
161
+ * short commit hash in the form of `version (shortCommitHash)`.
162
+ */
163
+ declare const insertVersionString: () => Plugin;
164
+
165
+ interface ResolveAsyncResult {
166
+ /** Absolute path to resolved package */
167
+ path: string;
168
+ /** package.json of resolved package */
169
+ package: PackageJSON;
170
+ }
171
+ /**
172
+ * Resolves a package asynchronously.
173
+ *
174
+ * @remarks This is a wrapper around https://www.npmjs.com/package/resolve
175
+ * that returns a promise, rather than using the callback style.
176
+ *
177
+ * @param id - Identifier to resolve
178
+ * @param options - Options to use for resolving, optional.
179
+ * @returns Promise of {@link ResolveAsyncResult}
180
+ */
181
+ declare function resolveAsync(id: string, options: AsyncOpts): Promise<ResolveAsyncResult>;
182
+
183
+ export { type CopyAssetsOptions, type ResolveAsyncResult, addModuleMetadata, copyAssets, hashM2c2kitAssets, insertVersionString, makeM2c2kitServiceWorker, resolveAsync, restoreImportMeta };