Skip to content

Loading…

Load shim from package name if no shim for moduleName. #331

Merged
merged 3 commits into from

2 participants

@maxpeterson

This is a possible fix for jrburke/r.js#310

The idea is to try and get the the shim for a module based on the package name in the case that the module name includes packageConfig.main.

In the case that you have a package

  {
      "name": "backbone",
      "location": "jam/backbone",
      "main": "backbone.js"
  }

then it would try and get shim['backbone'] if there was no shim['backbone/backbone']

@jrburke jrburke added a commit that referenced this pull request
@jrburke merge of pull #331 60eb8aa
@jrburke jrburke added a commit that referenced this pull request
@jrburke Relates to #331, wire up test. 489b343
@jrburke jrburke merged commit a609335 into jrburke:master
@jrburke
Owner

Thank you, merged. You can try latest master snapshot to confirm the fix:
https://raw.github.com/jrburke/r.js/master/dist/r.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 11, 2012
  1. @maxpeterson
Commits on Dec 13, 2012
  1. @maxpeterson

    Use nonPackageName.

    maxpeterson committed
  2. @maxpeterson
View
2 build/jslib/build.js
@@ -1506,7 +1506,7 @@ define(function (require) {
//after the module is processed.
//If we have a name, but no defined module, then add in the placeholder.
if (moduleName && falseProp(layer.modulesWithNames, moduleName) && !config.skipModuleInsertion) {
- shim = config.shim && getOwn(config.shim, moduleName);
+ shim = config.shim && (getOwn(config.shim, moduleName) || (packageConfig && getOwn(config.shim, nonPackageName)));
if (shim) {
fileContents += '\n' + namespaceWithDot + 'define("' + moduleName + '", ' +
(shim.deps && shim.deps.length ?
View
8 build/tests/lib/configPackageShim/bar/bar.js
@@ -0,0 +1,8 @@
+define('bar', ['foo'], function (foo) {
+ return {
+ bar: function () {
+ console.log('barrrr');
+ foo.foo();
+ }
+ }
+});
View
30 build/tests/lib/configPackageShim/build.js
@@ -0,0 +1,30 @@
+({
+ appDir: '.',
+ baseUrl: '.',
+ dir: 'built',
+ optimize: 'none',
+ modules: [{
+ name: 'main'
+ }],
+ packages: [
+ {
+ 'name': 'foo',
+ 'location': 'foo',
+ 'main': 'foo.js'
+ },
+ {
+ 'name': 'bar',
+ 'location': 'bar',
+ 'main': 'bar.js'
+ }
+ ],
+ shim: {
+ 'foo': {
+ 'exports': 'Foo'
+ },
+ 'bar': {
+ 'deps': ['foo']
+ }
+
+ }
+})
View
58 build/tests/lib/configPackageShim/expected.js
@@ -0,0 +1,58 @@
+
+var Foo = {
+ foo: function () {
+ console.log('foooo');
+ }
+}
+;
+define('foo', ['foo/foo'], function (main) { return main; });
+
+define("foo/foo", (function (global) {
+ return function () {
+ var ret, fn;
+ return ret || global.Foo;
+ };
+}(this)));
+
+define('bar', ['foo'], function (foo) {
+ return {
+ bar: function () {
+ console.log('barrrr');
+ foo.foo();
+ }
+ }
+});
+
+define("bar/bar", ["foo"], function(){});
+
+require.config({
+ packages: [
+ {
+ 'name': 'foo',
+ 'location': 'foo',
+ 'main': 'foo.js'
+ },
+ {
+ 'name': 'bar',
+ 'location': 'bar',
+ 'main': 'bar.js'
+ }
+ ],
+ shim: {
+ 'foo': {
+ 'exports': 'Foo'
+ },
+ 'bar': {
+ 'deps': ['foo']
+ }
+
+ }
+});
+require(
+['bar'],
+function (bar) {
+ console.log('main');
+ bar.bar();
+});
+
+define("main", function(){});
View
5 build/tests/lib/configPackageShim/foo/foo.js
@@ -0,0 +1,5 @@
+var Foo = {
+ foo: function () {
+ console.log('foooo');
+ }
+}
View
9 build/tests/lib/configPackageShim/index.html
@@ -0,0 +1,9 @@
+<html lang="en">
+<head>
+ <title>Package config and shim config do not mix</title>
+
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <script data-main="main" src="require.js"></script>
+</head>
+<body></body>
+</html>
View
29 build/tests/lib/configPackageShim/main.js
@@ -0,0 +1,29 @@
+require.config({
+ packages: [
+ {
+ 'name': 'foo',
+ 'location': 'foo',
+ 'main': 'foo.js'
+ },
+ {
+ 'name': 'bar',
+ 'location': 'bar',
+ 'main': 'bar.js'
+ }
+ ],
+ shim: {
+ 'foo': {
+ 'exports': 'Foo'
+ },
+ 'bar': {
+ 'deps': ['foo']
+ }
+
+ }
+});
+require(
+['bar'],
+function (bar) {
+ console.log('main');
+ bar.bar();
+});
View
1 build/tests/lib/configPackageShim/require.js
View
6 dist/r.js
@@ -1,5 +1,5 @@
/**
- * @license r.js 2.1.2+ Wed, 05 Dec 2012 21:49:23 GMT Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
+ * @license r.js 2.1.2+ Thu, 13 Dec 2012 21:01:00 GMT Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
@@ -21,7 +21,7 @@ var requirejs, require, define;
var fileName, env, fs, vm, path, exec, rhinoContext, dir, nodeRequire,
nodeDefine, exists, reqMain, loadedOptimizedLib, existsForNode,
- version = '2.1.2+ Wed, 05 Dec 2012 21:49:23 GMT',
+ version = '2.1.2+ Thu, 13 Dec 2012 21:01:00 GMT',
jsSuffixRegExp = /\.js$/,
commandOption = '',
useLibLoaded = {},
@@ -23231,7 +23231,7 @@ define('build', function (require) {
//after the module is processed.
//If we have a name, but no defined module, then add in the placeholder.
if (moduleName && falseProp(layer.modulesWithNames, moduleName) && !config.skipModuleInsertion) {
- shim = config.shim && getOwn(config.shim, moduleName);
+ shim = config.shim && (getOwn(config.shim, moduleName) || (packageConfig && getOwn(config.shim, nonPackageName)));
if (shim) {
fileContents += '\n' + namespaceWithDot + 'define("' + moduleName + '", ' +
(shim.deps && shim.deps.length ?
Something went wrong with that request. Please try again.