Skip to content

Commit cd0ce0d

Browse files
authored
build: esm (#134)
1 parent b819ac2 commit cd0ce0d

22 files changed

+427
-76
lines changed

.github/workflows/pull_request.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ jobs:
2121
uses: actions/setup-node@v3
2222
with:
2323
node-version: lts/*
24-
- name: Install
25-
run: npm install --no-package-lock
24+
- name: Setup PNPM
25+
uses: pnpm/action-setup@v2
26+
with:
27+
version: latest
28+
run_install: true
2629
- name: Test
2730
run: npm test
2831
- name: Report

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ stats.html
3838
examples
3939
src/ky.js
4040
dist
41+
lightweight/index.js

.npmrc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
unsafe-perm=true
1+
audit=false
2+
enable-pre-post-scripts=true
3+
fund=false
4+
package-lock=false
5+
prefer-dedupe=true
6+
prefer-offline=true
27
save-prefix=~
38
save=false
9+
strict-peer-dependencies=false
10+
unsafe-perm=true
11+
loglevel=error
12+
shamefully-hoist=true
13+
resolution-mode=highest

demo.html

Lines changed: 0 additions & 28 deletions
This file was deleted.

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
</script>
6969
<script type="module">
70-
import mql from './dist/mql.min.mjs'
70+
import mql from './dist/lightweight.mjs'
7171

7272
function getBrowser() {
7373
var ua = navigator.userAgent,

lightweight/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2-
"main": "../src/lightweight.js",
2+
"main": "index.js",
3+
"type": "module",
34
"types": "index.d.ts"
45
}

package.json

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
"description": "Microlink Query Language. The official HTTP client to interact with Microlink API for Node.js, browsers & Deno.",
44
"homepage": "https://microlink.io/mql",
55
"version": "0.10.39",
6-
"types": "index.d.ts",
6+
"types": "lightweight/index.d.ts",
77
"browser": "src/lightweight.js",
88
"umd:main": "dist/mql.js",
99
"unpkg": "dist/mql.js",
1010
"exports": {
1111
".": {
12-
"import": "./dist/mql.js",
12+
"import": "./lightweight/index.js",
1313
"require": "./src/node.js"
14-
},
15-
"./lightweight": {
16-
"import": "./dist/mql.js",
17-
"require": "./src/lightweight.js"
1814
}
1915
},
2016
"author": {
@@ -69,18 +65,21 @@
6965
"@rollup/plugin-replace": "latest",
7066
"@rollup/plugin-terser": "latest",
7167
"async-listen": "latest",
72-
"ava": "3",
68+
"ava": "latest",
7369
"c8": "latest",
7470
"ci-publish": "latest",
7571
"conventional-github-releaser": "latest",
7672
"esm": "latest",
73+
"execa": "~8.0.1",
7774
"git-authors-cli": "latest",
7875
"ky": "latest",
7976
"nano-staged": "latest",
8077
"npm-check-updates": "latest",
8178
"prettier-standard": "latest",
8279
"rollup": "latest",
8380
"rollup-plugin-filesize": "latest",
81+
"rollup-plugin-magic-string": "~1.0.4",
82+
"rollup-plugin-rewrite": "~0.0.4",
8483
"rollup-plugin-visualizer": "latest",
8584
"simple-git-hooks": "latest",
8685
"standard": "latest",
@@ -93,16 +92,17 @@
9392
"node": ">= 12"
9493
},
9594
"files": [
96-
"dist",
97-
"index.d.ts",
9895
"lightweight",
99-
"src"
96+
"src/factory.js",
97+
"src/index.d.ts",
98+
"src/ky.js",
99+
"src/node.js"
100100
],
101101
"scripts": {
102102
"build": "rollup -c rollup.config.js --bundleConfigAsCjs",
103103
"build:kys": "[ -f src/ky.js ] || rollup node_modules/ky/distribution/index.js --format=umd --name=ky --exports=named --file=src/ky.js",
104104
"clean": "rm -rf node_modules",
105-
"clean:build": "rm -rf dist",
105+
"clean:build": "rm -rf dist lightweight/index.js",
106106
"contributors": "(npx git-authors-cli && npx finepack --sort-ignore-object-at ava && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
107107
"dev": "npm run build -- -w",
108108
"lint": "standard && tsd",
@@ -121,11 +121,7 @@
121121
"ava": {
122122
"files": [
123123
"test/**/*",
124-
"!test/browser-globals.js",
125-
"!test/clients.js"
126-
],
127-
"require": [
128-
"esm"
124+
"!test/clients.mjs"
129125
],
130126
"timeout": "1m"
131127
},
@@ -153,6 +149,7 @@
153149
"standard": {
154150
"ignore": [
155151
"dist",
152+
"lightweight/index.js",
156153
"src/ky.js"
157154
]
158155
},

