Skip to content

Commit

Permalink
fixes for proper JS/TS-agnostic import from twinkle-starter
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
siddharthvp committed Apr 18, 2021
1 parent 47a2e72 commit 9df33ba
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 52 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['mock-mediawiki'],
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
60 changes: 30 additions & 30 deletions scripts/build-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,44 @@
* 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));
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) {
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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;
}
Expand Down Expand Up @@ -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.');
12 changes: 4 additions & 8 deletions scripts/check-msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
() => {
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9df33ba

Please sign in to comment.