How to import Firebase only on client in Sapper?

后端 未结 3 925
猫巷女王i
猫巷女王i 2021-02-05 15:09

I\'m importing Firebase into my Sapper application, I do not want the imports to be evaluated on the server. How do I make sure imports are only on the client-side?

I

3条回答
  •  遥遥无期
    2021-02-05 15:49

    I was able to import firebase using ES6. If you are using rollup you need to consfigure namedExports in commonjs plugin:

    //--- rollup.config.js ---
    ...
    commonjs({
            namedExports: {
              // left-hand side can be an absolute path, a path
              // relative to the current directory, or the name
              // of a module in node_modules
              'node_modules/idb/build/idb.js': ['openDb'],
              'node_modules/firebase/dist/index.cjs.js': ['initializeApp', 'firestore'],
            },
          }),
    

    The you can use it like this:

    //--- db.js ---
    import * as firebase from 'firebase';
    import 'firebase/database';
    import { firebaseConfig } from '../config'; //<-- Firebase initialization config json
    
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    export { firebase };
    // Initialize db
    export const db = firebase.firestore();
    

    and maybe use it in a service like such:

    // --- userService.js ----
    import { db } from './common';
    
    const usersCol = db.collection('users');
    export default {
      async login(username, password) {
        const userDoc = await usersCol.doc(username).get();
        const user = userDoc.data();
        if (user && user.password === password) {
          return user;
        }
        return null;
      },
    };
    
    

    EDITED Full rollup config

    /* eslint-disable global-require */
    import resolve from 'rollup-plugin-node-resolve';
    import replace from 'rollup-plugin-replace';
    import commonjs from 'rollup-plugin-commonjs';
    import svelte from 'rollup-plugin-svelte';
    import babel from 'rollup-plugin-babel';
    import { terser } from 'rollup-plugin-terser';
    import config from 'sapper/config/rollup';
    import { sass } from 'svelte-preprocess-sass';
    import pkg from './package.json';
    
    const mode = process.env.NODE_ENV;
    const dev = mode === 'development';
    const legacy = !!process.env.SAPPER_LEGACY_BUILD;
    
    // eslint-disable-next-line no-shadow
    const onwarn = (warning, onwarn) =>
      (warning.code === 'CIRCULAR_DEPENDENCY' && warning.message.includes('/@sapper/')) || onwarn(warning);
    
    export default {
      client: {
        input: config.client.input(),
        output: config.client.output(),
        plugins: [
          replace({
            'process.browser': true,
            'process.env.NODE_ENV': JSON.stringify(mode),
          }),
          svelte({
            dev,
            hydratable: true,
            emitCss: true,
            preprocess: {
              style: sass(),
            },
          }),
          resolve({
            browser: true,
          }),
          commonjs({
            namedExports: {
              // left-hand side can be an absolute path, a path
              // relative to the current directory, or the name
              // of a module in node_modules
              'node_modules/idb/build/idb.js': ['openDb'],
              'node_modules/firebase/dist/index.cjs.js': ['initializeApp', 'firestore'],
            },
          }),
    
          legacy &&
            babel({
              extensions: ['.js', '.mjs', '.html', '.svelte'],
              runtimeHelpers: true,
              exclude: ['node_modules/@babel/**'],
              presets: [
                [
                  '@babel/preset-env',
                  {
                    targets: '> 0.25%, not dead',
                  },
                ],
              ],
              plugins: [
                '@babel/plugin-syntax-dynamic-import',
                [
                  '@babel/plugin-transform-runtime',
                  {
                    useESModules: true,
                  },
                ],
              ],
            }),
    
          !dev &&
            terser({
              module: true,
            }),
        ],
    
        onwarn,
      },
    
      server: {
        input: config.server.input(),
        output: config.server.output(),
        plugins: [
          replace({
            'process.browser': false,
            'process.env.NODE_ENV': JSON.stringify(mode),
          }),
          svelte({
            generate: 'ssr',
            dev,
          }),
          resolve(),
          commonjs(),
        ],
        external: Object.keys(pkg.dependencies).concat(require('module').builtinModules || Object.keys(process.binding('natives'))),
    
        onwarn,
      },
    
      serviceworker: {
        input: config.serviceworker.input(),
        output: config.serviceworker.output(),
        plugins: [
          resolve(),
          replace({
            'process.browser': true,
            'process.env.NODE_ENV': JSON.stringify(mode),
          }),
          commonjs(),
          !dev && terser(),
        ],
    
        onwarn,
      },
    };
    
    

提交回复
热议问题