lowspace / chatgpt-token-counter

Token Counter Chrome Extension for ChatGPT: A handy tool designed to monitor and display the token usage of ChatGPT sessions directly in your browser, helping you manage and optimize interactions efficiently.
MIT License
0 stars 0 forks source link

How to include third-party libraries in a Chrome Extension? #5

Closed lowspace closed 3 months ago

lowspace commented 3 months ago

Importing modules from remote servers is not recommended due to security concerns, thus the best way to Paris is to import locally.

lowspace commented 3 months ago

Packaging third-party libraries in a Chrome extension using Webpack involves a few key steps to ensure that the extension is built efficiently and securely. Webpack is a powerful tool that bundles JavaScript applications, and it is very useful for managing and bundling dependencies in Chrome extensions. Here’s a step-by-step guide to help you get started:

Step 1: Set Up Your Project

  1. Create a directory for your Chrome extension.
  2. Initialize a new npm project:
    npm init -y
  3. Install Webpack and its CLI:
    npm install --save-dev webpack webpack-cli

Step 2: Structure Your Project

Organize your project folder. A typical structure might look like this:

my-extension/
│
├── src/
│   ├── background.js
│   ├── content.js
│   └── popup.js
│
├── node_modules/
│
├── public/
│   ├── popup.html
│   └── manifest.json
│
├── dist/
│
├── webpack.config.js
│
└── package.json

Step 3: Configure Webpack

Create a webpack.config.js file in your project root to configure Webpack. Here’s a basic configuration for a Chrome extension:

const path = require('path');

module.exports = {
  mode: 'development',
  entry: {
    background: './src/background.js',
    content: './src/content.js',
    popup: './src/popup.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  }
};

This configuration:

Step 4: Add Third-Party Libraries

  1. Install the libraries you need via npm. For example:
    npm install axios
  2. Import the libraries in your JavaScript files as needed:
    import axios from 'axios';

Step 5: Build the Extension

Run Webpack to build your extension:

npx webpack --config webpack.config.js

This command will generate bundled files in the dist directory.

Step 6: Load Your Extension into Chrome

  1. Open Chrome and navigate to chrome://extensions/.
  2. Enable Developer mode.
  3. Click Load unpacked and select your dist folder (make sure your manifest.json points to the correct bundled script files).

Step 7: Test and Debug

Test your extension thoroughly in Chrome. Debug any issues that arise and adjust your webpack.config.js as needed to accommodate any specific requirements of your extension.

Conclusion

Using Webpack for Chrome extension development allows you to utilize modern JavaScript and efficiently manage dependencies. It simplifies development and can help streamline the process of updating and maintaining your extension.

lowspace commented 3 months ago

Here are some insights:

  1. If both B and C depend on A, then during the packaging process, A can serve as a shared dependency for both B and C.
  2. Some libraries offer an offline (packaged) version. Be sure to check their official websites for this option.