corupta / chrome-extension-hot-reload

Hot reloads a chrome extension, without a full page reload, (works for both options, and popup and tabs)
5 stars 0 forks source link

background.js is not a module, can not import packge #1

Open xush6528 opened 3 years ago

xush6528 commented 3 years ago

I saw this error using yoru method, "Uncaught SyntaxError: Cannot use import statement outside a module".

I wonder how do you make background.js being able to load a pacakge module.

corupta commented 3 years ago

oh hi,

I saw your issue very late, sorry.

In my case I was using webpack with multiple entries to build background.js and other scripts.

I'm putting an example webpack config I used in my extension project, so you can use it as a reference.

For a vanilla way to use (without babel) I think, it would be useful for me to build it accordingly as well.

// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanObsoleteChunks = require('webpack-clean-obsolete-chunks');
const Dotenv = require('dotenv-webpack');

const HWPConfig = () => ({
  inject: true,
  minify: {
    removeComments: true,
    collapseWhitespace: true,
    removeRedundantAttributes: true,
    useShortDoctype: true,
    removeEmptyAttributes: true,
    removeStyleLinkTypeAttributes: true,
    keepClosingSlash: true,
    minifyJS: true,
    minifyCSS: true,
    minifyURLs: true
  }
});

const HWPOptionsPage = new HtmlWebpackPlugin({
  ...HWPConfig(),
  template: path.join('.', 'public', 'options.html'),
  filename: path.join('.', 'options.html'),
  chunks: ['options']
});

const HWPPopupPage = new HtmlWebpackPlugin({
  ...HWPConfig(),
  template: path.join('.', 'public', 'popup.html'),
  filename: path.join('.', 'popup.html'),
  chunks: ['popup']
});

const webpackConf = {
  entry: {
    options: './src/options/index.tsx',
    popup: './src/popup/index.tsx',
    background: './src/background/index.tsx'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: ({ chunk: { name } }) =>
      path.join('static', 'js', `[name]${name === 'background' ? '' : '.[chunkhash:8]'}.js`)
  },
  resolve: {
    extensions: [
      '.js',
      '.jsx',
      '.ts',
      '.tsx'
    ]
  },
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {

      }
    ]
  },
  plugins: [
    HWPOptionsPage,
    HWPPopupPage,
    new CleanObsoleteChunks(),
    new Dotenv()
  ],
  devtool: false
};

// eslint-disable-next-line no-process-env
if (process.env.NODE_ENV === 'development') {
  webpackConf.resolve.alias = {
    inferno: 'inferno/dist/index.dev.esm.js'
  };
}

module.exports = webpackConf;