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

ES Module Support #455

Open
wants to merge 1 commit into
base: v3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
"presets": ["@babel/env"],
"env": {
"cjs": {
"plugins": [["add-import-extension", {extension: 'cjs', replace: true}]]
},
"esm": {
"presets": [["@babel/env", { "modules": false }]]
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Javascript Matrix and Vector library for High Performance WebGL apps",
"private": true,
"sideEffects": false,
"main": "dist/cjs/index.js",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.js",
"homepage": "http://glmatrix.net",
"license": "MIT",
Expand Down Expand Up @@ -32,7 +32,7 @@
"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-cjs": "cross-env BABEL_ENV=cjs babel src -d dist/cjs --out-file-extension .cjs",
"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",
"prepare": "npm run build"
Expand All @@ -42,6 +42,7 @@
"@babel/core": "7.9.0",
"@babel/preset-env": "7.9.0",
"@babel/register": "7.9.0",
"babel-plugin-add-import-extension": "1.6.0",
"cross-env": "7.0.2",
"del-cli": "3.0.0",
"jsdoc": "3.6.3",
Expand Down
18 changes: 15 additions & 3 deletions utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ const copyFileSync = (source, dest) => {
delete pkg.private;
delete pkg.scripts;
delete pkg.devDependencies;
pkg.main = 'cjs/index.js'
pkg.type = 'module';
pkg.main = 'cjs/index.cjs'
pkg.module = 'esm/index.js'
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, 2));
pkg.exports = {
'.': {
require: './cjs/index.cjs',
import: './esm/index.js'
}
};

copyFileSync('README.md', 'dist/README.md');
copyFileSync('LICENSE.md', 'dist/LICENSE.md');
Expand All @@ -23,9 +29,13 @@ const files = fs.readdirSync('src')
const name = file.endsWith('.js') ? file.slice(0, -3) : file;
const filePkg = {
name: `gl-matrix/${name}`,
main: `../cjs/${file}`,
main: `../cjs/${file.replace('.js', '.cjs')}`,
module: `../esm/${file}`,
};
pkg.exports[`./${name}`] = {
require: `./cjs/${file.replace('.js', '.cjs')}`,
import: `./esm/${file}`,
};
if(!fs.existsSync(`dist/${name}`)) {
fs.mkdirSync(`dist/${name}`);
}
Expand All @@ -34,3 +44,5 @@ const files = fs.readdirSync('src')
JSON.stringify(filePkg, null, 2)
);
});

fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, 2));