Skip to content

Commit 25bd479

Browse files
author
Chuck Dumont
authored
Merge pull request #143 from chuckdumont/work2
Add globalContext option
2 parents 437f931 + 747b850 commit 25bd479

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [loaderConfig](#loaderconfig)
3030
- [environment](#environment)
3131
- [buildEnvironment](#buildenvironment)
32+
- [globalContext](#globalcontext)
3233
- [loader](#loader)
3334
- [locales](#locales)
3435
- [cjsRequirePatterns](#cjsrequirepatterns)
@@ -226,6 +227,12 @@ If the loader config evaluates to a function, then this property specifies the o
226227

227228
Simialr to `environment`, but used exclusively at build time. If both are specified, then `buildEnvironment` will be passed to the `loaderConfig` function when building, and `environment` will be passed to the `loaderConfig` function when the config is evaluated for use in the browser. This facilitates specifying different `loaderConfig` paths (e.g. `baseUrl`) for build vs. run. If only `environment` is specified, then it is used for both.
228229

230+
### globalContext
231+
232+
Specifies the path to use for resolving relative module ids passed to the [global require function](#the-global-require-function). Dojo resolves these against the page URL. If this option is specified as a relative path, then it is resolved against the webpack compiler context. If not specified, it defaults to the webpack compiler context.
233+
234+
Note that the `globalContext` option is different than the Dojo loader config's `baseUrl` property in that it is used only for resolving relative path module identifiers (those that begin with a `.`) passed to the global require function. In contrast, the `baseUrl` property is used for resolving non-relative module ids that don't map to a defined package or path.
235+
229236
### loader
230237

231238
This property is optional and specifies the module path of the built Dojo loader. See [Building the Dojo loader](#building-the-dojo-loader) for details. If not specified, then the loader will be built as part of the Webpack build.
@@ -320,7 +327,7 @@ When using Webpack's NormalModuleReplacementPlugin, the order of the plugin regi
320327

321328
# The global require function
322329

323-
Like Dojo, this plugin differentiates between the global require function and [context-sensitive require](https://dojotoolkit.org/reference-guide/1.10/loader/amd.html#context-sensitive-require). This distinction affects how module identifiers with relative paths are resolved. When using context-sensitive require, relative paths are resolved against the path of the containing module. When using global require, Dojo resolves relative paths against the page URL, while this plugin resolves them against the webpack compiler context path.
330+
Like Dojo, this plugin differentiates between the global require function and [context-sensitive require](https://dojotoolkit.org/reference-guide/1.10/loader/amd.html#context-sensitive-require). This distinction affects how module identifiers with relative paths are resolved. When using context-sensitive require, relative paths are resolved against the path of the containing module. When using global require, Dojo resolves relative paths against the page URL, while this plugin resolves them against the path specified by the [globalContext](#globalcontext) option, or the webpack compiler context path.
324331

325332
Also like Dojo, this plugin defines `window.require` in global scope on the client. The global require function implements Dojo's [synchronous require](#commonjs-require-vs-dojo-synchronous-require) capability. This works great for Dojo applications but it can be a problem in some scenarios involving other (non-webpack) loaders or frameworks. For those situations where it is not desirable to overwrite `window.require`, you can use the ScopedRequirePlugin plugin. The ScopedRequirePlugin plugin leaves `window.require` untouched, and instead defines `require` in a scope that encloses each AMD module. Note that this scoped `require` is similar to the global `require` in that it is not associated with any module's context and cannot be used to load modules with paths relative to the calling module. For that you need to use a context-sensitive require defined within your module.
326333

lib/DojoAMDMainTemplatePlugin.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2222
*/
2323

24+
const path = require('path');
2425
const util = require('util');
2526
const Template = require("webpack/lib/Template");
2627
const stringify = require("node-stringify");
@@ -131,7 +132,8 @@ but the loader specified at ${this.embeddedLoaderFilename} was built without the
131132
}
132133
buf.push(mainTemplate.applyPluginsWaterfall("render-dojo-config-vars", loaderScope, "", ...rest));
133134
if (this.emitBuildContext) {
134-
var context = this.compiler.context.replace(/\\/g,'/');
135+
var context = path.resolve(this.compiler.context, this.options.globalContext || '.').replace(/\\/g,'/');
136+
/* istanbul ignore else */
135137
if (!context.endsWith('/')) {
136138
context += '/';
137139
}

lib/DojoAMDModuleFactoryPlugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ module.exports = class DojoAMDModuleFactoryPlugin {
170170
if (dep && dep.usingGlobalRequire && data.request.startsWith('.')) {
171171
// Global require with relative path. Dojo resolves against the page.
172172
// We'll resolve against the compiler context.
173-
data.request = path.resolve(this.compiler.context, data.request);
173+
data.request = path.resolve(this.compiler.context, this.options.globalContext||'.', data.request);
174174
this.addAbsMid(data, data.request.replace(/\\/g,'/'));
175175
this.compilation.mainTemplate.applyPlugins("set-emit-build-context");
176176
}

test/TestCases/dependencies/constGlobalRequire/webpack.config.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,34 @@ module.exports = [contextdir, contextdir+'/'].map(context => {
1111
baseUrl: "../",
1212
paths:{test: "."}
1313
},
14+
loader: path.join(__dirname, "../../../js/dojo/dojo.js")
15+
})
16+
]
17+
};
18+
}).concat(["./globalContext", "./globalContext/"].map(context => {
19+
return {
20+
entry: "test/index",
21+
plugins: [
22+
new DojoWebpackPlugin({
23+
loaderConfig: {
24+
paths:{test: "."}
25+
},
26+
loader: path.join(__dirname, "../../../js/dojo/dojo.js"),
27+
globalContext: context
28+
})
29+
]
30+
};
31+
})).concat([contextdir, contextdir+'/'].map(context => {
32+
return {
33+
entry: "test/index",
34+
plugins: [
35+
new DojoWebpackPlugin({
36+
loaderConfig: {
37+
paths:{test: "."}
38+
},
1439
loader: path.join(__dirname, "../../../js/dojo/dojo.js"),
15-
cjsRequirePatterns: [/subdir/]
40+
globalContext: context
1641
})
1742
]
1843
};
19-
});
44+
}));

test/TestCases/dependencies/globalRequire/webpack.config.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,34 @@ module.exports = [contextdir, contextdir+'/'].map(context => {
1111
baseUrl: "../",
1212
paths:{test: "."}
1313
},
14+
loader: path.join(__dirname, "../../../js/dojo/dojo.js")
15+
})
16+
]
17+
};
18+
}).concat(["./globalContext", "./globalContext/"].map(context => {
19+
return {
20+
entry: "test/index",
21+
plugins: [
22+
new DojoWebpackPlugin({
23+
loaderConfig: {
24+
paths:{test: "."}
25+
},
26+
loader: path.join(__dirname, "../../../js/dojo/dojo.js"),
27+
globalContext: context
28+
})
29+
]
30+
};
31+
})).concat([contextdir, contextdir+'/'].map(context => {
32+
return {
33+
entry: "test/index",
34+
plugins: [
35+
new DojoWebpackPlugin({
36+
loaderConfig: {
37+
paths:{test: "."}
38+
},
1439
loader: path.join(__dirname, "../../../js/dojo/dojo.js"),
15-
cjsRequirePatterns: [/subdir/]
40+
globalContext: context
1641
})
1742
]
1843
};
19-
});
44+
}));

0 commit comments

Comments
 (0)