Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in a bundle #528

Open
lijingmu opened this issue May 23, 2023 · 3 comments
Open

Work in a bundle #528

lijingmu opened this issue May 23, 2023 · 3 comments

Comments

@lijingmu
Copy link

I use webpack to create a bundle, vm2 is one dependency. webpack can work well with following config:

ignoreWarnings: [
        {
                message: /Can't resolve 'coffee-script'/,
        },
]
module: {
        parser: {
                javascript: {
                        commonjsMagicComments: true
                }
        }
}

when I run the project I encountered:

Error: ENOENT: no such file or directory, open 'xx/dist/bridge.js'

I think it is because below:

vm2/lib/vm.js
    const bridgeScript = compileScript(`${__dirname}/bridge.js`,
        `(function(global) {"use strict"; const exports = {};${fs.readFileSync(`${__dirname}/bridge.js`, 'utf8')}\nreturn exports;})`);
    const setupSandboxScript = compileScript(`${__dirname}/setup-sandbox.js`,
        `(function(global, host, bridge, data, context) { ${fs.readFileSync(`${__dirname}/setup-sandbox.js`, 'utf8')}\n})`);

vm2/lib/nodevm.js
        if (!cacheSandboxScript) {
            cacheSandboxScript = compileScript(`${__dirname}/setup-node-sandbox.js`,
                `(function (host, data) { ${fs.readFileSync(`${__dirname}/setup-node-sandbox.js`, 'utf8')}\n})`);
        }

vm2/lib/nodevm.js
     if (!cacheEventsScript) {
    const eventsSource = fs.readFileSync(`${__dirname}/events.js`, 'utf8');
    cacheEventsScript = new VMScript(`(function (fromhost) { const module = {}; module.exports={};{ ${eventsSource}
            } return module.exports;})`, {filename: 'events.js'});
     }

I have to copy these files to dist. Any suggestions or workaround?
Thanks

@lijingmu
Copy link
Author

Finally I use scripts to bundle them, it works well now.

1. add post build in NPM script
"build": "webpack --config ./webpack.prod.config.js && node prepare-package.js postBuild",

2. copy files from node_modules to output directory after build in prepare-package.js
const postBuild = async () => {
  await cpvm2("bridge.js");
  await cpvm2("events.js");
  await cpvm2("setup-sandbox.js");
  await cpvm2("setup-node-sandbox.js");
};

3. add them in package.json files property to bundle them

@karlhorky
Copy link

Seems like this is causing all Puppeteer bundled scripts to fail as well (eg. bundling with esbuild):

@patriksimek any idea what could be causing this?

@karlhorky
Copy link

karlhorky commented Jun 8, 2023

Oh it seems like vm2 has been incompatible with bundling for years, hope the approach of using fs.readFileSync() and similar to read files inside of node_modules on runtime can be reconsidered 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants