From 2fcff9ffa6403c0f8d1f3ba35a3b2fea86119ed7 Mon Sep 17 00:00:00 2001 From: CountBleck Date: Mon, 4 Aug 2025 16:49:21 -0700 Subject: [PATCH] fix: make asinit exit if package.json has a "type" not equal to "module" Fixes AssemblyScript/website#237. When the "type" field isn't specified, or if package.json doesn't exist, it's already set to "module". This removes the remaining hole when "type" is set to, say, "commonjs", by refusing to continue. --- bin/asinit.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bin/asinit.js b/bin/asinit.js index 1da4ddedd1..6950a2341a 100755 --- a/bin/asinit.js +++ b/bin/asinit.js @@ -115,12 +115,23 @@ const paths = [ [gitignoreFile, "Git configuration that excludes compiled binaries from source control."], [asconfigFile, "Configuration file defining both a 'debug' and a 'release' target."], [packageFile, "Package info containing the necessary commands to compile to WebAssembly."], - [testsIndexFile, "Stater test to check that the module is functioning."], + [testsIndexFile, "Starter test to check that the module is functioning."], [indexHtmlFile, "Starter HTML file that loads the module in a browser."] ]; const formatPath = filePath => "./" + path.relative(projectDir, filePath).replace(/\\/g, "/"); +if (fs.existsSync(packageFile)) { + const pkg = JSON.parse(fs.readFileSync(packageFile)); + if ("type" in pkg && pkg["type"] !== "module") { + console.error(stdoutColors.red([ + `Error: The "type" field in ${formatPath(packageFile)} is set to "${pkg["type"]}".`, + ` asinit requires the "type" field to be set to "module" (ES modules).` + ].join("\n"))); + process.exit(1); + } +} + console.log([ "Version: " + version, "",