From 58386831cb28e2909f845b7f40a39409a928c037 Mon Sep 17 00:00:00 2001 From: Jordan Lane Date: Tue, 17 Dec 2024 10:12:54 -0700 Subject: [PATCH 1/2] feat: export library as both umd and esm --- package.json | 13 +++++++++-- webpack.config.js | 11 ++++++---- webpack.config.prod.js | 50 ++++++++++++++++++++++++++++-------------- webpack.esm.config.js | 44 +++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 webpack.esm.config.js diff --git a/package.json b/package.json index d0f005b2..af01aba9 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "url": "https://github.com/pnext/three-loader/issues" }, "license": "MIT", - "main": "build/potree.js", + "main": "build/potree.cjs", + "module": "build/potree.mjs", "typings": "build/declarations/index.d.ts", "scripts": { "clean": "rimraf build", @@ -25,6 +26,14 @@ "prettier": "prettier --write \"**/*.js\" \"**/*.jsx\" \"**/*.tsx\" \"**/*.ts\"", "prepare-release": "standard-version" }, + "exports": { + "./package.json": "./package.json", + ".": { + "types": "./build/declarations/index.d.ts", + "import": "./build/potree.mjs", + "default": "./build/potree.cjs" + } + }, "dependencies": {}, "peerDependencies": { "three": "~0.160.0" @@ -73,4 +82,4 @@ ], "homepage": "https://github.com/pnext/three-loader#readme", "author": "" -} \ No newline at end of file +} diff --git a/webpack.config.js b/webpack.config.js index f58d27f6..5ab92570 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,14 +1,17 @@ const path = require('path'); + const SizePlugin = require('size-plugin'); module.exports = { + name: 'umd', entry: './src/index.ts', output: { path: path.resolve(__dirname, 'build'), - filename: 'potree.js', - library: 'potree', - libraryTarget: 'umd', - umdNamedDefine: true, + filename: 'potree.cjs', + library: { + type: 'umd', + umdNamedDefine: true, + }, }, devtool: 'eval-cheap-source-map', stats: 'errors-only', diff --git a/webpack.config.prod.js b/webpack.config.prod.js index 4861ef71..65c53ecd 100644 --- a/webpack.config.prod.js +++ b/webpack.config.prod.js @@ -1,20 +1,38 @@ const CircularDependencyPlugin = require('circular-dependency-plugin'); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; const webpack = require('webpack'); -const baseConfig = require('./webpack.config'); -module.exports = Object.assign(baseConfig, { - stats: 'normal', - plugins: [ - ...baseConfig.plugins, - new webpack.DefinePlugin({ - PRODUCTION: JSON.stringify(true), - }), - new CircularDependencyPlugin({ - exclude: /node_modules/, - failOnError: true, - cwd: process.cwd(), - }), - new BundleAnalyzerPlugin(), - ], -}); +const umdConfig = require('./webpack.config'); +const esmConfig = require('./webpack.esm.config'); + +module.exports = [ + Object.assign(umdConfig, { + stats: 'normal', + plugins: [ + ...umdConfig.plugins, + new webpack.DefinePlugin({ + PRODUCTION: JSON.stringify(true), + }), + new CircularDependencyPlugin({ + exclude: /node_modules/, + failOnError: true, + cwd: process.cwd(), + }), + new BundleAnalyzerPlugin(), + ], + }), + Object.assign(esmConfig, { + stats: 'normal', + plugins: [ + ...esmConfig.plugins, + new webpack.DefinePlugin({ + PRODUCTION: JSON.stringify(true), + }), + new CircularDependencyPlugin({ + exclude: /node_modules/, + failOnError: true, + cwd: process.cwd(), + }), + ], + }), +]; diff --git a/webpack.esm.config.js b/webpack.esm.config.js new file mode 100644 index 00000000..4c31dada --- /dev/null +++ b/webpack.esm.config.js @@ -0,0 +1,44 @@ +const path = require('path'); + +module.exports = { + name: 'module', + entry: './src/index.ts', + experiments: { + outputModule: true, + }, + output: { + path: path.resolve(__dirname, 'build'), + filename: 'potree.mjs', + module: true, + library: { + type: 'module', + }, + }, + stats: 'errors-only', + resolve: { + extensions: ['.tsx', '.ts', '.js'], + }, + externals: { three: 'three' }, + module: { + rules: [ + { + test: /\.worker\.js$/, + loader: 'worker-loader', + options: { inline: 'no-fallback' }, + }, + { + test: /\.js$/, + exclude: /node_modules/, + loader: 'babel-loader', + }, + { + test: /\.tsx?$/, + loader: 'ts-loader', + exclude: /node_modules/, + }, + + { test: /\.(vs|fs|glsl|vert|frag)$/, loader: 'raw-loader' }, + ], + }, + plugins: [], +}; From 4dc7382fa6d5392fe3c2155b320266422e979eb8 Mon Sep 17 00:00:00 2001 From: Jordan Lane Date: Thu, 19 Dec 2024 09:37:36 -0800 Subject: [PATCH 2/2] chore: standard library export conventions --- package.json | 13 +++++++------ tsconfig.json | 2 +- webpack.config.js | 6 +++++- webpack.esm.config.js | 6 +++++- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index af01aba9..fcfce8aa 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,9 @@ "url": "https://github.com/pnext/three-loader/issues" }, "license": "MIT", - "main": "build/potree.cjs", - "module": "build/potree.mjs", - "typings": "build/declarations/index.d.ts", + "main": "build/index.cjs", + "module": "build/index.mjs", + "typings": "build/index.d.ts", "scripts": { "clean": "rimraf build", "start": "webpack --mode development --watch --progress", @@ -29,9 +29,10 @@ "exports": { "./package.json": "./package.json", ".": { - "types": "./build/declarations/index.d.ts", - "import": "./build/potree.mjs", - "default": "./build/potree.cjs" + "types": "./build/index.d.ts", + "import": "./build/index.mjs", + "require": "./build/index.cjs", + "default": "./build/index.cjs" } }, "dependencies": {}, diff --git a/tsconfig.json b/tsconfig.json index 2b3fc949..b1011ae2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "baseUrl": "./src/", "sourceMap": true, "declaration": true, - "declarationDir": "build/declarations", + "declarationDir": "build/", "module": "es2015", "moduleResolution": "node", "strict": true, diff --git a/webpack.config.js b/webpack.config.js index 5ab92570..6d7ea3e7 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,7 +7,7 @@ module.exports = { entry: './src/index.ts', output: { path: path.resolve(__dirname, 'build'), - filename: 'potree.cjs', + filename: 'index.cjs', library: { type: 'umd', umdNamedDefine: true, @@ -17,6 +17,10 @@ module.exports = { stats: 'errors-only', resolve: { extensions: ['.ts', '.tsx', '.js'], + fallback: { + fs: false, + path: false, + }, }, externals: ['three'], module: { diff --git a/webpack.esm.config.js b/webpack.esm.config.js index 4c31dada..d72e0693 100644 --- a/webpack.esm.config.js +++ b/webpack.esm.config.js @@ -8,7 +8,7 @@ module.exports = { }, output: { path: path.resolve(__dirname, 'build'), - filename: 'potree.mjs', + filename: 'index.mjs', module: true, library: { type: 'module', @@ -17,6 +17,10 @@ module.exports = { stats: 'errors-only', resolve: { extensions: ['.tsx', '.ts', '.js'], + fallback: { + fs: false, + path: false, + }, }, externals: { three: 'three' }, module: {