I'm trying to import material-ui into my React app using System.JS
In my app, I'm doing this:
import {AppBar, Tabs, Tab, Card, CardTitle} from 'material-ui';
Here's my System.JS config:
System.config({
baseURL: '/node_modules',
packageConfigPaths: [
'*/package.json'
],
packages: {
'.': {
defaultExtension: 'js',
main: 'index.js'
},
}
});
It loads node_modules/material-ui/index.js
which has a bunch of imports inside it:
var _AppBar2 = require('./AppBar');
var _AppBar3 = _interopRequireDefault(_AppBar2);
var _AutoComplete2 = require('./AutoComplete');
var _AutoComplete3 = _interopRequireDefault(_AutoComplete2);
// ... etc, about 40 of them.
exports.AppBar = _AppBar3.default;
exports.AutoComplete = _AutoComplete3.default;
// ... etc
In the package's tree structure, each of these modules is stored under its own directory like this:
material-ui/
index.js
AppBar/
AppBar.js
index.js -- just reexports './AppBar'
AutoComplete/
AutoComplete.js
index.js -- just reexports './AutoComplete'
, etc., so in fact to import material-ui/AppBar
, I need System.JS to load node_modules/material-ui/AppBar/AppBar.js
or node_modules/material-ui/AppBar/index.js
.
Instead, System.JS is trying to load node_modules/material-ui/AppBar.js
which is not there.
If I add individual entries for each module under packages
:
'material-ui': {
map: {
'./AppBar': './AppBar/AppBar.js'
}
}
it works, however wildcards:
'material-ui': {
map: {
'./*': './*/*.js'
}
}
don't.
How do I make System.JS map ./*
to ./*/*.js
under a certain package?
As a side note, browserify does not have any problems with this layout, so when I bundle my app using browserify just by calling browserify('./path/to/root/index.js')
, all material-ui modules get imported without any issues.
You'll also learn how to import module code and see how code is lazy-loaded in a browser. An import map is a JavaScript object where the key is the name of the import ( @lodash/startCase ) and the value is the location of the code. Inside of index.html add a new script tag with a type of importmap .
1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.
SystemJS is a module loader that can import modules at run time in any of the popular formats used today (CommonJS, UMD, AMD, ES6). It is built on top of the ES6 module loader polyfill and is smart enough to detect the format being used and handle it appropriately.
Wildcard paths are not supported in System.js. You will have to manually add an entry for each module:
'material-ui': {
map: {
'./AppBar': './AppBar/AppBar.js',
'./AppHeader': './AppHeader/AppHeader.js',
'./AppFooter': './AppFooter/AppFooter.js',
//etc
}
}
You can also use jspm to generate that list for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With