Skip to content

Commit

Permalink
Merge pull request #432 from lohfu/full-esm-support
Browse files Browse the repository at this point in the history
Convert to type "module" and use package exports
  • Loading branch information
stefnotch authored Jun 7, 2021
2 parents 6f5c321 + 3a42194 commit c19b07f
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 82 deletions.
9 changes: 2 additions & 7 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
module.exports = {
"presets": ["@babel/env"],
"env": {
"esm": {
"presets": [["@babel/env", { "modules": false }]]
}
}
export default {
"presets": [["@babel/env", { "modules": false }]]
};
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
sudo: false
node_js:
- "12.16.1"
- "14.17.0"
script:
- npm run build
- npm run test
34 changes: 23 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
"description": "Javascript Matrix and Vector library for High Performance WebGL apps",
"private": true,
"sideEffects": false,
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"type": "module",
"main": "./dist/esm/index.js",
"module": "./dist/esm/index.js",
"exports": {
".": "./dist/esm/index.js",
"./common": "./dist/esm/common.js",
"./mat2": "./dist/esm/mat2.js",
"./mat2d": "./dist/esm/mat2d.js",
"./mat3": "./dist/esm/mat3.js",
"./mat4": "./dist/esm/mat4.js",
"./quat": "./dist/esm/quat.js",
"./quat2": "./dist/esm/quat2.js",
"./vec2": "./dist/esm/vec2.js",
"./vec3": "./dist/esm/vec3.js",
"./vec4": "./dist/esm/vec4.js"
},
"types": "./dist/index.d.ts",
"homepage": "http://glmatrix.net",
"license": "MIT",
"bugs": {
Expand All @@ -31,26 +45,24 @@
"doc": "jsdoc -c jsdoc.config.json",
"update-license-version": "node utils/update-license-version.js",
"build-umd": "rollup -c",
"build-esm": "cross-env BABEL_ENV=esm babel src -d dist/esm",
"build-cjs": "babel src -d dist/cjs",
"build-esm": "babel src -d dist/esm",
"build-dts": "tsc --allowJs --declaration --emitDeclarationOnly --module amd --outFile ./dist/index.d.ts ./src/index.js ./src/types.d.ts && node ./utils/bundle-dts.js && tsc --noEmit ./dist/index.d.ts",
"build": "del dist && npm run update-license-version && npm run build-umd && npm run build-esm && npm run build-cjs && npm run build-dts && node ./utils/build.js",
"build": "del dist && npm run update-license-version && npm run build-umd && npm run build-esm && npm run build-dts && node ./utils/build.js",
"prepare": "npm run build"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@babel/register": "^7.9.0",
"cross-env": "^7.0.2",
"@rollup/plugin-babel": "^5.3.0",
"del-cli": "^3.0.0",
"jsdoc": "^3.6.3",
"mocha": "^7.1.1",
"node-libs-browser": "^2.2.1",
"rollup": "^2.3.2",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-size-snapshot": "^0.11.0",
"rollup-plugin-terser": "^5.3.0",
"rollup": "^2.50.5",
"rollup-plugin-size-snapshot": "^0.12.0",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^3.8.3"
},
"dependencies": {}
Expand Down
8 changes: 5 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import babel from 'rollup-plugin-babel';
import fs from 'fs';
import babel from '@rollup/plugin-babel';
import { terser } from 'rollup-plugin-terser';
import { sizeSnapshot } from 'rollup-plugin-size-snapshot';

const version = require('./package.json').version;
const license = require('./utils/license-template');
import license from './utils/license-template.js';

const { version } = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url)));

const input = './src/index.js';
const name = 'glMatrix';
Expand Down
2 changes: 1 addition & 1 deletion spec/gl-matrix/common-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as glMatrix from "../../src/common"
import * as glMatrix from "../../src/common.js"

