Skip to content

Commit 3c6fcd6

Browse files
committed
Initial commit.
0 parents  commit 3c6fcd6

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# requirejs-export-plugin
2+
3+
Exports all imported modules to browser requirejs via `window.define`.
4+
5+
This is specifically useful for Magento 2, which extensively uses requirejs.
6+
7+
## Usage
8+
9+
const RequireJsExportPlugin = require('@sdinteractive/requirejs-export-plugin');
10+
11+
module.exports = {
12+
plugins: [
13+
new RequireJsExportPlugin(),
14+
],
15+
};

index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const ConcatSource = require('webpack-sources').ConcatSource;
4+
5+
function RequireJsExportPlugin() {
6+
}
7+
8+
RequireJsExportPlugin.prototype.apply = function(compiler) {
9+
compiler.plugin('compilation', function (compilation, data) {
10+
compilation.plugin('succeed-module', function(module) {
11+
// This ensures we don't export the import stubs for requirejs.
12+
// TODO: Determine loader better.
13+
if (module.request && String(module.request).indexOf('requirejs-loader') !== -1) {
14+
return;
15+
}
16+
17+
// TODO: Find a way around using _source.
18+
if (module.rawRequest && module._source) {
19+
// We use the raw request to define the same name, including loader.
20+
var name = module.rawRequest;
21+
// TODO: Maybe just strip everything but 'text!'?
22+
if (name.indexOf('script-loader!') === 0) {
23+
name = name.substr('script-loader!'.length);
24+
}
25+
26+
var definition = 'window.define(' + JSON.stringify(name) + ', [], function() { return module.exports; });';
27+
module._source = new ConcatSource(module._source, '\n', definition);
28+
}
29+
});
30+
});
31+
};
32+
33+
34+
module.exports = RequireJsExportPlugin;

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@sdinteractive/requirejs-export-plugin",
3+
"version": "1.0.0",
4+
"description": "webpack plugin to export modules to requirejs.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/sdinteractive/webpack-requirejs-export-plugin.git"
12+
},
13+
"keywords": [
14+
"webpack",
15+
"requirejs"
16+
],
17+
"author": "Something Digital",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/sdinteractive/webpack-requirejs-export-plugin/issues"
21+
},
22+
"homepage": "https://github.com/sdinteractive/webpack-requirejs-export-plugin#readme"
23+
}

0 commit comments

Comments
 (0)