I just started to get in touch with front-end modular development. I wrote a demo to learn webpack. I basically understand the configuration file and operation process, but on-demand loading always fails. Please help me:
In the entry file, three methods are used to load:
import test from './index/test.js';
// const test=(resolve) => require(['./index/test.js'], resolve)
// const test=resolve => { require.ensure(['./index/test.js'], () => { resolve(require('./index/test.js')) }) }
test.exe('显示测试文字');//执行
The content of test.js is very simple, just print to the console:
const test={
exe:function (res) {
console.log('test方法的输出:'+res);
}
};
export default test
All three methods have been tested. Only the first direct import method runs normally. The other two on-demand loading methods will report an error, indicating that the method cannot be found.
If test.exe('Display test text');
is commented out and only loaded but not executed, no error will be reported.
My understanding is that there is nothing wrong with loading the codes, but when they need to be loaded, they are not loaded successfully. Why is this? Did I write something wrong somewhere? Or do I need to make additional configuration to webpack.config.jx?
Give you an example for reference
html
The js file plugin.js that needs to be loaded asynchronously
webpack’s entry compilation file entry.js
The effect is that mod1.js is loaded and inserted into the head when clicked, but it is not loaded at the beginning
Finally, about the configuration of webpack.config.js.
I have encountered a similar problem recently, let me briefly explain it.
When webpack is upgraded to 2, your second and third credit methods are not directly packaged into main.js.
That is to say, for modules that are required for first-screen loading, the asynchronous loading mode can no longer be used, but can be loaded on demand.
You can take a look in the packaged file. Except for the first method, your test method has not been packaged into your js.
What do you want to do with the second and third writing methods? Do you want to simulate the writing method of AMD or CMD specifications?
The most common module specification is the ES6 module and the commonJS specification of node.js, because there are differences in specific loading details, such as loading time and different ways of referencing files. But the purpose of using webpack is to unify different specifications. Webpack will package all modules together in advance, give them an id respectively, and reference them by id, so that there is no difference between ES6 module and CommonJS specifications after webpack compiles. The same is true for AMD and CMD specifications.
If the poster wants to use webpack to implement delayed loading of CMD, this idea is wrong, because no matter which loading method, what webpack does is to package all the modules that you depend on (or will depend on) into one file, so that The corresponding package can be found by id at runtime, weakening the differences between specificationsI don’t know your specific environment. My own environment has been upgraded to Webpack2 + React Router v4. You can refer to the documentation: https://reacttraining.cn/web/...
First you need to code and create a Bundle component to load the required modules and component files on demand.
The above code is copied from the document, and the state initialization method is modified. If you do not modify the state initialization method, you need to use
babel-plugin-transform-class-properties
.There are three steps to use
Import
Bundle
moduleAsynchronous loading
Initialization
Of course, you still need to configure your
.babelrc
和webpack.config.js
, I will give mine below, you can study it.webpack.config.js
.babelrc
There is also the configuration of the public block output plugin
After passing the above N steps, the component
Home
can be used.