From 3cb40b4e9718ec32dd6ce20ead84c366eccd0955 Mon Sep 17 00:00:00 2001 From: Kolezhniuk Date: Tue, 8 Nov 2022 17:09:47 +0100 Subject: [PATCH] esm module build fix --- package.json | 8 ++++--- webpack.esm.config.js | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 webpack.esm.config.js diff --git a/package.json b/package.json index 23bf3ed..47f8de3 100644 --- a/package.json +++ b/package.json @@ -6,16 +6,18 @@ ".": { "import": { "types": "./dist/esm/types/index.d.ts", - "default": "./dist/esm/index.mjs" + "module": "./dist/esm/index.js", + "default": "./dist/esm/index.js" }, "require": { "types": "./dist/cjs/types/index.d.ts", + "module": "./dist/cjs/index.js", "default": "./dist/cjs/index.js" } } }, "types": "./dist/cjs/types/index.d.ts", - "main": "./dist/cjs/index.js", + "main": "index.js", "module": "./dist/esm/index.mjs", "source": "./src/index.ts", "files": [ @@ -23,7 +25,7 @@ ], "scripts": { "clean": "rimraf ./dist", - "build": "npm run clean && tsc -p configs/tsconfig.cjs.json && tsc -p configs/tsconfig.esm.json && webpack --mode production", + "build": "npm run clean && tsc -p configs/tsconfig.cjs.json && webpack --config webpack.esm.config.js --mode=production && webpack --mode production", "test:coverage": "jest --coverage", "test": "jest", "test:watch": "jest --watch", diff --git a/webpack.esm.config.js b/webpack.esm.config.js new file mode 100644 index 0000000..70b9a01 --- /dev/null +++ b/webpack.esm.config.js @@ -0,0 +1,54 @@ +const path = require('path'); +const webpack = require('webpack'); +const { CleanWebpackPlugin } = require('clean-webpack-plugin'); +const { merge } = require('webpack-merge'); + +const prodConfig = require('./webpack.prod'); +module.exports = function () { + const config = { + mode: "production", + // devtool: 'source-map', + entry: './src/index.ts', + experiments: { + outputModule: true + }, + output: { + filename: 'index.js', + path: path.resolve(__dirname, 'dist/esm'), + library: { + type: "commonjs2", + } + }, + module: { + rules: [ + { + test: /\.tsx?$/, + exclude: /(node_modules|bower_components)/, + loader: 'ts-loader' + } + ] + }, + plugins: [ + new webpack.ProgressPlugin(), + new CleanWebpackPlugin(), + new webpack.ProvidePlugin({ + Buffer: ['buffer', 'Buffer'], + }), + new webpack.ProvidePlugin({ + process: 'process/browser', + }) + ], + resolve: { + extensions: ['.tsx', '.ts', '.js'], + fallback: { + assert: require.resolve('assert'), + crypto: require.resolve('crypto-browserify'), + os: require.resolve('os-browserify/browser'), + stream: require.resolve('stream-browserify'), + process: require.resolve('process/browser'), + buffer: require.resolve('buffer') + } + } + } + return merge(config, prodConfig); +};