diff --git a/package.json b/package.json index d0f005b2..fcfce8aa 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,9 @@ "url": "https://github.com/pnext/three-loader/issues" }, "license": "MIT", - "main": "build/potree.js", - "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", @@ -25,6 +26,15 @@ "prettier": "prettier --write \"**/*.js\" \"**/*.jsx\" \"**/*.tsx\" \"**/*.ts\"", "prepare-release": "standard-version" }, + "exports": { + "./package.json": "./package.json", + ".": { + "types": "./build/index.d.ts", + "import": "./build/index.mjs", + "require": "./build/index.cjs", + "default": "./build/index.cjs" + } + }, "dependencies": {}, "peerDependencies": { "three": "~0.160.0" @@ -73,4 +83,4 @@ ], "homepage": "https://github.com/pnext/three-loader#readme", "author": "" -} \ No newline at end of file +} 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 f58d27f6..6d7ea3e7 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,19 +1,26 @@ 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: 'index.cjs', + library: { + type: 'umd', + umdNamedDefine: true, + }, }, devtool: 'eval-cheap-source-map', stats: 'errors-only', resolve: { extensions: ['.ts', '.tsx', '.js'], + fallback: { + fs: false, + path: false, + }, }, externals: ['three'], module: { 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..d72e0693 --- /dev/null +++ b/webpack.esm.config.js @@ -0,0 +1,48 @@ +const path = require('path'); + +module.exports = { + name: 'module', + entry: './src/index.ts', + experiments: { + outputModule: true, + }, + output: { + path: path.resolve(__dirname, 'build'), + filename: 'index.mjs', + module: true, + library: { + type: 'module', + }, + }, + stats: 'errors-only', + resolve: { + extensions: ['.tsx', '.ts', '.js'], + fallback: { + fs: false, + path: false, + }, + }, + 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: [], +};