Open xush6528 opened 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;
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.