Skip to content

Commit

Permalink
Major reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Feb 3, 2023
1 parent 3101675 commit 2ddf101
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 290 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"useTabs": true
}


24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ npm install -g wasm-feature-detect

```html
<script type="module">
import { simd } from "wasm-feature-detect";
simd().then(simdSupported => {
if (simdSupported) {
/* SIMD support */
} else {
/* No SIMD support */
}
});
import { simd } from "wasm-feature-detect";
simd().then((simdSupported) => {
if (simdSupported) {
/* SIMD support */
} else {
/* No SIMD support */
}
});
</script>
```

### Hotlinking from Unpkg

```html
<script type="module">
import { simd } from "https://unpkg.com/wasm-feature-detect?module";
// ...
import { simd } from "https://unpkg.com/wasm-feature-detect?module";
// ...
</script>
```

Expand All @@ -44,7 +44,7 @@ If required, there’s also a UMD version
```html
<script src="https://unpkg.com/wasm-feature-detect/dist/umd/index.js"></script>
<script>
wasmFeatureDetect.simd().then(/* same as above */);
wasmFeatureDetect.simd().then(/* same as above */);
</script>
```

Expand Down
42 changes: 21 additions & 21 deletions genwebsite.cjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
const hljs = require("highlight.js");
const md = require("markdown-it")({
highlight: function(str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (err) {
console.err("Highlighting failed:", err);
}
}
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (err) {
console.err("Highlighting failed:", err);
}
}

return ""; // use external default escaping
}
return ""; // use external default escaping
},
});
const ejs = require("ejs");
const fsp = require("fs").promises;

async function init() {
const readmeSrc = await fsp.readFile("./README.md", "utf8");
const readme = md.render(readmeSrc);
const readmeSrc = await fsp.readFile("./README.md", "utf8");
const readme = md.render(readmeSrc);

await fsp.mkdir("website").catch(e => {});
await fsp.copyFile("./dist/esm/index.js", "./website/wasm-feature-detect.js");
await fsp.copyFile(
"./node_modules/highlight.js/styles/github.css",
"./website/syntax.css"
);
await fsp.mkdir("website").catch((e) => {});
await fsp.copyFile("./dist/esm/index.js", "./website/wasm-feature-detect.js");
await fsp.copyFile(
"./node_modules/highlight.js/styles/github.css",
"./website/syntax.css"
);

const template = await fsp.readFile("./website.ejs", "utf8");
const output = ejs.render(template, { readme });
await fsp.writeFile("./website/index.html", output);
const template = await fsp.readFile("./website.ejs", "utf8");
const output = ejs.render(template, { readme });
await fsp.writeFile("./website/index.html", output);
}

init();
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"build": "npm run build:library && npm run build:readme && npm run build:dts && npm run fmt",
"build:website": "npm run build && node genwebsite.cjs",
"prepublishOnly": "npm run build",
"fmt": "prettier --write ./{,{src,rollup-plugins}/**/}*.{mjs,js,md}",
"fmt_test": "prettier --check ./{,{src,rollup-plugins}/**/}*.{mjs,js,md}",
"fmt": "prettier --no-error-on-unmatched-pattern --write ./{,src,test,rollup-plugins}/*.{mjs,cjs,js,md}",
"fmt_test": "prettier --check --no-error-on-unmatched-pattern --write ./{,src,test,rollup-plugins}/*.{mjs,cjs,js,md}",
"test": "npm run fmt_test && npm run build && node --no-warnings test/index.cjs"
},
"repository": "GoogleChromeLabs/wasm-feature-detect",
Expand All @@ -32,7 +32,7 @@
"highlight.js": "^11.7.0",
"magic-string": "^0.25.4",
"markdown-it": "^12.3.2",
"prettier": "^1.18.2",
"prettier": "^2.8.3",
"rollup": "^3.12.1",
"wabt": "^1.0.32"
}
Expand Down
8 changes: 4 additions & 4 deletions render-dts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { writeFileSync } from "fs";
import { plugins } from "./rollup-plugins/helpers.mjs";

writeFileSync(
"./dist/index.d.ts",
`export const\n${plugins
.map(({ name }) => `${name}: () => Promise<boolean>`)
.join(",\n")};`
"./dist/index.d.ts",
`export const\n${plugins
.map(({ name }) => `${name}: () => Promise<boolean>`)
.join(",\n")};`
);
46 changes: 23 additions & 23 deletions render-readme.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ import { gzipSync } from "zlib";
import { fileExists, plugins } from "./rollup-plugins/helpers.mjs";

