-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parcel-optimizer-electron-require): Add parcel optimizer polyfills the require function in an electron context feat(template): Install Parcel Optimizer Electron Require Co-authored-by: Pablo Klaschka <[email protected]>
- Loading branch information
Showing
11 changed files
with
281 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
root: true, | ||
parserOptions: { | ||
tsconfigRootDir: __dirname, | ||
project: [path.join(__dirname, 'tsconfig.json')] | ||
}, | ||
extends: [ | ||
path.join(__dirname, '..', '..', 'base-configs', 'eslint.typescript.js') | ||
], | ||
overrides: [ | ||
{ | ||
files: ['**/tests/lib/**/*', '**/tests/samples/**/*'], | ||
rules: { | ||
'jest/no-export': 'off', | ||
'import/no-extraneous-dependencies': 'off' | ||
} | ||
}, | ||
{ | ||
files: ['**/src/**/*.ts'], | ||
rules: { | ||
'import/no-default-export': 'off' | ||
} | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 WüSpace e. V. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Parcel Optimizer Electron Require | ||
|
||
npm: [`@wuespace/parcel-optimizer-electron-require`](https://www.npmjs.com/package/@wuespace/parcel-optimizer-electron-require) | ||
|
||
[![Maintainability](https://api.codeclimate.com/v1/badges/5fb6ccd02dd3152ef03f/maintainability)](https://codeclimate.com/github/wuespace/telestion-client/maintainability) | ||
[![Test Coverage](https://api.codeclimate.com/v1/badges/5fb6ccd02dd3152ef03f/test_coverage)](https://codeclimate.com/github/wuespace/telestion-client/test_coverage) | ||
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/wuespace/telestion-client/Test%20and%20Coverage?label=tests)](https://github.com/wuespace/telestion-client/actions?query=workflow%3A%22Test+and+Coverage%22) | ||
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/wuespace/telestion-client/CI)](https://github.com/wuespace/telestion-client/actions?query=workflow%3ACI) | ||
[![GitHub](https://img.shields.io/github/license/wuespace/telestion-client)](LICENSE) | ||
[![Node current](https://img.shields.io/badge/node-%3E%3D14-brightgreen)](package.json) | ||
[![NPM current](https://img.shields.io/badge/npm-%3E%3D7-blue)](package.json) | ||
[![Twitter Follow](https://img.shields.io/twitter/follow/wuespace?style=social)](https://twitter.com/wuespace) | ||
|
||
Parcel Optimizer Plugin that polyfills the require function in an electron context. (especially in preload) | ||
|
||
## Installation | ||
|
||
If you're using | ||
the [`@wuespace/telestion-client-template`](https://www.npmjs.com/package/@wuespace/telestion-client-template) the | ||
plugin is already installed and integrated in Parcel. | ||
|
||
If that's not the case, install the plugin as development dependency: | ||
|
||
```shell | ||
npm install --save-dev @wuespace/parcel-optimizer-electron-require | ||
``` | ||
|
||
Then add it to your `.parcelrc` configuration: | ||
|
||
```json5 | ||
{ | ||
optimizers: { | ||
// add optimizer to fix require in electron preload | ||
'*.js': ['@wuespace/parcel-optimizer-electron-require'] | ||
} | ||
} | ||
``` | ||
|
||
That's it! | ||
|
||
## Implementation Details | ||
|
||
In Electron preload contexts a global `require` function exists, but no `module` object. | ||
In Parcel's watch mode, the following code gets used to handle CommonJS imports: | ||
|
||
https://github.com/parcel-bundler/parcel/blob/10a56a7832c400025f23b6df6fcce3099a4d6302/packages/packagers/js/src/dev-prelude.js#L30-L33 | ||
|
||
This optimizer adds a prefix to every packaged JavaScript file that adds the `module` object with the `require` function | ||
to circumvent Electron not providing a `module` object: | ||
|
||
```js | ||
// Fix Electron renderer "require()" without a module issue | ||
// see: https://github.com/parcel-bundler/parcel/issues/2492 | ||
if (typeof require !== 'undefined' && typeof module === 'undefined') { | ||
var module = { require: require }; | ||
} | ||
``` | ||
|
||
## Contributing | ||
|
||
If you want to contribute to this package, please take a look at the [Telestion Client monorepo](https://github.com/wuespace/telestion-client/) that manages this package, among other Telestion Client packages. | ||
|
||
## Contributors | ||
|
||
Thank you to all contributors of this repository: | ||
|
||
[![Contributors](https://contrib.rocks/image?repo=wuespace/telestion-client)](https://github.com/wuespace/telestion-client/graphs/contributors) | ||
|
||
Made with [contributors-img](https://contrib.rocks). | ||
|
||
## About | ||
|
||
This is part of [Telestion](https://telestion.wuespace.de/), a project by [WüSpace e.V.](https://www.wuespace.de/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
"name": "@wuespace/parcel-optimizer-electron-require", | ||
"description": "Parcel Optimizer Plugin that polyfills the require function in an electron context", | ||
"license": "MIT", | ||
"version": "0.18.1", | ||
"homepage": "https://telestion.wuespace.de/", | ||
"source": "src/optimizer.ts", | ||
"main": "dist/optimizer.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"parcel": "2.x" | ||
}, | ||
"scripts": { | ||
"watch": "parcel watch", | ||
"build": "parcel build", | ||
"lint": "eslint", | ||
"check": "tsc --noEmit", | ||
"style": "pnpm -w style", | ||
"clean": "rimraf dist" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/wuespace/telestion-client.git", | ||
"directory": "packages/parcel-optimizer-electron-require" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/wuespace/telestion-client/issues" | ||
}, | ||
"author": { | ||
"name": "wuespace", | ||
"email": "[email protected]", | ||
"url": "https://www.wuespace.de/" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Liam Franssen", | ||
"email": "[email protected]", | ||
"url": "https://github.com/leecemin" | ||
}, | ||
{ | ||
"name": "Pablo Klaschka", | ||
"email": "[email protected]", | ||
"url": "https://github.com/pklaschka" | ||
}, | ||
{ | ||
"name": "Jan Tischhöfer", | ||
"email": "[email protected]", | ||
"url": "https://github.com/jantischhoefer" | ||
}, | ||
{ | ||
"name": "Ludwig Richter", | ||
"email": "[email protected]", | ||
"url": "https://github.com/fussel178" | ||
} | ||
], | ||
"dependencies": { | ||
"@parcel/plugin": "^2.5.0" | ||
}, | ||
"devDependencies": { | ||
"@parcel/types": "2.8.2", | ||
"@parcel/core": "2.8.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/parcel-optimizer-electron-require/src/optimizer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Optimizer } from '@parcel/plugin'; | ||
|
||
const fix = `// Fix Electron renderer "require()" without a module issue | ||
// see: https://github.com/parcel-bundler/parcel/issues/2492 | ||
if (typeof require !== "undefined" && typeof module === "undefined") { | ||
var module = { require: require }; | ||
} | ||
`; | ||
|
||
export default new Optimizer({ | ||
optimize({ contents, map }) { | ||
map?.offsetLines(1, fix.split('\n').length - 1); | ||
return { | ||
contents: fix + (contents as string), | ||
map | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"isolatedModules": true, | ||
"target": "ES5", | ||
"module": "esnext", | ||
"lib": ["es6", "dom", "es2016", "es2017"], | ||
"declaration": true, | ||
"sourceMap": true, | ||
/* Strict Type-Checking Options */ | ||
"strict": true, | ||
/* Module Resolution Options */ | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
/* Advanced Options */ | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
/* reset paths because typescript threat workspace dependencies as normal dependencies */ | ||
"paths": {} | ||
}, | ||
"include": ["src", "tests"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters