Saturday, 29 October 2022

I cant get rollup to compile properly when using dynamic imports and crypto-js in my svelte web app

I recently tried to codesplit my svelte web app for each page, but I haven't been able to get it to work while using the crypto-js package. If i remove the package everything works. The js compiles with the line import "crypto", and that causes the browser to error and not to work.

rollup.config.js

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import json from '@rollup/plugin-json';
import { config } from "dotenv"
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;
    
    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}
let envVar = {}
Object.entries(config().parsed).map(([prop, value]) => {
    if (prop.startsWith("APP")) envVar[prop] = value
});

export default {
    input: 'src/main.js',
    output: {
        sourcemap: !production,
        format: 'esm',
        name: 'app',
        dir: 'public/build',
    },
    plugins: [
        json(),
        replace({
            __myapp: JSON.stringify({
                env: envVar
            }),
        }),
        svelte({
            // enable run-time checks when not in production
            dev: !production,
            // we'll extract any component CSS out into
            // a separate file - better for performance
            css: css => {
                css.write('bundle.css');
            }
        }),
        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        commonjs({
            transformMixedEsModules:true,
            sourceMap: !production,
        }),
        resolve({
            browser: true,
            dedupe: ['svelte'],
            preferBuiltins: false
        }),

        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),

        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('public'),

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

Browser Error Uncaught TypeError: Failed to resolve module specifier "crypto". Relative references must start with either "/", "./", or "../".

main.js

(function(l, r) { if (l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = '//' + (window.location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1'; r.id = 'livereloadscript'; l.getElementsByTagName('head')[0].appendChild(r) })(window.document);
export { ao as default } from './main-d2838af7.js';
import 'crypto';
//# sourceMappingURL=main.js.map


from I cant get rollup to compile properly when using dynamic imports and crypto-js in my svelte web app

No comments:

Post a Comment