Fix aspnet-webpack package

This commit is contained in:
SteveSandersonMS
2016-03-11 00:34:57 +00:00
parent 590574a8a9
commit d5fbe4b3b6
5 changed files with 27 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "aspnet-webpack",
"version": "1.0.0",
"version": "1.0.1",
"description": "Helpers for using Webpack in ASP.NET projects. Works in conjunction with the Microsoft.AspNet.SpaServices NuGet package.",
"main": "index.js",
"scripts": {

View File

@@ -1,3 +0,0 @@
export function deepClone<T>(serializableObject: T): T {
return JSON.parse(JSON.stringify(serializableObject));
}

View File

@@ -8,7 +8,7 @@ import ExternalsPlugin from 'webpack-externals-plugin';
import requireFromString from 'require-from-string';
import MemoryFS from 'memory-fs';
import * as webpack from 'webpack';
import { deepClone } from './DeepClone';
import { requireNewCopy } from './RequireNewCopy';
// Ensure we only go through the compile process once per [config, module] pair
const loadViaWebpackPromisesCache: { [key: string]: any } = {};
@@ -32,7 +32,7 @@ export function loadViaWebpack<T>(webpackConfigPath: string, modulePath: string,
function loadViaWebpackNoCache<T>(webpackConfigPath: string, modulePath: string) {
return new Promise<T>((resolve, reject) => {
// Load the Webpack config and make alterations needed for loading the output into Node
const webpackConfig: webpack.Configuration = deepClone(require(webpackConfigPath));
const webpackConfig: webpack.Configuration = requireNewCopy(webpackConfigPath);
webpackConfig.entry = modulePath;
webpackConfig.target = 'node';
webpackConfig.output = { path: '/', filename: 'webpack-output.js', libraryTarget: 'commonjs' };

View File

@@ -0,0 +1,22 @@
export function requireNewCopy(moduleNameOrPath: string): any {
// Store a reference to whatever's in the 'require' cache,
// so we don't permanently destroy it, and then ensure there's
// no cache entry for this module
const resolvedModule = require.resolve(moduleNameOrPath);
const wasCached = resolvedModule in require.cache;
let cachedInstance;
if (wasCached) {
cachedInstance = require.cache[resolvedModule];
delete require.cache[resolvedModule];
}
try {
// Return a new copy
return require(resolvedModule);
} finally {
// Restore the cached entry, if any
if (wasCached) {
require.cache[resolvedModule] = cachedInstance;
}
}
}

View File

@@ -1,6 +1,6 @@
import * as express from 'express';
import * as webpack from 'webpack';
import { deepClone } from './DeepClone';
import { requireNewCopy } from './RequireNewCopy';
export interface CreateDevServerCallback {
(error: any, result: { Port: number, PublicPath: string }): void;
@@ -20,7 +20,7 @@ interface DevServerOptions {
export function createWebpackDevServer(callback: CreateDevServerCallback, optionsJson: string) {
const options: CreateDevServerOptions = JSON.parse(optionsJson);
const webpackConfig: webpack.Configuration = deepClone(require(options.webpackConfigPath));
const webpackConfig: webpack.Configuration = requireNewCopy(options.webpackConfigPath);
const publicPath = (webpackConfig.output.publicPath || '').trim();
if (!publicPath) {
callback('To use the Webpack dev server, you must specify a value for \'publicPath\' on the \'output\' section of your webpack.config.', null);