Skip to content

Commit 0bf59ff

Browse files
committed
Fix change of type crashes my electron app #11
1 parent 9a647a3 commit 0bf59ff

File tree

7 files changed

+99
-81
lines changed

7 files changed

+99
-81
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default {
7676
If you're working with monorepos, the `packagePath` is made for you. It can take a path, or an array of paths, to your package.json file(s). If not specified, the default is to start with the current directory's package.json then go up scan for all package.json files in parent directories recursively until either the root git directory is reached or until no other package.json can be found.
7777

7878
#### builtins?: boolean = true
79-
Set the `builtins` option to `false` if you'd like to use some shims for those. You'll most certainly need an other plugin for this.
79+
Set the `builtins` option to `false` if you'd like to use some shims for those. You'll most certainly need [an other plugin](https://github.com/rollup/plugins/tree/master/packages/node-resolve/#resolving-built-ins-like-fs) for this.
8080

8181
#### prefixedBuiltins?: boolean | 'strip' = true
8282
How to handle the `node:` (or sometimes `nodejs:`) prefix some authors use in their code (i.e., `import path from 'node:path'`). If `true` (default), prefixed builtins are treated as their unprefixed equivalent. If `strip`, the prefix is removed from the name and other plugins will never know it was there.

package-lock.json

Lines changed: 39 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
module.exports = require('./package.json')
1+
const pkg = require('./package.json')
2+
module.exports = pkg

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"engines": {
3232
"node": ">=14.0.0"
3333
},
34-
"type": "commonjs",
34+
"type": "module",
3535
"main": "./dist/cjs/index.js",
3636
"module": "./dist/esm/index.js",
3737
"exports": {
@@ -60,9 +60,6 @@
6060
"clean": "rimraf dist",
6161
"prepublishOnly": "npm run lint && npm run clean && npm run build"
6262
},
63-
"dependencies": {
64-
"find-up": "^6.2.0"
65-
},
6663
"devDependencies": {
6764
"@rollup/plugin-commonjs": "^21.0.1",
6865
"@rollup/plugin-node-resolve": "^13.0.6",
@@ -72,11 +69,15 @@
7269
"ava-fast-check": "^4.0.2",
7370
"eslint": "^8.4.1",
7471
"fast-check": "^2.20.0",
72+
"find-up": "^6.2.0",
7573
"rimraf": "^3.0.2",
7674
"rollup": "^2.61.1",
7775
"rollup-plugin-ts": "^2.0.4",
7876
"ts-node": "^10.4.0",
7977
"tslib": "^2.3.1",
8078
"typescript": "^4.5.3"
79+
},
80+
"dependencies": {
81+
"@rollup/plugin-alias": "^3.1.8"
8182
}
8283
}

rollup.config.mjs

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,77 @@
11
// @ts-check
22
import { dirname } from 'path'
3-
import { builtinModules } from 'module'
3+
import alias from '@rollup/plugin-alias'
44
import resolve from '@rollup/plugin-node-resolve'
55
import commonjs from '@rollup/plugin-commonjs'
6-
import ts from 'rollup-plugin-ts'
6+
import typescript from 'rollup-plugin-ts'
77

8-
// https://rollupjs.org/guide/en/#using-untranspiled-config-files
98
import pkg from './package.cjs'
109

11-
/**
12-
* A mini-plugin that resolves `node:` and `nodejs:` imports to their unprefixed equivalent.
13-
* @type { import('rollup').PluginImpl }
14-
*/
15-
function nodeColon() {
16-
return {
17-
name: 'node-colon',
18-
resolveId(id) {
19-
for (const scheme of [ 'node:', 'nodejs:' ]) {
20-
if (id.startsWith(scheme)) {
21-
return { id: id.slice(scheme.length), external: true}
22-
}
23-
}
24-
}
25-
}
10+
/** @type {import('rollup').OutputOptions} */
11+
const commonOutput = {
12+
sourcemap: false,
13+
generatedCode: 'es2015'
2614
}
2715

2816
/**
29-
* A mini-plugin that generates a package.json file next to the bundle.
30-
* @type { import('rollup').PluginImpl<'module' | 'commonjs'> }
17+
* @param {'commonjs'|'module'} format
18+
* @returns {import('rollup').Plugin[]}
3119
*/
32-
function emitPkg(type) {
33-
return {
20+
const plugins = format => ([
21+
alias({
22+
entries: [{
23+
find: /^node(?:js)?:(.*)$/,
24+
replacement: '$1'
25+
}]
26+
}),
27+
resolve(),
28+
commonjs(),
29+
typescript({
30+
tsconfig: cfg => ({
31+
...cfg,
32+
// No need to emit the declarations twice
33+
declaration: format === 'module',
34+
declarationDir: dirname(pkg.types)
35+
})
36+
}),
37+
{
38+
// An inlined mini-plugin that generates a package.json
39+
// file next to the bundle with just the 'type' field set.
3440
name: 'emit-pkg',
3541
generateBundle() {
3642
this.emitFile({
3743
type: 'asset',
3844
fileName: 'package.json',
39-
source: JSON.stringify({ type }, undefined, 2)
45+
source: JSON.stringify({ type: format })
4046
})
4147
}
4248
}
43-
}
49+
])
4450

45-
/** @type {import('rollup').OutputOptions} */
46-
const commonOutput = {
47-
sourcemap: true,
48-
generatedCode: 'es2015'
49-
}
50-
51-
/** @type {import('rollup').RollupOptions} */
52-
const config = {
53-
input: 'src/index.ts',
54-
output: [
55-
{
51+
/** @type {import('rollup').RollupOptions[]} */
52+
const configs = [
53+
{
54+
input: 'src/index.ts',
55+
output: {
5656
...commonOutput,
5757
format: 'commonjs',
5858
file: pkg.main,
59-
exports: 'auto',
60-
sourcemap: true,
61-
plugins: [
62-
emitPkg('commonjs')
63-
]
59+
exports: 'default', // externals is exported as default
60+
interop: 'default', // How to import external modules?
6461
},
65-
{
62+
plugins: plugins('commonjs')
63+
},
64+
{
65+
input: 'src/index.ts',
66+
output: {
6667
...commonOutput,
6768
format: 'module',
68-
file: pkg.module,
69-
sourcemap: true,
70-
plugins: [
71-
emitPkg('module')
72-
]
69+
file: pkg.module
7370
},
74-
],
75-
plugins: [
76-
nodeColon(),
77-
resolve(),
78-
commonjs(),
79-
ts({
80-
tsconfig: cfg => ({
81-
...cfg,
82-
declarationDir: dirname(pkg.types)
83-
})
84-
})
85-
],
86-
external: builtinModules.concat(Object.keys(pkg.dependencies))
87-
}
71+
plugins: plugins('module'),
72+
// Leave find-up out of the esm build
73+
external: 'find-up'
74+
}
75+
]
8876

89-
export default config
77+
export default configs

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface ExternalsOptions {
3434
* Useful when you don't want to bundle node/npm modules with your own code
3535
* but rather import or require them at runtime.
3636
*/
37-
export default function externals(options: ExternalsOptions = {}): Plugin {
37+
export default function externals(options: ExternalsOptions = {}): Plugin {
3838

3939
// Consolidate options
4040
const config: Required<ExternalsOptions> = {

0 commit comments

Comments
 (0)