Skip to content

Commit

Permalink
build: esm (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Sep 6, 2023
1 parent b819ac2 commit cd0ce0d
Show file tree
Hide file tree
Showing 22 changed files with 427 additions and 76 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ stats.html
examples
src/ky.js
dist
lightweight/index.js
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"
}
29 changes: 13 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
"description": "Microlink Query Language. The official HTTP client to interact with Microlink API for Node.js, browsers & Deno.",
"homepage": "https://microlink.io/mql",
"version": "0.10.39",
"types": "index.d.ts",
"types": "lightweight/index.d.ts",
"browser": "src/lightweight.js",
"umd:main": "dist/mql.js",
"unpkg": "dist/mql.js",
"exports": {
".": {
"import": "./dist/mql.js",
"import": "./lightweight/index.js",
"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 All @@ -93,16 +92,17 @@
"node": ">= 12"
},
"files": [
"dist",
"index.d.ts",
"lightweight",
"src"
"src/factory.js",
"src/index.d.ts",
"src/ky.js",
"src/node.js"
],
"scripts": {
"build": "rollup -c rollup.config.js --bundleConfigAsCjs",
"build:kys": "[ -f src/ky.js ] || rollup node_modules/ky/distribution/index.js --format=umd --name=ky --exports=named --file=src/ky.js",
"clean": "rm -rf node_modules",
"clean:build": "rm -rf dist",
"clean:build": "rm -rf dist lightweight/index.js",
"contributors": "(npx git-authors-cli && npx finepack --sort-ignore-object-at ava && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
"dev": "npm run build -- -w",
"lint": "standard && tsd",
Expand All @@ -121,11 +121,7 @@
"ava": {
"files": [
"test/**/*",
"!test/browser-globals.js",
"!test/clients.js"
],
"require": [
"esm"
"!test/clients.mjs"
],
"timeout": "1m"
},
Expand Down Expand Up @@ -153,6 +149,7 @@
"standard": {
"ignore": [
"dist",
"lightweight/index.js",
"src/ky.js"
]
},
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: 'lightweight/index.js', format: 'es' },
plugins: [
rewriteFlattie(),
nodeResolve({
mainFields: ['browser', 'module', 'main']
})
]
})
]

export default builds
4 changes: 2 additions & 2 deletions index.d.ts → src/node.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MqlPayload, MqlOptions, MicrolinkApiOptions } from './lightweight'
import { MqlPayload, MqlOptions, MicrolinkApiOptions } from '../lightweight'

export { MqlPayload } from './lightweight'
export { MqlPayload } from '../lightweight'

export type MqlResponse = MqlPayload & {
response: {
Expand Down
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 '../lightweight/index.js'
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/node.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { expectType } from 'tsd'

import mql from '..'
import mql from '../src/node'

/** response */

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

1 comment on commit cd0ce0d

@vercel
Copy link

@vercel vercel bot commented on cd0ce0d Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.