Skip to content

Commit 4b56f09

Browse files
committed
Get asset compilation working with ESM
1 parent 9df880f commit 4b56f09

File tree

6 files changed

+159
-161
lines changed

6 files changed

+159
-161
lines changed

jest.config.cjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ module.exports = {
1010
rootDir: "./src"
1111
}
1212
}
13-
}
13+
},
14+
testMatch: [
15+
'<rootDir>/tests/**/*.[jt]s?(x)',
16+
],
1417
};

package.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,28 @@
2222
],
2323
"readmeFilename": "README.md",
2424
"devDependencies": {
25+
"@rollup/plugin-terser": "^0.4.3",
26+
"@rollup/plugin-typescript": "^11.1.1",
2527
"@types/jest": "^29.5.2",
2628
"@types/node": "^20.3.1",
2729
"benchmark": "^2.1.4",
2830
"jest": "^29.5.0",
2931
"prettier": "^2.8.8",
3032
"rimraf": "^5.0.1",
3133
"rollup": "^3.25.1",
32-
"rollup-plugin-typescript": "^1.0.1",
33-
"rollup-plugin-typescript2": "^0.35.0",
34-
"terser": "^5.18.1",
3534
"ts-jest": "^29.1.0",
35+
"tslib": "^2.5.3",
3636
"tslint": "^6.1.3",
3737
"tslint-config-prettier": "^1.18.0",
3838
"typescript": "^5.1.3"
3939
},
4040
"scripts": {
4141
"clean": "rimraf dist/**/*.js dist/**/*.ts dist/**/*.map",
42-
"build:umd": "rollup -c rollup.config.es3.js --format umd -o dist/umd/random-js.js --name Random -m",
43-
"build:esm": "rollup -c rollup.config.js --format esm --sourcemap true -o dist/esm/random-js.js",
42+
"build:umd": "rollup --c --environment BUILD:prod",
43+
"build:dev": "rollup --c --environment BUILD:dev",
4444
"prebuild": "yarn clean",
45-
"build": "yarn build:umd && yarn build:esm",
46-
"minify": "terser --source-map content=dist/umd/random-js.js.map --compress --mangle --output dist/umd/random-js.min.js dist/umd/random-js.js",
47-
"postbuild": "yarn minify",
45+
"build": "yarn build:dev && yarn build:prod",
46+
"watch": "yarn build:dev -- -w",
4847
"benchmark": "for k in benchmark/*.cjs; do node $k; done",
4948
"test": "jest",
5049
"lint": "tslint --project .",

rollup.config.es3.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import typescript from "rollup-plugin-typescript2";
1+
import typescript from 'rollup-plugin-typescript2';
2+
3+
const inputPath = './src/index.ts';
24

35
export default {
4-
input: "./src/index.ts",
6+
input: inputPath,
57
plugins: [
68
typescript({
79
tsconfigOverride: {
@@ -10,7 +12,7 @@ export default {
1012
declaration: false,
1113
declarationMap: false
1214
},
13-
exclude: ["dist/**", "src/**/*.test.ts"]
15+
exclude: ["dist/**", "**/*.test.ts"]
1416
}
1517
})
1618
]

rollup.config.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
import typescript from "rollup-plugin-typescript2";
1+
import typescript from "@rollup/plugin-typescript";
2+
import terser from '@rollup/plugin-terser';
23

3-
export default {
4-
input: "./src/index.ts",
5-
plugins: [
6-
typescript({
7-
tsconfigOverride: {
8-
exclude: ["dist/**", "src/**/*.test.ts"]
9-
}
10-
})
11-
]
12-
};
4+
const production = !process.env.BUILD || (process.env.BUILD === 'prod');
5+
const inputPath = './src/index.ts';
6+
const outputPath = (format, minify = false) => `dist/${format}/random-js${minify ? '.min' : ''}.js`;
7+
const packageName = 'Random';
8+
9+
const plugins = (isProduction = false) => [
10+
typescript(),
11+
// minify for production
12+
//isProduction ? terser({ keep_classnames: true }) : null,
13+
];
14+
15+
export default [
16+
// ESM
17+
{
18+
input: inputPath,
19+
output: {
20+
file: outputPath('esm', production),
21+
format: 'esm',
22+
sourcemap: true,
23+
},
24+
plugins: plugins(production),
25+
},
26+
// UMD
27+
{
28+
input: inputPath,
29+
output: {
30+
file: outputPath('umd', production),
31+
format: 'umd',
32+
name: packageName,
33+
sourcemap: true,
34+
},
35+
plugins: plugins(production),
36+
},
37+
];

tsconfig.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
"compilerOptions": {
33
"target": "esnext",
44
"module": "esnext",
5-
"declaration": true,
5+
"declaration": false,
6+
"declarationDir": null,
67
"rootDir": "./src",
78
"strict": true,
8-
// "noUnusedLocals": true,
9-
// "noUnusedParameters": true,
10-
// "noImplicitReturns": true,
11-
// "noFallthroughCasesInSwitch": true
129
"sourceMap": true
1310
},
1411
"exclude": ["dist/**"]

0 commit comments

Comments
 (0)