Skip to content

Commit

Permalink
Revert to CommonJS for Webpack loader compat
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Apr 27, 2021
1 parent ad6dde0 commit 0f10b31
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 53 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.cjs → .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
}
5 changes: 1 addition & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
collectCoverageFrom: [
'src/**/*.js',
],
Expand All @@ -7,7 +7,4 @@ export default {
testMatch: [
'**/test/suite/**/*.spec.js',
],
transform: {
'.js': 'jest-esm-transformer',
},
}
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"main": "src/index.js",
"files": [
"/src/"
Expand All @@ -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"
}
}
12 changes: 8 additions & 4 deletions src/consumer.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
29 changes: 23 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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,
}
17 changes: 12 additions & 5 deletions src/path.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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')

Expand All @@ -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')

Expand All @@ -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 + '/'
Expand Down
12 changes: 8 additions & 4 deletions src/reader.js
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
16 changes: 11 additions & 5 deletions src/tag.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
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) {
return resolveTagDefinitions(definitions).map(renderTag)
}
}

export function createTagDefinitionResolver (consumer) {
function createTagDefinitionResolver (consumer) {
const {
output: {
document,
Expand Down Expand Up @@ -64,7 +70,7 @@ export function createTagDefinitionResolver (consumer) {
}
}

export function renderTag (definition) {
function renderTag (definition) {
const result = [definition]
const toRender = [result]

Expand Down
6 changes: 5 additions & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export function replaceBaseUrl (base) {
module.exports = {
replaceBaseUrl,
}

function replaceBaseUrl (base) {
return manifest => ({
...manifest,
urls: {...manifest.urls, base},
Expand Down
19 changes: 13 additions & 6 deletions src/url.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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')

Expand All @@ -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')

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/suite/consumer/paths.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createConsumer} from '../../../src/consumer.js'
const {createConsumer} = require('../../../src/consumer.js')

describe('Consumer path methods', () => {
let manifest
Expand Down
4 changes: 2 additions & 2 deletions test/suite/consumer/read.spec.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down
4 changes: 2 additions & 2 deletions test/suite/consumer/transform.spec.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/suite/consumer/urls.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createConsumer} from '../../../src/consumer.js'
const {createConsumer} = require('../../../src/consumer.js')

describe('Consumer URL methods', () => {
let manifest
Expand Down
2 changes: 1 addition & 1 deletion test/suite/relative-path.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/suite/relative-url.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/suite/render-tag.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {renderTag} from '../../src/tag.js'
const {renderTag} = require('../../src/tag.js')

describe('renderTag()', () => {
it('should require a tag name', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/suite/resolve-path.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/suite/resolve-url.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/suite/to-dir-url.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down

0 comments on commit 0f10b31

Please sign in to comment.