Skip to content

Commit

Permalink
Switched code running in Node directly to ECMAScript 6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
palant committed Jun 10, 2020
1 parent a3b2e0e commit 338972b
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 76 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
@@ -1,8 +1,5 @@
language: node_js
node_js:
- "10"
- "11"
- "12"
- "13"
- "14"
before_script:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ You can get an idea of how PfP works by using the [online version](https://pfp.w
Installing build prerequisites
------------------------------

In order to build PfP you will need to install [Node.js](https://nodejs.org/) first (Node 10.0 or higher is required). You will also need [Gulp](http://gulpjs.com/), run the following command to install it (administrator privileges required):
In order to build PfP you will need to install [Node.js](https://nodejs.org/) first (Node 13.0 or higher is required). You will also need [Gulp](http://gulpjs.com/), run the following command to install it (administrator privileges required):

npm install --global gulp-cli

Expand Down
3 changes: 1 addition & 2 deletions contentScript/.eslintrc.json
@@ -1,7 +1,6 @@
{
"env": {
"node": false,
"browser": true,
"commonjs": true
"browser": true
}
}
2 changes: 1 addition & 1 deletion generateVowelsRegexp.js
Expand Up @@ -8,7 +8,7 @@

"use strict";

const https = require("https");
import https from "https";

// Only Latin, Greek and Cyrillic vowels for now, derivations of those will
// be determined below.
Expand Down
4 changes: 2 additions & 2 deletions globalLoader.js
Expand Up @@ -6,7 +6,7 @@

"use strict";

module.exports = function(map)
export default function(map)
{
return {
name: "global-loader",
Expand All @@ -24,4 +24,4 @@ module.exports = function(map)
return "export default " + map[id];
}
};
};
}
44 changes: 21 additions & 23 deletions gulp-utils.js
Expand Up @@ -6,20 +6,22 @@

"use strict";

let fs = require("fs");
let path = require("path");
let {spawn} = require("child_process");
let {Duplex, Transform} = require("stream");

exports.readArg = function(prefix, defaultValue)
import fs from "fs";
import path from "path";
import {spawn} from "child_process";
import {Duplex, Transform} from "stream";
import {TextEncoder, TextDecoder} from "util";
import sandboxedModule from "sandboxed-module";

export function readArg(prefix, defaultValue)
{
for (let arg of process.argv)
if (arg.startsWith(prefix))
return arg.substr(prefix.length);
return defaultValue;
};
}

function transform(modifier, opts)
export function transform(modifier, opts)
{
if (!opts)
opts = {};
Expand Down Expand Up @@ -53,9 +55,8 @@ function transform(modifier, opts)
};
return stream;
}
exports.transform = transform;

exports.jsonModify = function(modifier, newName)
export function jsonModify(modifier, newName)
{
return transform((filepath, contents) =>
{
Expand All @@ -65,9 +66,9 @@ exports.jsonModify = function(modifier, newName)
filepath = path.resolve(filepath, "..", newName);
return [filepath, JSON.stringify(data, null, 2)];
});
};
}

exports.combineLocales = function()
export function combineLocales()
{
let rootDir = path.join(process.cwd(), "locale");
let locales = {};
Expand Down Expand Up @@ -120,9 +121,9 @@ exports.combineLocales = function()
});

return stream;
};
}

exports.toChromeLocale = function()
export function toChromeLocale()
{
return transform((filepath, contents) =>
{
Expand All @@ -132,7 +133,7 @@ exports.toChromeLocale = function()
data[key] = {message: strings[key]};

let locale = path.basename(filepath, ".json");
let manifest = require("./package.json");
let manifest = JSON.parse(fs.readFileSync("./package.json"));
data.name = {"message": manifest.title};
data.description = {"message": manifest.description};
if ("locales" in manifest && locale in manifest.locales)
Expand All @@ -149,9 +150,9 @@ exports.toChromeLocale = function()
JSON.stringify(data, null, 2)
];
});
};
}

exports.runTests = function()
export function runTests()
{
function escape_string(str)
{
Expand All @@ -170,8 +171,6 @@ exports.runTests = function()
}
}

let {TextEncoder, TextDecoder} = require("util");

