Skip to content

Commit

Permalink
tsc: building two types of modules - es6 and commonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed May 25, 2023
1 parent 9eefa5c commit 261970a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
"publishConfig": {
"access": "public"
},
"main": "dist/cjs/src/index.js",
"types": "dist/esm/src/index.d.ts",
"module": "dist/esm/src/index.js",
"files": [
"dist"
],
"type": "commonjs",
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "rm -fr dist; npx tsc -b -verbose; npx webpack",
"build": "rm -rf dist && pnpm build:node && pnpm build:browser && pnpm webpack",
"build:node": " npx tsc -p tsconfig.cjs.json --noEmit false",
"build:browser": "npx tsc -p tsconfig.esm.json --noEmit false",
"build:types": "npx tsc -p tsconfig.types.json --noEmit false",
"clean": "gts clean",
"prepare": "husky install",
"pretest": "pnpm lint",
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"module": "commonjs",
"target": "es6",
"outDir": "dist/cjs",
}
}
10 changes: 10 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"module": "es6",
"target": "es6",
"outDir": "dist/esm",
}
}
13 changes: 9 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": ["es2021"],
"target": "ES6",
"module": "CommonJS",
"module": "es6",
"rootDir": ".",
"outDir": "./dist",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
Expand All @@ -16,5 +13,13 @@
"experimentalDecorators": true,
"resolveJsonModule": true
},
"ts-node": {
// these options are overrides used only by ts-node
// same as the --compilerOptions flag and the TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": {
"module": "commonjs"
}
},
"include": ["./src/*"],
"exclude": ["./dist", "./test"]
}
49 changes: 39 additions & 10 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ const path = require('path')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')

module.exports = {
var config = {
mode: 'production',
entry: './src/index.ts',
module: {
rules: [
{
test: /\.[jt]sx?$/,
use: 'ts-loader',
use: [{
loader: 'ts-loader',
options: {
configFile: "tsconfig.esm.json"
},
}],
exclude: /node_modules/,
},
],
Expand All @@ -18,14 +23,8 @@ module.exports = {
extensions: ['.ts', '.js', '.json'],
fallback: {
fs: false,
},
},
output: {
filename: 'marinade-ts-sdk.min.js',
path: path.resolve(__dirname, 'dist'),
library: {
name: 'MarinadeSdk',
type: 'this',
tls: false,
net: false,
},
},
plugins: [new NodePolyfillPlugin()],
Expand All @@ -42,4 +41,34 @@ module.exports = {
}),
],
},
// devtool: 'source-map',
}

var outputEsm = Object.assign({}, config, {
experiments: {
outputModule: true,
},
target: 'es6',
output : {
filename: 'marinade-ts-sdk.esm.min.js',
path: path.resolve(__dirname, 'dist'),
library: {
type: 'module',
},
chunkFormat: 'module',
module: true,
},
})

var outputCommonJs = Object.assign({}, config, {
output: {
filename: 'marinade-ts-sdk.cjs.min.js',
path: path.resolve(__dirname, 'dist'),
library: {
name: 'MarinadeSdk',
type: 'commonjs',
},
}
})

module.exports = [outputCommonJs, outputEsm]

0 comments on commit 261970a

Please sign in to comment.