Skip to content

Commit f0aaa48

Browse files
committed
Build ESM
1 parent 9b292c7 commit f0aaa48

9 files changed

Lines changed: 105 additions & 2 deletions

File tree

.github/workflows/publish-cdn.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ jobs:
4141
# Also create a latest version for easy access
4242
cp dist/constructorio-client-javascript-$VERSION.js s3_dist/latest.js
4343
44+
# Modern ESM bundle
45+
cp dist/esm/constructorio-client-javascript-$VERSION.esm.js s3_dist/$VERSION.esm.js
46+
cp dist/esm/constructorio-client-javascript-$VERSION.esm.js s3_dist/latest.esm.js
47+
4448
# Sync to S3 with appropriate caching and permissions
4549
aws s3 sync s3_dist s3://constructor-frontend-prod/sdk/client-javascript --cache-control 'public, max-age=31536000, immutable' --acl 'public-read'
4650
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Run tests - Bundled ESM
2+
on:
3+
pull_request:
4+
branches:
5+
- "**"
6+
concurrency:
7+
group: run-tests-bundled-esm-${{ github.head_ref }}
8+
cancel-in-progress: true
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
node: ["22.18.0"]
15+
steps:
16+
- name: Check out code
17+
uses: actions/checkout@v2
18+
- name: Set up node
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: ${{ matrix.node }}
22+
- name: Install dependencies
23+
run: npm install
24+
- name: Run tests
25+
run: npm run test:bundled:esm:parallel
26+
env:
27+
TEST_REQUEST_API_KEY: ${{ secrets.TEST_REQUEST_API_KEY }}
28+
TEST_MEDIA_REQUEST_API_KEY: ${{ secrets.TEST_MEDIA_REQUEST_API_KEY }}
29+
SKIP_NETWORK_TIMEOUT_TESTS: true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ lib
44
# Test temporary directory
55
test
66

7+
# Test-only build artifact (modern source as IIFE for bundled tests)
8+
dist/test-only/
9+
710
# OS X specific files
811
.DS_Store
912

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.79.0",
44
"description": "Constructor.io JavaScript client",
55
"main": "lib/constructorio.js",
6+
"module": "lib/esm/constructorio.js",
67
"types": "lib/types/index.d.ts",
78
"scripts": {
89
"clean": "sudo rm -rf node_modules package-lock.json",
@@ -17,15 +18,19 @@
1718
"test:src:parallel": "mkdir -p test && cp -rf src/* test && mocha --parallel --retries 3 ./spec/*",
1819
"test:bundled": "npm run bundle && npm run generate-test-folder && BUNDLED=true PACKAGE_VERSION=$(echo $npm_package_version) mocha --retries 3 ./spec/*",
1920
"test:bundled:parallel": "npm run bundle && npm run generate-test-folder && BUNDLED=true PACKAGE_VERSION=$(echo $npm_package_version) mocha --parallel --retries 3 ./spec/*",
21+
"test:bundled:esm": "npm run bundle && node ./scripts/build-test-iife.js && npm run generate-test-folder && BUNDLED=true BUNDLED_VARIANT=esm PACKAGE_VERSION=$(echo $npm_package_version) mocha --retries 3 ./spec/*",
22+
"test:bundled:esm:parallel": "npm run bundle && node ./scripts/build-test-iife.js && npm run generate-test-folder && BUNDLED=true BUNDLED_VARIANT=esm PACKAGE_VERSION=$(echo $npm_package_version) mocha --parallel --retries 3 ./spec/*",
2023
"test:all": "npm run test && npm run test:bundled",
2124
"test:all:parallel": "npm run test:parallel && npm run test:bundled:parallel",
2225
"precoverage": "rm -rf ./coverage && rm -rf ./.nyc_output",
2326
"coverage": "nyc --all --reporter=html npm run test:src",
2427
"postcoverage": "open coverage/index.html && rm -rf test",
2528
"docs": "jsdoc --configure ./.jsdoc.json ./README.md --recurse ./src --destination ./docs",
2629
"compile": "rm -rf ./lib/* && babel src/ -d lib/ --copy-files && rm -rf ./lib/types/tests",
30+
"postcompile": "node ./scripts/build-esm.js",
2731
"prepublish": "npm run compile",
2832
"bundle": "rm -rf ./dist/* && npm run compile && node bundle.js",
33+
"postbundle": "node ./scripts/bundle-esm.js",
2934
"prepare": "husky install",
3035
"generate-test-folder": "mkdir -p test && cp -rf lib/* test"
3136
},