class WorkerEventTarget
{
constructor(other)
Expand Down Expand Up @@ -224,7 +223,7 @@ exports.runTests = function()
{
super();

require("sandboxed-module").require(url, {
sandboxedModule.require(url, {
globals: {
self: new WorkerEventTarget(this)
}
Expand All @@ -234,9 +233,8 @@ exports.runTests = function()

let atob = str => Buffer.from(str, "base64").toString("binary");
let btoa = str => Buffer.from(str, "binary").toString("base64");
let {URL} = require("url");

let nodeunit = require("sandboxed-module").require("nodeunit", {
let nodeunit = sandboxedModule.require("nodeunit", {
globals: {
console, process, Buffer, TextEncoder, TextDecoder, atob, btoa, URL,
Worker: FakeWorker,
Expand All @@ -260,4 +258,4 @@ exports.runTests = function()
});
});
});
};
}
61 changes: 32 additions & 29 deletions gulpfile.js
Expand Up @@ -6,26 +6,32 @@

"use strict";

const fs = require("fs");
const path = require("path");
const url = require("url");
import fs from "fs";
import path from "path";
import url from "url";

const del = require("del");
const gulp = require("gulp");
const eslint = require("gulp-eslint");
const htmlhint = require("gulp-htmlhint");
const rollupStream = require("gulp-better-rollup");
const sass = require("gulp-sass");
const stylelint = require("gulp-stylelint");
const zip = require("gulp-zip");
const merge = require("merge-stream");
const babel = require("@rollup/plugin-babel").default;
const commonjs = require("@rollup/plugin-commonjs");
const resolve = require("@rollup/plugin-node-resolve").default;
const vue = require("rollup-plugin-vue");
import del from "del";
import gulp from "gulp";
import eslint from "gulp-eslint";
import htmlhint from "gulp-htmlhint";
import rollupStream from "gulp-better-rollup";
import sass from "gulp-sass";
import stylelint from "gulp-stylelint";
import zip from "gulp-zip";
import merge from "merge-stream";
import babel from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import vue from "rollup-plugin-vue";

const replace = require("./replacePlugin");
const utils = require("./gulp-utils");
import globalLoader from "./globalLoader.js";
import * as utils from "./gulp-utils.js";
import localeLoader from "./localeLoader.js";
import replace from "./replacePlugin.js";
import workerLoader from "./workerLoader.js";

const VERSION = JSON.parse(fs.readFileSync("./manifest.json")).version;
const __dirname = process.cwd(); // import.meta unsupported by current eslint version

function rollup(overrides = {})
{
Expand All @@ -37,12 +43,12 @@ function rollup(overrides = {})
return rollupStream({
plugins: [
...prePlugins,
require("./globalLoader")({
globalLoader({
vue: "Vue",
jsqr: "JSQR",
zxcvbn: "zxcvbn"
}),
resolve(),
resolve.default(),
commonjs({
include: ["node_modules/**"]
}),
Expand Down Expand Up @@ -260,11 +266,11 @@ gulp.task("build-web", gulp.series("validate", function buildWeb()
[path.resolve(__dirname, "lib", "browserAPI.js")]: path.resolve(__dirname, "web", "backgroundBrowserAPI.js"),
[path.resolve(__dirname, "ui", "browserAPI.js")]: path.resolve(__dirname, "web", "contentBrowserAPI.js")
}),
require("./workerLoader")(/\/(scrypt|pbkdf2)\.js$/),
require("./localeLoader")(path.resolve(__dirname, "locale", "en_US"))
workerLoader(/\/(scrypt|pbkdf2)\.js$/),
localeLoader(path.resolve(__dirname, "locale", "en_US"))
],
postPlugins: [
babel({
babel.default({
babelrc: false,
babelHelpers: "bundled",
presets: ["@babel/preset-env"]
Expand All @@ -282,37 +288,34 @@ gulp.task("build-web", gulp.series("validate", function buildWeb()

gulp.task("crx", gulp.series("build-chrome", function buildCRX()
{
let manifest = require("./manifest.json");
return merge(
gulp.src([
"build-chrome/**",
"!build-chrome/manifest.json", "!build-chrome/reloader.js", "!build-chrome/random.json",
"!build-chrome/**/.*", "!build-chrome/**/*.zip", "!build-chrome/**/*.crx"
]),
gulp.src("build-chrome/manifest.json").pipe(utils.jsonModify(removeReloader))
).pipe(zip("pfp-" + manifest.version + ".zip")).pipe(gulp.dest("build-chrome"));
).pipe(zip("pfp-" + VERSION + ".zip")).pipe(gulp.dest("build-chrome"));
}));

gulp.task("xpi", gulp.series("build-firefox", function buildXPI()
{
let manifest = require("./manifest.json");
return merge(
gulp.src([
"build-firefox/**",
"!build-firefox/manifest.json", "!build-firefox/reloader.js", "!build-firefox/random.json",
"!build-firefox/**/.*", "!build-firefox/**/*.xpi"
]),
gulp.src("build-firefox/manifest.json").pipe(utils.jsonModify(removeReloader))
).pipe(zip("pfp-" + manifest.version + ".xpi")).pipe(gulp.dest("build-firefox"));
).pipe(zip("pfp-" + VERSION + ".xpi")).pipe(gulp.dest("build-firefox"));
}));

gulp.task("web", gulp.series("build-web", function zipWeb()
{
let manifest = require("./manifest.json");
return gulp.src([
"build-web/**",
"!build-web/**/.*", "!build-web/**/*.zip"
]).pipe(zip("pfp-web-" + manifest.version + ".zip")).pipe(gulp.dest("build-web"));
]).pipe(zip("pfp-web-" + VERSION + ".zip")).pipe(gulp.dest("build-web"));
}));

gulp.task("test", gulp.series("validate", "build-test", function doTest()
Expand Down
1 change: 0 additions & 1 deletion lib/.eslintrc.json
@@ -1,7 +1,6 @@
{
"env": {
"node": false,
"commonjs": true,
"webextensions": true
}
}
8 changes: 4 additions & 4 deletions localeLoader.js
Expand Up @@ -6,8 +6,8 @@

"use strict";

const fs = require("fs");
const path = require("path");
import fs from "fs";
import path from "path";

function walkDirectory(dir, callback)
{
Expand Down Expand Up @@ -35,7 +35,7 @@ function walkDirectory(dir, callback)
});
}

module.exports = function(localeRoot)
export default function(localeRoot)
{
return {
name: "locale-loader",
Expand Down Expand Up @@ -75,4 +75,4 @@ module.exports = function(localeRoot)
});
}
};
};
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -4,6 +4,7 @@
"description": "Use a single master password to generate secure passwords for all websites - without losing track of passwords you cannot change.",
"author": "Wladimir Palant",
"license": "MPL-2.0",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/palant/easypasswords.git"
Expand Down
8 changes: 4 additions & 4 deletions replacePlugin.js
Expand Up @@ -6,9 +6,9 @@

"use strict";

const path = require("path");
import path from "path";

module.exports = function(map)
export default function(map)
{
map = new Map(Object.entries(map).map(([key, value]) => [path.normalize(key), path.normalize(value)]));

Expand All @@ -19,7 +19,7 @@ module.exports = function(map)
if (!id.endsWith(".js"))
id += ".js";
if (!importer)
importer = __dirname;
importer = process.cwd(); // import.meta unsupported by current eslint version
let resolved = path.normalize(path.join(path.dirname(importer), ...id.split("/")));
let mapped = map.get(resolved);
if (mapped)
Expand All @@ -28,4 +28,4 @@ module.exports = function(map)
return null;
}
};
};
}
1 change: 0 additions & 1 deletion ui/.eslintrc.json
@@ -1,7 +1,6 @@
{
"env": {
"node": false,
"commonjs": true,
"webextensions": true
}
}
3 changes: 1 addition & 2 deletions web/.eslintrc.json
@@ -1,7 +1,6 @@
{
"env": {
"node": false,
"browser": true,
"commonjs": true
"browser": true
}
}
6 changes: 3 additions & 3 deletions workerLoader.js
Expand Up @@ -6,9 +6,9 @@

"use strict";

const rollup = require("rollup");
import rollup from "rollup";

module.exports = function(regexp)
export default function(regexp)
{
return {
name: "worker-loader",
Expand Down Expand Up @@ -40,4 +40,4 @@ module.exports = function(regexp)
});
}
};
};
}

0 comments on commit 338972b

Please sign in to comment.