Background script without type:"module" #848
Unanswered
AhmedKorim
asked this question in
Q&A
Replies: 1 comment
-
As a workaround, I ran rollup after vite finishes compilation to bundle the background script to commonjs and change remove the extension manifest //scripts/build.js
const { execSync } = require('child_process');
const { readFileSync, writeFileSync } = require('fs');
const { join } = require('path');
async function build() {
// build with vite
execSync('vite build', { stdio: 'inherit' });
// Compile the background script to commonjs
execSync('yarn rollup:build', { stdio: 'inherit' });
// Change the manifest file to be classic script
const manifestFilePath = join(process.cwd(), 'dist', 'manifest.json');
const manifestFile = readFileSync(manifestFilePath).toString();
const manifestdef = JSON.parse(manifestFile);
delete manifestdef.background.type;
writeFileSync(manifestFilePath, JSON.stringify(manifestdef,null,2));
console.log('Updated the background script');
}
build().catch(console.error); //rollup.config.mjs
// @ts-check
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
/**
* Creates an output options object for Rollup.js.
* @param {import('rollup').OutputOptions} options
* @returns {import('rollup').OutputOptions}
*/
function createOutputOptions(options) {
return {
name: '',
exports: 'named',
sourcemap: true,
...options,
};
}
/**
* @type {import('rollup').RollupOptions}
*/
const options = {
input: './dist/service-worker-loader.js',
output: [
createOutputOptions({
file: './dist/service-worker-loader.js',
format: 'commonjs',
}),
],
plugins: [
resolve(),
commonjs(),
],
};
export default options; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to build a Chrome extension that uses wasm, the wasm file uses
importScript
which isn't supported with ESM,is there a way to build the background script without ESM?
Trying to change the output manifest (removed type: "module" from the background script configuration) I got the error
Cannot use import statement outside a module
Beta Was this translation helpful? Give feedback.
All reactions