From 0f10b313a0b08dc4feab61353d06d905eb543753 Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Tue, 27 Apr 2021 12:50:15 +1000 Subject: [PATCH] Revert to CommonJS for Webpack loader compat --- .eslintrc.cjs => .eslintrc.js | 14 +++++++++++++ jest.config.js | 5 +---- package.json | 10 ++++----- src/consumer.js | 12 +++++++---- src/index.js | 29 +++++++++++++++++++++------ src/path.js | 17 +++++++++++----- src/reader.js | 12 +++++++---- src/tag.js | 16 ++++++++++----- src/transform.js | 6 +++++- src/url.js | 19 ++++++++++++------ test/suite/consumer/paths.spec.js | 2 +- test/suite/consumer/read.spec.js | 4 ++-- test/suite/consumer/transform.spec.js | 4 ++-- test/suite/consumer/urls.spec.js | 2 +- test/suite/relative-path.spec.js | 2 +- test/suite/relative-url.spec.js | 2 +- test/suite/render-tag.spec.js | 2 +- test/suite/resolve-path.spec.js | 2 +- test/suite/resolve-url.spec.js | 2 +- test/suite/to-dir-url.spec.js | 2 +- 20 files changed, 111 insertions(+), 53 deletions(-) rename .eslintrc.cjs => .eslintrc.js (56%) diff --git a/.eslintrc.cjs b/.eslintrc.js similarity index 56% rename from .eslintrc.cjs rename to .eslintrc.js index 1698044..eb87334 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.js @@ -30,6 +30,20 @@ module.exports = { 'prefer-const': 'error', 'quote-props': ['error', 'as-needed'], + 'node/file-extension-in-import': ['error', 'always'], + 'node/no-deprecated-api': 'error', + 'node/no-extraneous-import': 'error', + 'node/no-extraneous-require': 'error', + 'node/no-missing-import': 'error', + 'node/no-unpublished-bin': 'error', + 'node/no-unpublished-import': 'error', + 'node/no-unpublished-require': 'error', + 'node/no-unsupported-features/es-builtins': 'error', + 'node/no-unsupported-features/es-syntax': 'error', + 'node/no-unsupported-features/node-builtins': 'error', + 'node/process-exit-as-throw': 'error', + 'node/shebang': 'error', + 'jest/no-focused-tests': 'warn', }, } diff --git a/jest.config.js b/jest.config.js index 24cb828..942249f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,4 +1,4 @@ -export default { +module.exports = { collectCoverageFrom: [ 'src/**/*.js', ], @@ -7,7 +7,4 @@ export default { testMatch: [ '**/test/suite/**/*.spec.js', ], - transform: { - '.js': 'jest-esm-transformer', - }, } diff --git a/package.json b/package.json index d11fe58..6f1f365 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "publishConfig": { "access": "public" }, - "type": "module", "main": "src/index.js", "files": [ "/src/" @@ -22,13 +21,12 @@ "url-parse": "^1.5.1" }, "devDependencies": { - "eslint": "^7.24.0", + "eslint": "^7.25.0", "eslint-config-standard": "^16.0.2", "eslint-plugin-import": "^2.22.1", - "eslint-plugin-jest": "^24.3.5", + "eslint-plugin-jest": "^24.3.6", "eslint-plugin-node": "^11.1.0", - "eslint-plugin-promise": "^4.2.1", - "jest": "^26.6.3", - "jest-esm-transformer": "^1.0.0" + "eslint-plugin-promise": "^5.1.0", + "jest": "^26.6.3" } } diff --git a/src/consumer.js b/src/consumer.js index 9c0d1cb..7d3e941 100644 --- a/src/consumer.js +++ b/src/consumer.js @@ -1,8 +1,12 @@ -import {isAbsolutePath, relativePath, resolvePath} from './path.js' -import {isAbsoluteUrl, relativeUrl, resolveUrl} from './url.js' -import {createTagDefinitionRenderer, createTagDefinitionResolver} from './tag.js' +const {isAbsolutePath, relativePath, resolvePath} = require('./path.js') +const {isAbsoluteUrl, relativeUrl, resolveUrl} = require('./url.js') +const {createTagDefinitionRenderer, createTagDefinitionResolver} = require('./tag.js') -export function createConsumer (manifest, options = {}) { +module.exports = { + createConsumer, +} + +function createConsumer (manifest, options = {}) { const { output: {document, image}, outputPath: manifestOutputPath, diff --git a/src/index.js b/src/index.js index a279ba5..c58c314 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,23 @@ -export {replaceBaseUrl} from './transform.js' -export {createConsumer} from './consumer.js' -export {createTagDefinitionRenderer, createTagDefinitionResolver, renderTag} from './tag.js' -export {isAbsolutePath, relativePath, resolvePath, toDirPath} from './path.js' -export {isAbsoluteUrl, relativeUrl, resolveUrl, toDirUrl} from './url.js' -export {readConsumer} from './reader.js' +const transform = require('./transform.js') +const {createConsumer} = require('./consumer.js') +const {createTagDefinitionRenderer, createTagDefinitionResolver, renderTag} = require('./tag.js') +const {isAbsolutePath, relativePath, resolvePath, toDirPath} = require('./path.js') +const {isAbsoluteUrl, relativeUrl, resolveUrl, toDirUrl} = require('./url.js') +const {readConsumer} = require('./reader.js') + +module.exports = { + createConsumer, + createTagDefinitionRenderer, + createTagDefinitionResolver, + isAbsolutePath, + isAbsoluteUrl, + readConsumer, + relativePath, + relativeUrl, + renderTag, + resolvePath, + resolveUrl, + toDirPath, + toDirUrl, + transform, +} diff --git a/src/path.js b/src/path.js index 3a3d6d4..58038aa 100644 --- a/src/path.js +++ b/src/path.js @@ -1,9 +1,16 @@ -import {dirname, isAbsolute, normalize, relative, sep} from 'path' +const {dirname, isAbsolute, normalize, relative, sep} = require('path') + +module.exports = { + isAbsolutePath, + relativePath, + resolvePath, + toDirPath, +} /** * Determine whether the supplied path is absolute. */ -export function isAbsolutePath (path) { +function isAbsolutePath (path) { if (typeof path !== 'string') throw new Error('Path must be a string') return isAbsolute(path) @@ -15,7 +22,7 @@ export function isAbsolutePath (path) { * * Trailing slashes are significant. */ -export function relativePath (fromPath, toPath) { +function relativePath (fromPath, toPath) { if (typeof fromPath !== 'string') throw new Error('From path must be a string') if (typeof toPath !== 'string') throw new Error('To path must be a string') @@ -38,7 +45,7 @@ export function relativePath (fromPath, toPath) { * * Trailing slashes are significant. */ -export function resolvePath (basePath, path) { +function resolvePath (basePath, path) { if (typeof basePath !== 'string') throw new Error('Base path must be a string') if (typeof path !== 'string') throw new Error('Path must be a string') @@ -61,7 +68,7 @@ export function resolvePath (basePath, path) { /** * Append a trailing slash if the supplied path is not already a directory path. */ -export function toDirPath (path) { +function toDirPath (path) { if (typeof path !== 'string') throw new Error('Path must be a string') return isDirPath(path) ? path : path + '/' diff --git a/src/reader.js b/src/reader.js index 202f630..94d9310 100644 --- a/src/reader.js +++ b/src/reader.js @@ -1,9 +1,13 @@ -import {readFileSync} from 'fs' +const {readFileSync} = require('fs') -import {createConsumer} from './consumer.js' -import {resolvePath} from './path.js' +const {createConsumer} = require('./consumer.js') +const {resolvePath} = require('./path.js') -export function readConsumer (manifestPath) { +module.exports = { + readConsumer, +} + +function readConsumer (manifestPath) { const manifest = readManifest(manifestPath) const outputPath = resolvePath(manifestPath, manifest.outputPath) diff --git a/src/tag.js b/src/tag.js index 3026bfe..13bb7c5 100644 --- a/src/tag.js +++ b/src/tag.js @@ -1,7 +1,13 @@ -import escapeHtml from 'escape-html' -import {render} from 'ejs' +const escapeHtml = require('escape-html') +const {render} = require('ejs') -export function createTagDefinitionRenderer (consumer) { +module.exports = { + createTagDefinitionRenderer, + createTagDefinitionResolver, + renderTag, +} + +function createTagDefinitionRenderer (consumer) { const resolveTagDefinitions = createTagDefinitionResolver(consumer) return function renderTagDefinitions (definitions) { @@ -9,7 +15,7 @@ export function createTagDefinitionRenderer (consumer) { } } -export function createTagDefinitionResolver (consumer) { +function createTagDefinitionResolver (consumer) { const { output: { document, @@ -64,7 +70,7 @@ export function createTagDefinitionResolver (consumer) { } } -export function renderTag (definition) { +function renderTag (definition) { const result = [definition] const toRender = [result] diff --git a/src/transform.js b/src/transform.js index 966527d..7c95263 100644 --- a/src/transform.js +++ b/src/transform.js @@ -1,4 +1,8 @@ -export function replaceBaseUrl (base) { +module.exports = { + replaceBaseUrl, +} + +function replaceBaseUrl (base) { return manifest => ({ ...manifest, urls: {...manifest.urls, base}, diff --git a/src/url.js b/src/url.js index 3692804..07dc5cc 100644 --- a/src/url.js +++ b/src/url.js @@ -1,11 +1,18 @@ -import urlParse from 'url-parse' +const urlParse = require('url-parse') -import {relativePath, resolvePath, toDirPath} from './path.js' +const {relativePath, resolvePath, toDirPath} = require('./path.js') + +module.exports = { + isAbsoluteUrl, + relativeUrl, + resolveUrl, + toDirUrl, +} /** * Determine whether the supplied URL is absolute. */ -export function isAbsoluteUrl (url) { +function isAbsoluteUrl (url) { if (typeof url !== 'string') throw new Error('URL must be a string') const parsed = urlParse(url) @@ -18,7 +25,7 @@ export function isAbsoluteUrl (url) { * * Supports relative URLs. */ -export function relativeUrl (fromUrl, toUrl) { +function relativeUrl (fromUrl, toUrl) { if (typeof fromUrl !== 'string') throw new Error('From URL must be a string') if (typeof toUrl !== 'string') throw new Error('To URL must be a string') @@ -43,7 +50,7 @@ export function relativeUrl (fromUrl, toUrl) { * * Supports relative URLs. */ -export function resolveUrl (baseUrl, url) { +function resolveUrl (baseUrl, url) { if (typeof baseUrl !== 'string') throw new Error('Base URL must be a string') if (typeof url !== 'string') throw new Error('URL must be a string') @@ -62,7 +69,7 @@ export function resolveUrl (baseUrl, url) { /** * Append a trailing slash if the supplied URL is not already a directory URL. */ -export function toDirUrl (url) { +function toDirUrl (url) { if (typeof url !== 'string') throw new Error('URL must be a string') const urlParsed = urlParse(url) diff --git a/test/suite/consumer/paths.spec.js b/test/suite/consumer/paths.spec.js index 19b9b30..fe17816 100644 --- a/test/suite/consumer/paths.spec.js +++ b/test/suite/consumer/paths.spec.js @@ -1,4 +1,4 @@ -import {createConsumer} from '../../../src/consumer.js' +const {createConsumer} = require('../../../src/consumer.js') describe('Consumer path methods', () => { let manifest diff --git a/test/suite/consumer/read.spec.js b/test/suite/consumer/read.spec.js index 296994e..49faa4c 100644 --- a/test/suite/consumer/read.spec.js +++ b/test/suite/consumer/read.spec.js @@ -1,6 +1,6 @@ -import {join, resolve} from 'path' +const {join, resolve} = require('path') -import {readConsumer} from '../../../src/reader.js' +const {readConsumer} = require('../../../src/reader.js') const fixtureDirPath = resolve(__dirname, '../../fixture') diff --git a/test/suite/consumer/transform.spec.js b/test/suite/consumer/transform.spec.js index 7049dd6..38772eb 100644 --- a/test/suite/consumer/transform.spec.js +++ b/test/suite/consumer/transform.spec.js @@ -1,5 +1,5 @@ -import {createConsumer} from '../../../src/consumer.js' -import {replaceBaseUrl} from '../../../src/transform.js' +const {createConsumer} = require('../../../src/consumer.js') +const {replaceBaseUrl} = require('../../../src/transform.js') describe('consumer.transform()', () => { let baseConsumer diff --git a/test/suite/consumer/urls.spec.js b/test/suite/consumer/urls.spec.js index 786b980..87f154b 100644 --- a/test/suite/consumer/urls.spec.js +++ b/test/suite/consumer/urls.spec.js @@ -1,4 +1,4 @@ -import {createConsumer} from '../../../src/consumer.js' +const {createConsumer} = require('../../../src/consumer.js') describe('Consumer URL methods', () => { let manifest diff --git a/test/suite/relative-path.spec.js b/test/suite/relative-path.spec.js index 9419d2d..0b82ad6 100644 --- a/test/suite/relative-path.spec.js +++ b/test/suite/relative-path.spec.js @@ -1,4 +1,4 @@ -import {relativePath} from '../../src/path.js' +const {relativePath} = require('../../src/path.js') describe('relativePath()', () => { it('should support resolving from absolute paths', () => { diff --git a/test/suite/relative-url.spec.js b/test/suite/relative-url.spec.js index 5ec56f3..25ecc95 100644 --- a/test/suite/relative-url.spec.js +++ b/test/suite/relative-url.spec.js @@ -1,4 +1,4 @@ -import {relativeUrl} from '../../src/url.js' +const {relativeUrl} = require('../../src/url.js') describe('relativeUrl()', () => { it('should support resolving from absolute URLs with the same origin', () => { diff --git a/test/suite/render-tag.spec.js b/test/suite/render-tag.spec.js index df8c84c..2528751 100644 --- a/test/suite/render-tag.spec.js +++ b/test/suite/render-tag.spec.js @@ -1,4 +1,4 @@ -import {renderTag} from '../../src/tag.js' +const {renderTag} = require('../../src/tag.js') describe('renderTag()', () => { it('should require a tag name', () => { diff --git a/test/suite/resolve-path.spec.js b/test/suite/resolve-path.spec.js index 6709d9b..035bb92 100644 --- a/test/suite/resolve-path.spec.js +++ b/test/suite/resolve-path.spec.js @@ -1,4 +1,4 @@ -import {resolvePath} from '../../src/path.js' +const {resolvePath} = require('../../src/path.js') describe('resolvePath()', () => { it('should support resolving against absolute paths', () => { diff --git a/test/suite/resolve-url.spec.js b/test/suite/resolve-url.spec.js index 196e6b2..1a18f79 100644 --- a/test/suite/resolve-url.spec.js +++ b/test/suite/resolve-url.spec.js @@ -1,4 +1,4 @@ -import {resolveUrl} from '../../src/url.js' +const {resolveUrl} = require('../../src/url.js') describe('resolveUrl()', () => { it('should support resolving against absolute URLs', () => { diff --git a/test/suite/to-dir-url.spec.js b/test/suite/to-dir-url.spec.js index 3fb8f4d..584c702 100644 --- a/test/suite/to-dir-url.spec.js +++ b/test/suite/to-dir-url.spec.js @@ -1,4 +1,4 @@ -import {toDirUrl} from '../../src/url.js' +const {toDirUrl} = require('../../src/url.js') describe('toDirUrl()', () => { it('should append a trailing slash to non-directory URLs', () => {