describe("common", function(){
let result;
Expand Down
2 changes: 1 addition & 1 deletion spec/gl-matrix/mat2-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as mat2 from "../../src/mat2"
import * as mat2 from "../../src/mat2.js"

describe("mat2", function() {
let out, matA, matB, identity, result;
Expand Down
2 changes: 1 addition & 1 deletion spec/gl-matrix/mat2d-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as mat2d from "../../src/mat2d"
import * as mat2d from "../../src/mat2d.js"

describe("mat2d", function() {
let out, matA, matB, oldA, oldB, identity, result;
Expand Down
6 changes: 3 additions & 3 deletions spec/gl-matrix/mat3-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as mat3 from "../../src/mat3"
import * as mat4 from "../../src/mat4"
import * as vec3 from "../../src/vec3"
import * as mat3 from "../../src/mat3.js"
import * as mat4 from "../../src/mat4.js"
import * as vec3 from "../../src/vec3.js"

describe("mat3", function() {
let out, matA, matB, identity, result;
Expand Down
8 changes: 4 additions & 4 deletions spec/gl-matrix/mat4-spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as glMatrix from "../../src/common"
import * as mat4 from "../../src/mat4"
import * as quat from "../../src/quat"
import * as vec3 from "../../src/vec3"
import * as glMatrix from "../../src/common.js"
import * as mat4 from "../../src/mat4.js"
import * as quat from "../../src/quat.js"
import * as vec3 from "../../src/vec3.js"

function buildMat4Tests() {
return function() {
Expand Down
8 changes: 4 additions & 4 deletions spec/gl-matrix/quat-spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as mat3 from "../../src/mat3"
import * as mat4 from "../../src/mat4"
import * as quat from "../../src/quat"
import * as vec3 from "../../src/vec3"
import * as mat3 from "../../src/mat3.js"
import * as mat4 from "../../src/mat4.js"
import * as quat from "../../src/quat.js"
import * as vec3 from "../../src/vec3.js"

describe("quat", function() {
let out, quatA, quatB, result;
Expand Down
6 changes: 3 additions & 3 deletions spec/gl-matrix/quat2-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as quat from "../../src/quat";
import * as quat2 from "../../src/quat2";
import * as mat4 from "../../src/mat4";
import * as quat from "../../src/quat.js";
import * as quat2 from "../../src/quat2.js";
import * as mat4 from "../../src/mat4.js";

describe("quat2", function() {
let out, outVec, quat2A, quat2B, result, resultVec, outQuat;
Expand Down
2 changes: 1 addition & 1 deletion spec/gl-matrix/vec2-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as vec2 from "../../src/vec2"
import * as vec2 from "../../src/vec2.js"

describe("vec2", function() {
let out, vecA, vecB, result;
Expand Down
6 changes: 3 additions & 3 deletions spec/gl-matrix/vec3-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as mat3 from "../../src/mat3"
import * as mat4 from "../../src/mat4"
import * as vec3 from "../../src/vec3"
import * as mat3 from "../../src/mat3.js"
import * as mat4 from "../../src/mat4.js"
import * as vec3 from "../../src/vec3.js"

describe("vec3", function() {
let out, vecA, vecB, result;
Expand Down
4 changes: 2 additions & 2 deletions spec/gl-matrix/vec4-spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vec3 from "../../src/vec3"
import * as vec4 from "../../src/vec4"
import * as vec3 from "../../src/vec3.js"
import * as vec4 from "../../src/vec4.js"

describe("vec4", function() {
let out, vecA, vecB, result;
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/spec-helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
const EPSILON = 0.00001;
const assert = require('assert');


global.expect = function(e) {
Expand Down
27 changes: 4 additions & 23 deletions utils/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const pkg = require('../package.json');
import fs from 'fs';

const pkgStr = fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8');
const pkg = JSON.parse(pkgStr.replace(/\.\/dist\//g, './'));

const copyFileSync = (source, dest) => {
const content = fs.readFileSync(source, 'utf-8');
Expand All @@ -10,27 +11,7 @@ const copyFileSync = (source, dest) => {
delete pkg.private;
delete pkg.scripts;
delete pkg.devDependencies;
pkg.main = 'cjs/index.js'
pkg.module = 'esm/index.js'
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, 2));

copyFileSync('README.md', 'dist/README.md');
copyFileSync('LICENSE.md', 'dist/LICENSE.md');

const files = fs.readdirSync('src')
.filter(file => !file.includes('common') && !file.includes('index'))
.forEach(file => {
const name = file.endsWith('.js') ? file.slice(0, -3) : file;
const filePkg = {
name: `gl-matrix/${name}`,
main: `../cjs/${file}`,
module: `../esm/${file}`,
};
if(!fs.existsSync(`dist/${name}`)) {
fs.mkdirSync(`dist/${name}`);
}
fs.writeFileSync(
`dist/${name}/package.json`,
JSON.stringify(filePkg, null, 2)
);
});
3 changes: 1 addition & 2 deletions utils/bundle-dts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require("fs");
const path = require("path");
import fs from "fs";

let sourcePath = "./dist/index.d.ts";
let sourceTypingsPath = "./src/types.d.ts";
Expand Down
4 changes: 1 addition & 3 deletions utils/license-template.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

module.exports = `Copyright (c) 2015-${new Date().getFullYear()}, Brandon Jones, Colin MacKenzie IV.
export default `Copyright (c) 2015-${new Date().getFullYear()}, Brandon Jones, Colin MacKenzie IV.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 5 additions & 8 deletions utils/update-license-version.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict'
import fs from 'fs';
import template from './license-template.js';

const fs = require('fs');
const path = require('path');
const { version } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));

const version = require(path.join(process.cwd(), 'package.json')).version;
const template = require('./license-template');

fs.writeFileSync(path.join(process.cwd(), 'LICENSE.md'), template, 'utf8');
fs.writeFileSync(path.join(process.cwd(), 'VERSION'), version, 'utf8');
fs.writeFileSync(new URL('../LICENSE.md', import.meta.url), template, 'utf8');
fs.writeFileSync(new URL('../VERSION', import.meta.url), version, 'utf8');

0 comments on commit c19b07f

Please sign in to comment.