async function run() {
let detectors = await Promise.all(
plugins.map(async ({ path, name: func }) => {
let index;
if (await fileExists(`${path}/module.wat`)) {
index = await fsp.readFile(`${path}/module.wat`, "utf8");
} else if (await fileExists(`${path}/module.wast`)) {
index = await fsp.readFile(`${path}/module.wast`, "utf8");
} else {
index = await fsp.readFile(`${path}/index.js`, "utf8");
}
const name = (/;;\s*Name:\s*(.+)$/im.exec(index) || ["", ""])[1];
const proposal = (/;;\s*Proposal:\s*(.+)$/im.exec(index) || ["", ""])[1];
return { name, proposal, func };
})
);
const lib = await fsp.readFile("./dist/esm/index.js");
const gzippedLib = gzipSync(lib, { level: 9 });
const readme = await ejs.renderFile("./README.md.ejs", {
gzippedSize: Math.round(gzippedLib.length / 10) * 10,
detectors
});
await fsp.writeFile("./README.md", readme);
let detectors = await Promise.all(
plugins.map(async ({ path, name: func }) => {
let index;
if (await fileExists(`${path}/module.wat`)) {
index = await fsp.readFile(`${path}/module.wat`, "utf8");
} else if (await fileExists(`${path}/module.wast`)) {
index = await fsp.readFile(`${path}/module.wast`, "utf8");
} else {
index = await fsp.readFile(`${path}/index.js`, "utf8");
}
const name = (/;;\s*Name:\s*(.+)$/im.exec(index) || ["", ""])[1];
const proposal = (/;;\s*Proposal:\s*(.+)$/im.exec(index) || ["", ""])[1];
return { name, proposal, func };
})
);
const lib = await fsp.readFile("./dist/esm/index.js");
const gzippedLib = gzipSync(lib, { level: 9 });
const readme = await ejs.renderFile("./README.md.ejs", {
gzippedSize: Math.round(gzippedLib.length / 10) * 10,
detectors,
});
await fsp.writeFile("./README.md", readme);
}
run().catch(err => console.error(err));
run().catch((err) => console.error(err));
138 changes: 69 additions & 69 deletions rollup-plugins/export-in-place.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,74 +28,74 @@
import MagicString from "magic-string";
import assert from "assert";

export default function() {
return {
name: "export-in-place",
export default function () {
return {
name: "export-in-place",

renderChunk(code) {
try {
const ast = this.parse(code);
// Assert for the AST shape.
// 1) We should have only two declarations at top level.
assert.strictEqual(
ast.body.length,
2,
"Bundle should have only two items at the top level."
);
const [varDecl, exportDecl] = ast.body;
// 2) First is a variable declaration (for `const ...`).
assert.strictEqual(
varDecl.type,
"VariableDeclaration",
"First top-level item should be a variable declaration."
);
// 3) Second is a local export list (`export { ... }`).
assert.strictEqual(
exportDecl.type,
"ExportNamedDeclaration",
"Second top-level item should be an export declaration."
);
assert.strictEqual(
exportDecl.declaration,
null,
"Export declaration should contain a list of items."
);
assert.strictEqual(
exportDecl.source,
null,
"Export declaration should export local items."
);
// 4) Their counts must match.
assert.strictEqual(
varDecl.declarations.length,
exportDecl.specifiers.length,
"List of exports should contain as many items as there are variables."
);
// Now, perform actual transformation - inline exported name back
// into each variable declarator.
const output = new MagicString(code);
const exportMap = new Map(
exportDecl.specifiers.map(spec => [
spec.local.name,
spec.exported.name
])
);
for (const { id } of varDecl.declarations) {
const exportedName = exportMap.get(id.name);
// Make sure we're exporting only declared vars.
assert(
exportedName,
`Export declaration for ${id.name} does not match a local variable.`
);
exportMap.delete(id.name);
output.overwrite(id.start, id.end, exportedName);
}
// Finally, prepend `export ` right before `const ...` and return it.
return `export ${output.slice(varDecl.start, varDecl.end)}`;
} catch (e) {
console.warn("Could not inline exports:", e);
return code;
}
}
};
renderChunk(code) {
try {
const ast = this.parse(code);
// Assert for the AST shape.
// 1) We should have only two declarations at top level.
assert.strictEqual(
ast.body.length,
2,
"Bundle should have only two items at the top level."
);
const [varDecl, exportDecl] = ast.body;
// 2) First is a variable declaration (for `const ...`).
assert.strictEqual(
varDecl.type,
"VariableDeclaration",
"First top-level item should be a variable declaration."
);
// 3) Second is a local export list (`export { ... }`).
assert.strictEqual(
exportDecl.type,
"ExportNamedDeclaration",
"Second top-level item should be an export declaration."
);
assert.strictEqual(
exportDecl.declaration,
null,
"Export declaration should contain a list of items."
);
assert.strictEqual(
exportDecl.source,
null,
"Export declaration should export local items."
);
// 4) Their counts must match.
assert.strictEqual(
varDecl.declarations.length,
exportDecl.specifiers.length,
"List of exports should contain as many items as there are variables."
);
// Now, perform actual transformation - inline exported name back
// into each variable declarator.
const output = new MagicString(code);
const exportMap = new Map(
exportDecl.specifiers.map((spec) => [
spec.local.name,
spec.exported.name,
])
);
for (const { id } of varDecl.declarations) {
const exportedName = exportMap.get(id.name);
// Make sure we're exporting only declared vars.
assert(
exportedName,
`Export declaration for ${id.name} does not match a local variable.`
);
exportMap.delete(id.name);
output.overwrite(id.start, id.end, exportedName);
}
// Finally, prepend `export ` right before `const ...` and return it.
return `export ${output.slice(varDecl.start, varDecl.end)}`;
} catch (e) {
console.warn("Could not inline exports:", e);
return code;
}
},
};
}
Loading

0 comments on commit 2ddf101

Please sign in to comment.