AMD module AMD module moduleA.js moduleB.js moduleC.js moduleD.js layout.cshtml Index.cshtml Compile AMD module {Common.js moduleA.js moduleB.js} {Map.js moduleC.js moduleD.js} Non-AMD module Non-AMD module Common.js Map.js Boss: How to improve the module system? Require.js define/ require (AMD) BundleConfig.cs Embedded module 7 Requirejs config 1. To reduce the bundle size (We need module dependency management) 2. To reduce the number of requests from the client (We need common dependencies extracting) 3. To improve the development environment (We need plugin supports) It’s a 10-year ASP.NET MVC project … Well.. that’s change the module bundler
.js .js .js .js • require('modules') in the browser by bundling up all of your dependencies. • Github stars: 13 k • Github forks: 1.1 k • Companies: • A bundler for javascript and friends. Packs many modules into a few bundled assets • Github stars: 52.1 k • Github forks: 6.6 k • Companies: Browserify Webpack Gulp Gulp Npm scripts
Task runner AMD module moduleA.js moduleB.js moduleC.js Non-AMD module Non-AMD module map.bundle.js Webpack config.js Boss: what’s the efforts for migrating to Webpack? Webpack.js entry.bundle.js Embedded module 9 Well.. What do I exactly have to do in this case? The efforts depend on the previous JS modules, previous module loader’s configuration, and the previous bundler system Shimming
new JS file into the bundleConfig.cs before migrating to Webpack Note 2: It may occur error if you forgot to attach the bundle to other html/cshtml files common.js common.cshtml _layout.cshtml Config.js AController 2. Add 3. [email protected] 1.Move embedded modules to stand-alone modules Render. Pass data by model A.cshtml BController B .cshtml bundles.Add(new ScriptWithSourceMapBundle("~/bundles/Home.js").Include( …, "~/Scripts/config.js" )); Render define('config', function () { … }); [email protected] require(['config’], function (config) { … }); require
'/Scripts/', paths: { 'knockout': 'knockout-3.0.0.debug', 'koMapper': 'knockout.mapping-latest' } }); • Solution 1: specific the path on the alias field //webpack.config.js const path = require('path'); const config = { resolve: { extensions: ['.js', '.jsx'], alias: { knockout: path.resolve(__dirname, './Scripts/knockout-3.2.0.js'), koMapper: path.resolve(__dirname, './Scripts/knockout.mapping.js’), 'common/svgSheet': path.resolve(__dirname, './Scripts/svgSheet.js’), common: path.resolve(__dirname, './Scripts/common’) } }, }; module.exports = config; • Original module loader (Require.js) Note 2: The order of the module’s name should be prior than folder’s name Module not found: Error: Can’t resolve ‘common/svgSheet’ Note 1: It will lead conflicts if the node module has the same name as the local library’s name 1. Remove that module in the package.json. 2. npm install again. 3. Put AMD modules’ name and path on the alias field const config = { resolve: { extensions: ['.js', '.jsx'], modules: [ path.resolve(__dirname, './Scripts’), 'node_modules' ], alias: { knockout: "knockout-3.2.0.debug ", koMapper: 'knockout.mapping-latest', ... } } }; module.exports = config; • Solution 2: search the module with the alias and modules field
2. Add Html-Webpack-Plugin in the plugin field in the webpack.config.js 3. Create a template file 4. Render the generated file from the original view 27 npm install Html-Webpack-Plugin --save-dev plugins: [ ..., new HtmlWebpackPlugin({ filename: './Views/_home.bundle.cshtml', template: './Views/BundlesTemp/_JsTemplate.cshtml', chunks: ['home'], inject: false }) ] <% for (var chunk in htmlWebpackPlugin.files.chunks) { %> <script src="<%=htmlWebpackPlugin.files.chunks[chunk].entry %>"></script> <%}%> @Html.Partial("~/Views/Bundle/_home.bundle.cshtml")
JS modules, previous module system’s configuration, and the previous bundler system 2. Webpack can’t resolve embedded modules. We have to make them stand-alone 3. The validation is important for refactoring layouts 31