Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '@rollup/plugin-esm-shim' #123

Merged
merged 8 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
on:
push:
branches:
- main
- main
tags:
- '!*'
- '!*'
pull_request:
branches:
- main
- main

name: main
jobs:
Expand Down Expand Up @@ -40,6 +40,8 @@ jobs:

- run: npm run test

- run: npm run test-dist

- run: npm run demo

- if: matrix.node == '18' && matrix.os == 'ubuntu-latest'
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ publish/etc
publish/*.tgz
tools/*.js
issue/*.js
dist
/dist
NOTICE
21 changes: 21 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
"issue": "cd issue && tsc && node issue.js",
"prepare": "copyfiles -f ./node_modules/@dlemstra/magick-native/NOTICE . && copyfiles -f ./node_modules/@dlemstra/magick-native/magick.wasm dist",
"test": "vitest run",
"test-dist": "cd tests/dist && node test-dist",
"update-index": "cd tools && tsc && node update-index.js ../src"
},
"devDependencies": {
"@dlemstra/magick-native": "0.202310.142050",
"@rollup/plugin-esm-shim": "^0.1.4",
"@types/jsdom": "21.1.4",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
Expand Down
10 changes: 10 additions & 0 deletions tests/dist/test-CJS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

const { initializeImageMagick, Magick } = require('../../dist/index.umd.js');

const wasmLocation = '../../node_modules/@dlemstra/magick-native/magick.wasm';

initializeImageMagick(wasmLocation).then(() => {
console.log(Magick.imageMagickVersion);
});
10 changes: 10 additions & 0 deletions tests/dist/test-ESM.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

import { initializeImageMagick, Magick } from '../../dist/index.mjs';

const wasmLocation = '../../node_modules/@dlemstra/magick-native/magick.wasm';

initializeImageMagick(wasmLocation).then(() => {
console.log(Magick.imageMagickVersion);
Copy link
Owner

Choose a reason for hiding this comment

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

We could also use the features here instead so we don't need a regex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!

});
56 changes: 56 additions & 0 deletions tests/dist/test-dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);

const colorReset = '\x1b[0m';
const colorGreen = '\x1b[32m';
const colorRed = '\x1b[31m';

const versionRegEx =
/^ImageMagick \d+\.\d+\.\d+-\d+ Q8 x86_64 [\w\d]+:[\w\d]+ https:\/\/imagemagick\.org$/;

let foundError = false;

async function runTest(filename) {
try {
return await exec(`node ${filename}`);
} catch (error) {
return {
stdout: '',
stderr: error.stderr,
};
}
}

async function testDistFile(filename) {
const { stdout, stderr } = await runTest(filename);

const name = filename.substring(5, 8);

if (stdout.trim().match(versionRegEx)) {
console.log(`${colorGreen}${name} build passed${colorReset}`);
return;
}

foundError = true;

console.error(`${colorRed}${name} build failed:${colorReset}`);
console.error(
stderr || `"${stdout.trim()}"\ndoes not match\n${versionRegEx}`
);
}

async function testDist() {
console.log('');

await testDistFile('test-ESM.mjs');
await testDistFile('test-CJS.js');

if (foundError) process.exit(1);

console.log('');
}

testDist();
6 changes: 5 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { builtinModules } from 'module';
import { defineConfig } from 'vitest/config';
import esmShim from '@rollup/plugin-esm-shim';
import path from 'path';

export default defineConfig({
Expand All @@ -13,6 +14,9 @@ export default defineConfig({
commonjsOptions: {
ignore: [...builtinModules, 'ws'],
},
rollupOptions: {
plugins: [esmShim()],
},
},
test: {
globals: true,
Expand All @@ -23,7 +27,7 @@ export default defineConfig({
resolve: {
alias: {
'@src': path.resolve(__dirname, './src'),
'@test': path.resolve(__dirname, './tests')
'@test': path.resolve(__dirname, './tests'),
},
},
});
Loading