Skip to content

Commit

Permalink
meta: Multiple changes
Browse files Browse the repository at this point in the history
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
fussel178 and pklaschka committed Dec 27, 2022
1 parent 790cc30 commit 7418c7a
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 19 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
"typescript": "4.9.4"
},
"dependencies": {
"@wuespace/parcel-optimizer-electron-require": "workspace:^",
"@wuespace/parcel-reporter-tc-cli": "workspace:^",
"@wuespace/parcel-resolver-react": "workspace:^",
"@wuespace/telestion-client-cli": "workspace:^",
"@wuespace/telestion-client-common": "workspace:^",
"@wuespace/telestion-client-core": "workspace:^",
Expand Down
27 changes: 27 additions & 0 deletions packages/parcel-optimizer-electron-require/.eslintrc.js
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'
}
}
]
};
21 changes: 21 additions & 0 deletions packages/parcel-optimizer-electron-require/LICENSE
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.
73 changes: 73 additions & 0 deletions packages/parcel-optimizer-electron-require/README.md
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/).
68 changes: 68 additions & 0 deletions packages/parcel-optimizer-electron-require/package.json
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 packages/parcel-optimizer-electron-require/src/optimizer.ts
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
};
}
});
25 changes: 25 additions & 0 deletions packages/parcel-optimizer-electron-require/tsconfig.json
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"]
}
1 change: 1 addition & 0 deletions packages/telestion-client-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@types/node": "17.0.45",
"@types/react": "17.0.52",
"@types/react-dom": "17.0.18",
"@wuespace/parcel-optimizer-electron-require": "workspace:^",
"@wuespace/parcel-reporter-tc-cli": "workspace:^",
"@wuespace/parcel-resolver-react": "workspace:^",
"@wuespace/telestion-client-cli": "workspace:^",
Expand Down
4 changes: 3 additions & 1 deletion packages/telestion-client-template/template/.parcelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
},
"optimizers": {
// disable terser because it resolves badly
"*.{js,mjs,cjs}": []
"*.{mjs,cjs}": [],
// add optimizer to fix require in electron preload
"*.js": ["@wuespace/parcel-optimizer-electron-require"]
},
"reporters": ["...", "@wuespace/parcel-reporter-tc-cli"]
}
30 changes: 20 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@
"esModuleInterop": true,
/* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"paths": {
"@wuespace/vertx-event-bus": ["vertx-event-bus/src"],
"@wuespace/telestion-client-types": ["telestion-client-types/src"],
"@wuespace/parcel-optimizer-electron-require": [
"parcel-optimizer-electron-require/src"
],
"@wuespace/parcel-reporter-tc-cli": ["parcel-reporter-tc-cli/src"],
"@wuespace/parcel-resolver-react": ["parcel-resolver-react/src"],
"@wuespace/telestion-client-cli": ["telestion-client-cli/src"],
"@wuespace/telestion-client-common": ["telestion-client-common/src"],
"@wuespace/telestion-client-core": ["telestion-client-core/src"],
"@wuespace/telestion-client-prop-types": [
"telestion-client-prop-types/src"
],
"@wuespace/telestion-client-core": ["telestion-client-core/src"],
"@wuespace/telestion-client-common": ["telestion-client-common/src"]
"@wuespace/telestion-client-types": ["telestion-client-types/src"],
"@wuespace/vertx-event-bus": ["vertx-event-bus/src"],
"@wuespace/vertx-mock-server": ["vertx-mock-server/src"]
},

//
Expand All @@ -55,10 +62,16 @@
},
"exclude": ["node_modules"],
"references": [
{ "path": "./packages/vertx-event-bus" },
{ "path": "./packages/telestion-client-types" },
{ "path": "./packages/telestion-client-prop-types" },
{ "path": "./packages/parcel-optimizer-electron-require" },
{ "path": "./packages/parcel-reporter-tc-cli" },
{ "path": "./packages/parcel-resolver-react" },
{ "path": "./packages/telestion-client-cli" },
{ "path": "./packages/telestion-client-common" },
{ "path": "./packages/telestion-client-core" },
{ "path": "./packages/telestion-client-common" }
{ "path": "./packages/telestion-client-prop-types" },
{ "path": "./packages/telestion-client-template" },
{ "path": "./packages/telestion-client-types" },
{ "path": "./packages/vertx-event-bus" },
{ "path": "./packages/vertx-mock-server" }
]
}

0 comments on commit 7418c7a

Please sign in to comment.