Skip to content

Commit

Permalink
Merge 300cff0 into 0d86f52
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 5, 2023
2 parents 0d86f52 + 300cff0 commit 8d38f0c
Show file tree
Hide file tree
Showing 20 changed files with 471 additions and 68 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: lts/*
- name: Install
run: npm install --no-package-lock
- name: Setup PNPM
uses: pnpm/action-setup@v2
with:
version: latest
run_install: true
- name: Test
run: npm test
- name: Report
Expand Down
12 changes: 11 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
unsafe-perm=true
audit=false
enable-pre-post-scripts=true
fund=false
package-lock=false
prefer-dedupe=true
prefer-offline=true
save-prefix=~
save=false
strict-peer-dependencies=false
unsafe-perm=true
loglevel=error
shamefully-hoist=true
resolution-mode=highest
28 changes: 0 additions & 28 deletions demo.html

This file was deleted.

2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

</script>
<script type="module">
import mql from './dist/mql.min.mjs'
import mql from './dist/lightweight.mjs'

function getBrowser() {
var ua = navigator.userAgent,
Expand Down
3 changes: 2 additions & 1 deletion lightweight/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../src/lightweight.js",
"main": "index.js",
"type": "module",
"types": "index.d.ts"
}
17 changes: 6 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
"unpkg": "dist/mql.js",
"exports": {
".": {
"import": "./dist/mql.js",
"import": "./dist/lightweight.mjs",
"require": "./src/node.js"
},
"./lightweight": {
"import": "./dist/mql.js",
"require": "./src/lightweight.js"
}
},
"author": {
Expand Down Expand Up @@ -69,18 +65,21 @@
"@rollup/plugin-replace": "latest",
"@rollup/plugin-terser": "latest",
"async-listen": "latest",
"ava": "3",
"ava": "latest",
"c8": "latest",
"ci-publish": "latest",
"conventional-github-releaser": "latest",
"esm": "latest",
"execa": "~8.0.1",
"git-authors-cli": "latest",
"ky": "latest",
"nano-staged": "latest",
"npm-check-updates": "latest",
"prettier-standard": "latest",
"rollup": "latest",
"rollup-plugin-filesize": "latest",
"rollup-plugin-magic-string": "~1.0.4",
"rollup-plugin-rewrite": "~0.0.4",
"rollup-plugin-visualizer": "latest",
"simple-git-hooks": "latest",
"standard": "latest",
Expand Down Expand Up @@ -121,11 +120,7 @@
"ava": {
"files": [
"test/**/*",
"!test/browser-globals.js",
"!test/clients.js"
],
"require": [
"esm"
"!test/clients.mjs"
],
"timeout": "1m"
},
Expand Down
46 changes: 30 additions & 16 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@ import { visualizer } from 'rollup-plugin-visualizer'
import commonjs from '@rollup/plugin-commonjs'
import filesize from 'rollup-plugin-filesize'
import replace from '@rollup/plugin-replace'
import rewrite from 'rollup-plugin-rewrite'
import terser from '@rollup/plugin-terser'
import MagicString from 'magic-string'

const build = ({ format, file }) => {
const compress = file.includes('.min.')
const rewriteFlattie = () =>
rewrite({
find: /.* from 'flattie'/gm,
replace: (match) => new MagicString(match[0])
.replace('import', 'import * as')
.toString()
})

const build = ({ input, output, plugins = [], compress }) => {
return {
input: './src/lightweight.js',
output: {
name: 'mql',
format,
file,
sourcemap: true
},
input,
output,
plugins: [
replace({
values: {
"require('../package.json').version": "'__MQL_VERSION__'",
__MQL_VERSION__: require('./package.json').version
}
}),
nodeResolve({
mainFields: ['browser', 'module', 'main']
}),
commonjs(),
...plugins,
compress && terser(),
filesize(),
visualizer()
Expand All @@ -34,10 +36,22 @@ const build = ({ format, file }) => {
}

const builds = [
build({ format: 'umd', file: 'dist/mql.js' }),
build({ format: 'umd', file: 'dist/mql.min.js' }),
build({ format: 'es', file: 'dist/mql.mjs' }),
build({ format: 'es', file: 'dist/mql.min.mjs' })
build({
input: './src/node.js',
output: { file: 'dist/node.mjs', format: 'es' },
plugins: [rewriteFlattie()]
}),
build({
compress: true,
input: 'src/lightweight.js',
output: { file: 'dist/lightweight.mjs', format: 'es' },
plugins: [
rewriteFlattie(),
nodeResolve({
mainFields: ['browser', 'module', 'main']
})
]
})
]

export default builds
55 changes: 55 additions & 0 deletions src/lightweight.tpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const urlHttp = require('url-http/lightweight')
const { flattie: flatten } = require('flattie')

const factory = require('./factory')
const { default: ky } = require('ky')

class MicrolinkError extends Error {
constructor (props) {
super()
this.name = 'MicrolinkError'
Object.assign(this, props)
this.description = this.message
this.message = this.code
? `${this.code}, ${this.description}`
: this.description
}
}

const got = async (url, opts) => {
try {
if (opts.retry > 0) opts.retry = opts.retry + 1
if (opts.timeout === undefined) opts.timeout = false
const response = await ky(url, opts)
const body = await response.json()
const { headers, status: statusCode } = response
return { url: response.url, body, headers, statusCode }
} catch (err) {
if (err.response) {
const { response } = err
err.response = {
...response,
headers: Array.from(response.headers.entries()).reduce(
(acc, [key, value]) => {
acc[key] = value
return acc
},
{}
),
statusCode: response.status,
body: await response.text()
}
}
throw err
}
}

module.exports = factory({
MicrolinkError,
urlHttp,
got,
flatten,
VERSION: require('../package.json').version
})
17 changes: 17 additions & 0 deletions test/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

import { createRequire } from 'module'
import { $ } from 'execa'
import test from 'ava'

const require = createRequire(import.meta.url)
const pkg = require('../package.json')

const evalScript = code => $`node -e ${code}`

test('cjs', async t => {
// eslint-disable-next-line no-template-curly-in-string
const code = "console.log(`mql v${require('@microlink/mql').version}`)"
const { stdout } = await evalScript(code)
t.is(stdout, `mql v${pkg.version}`)
})
4 changes: 2 additions & 2 deletions test/clients.js → test/clients.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mqlLightweight from '../src/lightweight'
import mqlNode from '../src/node'
import mqlLightweight from '../dist/lightweight.mjs'
import mqlNode from '../dist/node.mjs'

export default [
{ constructor: mqlNode, target: 'node' },
Expand Down
2 changes: 1 addition & 1 deletion test/error/client.js → test/error/client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import test from 'ava'

import clients from '../clients'
import clients from '../clients.mjs'

clients.forEach(({ constructor: mql, target }) => {
test(`${target} » EINVALURLCLIENT`, async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/error/server.js → test/error/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { listen } from 'async-listen'
import http from 'http'
import test from 'ava'

import clients from '../clients'
import clients from '../clients.mjs'

clients.forEach(({ constructor: mql, target }) => {
test(`${target} » server side unexpected`, async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/get-api-url.js → test/get-api-url.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import test from 'ava'

import clients from './clients'
import clients from './clients.mjs'

clients.forEach(({ constructor: mql, target }) => {
test(`${target} » url without query params`, t => {
Expand Down
2 changes: 1 addition & 1 deletion test/opts.js → test/opts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import test from 'ava'

import clients from './clients'
import clients from './clients.mjs'

clients.forEach(({ constructor: mql, target }) => {
test(`${target} » url`, async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/rules.js → test/rules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import test from 'ava'

import clients from './clients'
import clients from './clients.mjs'

clients.forEach(({ constructor: mql, target }) => {
test(`${target} » no rules`, t => {
Expand Down
Loading

0 comments on commit 8d38f0c

Please sign in to comment.