简体   繁体   English

在 Rollup.js 中使用 jQuery 数据表

[英]Using jQuery DataTables with Rollup.js

Ok I'm using the tool rollup for the first time and I love how small it is making the code.好的,我第一次使用工具汇总,我喜欢它制作代码的程度。 Tree shaking is great.摇树很棒。 However, I'm having some trouble getting it to include everything correctly.但是,我在让它正确包含所有内容时遇到了一些麻烦。 I tried having a single entry point, exp.js, where I export things from various files like this:我尝试使用单个入口点 exp.js,在其中我从各种文件中导出内容,如下所示:

export {
    dashboardCharts
} from './dashboard.js';

my rollup.config.js looks like我的 rollup.config.js 看起来像

export default {
    // tell rollup our main entry point
    input: [
        'assets/js/exp.js',
    ],

    output: {
      name: 'helloworld',
      file: 'build/js/main.js',
        format: 'iife'
        // format: 'umd'
    },
    plugins: [
        resolve({
            // pass custom options to the resolve plugin
            customResolveOptions: {
              moduleDirectory: 'node_modules'
            }
        }),
        multiEntry()
        // terser(),
    ],
};

The file dashboard.js includes the datatables library, so datatables gets included in the bundle main.js.文件dashboard.js 包含数据表库,因此数据表包含在包main.js 中。 However, datatables tests whether it should take the commonjs path or not by testing但是,datatables 通过测试来测试它是否应该采用 commonjs 路径

else if ( typeof exports === 'object' ) {
    // CommonJS
    module.exports = function (root, $) {

and I'm trying to execute this in the browser, so I don't want the commonjs path.我试图在浏览器中执行它,所以我不想要 commonjs 路径。 Rollup's top level scope is declared like Rollup 的顶级范围声明如下

var helloworld = (function (exports) {

so exports ends up being an empty object, the browser tries to execute the commonjs path and we get a "module is not defined" error.所以导出最终是一个空对象,浏览器尝试执行 commonjs 路径,我们得到一个“模块未定义”错误。

I feel like I'm really close, but I'm missing a simple solution here.我觉得我真的很接近,但我在这里缺少一个简单的解决方案。 I also tried doing a umd format instead of iife, but it didn't help.我也尝试使用 umd 格式而不是 iife,但它没有帮助。 Is there a different version of datatables I should be using?我应该使用不同版本的数据表吗?

I used rollup with svelte and I had some jquery legacy to import;我使用了带有 svelte 的汇总,并且我有一些 jquery 遗留要导入; here is how I did it:这是我如何做到的:

import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import autoPreprocess from 'svelte-preprocess';
import json from '@rollup/plugin-json';

const production = !process.env.ROLLUP_WATCH;

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js',
    },
    plugins: [
        json(),
        svelte({
            // Enables run-time checks when not in production.
            dev: !production,

            // Extracts any component CSS out into a separate file — better for performance.
            css: css => css.write('public/build/bundle.css'),

            // Emit CSS as "files" for other plugins to process
            emitCss: true,

            preprocess: autoPreprocess()
        }),

        resolve({
            browser: true,
            dedupe: importee => importee === 'svelte' || importee.startsWith('svelte/')
        }),
        commonjs(),

        postcss({
            extract: true,
            minimize: true,
            use: [
                ['sass', {
                includePaths: [
                    './src/theme',
                    './node_modules'
                ]
                }]
            ]
        }),

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

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

        // Minify for production.
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

function serve() {
    let started = false;

    return {
        writeBundle() {
            if (!started) {
                started = true;

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

Maybe is too much what I posted (I wanted to show you the context from a working config), but you can extract from it the needed part.也许我发布的内容太多了(我想向您展示工作配置中的上下文),但您可以从中提取所需的部分。 I think you need the commonjs plugin.我认为您需要commonjs插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM