How to exclude plugin dependencies during requirejs optimization

608 views Asked by At

I have been looking for a while but i haven't found a solution to my problem. I am using require.js and using a plugin to load data dynamically for some of the modules

define(['Plugin!data', 'dependency'], function(data, dep) {
    ...
}

The problem arises when I try to optimize all modules into a single build using r.js

requirejs.optimize({
   baseURL: '/js',
   name: 'main',
   ...
})

The optimizer tries to load the data through the plugin. Is there a way to avoid that ?

ie: how to tell the optimizer to not go through anything in the format Plugin!...

1

There are 1 answers

1
Louis On

You can use exclude in your build configuration to exclude such modules. For instance:

({
    baseUrl: "js",
    dir: "build",
    removeCombined: true,
    optimize: "none",
    modules: [
        {
            name: "built",
            create: true,
            include: [
                "modA",
            ],
            exclude: [
                "text!./x.json",
            ],
        },
    ],
})

The problem with this though is that there is no pattern matching in exclude and you'll end up having to list each module individually, which can be a bit problematic on large code bases.

Another method is to use the onBuildWrite to remove all modules starting with text!:

({
    baseUrl: "js",
    dir: "build",
    removeCombined: true,
    optimize: "none",
    modules: [
        {
            name: "built",
            create: true,
            include: [
                "modA",
            ],
        },
    ],
    // Use onBuildWrite to exclude modules loaded through plugin.
    onBuildWrite: function (moduleName, path, contents) {
        if (moduleName.lastIndexOf("text!", 0) === 0) {
            return "";
        }
        return contents;
    },
})