public WebpackServiceMock()
 {
     _files              = new Dictionary <string, IWebpackFile>();
     _options            = new WebpackOptions();
     _options.ConfigFile = "webpack.config.js";
     _options.PublicPath = "/webpack/";
     _options.WebRoot    = "wwwroot/";
     _options.LogLevel   = WebpackLogLevel.Normal;
     _options.Heartbeat  = 1000;
 }
        private static WebpackOptions CreateWebpack(IHostingEnvironment env)
        {
            var options = new WebpackOptions("Content/client/Startup.ts")
            {
                EnableES2015 = true,
                StylesTypes  = new List <StylesType>
                {
                    StylesType.Css,
                    StylesType.Less,
                    StylesType.Sass
                }
            };

            if (env.IsDevelopment())
            {
                options.DevServerOptions = new WebpackDevServerOptions(port: 5000);
                options.EnableHotLoading = true;
            }

            return(options);
        }
Beispiel #3
0
        private void ActivateCore()
        {
            lock (this._syncRoot) {
                Interlocked.MemoryBarrier();
                if (Instance.IsActivated)
                {
                    return;
                }

                WebpackOptions options = WebpackHttpModule.Options;

                if (options == null)
                {
                    throw new InvalidOperationException($"{typeof(WebpackHttpModule)}.{nameof(WebpackHttpModule.Options)} is null");
                }

                // Unlike other consumers of NodeServices, WebpackDevMiddleware doesn't share Node instances, nor does it
                // use your DI configuration. It's important for WebpackDevMiddleware to have its own private Node instance
                // because it must *not* restart when files change (if it did, you'd lose all the benefits of Webpack
                // middleware). And since this is a dev-time-only feature, it doesn't matter if the default transport isn't
                // as fast as some theoretical future alternative.
                NodeServicesOptions nodeServicesOptions = new NodeServicesOptions(WebAppAbstractions.ApplicationServices);
                nodeServicesOptions.WatchFileExtensions = new string[] { }; // Don't watch anything
                if (!string.IsNullOrEmpty(options.ProjectPath))
                {
                    nodeServicesOptions.ProjectPath = options.ProjectPath;
                }

                if (options.EnvironmentVariables != null)
                {
                    foreach (var kvp in options.EnvironmentVariables)
                    {
                        nodeServicesOptions.EnvironmentVariables[kvp.Key] = kvp.Value;
                    }
                }

                var nodeServices = NodeServicesFactory.CreateNodeServices(nodeServicesOptions);

                // Get a filename matching the middleware Node script
                var script = EmbeddedResourceReader.Read(typeof(WebpackApplication),
                                                         "/Content/Node/webpack-dev-middleware.js");
                var nodeScript = new StringAsTempFile(script, nodeServicesOptions.ApplicationStoppingToken); // Will be cleaned up on process exit

                // Ideally, this would be relative to the application's PathBase (so it could work in virtual directories)
                // but it's not clear that such information exists during application startup, as opposed to within the context
                // of a request.
                var hmrEndpoint = !string.IsNullOrEmpty(options.HotModuleReplacementEndpoint)
                    ? options.HotModuleReplacementEndpoint
                    : "/__webpack_hmr"; // Matches webpack's built-in default

                // Tell Node to start the server hosting webpack-dev-middleware
                var devServerOptions = new {
                    webpackConfigPath = Path.Combine(nodeServicesOptions.ProjectPath, options.ConfigFile ?? DefaultConfigFile),
                    suppliedOptions   = options,
                    understandsMultiplePublicPaths  = true,
                    hotModuleReplacementEndpointUrl = hmrEndpoint
                };
                var devServerInfo =
                    nodeServices.InvokeExportAsync <WebpackDevServerInfo>(nodeScript.FileName, "createWebpackDevServer",
                                                                          JsonConvert.SerializeObject(devServerOptions, jsonSerializerSettings)).Result;

                // If we're talking to an older version of aspnet-webpack, it will return only a single PublicPath,
                // not an array of PublicPaths. Handle that scenario.
                if (devServerInfo.PublicPaths == null)
                {
                    devServerInfo.PublicPaths = new[] { devServerInfo.PublicPath };
                }

                // Proxy the corresponding requests through ASP.NET and into the Node listener
                // Anything under /<publicpath> (e.g., /dist) is proxied as a normal HTTP request with a typical timeout (100s is the default from HttpClient),
                // plus /__webpack_hmr is proxied with infinite timeout, because it's an EventSource (long-lived request).
                foreach (var publicPath in devServerInfo.PublicPaths)
                {
                    this.RegisterProxyToLocalWebpackDevMiddleware(publicPath + hmrEndpoint, devServerInfo.Port, Timeout.InfiniteTimeSpan);
                    this.RegisterProxyToLocalWebpackDevMiddleware(publicPath, devServerInfo.Port, TimeSpan.FromSeconds(100));
                }
            }
        }