From 9df33ba4d304844f095beda1c9475ed5861279d1 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Sun, 18 Apr 2021 14:56:48 +0530 Subject: [PATCH] fixes for proper JS/TS-agnostic import from twinkle-starter - Don't use type=module in package.json as this is resulting in issues with Webpack. Use CommonJS require() syntax in all node scripts - Build JS files to `js` directory. Don't emit declarations. - Remove main and types fields from package.json. This means that code like `import ... from 'twinkle-core'` no longer works. Import must necessarily mention the path, which is different depending on the whether JS is being used or TS. --- Gruntfile.cjs => Gruntfile.js | 0 jest.config.js | 2 +- package-lock.json | 4 +-- package.json | 9 ++---- scripts/build-i18n.js | 60 +++++++++++++++++------------------ scripts/check-msg.js | 12 +++---- scripts/generate-docs.js | 4 +-- src/init.ts | 4 ++- tsconfig.json | 3 +- 9 files changed, 46 insertions(+), 52 deletions(-) rename Gruntfile.cjs => Gruntfile.js (100%) diff --git a/Gruntfile.cjs b/Gruntfile.js similarity index 100% rename from Gruntfile.cjs rename to Gruntfile.js diff --git a/jest.config.js b/jest.config.js index c9adc03..9e0ecbd 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,4 +1,4 @@ -export default { +module.exports = { preset: 'ts-jest', testEnvironment: 'jsdom', setupFilesAfterEnv: ['mock-mediawiki'], diff --git a/package-lock.json b/package-lock.json index a057dce..d83c54a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "twinkle-core", - "version": "3.0.6-beta", + "version": "3.0.7-beta", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "3.0.6-beta", + "version": "3.0.7-beta", "license": "CC-BY-SA-3.0", "dependencies": { "@types/select2": "^4.0.53", diff --git a/package.json b/package.json index 93993b9..30366dc 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,24 @@ { "title": "Twinkle core", "name": "twinkle-core", - "version": "3.0.6-beta", + "version": "3.0.7-beta", "repository": { "type": "git", "url": "https://github.com/wikimedia-gadgets/twinkle-core" }, - "main": "./build/src/index.js", - "types": "./build/src/index.d.ts", - "type": "module", "scripts": { "format": "prettier --write .", "morebits:lint": "eslint morebits/", "morebits:lint:fix": "eslint morebits --fix", "morebits:test": "qunit --require ./morebits/tests/mocking/mb_repl.js ./morebits/tests/*", - "lint": "grunt --gruntfile Gruntfile.cjs lint", + "lint": "grunt lint", "test": "jest", "build": "tsc || echo", "docs": "node scripts/generate-docs.js" }, "files": [ + "js/", "src/", - "build/", "i18n/", "morebits/", "lib/" diff --git a/scripts/build-i18n.js b/scripts/build-i18n.js index 8478547..a57b9dc 100644 --- a/scripts/build-i18n.js +++ b/scripts/build-i18n.js @@ -6,13 +6,13 @@ * Licence: MIT */ -import fs from "fs"; -import path from "path"; -import chalk from "chalk"; -import createDOMPurify from "dompurify"; -import { JSDOM } from "jsdom"; +const fs = require('fs'); +const path = require('path'); +const chalk = require('chalk'); +const createDOMPurify = require('dompurify'); +const { JSDOM } = require('jsdom'); -const window = new JSDOM("").window; +const window = new JSDOM('').window; const DOMPurify = createDOMPurify(window); const warning = (text) => console.log(chalk.yellowBright(text)); @@ -20,30 +20,30 @@ const code = chalk.inverse; const keyword = chalk.cyan; const ALLOWED_TAGS = [ - "b", + 'b', // Haven't met in practice yet, but perhaps these tags could be helpful for RTL languages? - "bdi", - "bdo", - - "code", - "em", - "i", - "kbd", - "li", - "nowiki", - "ol", - "p", - "pre", - "span", - "strong", - "syntaxhighlight", - "ul", - "var", + 'bdi', + 'bdo', + + 'code', + 'em', + 'i', + 'kbd', + 'li', + 'nowiki', + 'ol', + 'p', + 'pre', + 'span', + 'strong', + 'syntaxhighlight', + 'ul', + 'var', ]; function hideText(text, regexp, hidden) { - return text.replace(regexp, (s) => "\x01" + hidden.push(s) + "\x02"); + return text.replace(regexp, (s) => '\x01' + hidden.push(s) + '\x02'); } function unhideText(text, hidden) { @@ -54,8 +54,8 @@ function unhideText(text, hidden) { return text; } -DOMPurify.addHook("uponSanitizeElement", (currentNode, data, config) => { - if (!Object.keys(data.allowedTags).includes(data.tagName) && data.tagName !== "body") { +DOMPurify.addHook('uponSanitizeElement', (currentNode, data, config) => { + if (!Object.keys(data.allowedTags).includes(data.tagName) && data.tagName !== 'body') { // `< /li>` qualifies as "#comment" and has content available under `currentNode.textContent`. warning( `Disallowed tag found and sanitized in string "${keyword(config.stringName)}" in ${keyword( @@ -67,7 +67,7 @@ DOMPurify.addHook("uponSanitizeElement", (currentNode, data, config) => { } }); -DOMPurify.addHook("uponSanitizeAttribute", (currentNode, hookEvent, config) => { +DOMPurify.addHook('uponSanitizeAttribute', (currentNode, hookEvent, config) => { if (!Object.keys(hookEvent.allowedAttributes).includes(hookEvent.attrName)) { warning( `Disallowed attribute found and sanitized in string "${keyword(config.stringName)}" in ${keyword( @@ -79,7 +79,7 @@ DOMPurify.addHook("uponSanitizeAttribute", (currentNode, hookEvent, config) => { } }); -fs.readdirSync("./i18n/").forEach((fileName) => { +fs.readdirSync('./i18n/').forEach((fileName) => { if (!(path.extname(fileName) === '.json' && fileName !== 'qqq.json')) { return; } @@ -143,4 +143,4 @@ fs.readdirSync("./i18n/").forEach((fileName) => { fs.writeFileSync(`build-i18n/${lang}.json`, json); }); -console.log("Internationalization files have been built successfully."); +console.log('Internationalization files have been built successfully.'); diff --git a/scripts/check-msg.js b/scripts/check-msg.js index c402668..a9c230b 100644 --- a/scripts/check-msg.js +++ b/scripts/check-msg.js @@ -2,21 +2,17 @@ * Script to check that no messages are used in the code are undefined, * and that all defined messages are actually used. Also flags use of parameters * for messages with no parameters and vice-versa. - * Requires Node.js v13 or above. + * * Run as: * node check-msg.js * Or via grunt as * grunt exec:check_msg */ -import fs from 'fs/promises'; -import path from 'path'; -import { mwn } from 'mwn'; +const fs = require('fs/promises'); +const path = require('path'); +const { mwn } = require('mwn'); -import { dirname } from 'path'; -import { fileURLToPath } from 'url'; - -const __dirname = dirname(fileURLToPath(import.meta.url)); const repoRoot = __dirname + '/../'; async function readFile(path) { diff --git a/scripts/generate-docs.js b/scripts/generate-docs.js index dc8a750..f793c2c 100644 --- a/scripts/generate-docs.js +++ b/scripts/generate-docs.js @@ -9,8 +9,8 @@ // @ts-check -import td from 'typedoc'; -import ts from 'typescript'; +const td = require('typedoc'); +const ts = require('typescript'); const app = new td.Application(); // For reading typedoc.json - optional diff --git a/src/init.ts b/src/init.ts index dcf3523..b46274e 100644 --- a/src/init.ts +++ b/src/init.ts @@ -34,7 +34,9 @@ export function init() { Twinkle.preModuleInitHooks.push( // Get messages - initMessaging, + () => { + initMessaging(); + }, // Get user config and perform init actions that rely on the config () => { diff --git a/tsconfig.json b/tsconfig.json index 431c7c6..594ed9c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,9 +8,8 @@ "es2015.collection" // Only Map and Set from this are supported in IE 11. ], - "outDir": "./build", + "outDir": "./js", "sourceMap": true, - "declaration": true, "moduleResolution": "node", "esModuleInterop": true, "newLine": "lf",