javascriptrequirejsrequirejs-optimizer

RequireJS optimization with define functions


I want to build my project to one file. I have three files.

test2.js:

define(
  'test2',
  ['test3.js'],
  function (t2) {
      // test 2
  }
);

test3.js:

define(
  'test3',
  [],
  function () {
      // test 3
  }
);

and main file test1.js:

require(
  ['test2.js'],
  function (t2) {
      // test 1
  }
);

Build config:

({
  baseUrl: '.',
  name: 'test1',
  out: 'built.js',
  optimize: "none"
})

Result:

define(
  'test2',
  ['test3.js'],
  function (t2) {
      // test 2
  }
);

define("test2.js", function(){});

require(
  ['test2.js'],
  function (t2) {
    // test 1
  }
);

define("test1", function(){});

So I dont's see the test3.js code there. Does r.js work only with require dependencies? Can I make it build with define dependencies?


Solution

  • Remove the .js suffix for dependencies, and don't use named modules.

    File: test2.js:

    define(['test3'],
      function (t2) {
          // test 2
      }
    );
    

    File: test3.js:

    define([],
      function () {
          // test 3
      }
    );
    

    File: test1.js:

    require(
      ['test2'],
      function (t2) {
          // test 1
      }
    );