rollup.config.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@ import { visualizer } from 'rollup-plugin-visualizer'
33
import commonjs from '@rollup/plugin-commonjs'
44
import filesize from 'rollup-plugin-filesize'
55
import replace from '@rollup/plugin-replace'
6+
import rewrite from 'rollup-plugin-rewrite'
67
import terser from '@rollup/plugin-terser'
8+
import MagicString from 'magic-string'
79

8-
const build = ({ format, file }) => {
9-
const compress = file.includes('.min.')
10+
const rewriteFlattie = () =>
11+
rewrite({
12+
find: /.* from 'flattie'/gm,
13+
replace: (match) => new MagicString(match[0])
14+
.replace('import', 'import * as')
15+
.toString()
16+
})
1017

18+
const build = ({ input, output, plugins = [], compress }) => {
1119
return {
12-
input: './src/lightweight.js',
13-
output: {
14-
name: 'mql',
15-
format,
16-
file,
17-
sourcemap: true
18-
},
20+
input,
21+
output,
1922
plugins: [
2023
replace({
2124
values: {
25+
"require('../package.json').version": "'__MQL_VERSION__'",
2226
__MQL_VERSION__: require('./package.json').version
2327
}
2428
}),
25-
nodeResolve({
26-
mainFields: ['browser', 'module', 'main']
27-
}),
2829
commonjs(),
30+
...plugins,
2931
compress && terser(),
3032
filesize(),
3133
visualizer()
@@ -34,10 +36,22 @@ const build = ({ format, file }) => {
3436
}
3537

3638
const builds = [
37-
build({ format: 'umd', file: 'dist/mql.js' }),
38-
build({ format: 'umd', file: 'dist/mql.min.js' }),
39-
build({ format: 'es', file: 'dist/mql.mjs' }),
40-
build({ format: 'es', file: 'dist/mql.min.mjs' })
39+
build({
40+
input: './src/node.js',
41+
output: { file: 'dist/node.mjs', format: 'es' },
42+
plugins: [rewriteFlattie()]
43+
}),
44+
build({
45+
compress: true,
46+
input: 'src/lightweight.js',
47+
output: { file: 'lightweight/index.js', format: 'es' },
48+
plugins: [
49+
rewriteFlattie(),
50+
nodeResolve({
51+
mainFields: ['browser', 'module', 'main']
52+
})
53+
]
54+
})
4155
]
4256

4357
export default builds

index.d.ts renamed to src/node.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { MqlPayload, MqlOptions, MicrolinkApiOptions } from './lightweight'
1+
import { MqlPayload, MqlOptions, MicrolinkApiOptions } from '../lightweight'
22

3-
export { MqlPayload } from './lightweight'
3+
export { MqlPayload } from '../lightweight'
44

55
export type MqlResponse = MqlPayload & {
66
response: {

test/build.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
import { createRequire } from 'module'
4+
import { $ } from 'execa'
5+
import test from 'ava'
6+
7+
const require = createRequire(import.meta.url)
8+
const pkg = require('../package.json')
9+
10+
const evalScript = code => $`node -e ${code}`
11+
12+
test('cjs', async t => {
13+
// eslint-disable-next-line no-template-curly-in-string
14+
const code = "console.log(`mql v${require('@microlink/mql').version}`)"
15+
const { stdout } = await evalScript(code)
16+
t.is(stdout, `mql v${pkg.version}`)
17+
})

0 commit comments

Comments
 (0)