scripts/build-esm.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const packageJSON = require('../package.json');
2+
3+
require('esbuild').build({
4+
entryPoints: ['./src/constructorio.js'],
5+
bundle: true,
6+
format: 'esm',
7+
target: 'es2017',
8+
platform: 'browser',
9+
sourcemap: true,
10+
external: ['@constructor-io/constructorio-id', 'crc-32'],
11+
outfile: './lib/esm/constructorio.js',
12+
}).catch(() => process.exit(1));

scripts/build-test-iife.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const packageJSON = require('../package.json');
2+
3+
require('esbuild').build({
4+
entryPoints: ['./src/constructorio.js'],
5+
bundle: true,
6+
minify: true,
7+
format: 'iife',
8+
globalName: 'ConstructorioClient',
9+
target: 'es2017',
10+
platform: 'browser',
11+
define: {
12+
process: '{"env":{"BUNDLED":"true"}}',
13+
},
14+
outfile: `./dist/test-only/constructorio-client-javascript-${packageJSON.version}.iife.js`,
15+
}).catch(() => process.exit(1));

scripts/bundle-esm.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const packageJSON = require('../package.json');
2+
3+
const banner = `/*!
4+
*
5+
* Constructor.io JavaScript Client, version ${packageJSON.version}
6+
* (c) 2015-${new Date().getFullYear()} Constructor.io
7+
* ---
8+
* Constructor Search uses artificial intelligence to provide AI-first search, browse, and recommendations results that increase conversions and revenue.
9+
* - https://constructor.io
10+
* - https://github.com/Constructor-io/constructorio-client-javascript
11+
* ---
12+
* Includes code from the 'browserify/events' library, licensed under the MIT License.
13+
* For full license details, see the library documentation.
14+
*
15+
*/`;
16+
17+
require('esbuild').build({
18+
entryPoints: ['./src/constructorio.js'],
19+
bundle: true,
20+
minify: true,
21+
format: 'esm',
22+
target: 'es2017',
23+
platform: 'browser',
24+
define: {
25+
process: '{"env":{"BUNDLED":"true"}}',
26+
},
27+
banner: { js: banner },
28+
outfile: `./dist/esm/constructorio-client-javascript-${packageJSON.version}.esm.js`,
29+
}).catch(() => process.exit(1));

spec/src/modules/quizzes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
2727
let cleanup;
2828

2929
if (bundled) {
30-
jsdomOptions.src = fs.readFileSync(`./dist/constructorio-client-javascript-${process.env.PACKAGE_VERSION}.js`, 'utf-8');
30+
const bundlePath = process.env.BUNDLED_VARIANT === 'esm'
31+
? `./dist/test-only/constructorio-client-javascript-${process.env.PACKAGE_VERSION}.iife.js`
32+
: `./dist/constructorio-client-javascript-${process.env.PACKAGE_VERSION}.js`;
33+
jsdomOptions.src = fs.readFileSync(bundlePath, 'utf-8');
3134
}
3235

3336
beforeEach(() => {

spec/src/utils/jsdom-global.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const bundled = process.env.BUNDLED === 'true';
1010
// to make it available in the tests
1111
let cioScriptTag = '';
1212
if (bundled) {
13-
const cioJSBundle = fs.readFileSync(`./dist/constructorio-client-javascript-${process.env.PACKAGE_VERSION}.js`, 'utf-8');
13+
const bundlePath = process.env.BUNDLED_VARIANT === 'esm'
14+
? `./dist/test-only/constructorio-client-javascript-${process.env.PACKAGE_VERSION}.iife.js`
15+
: `./dist/constructorio-client-javascript-${process.env.PACKAGE_VERSION}.js`;
16+
const cioJSBundle = fs.readFileSync(bundlePath, 'utf-8');
1417
cioScriptTag = `<script>${cioJSBundle}</script>`;
1518
}
1619

0 commit comments

Comments
 (0)