Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ As our public scripts are very important in our mission to privacy we decided to

## Contribute

If you want to contribute, you probabaly want to contribute to our [`/src/default.js`](src/default.js) or [`/src/auto-events.js`](src/auto-events.js) files. These files compile into a few versions of our script by [`compile.js`](compile.js). They end up on [our CDN](https://scripts.simpleanalyticscdn.com/latest.js), custom domains, and [the Cloudflare app](https://www.cloudflare.com/apps/simpleanalytics).
If you want to contribute, you probabaly want to contribute to our [`/src/default.js`](src/default.js) or [`/src/auto-events.js`](src/auto-events.js) files. These files compile into a few versions of our script by [`compile.js`](compile.js). They end up on [our CDN](https://scripts.simpleanalyticscdn.com/latest.js) and custom domains.

In `default.js` you see some weird syntax in the comments. For example, `/** if ignorepages **/` _\[some code\]_ `/** endif **/`. These comments are converted to handlebars like this: `{{#if ignorepages }}` _\[some code\]_ `{{/if}}`. In this case, [`ignorepages` is a variable](https://github.com/simpleanalytics/scripts/blob/3874b44ce5f1b0b8a7d50fb512fdcf5285a0138f/minify.js#L66) used in `minify.js` that takes care of what functionality to show in what script. Our main script is the `latest.js` script which includes all features. We also have a `light.js` script that obviously doesn't.

Expand Down
52 changes: 4 additions & 48 deletions compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const MINIFY_OPTIONS = {
nameCache: null,
};

const fillTemplate = (template, { overwriteOptions = null } = {}) => {
const fillTemplate = (template) => {
if (!template)
throw new Error("Parsing of the JavaScript failed, please use ES5 only.");
return template
Expand All @@ -36,16 +36,6 @@ const fillTemplate = (template, { overwriteOptions = null } = {}) => {
.replace(
/\{\{\s?nginxProxyHost\s?\}\}/gi,
'<!--# echo var="proxy_hostname" default="" --><!--# echo var="proxy_path" default="/simple" -->'
)
.replace(
/\\?"\{\{\s?overwriteOptions\s?\}\}\\?"/gi,
overwriteOptions
? JSON.stringify(overwriteOptions).replace(/:"([^"]+)"/gi, ":$1")
: "{}"
)
.replace(
/"\{\{\s?cloudFlareCustomDomain\s?\}\}"/gi,
'INSTALL_OPTIONS.custom_domain || "queue.simpleanalyticscdn.com"'
);
};

Expand Down Expand Up @@ -78,7 +68,6 @@ const DEFAULTS = {
nonuniquehostnames: true,
ignoremetrics: true,
dev: false,
skipnonwindow: false,
};

const LIGHT = {
Expand Down Expand Up @@ -157,36 +146,6 @@ const templates = [
apiUrlPrefix: "queue.",
},
},
{
type: "js",
input: `${__dirname}/src/default.js`,
output: `cloudflare.js`,
variables: {
...DEFAULTS,
skipnonwindow: true,
minify: false,
version: VERSION,
scriptName: `cloudflare_${VERSION}`,
sri: false,
baseUrl: "{{cloudFlareCustomDomain}}",
overwriteOptions: {
hostname: "INSTALL_OPTIONS.hostname",
collectDnt:
"typeof INSTALL_OPTIONS.collect_dnt === 'boolean' ? INSTALL_OPTIONS.collect_dnt : null",
mode: "INSTALL_OPTIONS.hash_mode ? 'hash' : 'normal'",
strictUtm:
"INSTALL_OPTIONS.advanced_settings_toggle && INSTALL_OPTIONS.strict_utm",
allowParams:
"INSTALL_OPTIONS.advanced_settings_toggle && INSTALL_OPTIONS.allow_url_parameters",
nonUniqueHostnames:
"INSTALL_OPTIONS.advanced_settings_toggle && INSTALL_OPTIONS.non_unique_hostnames",
ignorePages:
"INSTALL_OPTIONS.advanced_settings_toggle && INSTALL_OPTIONS.ignore_pages",
namespace:
"INSTALL_OPTIONS.overwrite_namespace && INSTALL_OPTIONS.namespace",
},
},
},
{
type: "js",
input: `${__dirname}/src/default.js`,
Expand Down Expand Up @@ -309,10 +268,7 @@ for (const file of files) {
const finalFileName = output.split("/").pop();

const template = Handlebars.compile(contents);
const rawCode = template({
...variables,
overwriteOptions: "{{overwriteOptions}}",
});
const rawCode = template(variables);

const date = new Date().toISOString().slice(0, 10);
const hash = require("crypto")
Expand Down Expand Up @@ -384,7 +340,7 @@ for (const file of files) {
`${error.message} at line ${sentence} position ${column}: ${codeSnippet}`
);
}
const code = fillTemplate(codeTemplate, variables);
const code = fillTemplate(codeTemplate);

const validate = template({
...variables,
Expand All @@ -411,7 +367,7 @@ for (const file of files) {
fs.mkdirSync(path.dirname(latestFile), { recursive: true });
}

const compiledMap = map ? fillTemplate(map, variables) : null;
const compiledMap = map ? fillTemplate(map) : null;
const isCustom = /^custom\//.test(output);

if (variables.version && variables.sri) {
Expand Down
Loading
Loading