Skip to content

Commit

Permalink
feat: add ESM bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Nov 4, 2023
1 parent d7f81ed commit 77eccc3
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 133 deletions.
5 changes: 3 additions & 2 deletions packages/npm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
debug: writeLog('samp')
}
</script>
<script src="./dist/microlink-function.js"></script>
<script>
<script type="module">
import microlink from './lightweight/index.js'

function getBrowser() {
var ua=navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
Expand Down
1 change: 1 addition & 0 deletions packages/npm/lightweight/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/npm/lightweight/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "index.js",
"type": "module"
}
37 changes: 22 additions & 15 deletions packages/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"name": "@microlink/function",
"homepage": "https://github.com/microlinkhq/function#readme",
"url": "https://function.microlink.io",
"description": "Browser functions as Service. Interacting with browser pages, remotely.",
"homepage": "https://function.microlink.io",
"version": "0.1.7",
"main": "src/node.js",
"browser": "src/browser.js",
"exports": {
".": {
"import": "./lightweight/index.js",
"require": "./src/node.js"
}
},
"author": {
"name": "Kiko Beats",
"email": "[email protected]",
"name": "Kiko Beats",
"url": "https://kikobeats.com"
},
"repository": {
Expand All @@ -29,47 +35,48 @@
"serverless"
],
"dependencies": {
"@microlink/mql": "~0.11.4",
"@microlink/mql": "~0.11.5",
"base64-url": "~2.3.3"
},
"devDependencies": {
"@rollup/plugin-commonjs": "latest",
"@rollup/plugin-node-resolve": "latest",
"@rollup/plugin-replace": "latest",
"abort-controller": "latest",
"@rollup/plugin-terser": "latest",
"ava": "latest",
"c8": "latest",
"conventional-github-releaser": "latest",
"node-fetch": "2",
"execa": "latest",
"npm-check-updates": "latest",
"rollup": "latest",
"rollup-plugin-filesize": "latest",
"rollup-plugin-shim": "latest",
"rollup-plugin-terser": "latest",
"rollup-plugin-visualizer": "latest"
},
"engines": {
"node": ">= 12"
"node": ">= 18"
},
"files": [
"dist",
"src"
],
"scripts": {
"build": "rollup -c rollup.config.js",
"postversion": "pnpm run build",
"build": "rollup -c rollup.config.js --bundleConfigAsCjs",
"dev": "npm run build -- -w",
"postversion": "pnpm run build",
"test": "c8 ava --verbose"
},
"license": "MIT",
"ava": {
"files": [
"test/**/*",
"!test/browser-globals.js",
"!test/clients.js"
"!test/clients.mjs"
],
"timeout": "1m"
},
"umd:main": "dist/microlink-function.js",
"unpkg": "dist/microlink-function.js"
"standard": {
"ignore": [
"dist",
"lightweight/index.js"
]
}
}
67 changes: 38 additions & 29 deletions packages/npm/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
import nodeResolve from '@rollup/plugin-node-resolve'
import visualizer from 'rollup-plugin-visualizer'
import { visualizer } from 'rollup-plugin-visualizer'
import commonjs from '@rollup/plugin-commonjs'
import replace from '@rollup/plugin-replace'
import { terser } from 'rollup-plugin-terser'
import filesize from 'rollup-plugin-filesize'
import replace from '@rollup/plugin-replace'
import terser from '@rollup/plugin-terser'

const build = ({ format, exports, input } = {}) => {
const base = ({ file, compress = false }) => ({
const build = ({ input, output, plugins = [], compress }) => {
return {
input,
output: {
exports,
name: 'microlink',
format,
file,
sourcemap: true
},
output,
plugins: [
nodeResolve({
mainFields: ['browser', 'module', 'main']
}),
replace({
__VERSION__: require('./package.json').version,
__MQL_VERSION__: require('@microlink/mql').version
values: {
"require('../package.json').version": "'__MFN_VERSION__'",
__MFN_VERSION__: require('./package.json').version
}
}),
commonjs(),
compress && terser(),
...plugins,
compress &&
terser({
format: {
comments: false
}
}),
filesize(),
visualizer()
]
})

return [
base({ file: 'dist/microlink-function.js', compress: false }),
base({ file: 'dist/microlink-function.min.js', compress: true })
]
}
}

export default build({
format: 'umd',
input: './src/browser.js'
})
const builds = [
/* This build is just for testing using ESM interface */
build({
input: './src/node.js',
output: { file: 'dist/node.mjs', format: 'es' },
plugins: [
commonjs()
]
}),
build({
compress: true,
input: 'src/lightweight.mjs',
output: { file: 'lightweight/index.js', format: 'es' },
plugins: [
nodeResolve()
]
})
]

export default builds
7 changes: 0 additions & 7 deletions packages/npm/src/browser.js

This file was deleted.

30 changes: 0 additions & 30 deletions packages/npm/src/factory.js

This file was deleted.

22 changes: 22 additions & 0 deletions packages/npm/src/lightweight.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mql from '@microlink/mql'

const fn = (code, mqlOpts, gotOpts) => async (url, opts) => {
const { data } = await mql(
url,
{
function: code.toString(),
meta: false,
...mqlOpts,
...opts
},
gotOpts
)

return data.function
}

fn.mql = mql
fn.render = mql.render
fn.version = require('../package.json').version

export default fn
30 changes: 25 additions & 5 deletions packages/npm/src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,28 @@ const toCompress = code =>
data => `br#${escape(data.toString('base64'))}`
)

module.exports = require('./factory')({
mql,
toCompress,
VERSION: require('../package.json').version
})

const fn = (code, mqlOpts, gotOpts) => {
const compress = toCompress(code)

return async (url, opts) => {
const { data } = await mql(
url,
{
function: await compress,
meta: false,
...mqlOpts,
...opts
},
gotOpts
)

return data.function
}
}

fn.mql = mql
fn.render = mql.render
fn.version = require('../package.json').version

module.exports = fn
Loading

0 comments on commit 77eccc3

Please sign in to comment.