From 0f20aa265efd3355329bc4af7d827fa768f87ca2 Mon Sep 17 00:00:00 2001 From: Vikram Subramanian Date: Fri, 17 Apr 2020 23:57:02 -0700 Subject: [PATCH 1/3] Support node v13 with es modules Creates conditional export that points "import" conditional exports to mjs files. This lets node load the esm version with having to set "type": "module" on the main package.json. This is similar to the approach in the main preact repo - https://github.com/preactjs/preact/pull/2451 --- package.json | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/package.json b/package.json index b39f0de..6bfcc54 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,39 @@ "umd:main": "dist/htm.umd.js", "module": "dist/htm.module.js", "types": "dist/htm.d.ts", + "exports": { + ".": { + "import": "./dist/htm.mjs", + "require": "./dist/htm.js", + "browser": "./dist/htm.module.js", + "umd": "./dist/htm.umd.js" + }, + "./": "./", + "./preact": { + "import": "./preact/index.mjs", + "require": "./preact/index.js", + "browser": "./preact/index.module.js", + "umd": "./preact/index.umd.js" + }, + "./preact/standalone": { + "import": "./preact/standalone.mjs", + "require": "./preact/standalone.js", + "browser": "./preact/standalone.module.js", + "umd": "./preact/standalone.umd.js" + }, + "./react": { + "import": "./react/index.mjs", + "require": "./react/index.js", + "browser": "./react/index.module.js", + "umd": "./react/index.umd.js" + }, + "./mini": { + "import": "./mini/index.mjs", + "require": "./mini/index.js", + "browser": "./mini/index.module.js", + "umd": "./mini/index.umd.js" + } + }, "scripts": { "build": "npm run -s build:main && npm run -s build:mini && npm run -s build:preact && npm run -s build:react && npm run -s build:babel && npm run -s build:babel-transform-jsx && npm run -s build:mjsalias", "build:main": "microbundle src/index.mjs -f es,umd --no-sourcemap --target web && microbundle src/cjs.mjs -f iife --no-sourcemap --target web && cp src/index.d.ts dist/htm.d.ts", From dbba970e4784b54256759135bb96fe105a98e459 Mon Sep 17 00:00:00 2001 From: Vikram Subramanian Date: Wed, 29 Apr 2020 19:29:17 -0700 Subject: [PATCH 2/3] Add test for Node ESM conditional exports --- .github/workflows/test.yml | 24 ++++++++++++++---------- .gitignore | 1 + package.json | 3 ++- test/fixtures/esm/index.js | 5 +++++ test/fixtures/esm/package.json | 10 ++++++++++ 5 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/esm/index.js create mode 100644 test/fixtures/esm/package.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 33c84ef..0aa1f95 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,15 +7,19 @@ jobs: runs-on: ubuntu-latest strategy: matrix: + # Change the condition for ESM Dist Test below when changing this. node-version: [10.x, 12.x, 14.x] steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - name: Install - run: npm install - - name: Build and Test - run: npm test + - name: Checkout + uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: Install + run: npm install + - name: Build and Test + run: npm test + - if: matrix.node-version == '14.x' + name: ESM Dist Test + run: npm run test:dist diff --git a/.gitignore b/.gitignore index a32229b..fbca3fd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ package-lock.json dist mini yarn.lock +htm.tgz diff --git a/package.json b/package.json index 6bfcc54..60b0a31 100644 --- a/package.json +++ b/package.json @@ -48,8 +48,9 @@ "build:babel": "cd packages/babel-plugin-htm && npm run build", "build:babel-transform-jsx": "cd packages/babel-plugin-transform-jsx-to-htm && npm run build", "build:mjsalias": "cp dist/htm.module.js dist/htm.mjs && cp mini/index.module.js mini/index.mjs && cp preact/index.module.js preact/index.mjs && cp preact/standalone.module.js preact/standalone.mjs && cp react/index.module.js react/index.mjs", - "test": "eslint src/**/*.mjs test/**/*.mjs && npm run build && jest test", + "test": "eslint src/**/*.mjs test/**/*.mjs --ignore-path .gitignore && npm run build && jest test", "test:perf": "v8 test/__perftest.mjs", + "test:dist": "npm pack && mv htm*.tgz test/fixtures/esm/htm.tgz && cd test/fixtures/esm && npm install && node index.js", "release": "npm t && git commit -am \"$npm_package_version\" && git tag $npm_package_version && git push && git push --tags && npm publish" }, "files": [ diff --git a/test/fixtures/esm/index.js b/test/fixtures/esm/index.js new file mode 100644 index 0000000..317ebc7 --- /dev/null +++ b/test/fixtures/esm/index.js @@ -0,0 +1,5 @@ +import "htm"; +import "htm/preact"; +import "htm/preact/standalone"; +// TODOD: Enable once react distro is ESM compatible. +// import "htm/react"; diff --git a/test/fixtures/esm/package.json b/test/fixtures/esm/package.json new file mode 100644 index 0000000..5f7e92b --- /dev/null +++ b/test/fixtures/esm/package.json @@ -0,0 +1,10 @@ +{ + "name": "htm_dist_test", + "type": "module", + "private": true, + "description": "A package to test importing htm as ES modules in Node", + "dependencies": { + "htm": "file:htm.tgz", + "preact": "^10.4.1" + } +} From f4bec0a68987826db1c24ae8e634a63242bb7e4a Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 1 May 2020 10:40:03 -0400 Subject: [PATCH 3/3] Add a couple basic assertions to the dist output --- test/fixtures/esm/index.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/test/fixtures/esm/index.js b/test/fixtures/esm/index.js index 317ebc7..560bee7 100644 --- a/test/fixtures/esm/index.js +++ b/test/fixtures/esm/index.js @@ -1,5 +1,14 @@ -import "htm"; -import "htm/preact"; -import "htm/preact/standalone"; -// TODOD: Enable once react distro is ESM compatible. -// import "htm/react"; +import assert from 'assert'; +import htm from 'htm'; +import * as preact from 'htm/preact'; +import * as standalone from 'htm/preact/standalone'; +// TODO: Enable once react distro is ESM compatible. +// import * as react 'htm/react'; + +assert(typeof htm === 'function', 'import htm from "htm"'); + +assert(typeof preact.html === 'function', 'import { html } from "preact"'); + +assert(typeof standalone.html === 'function', 'import { html } from "preact/standalone"'); + +console.log('✅ Dist Tests Passed');