mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 18:47:30 +00:00
Rename Microsoft.AspNet.* packages folders to Microsoft.AspNetCore.*
This commit is contained in:
4
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-prerendering/.gitignore
vendored
Normal file
4
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-prerendering/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/typings/
|
||||
/node_modules/
|
||||
/*.js
|
||||
/*.d.ts
|
||||
@@ -0,0 +1,3 @@
|
||||
!/*.js
|
||||
!/*.d.ts
|
||||
/typings/
|
||||
@@ -0,0 +1,12 @@
|
||||
Copyright (c) .NET Foundation. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
||||
these files except in compliance with the License. You may obtain a copy of the
|
||||
License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
@@ -0,0 +1,6 @@
|
||||
# Not for general use
|
||||
|
||||
This NPM package is an internal implementation detail of the `Microsoft.AspNet.SpaServices` NuGet package.
|
||||
|
||||
You should not use this package directly in your own applications, because it is not supported, and there are no
|
||||
guarantees about how its APIs will change in the future.
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "aspnet-prerendering",
|
||||
"version": "1.0.1",
|
||||
"description": "Helpers for server-side rendering of JavaScript applications in ASP.NET projects. Works in conjunction with the Microsoft.AspNet.SpaServices NuGet package.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"prepublish": "tsd update && tsc && echo 'Finished building NPM package \"aspnet-prerendering\"'",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Microsoft",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"domain-task": "^1.0.1",
|
||||
"es6-promise": "^3.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^1.8.10"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
import 'es6-promise';
|
||||
import * as url from 'url';
|
||||
import * as path from 'path';
|
||||
import * as domain from 'domain';
|
||||
import { run as domainTaskRun } from 'domain-task/main';
|
||||
import { baseUrl } from 'domain-task/fetch';
|
||||
|
||||
export interface RenderToStringCallback {
|
||||
(error: any, result: RenderToStringResult): void;
|
||||
}
|
||||
|
||||
export interface RenderToStringResult {
|
||||
html: string;
|
||||
globals: { [key: string]: any };
|
||||
}
|
||||
|
||||
export interface BootFunc {
|
||||
(params: BootFuncParams): Promise<RenderToStringResult>;
|
||||
}
|
||||
|
||||
export interface BootFuncParams {
|
||||
location: url.Url; // e.g., Location object containing information '/some/path'
|
||||
origin: string; // e.g., 'https://example.com:1234'
|
||||
url: string; // e.g., '/some/path'
|
||||
absoluteUrl: string; // e.g., 'https://example.com:1234/some/path'
|
||||
domainTasks: Promise<any>;
|
||||
}
|
||||
|
||||
export interface BootModuleInfo {
|
||||
moduleName: string;
|
||||
exportName?: string;
|
||||
webpackConfig?: string;
|
||||
}
|
||||
|
||||
export function renderToString(callback: RenderToStringCallback, applicationBasePath: string, bootModule: BootModuleInfo, absoluteRequestUrl: string, requestPathAndQuery: string) {
|
||||
findBootFunc(applicationBasePath, bootModule, (findBootFuncError, bootFunc) => {
|
||||
if (findBootFuncError) {
|
||||
callback(findBootFuncError, null);
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare a promise that will represent the completion of all domain tasks in this execution context.
|
||||
// The boot code will wait for this before performing its final render.
|
||||
let domainTaskCompletionPromiseResolve;
|
||||
const domainTaskCompletionPromise = new Promise((resolve, reject) => {
|
||||
domainTaskCompletionPromiseResolve = resolve;
|
||||
});
|
||||
const parsedAbsoluteRequestUrl = url.parse(absoluteRequestUrl);
|
||||
const params: BootFuncParams = {
|
||||
location: url.parse(requestPathAndQuery),
|
||||
origin: parsedAbsoluteRequestUrl.protocol + '//' + parsedAbsoluteRequestUrl.host,
|
||||
url: requestPathAndQuery,
|
||||
absoluteUrl: absoluteRequestUrl,
|
||||
domainTasks: domainTaskCompletionPromise
|
||||
};
|
||||
|
||||
// Open a new domain that can track all the async tasks involved in the app's execution
|
||||
domainTaskRun(/* code to run */ () => {
|
||||
// Workaround for Node bug where native Promise continuations lose their domain context
|
||||
// (https://github.com/nodejs/node-v0.x-archive/issues/8648)
|
||||
// The domain.active property is set by the domain-context module
|
||||
bindPromiseContinuationsToDomain(domainTaskCompletionPromise, domain['active']);
|
||||
|
||||
// Make the base URL available to the 'domain-tasks/fetch' helper within this execution context
|
||||
baseUrl(absoluteRequestUrl);
|
||||
|
||||
// Actually perform the rendering
|
||||
bootFunc(params).then(successResult => {
|
||||
callback(null, { html: successResult.html, globals: successResult.globals });
|
||||
}, error => {
|
||||
callback(error, null);
|
||||
});
|
||||
}, /* completion callback */ errorOrNothing => {
|
||||
if (errorOrNothing) {
|
||||
callback(errorOrNothing, null);
|
||||
} else {
|
||||
// There are no more ongoing domain tasks (typically data access operations), so we can resolve
|
||||
// the domain tasks promise which notifies the boot code that it can do its final render.
|
||||
domainTaskCompletionPromiseResolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function findBootModule<T>(applicationBasePath: string, bootModule: BootModuleInfo, callback: (error: any, foundModule: T) => void) {
|
||||
const bootModuleNameFullPath = path.resolve(applicationBasePath, bootModule.moduleName);
|
||||
if (bootModule.webpackConfig) {
|
||||
const webpackConfigFullPath = path.resolve(applicationBasePath, bootModule.webpackConfig);
|
||||
|
||||
let aspNetWebpackModule: any;
|
||||
try {
|
||||
aspNetWebpackModule = require('aspnet-webpack');
|
||||
} catch (ex) {
|
||||
callback('To load your boot module via webpack (i.e., if you specify a \'webpackConfig\' option), you must install the \'aspnet-webpack\' NPM package.', null);
|
||||
return;
|
||||
}
|
||||
|
||||
aspNetWebpackModule.loadViaWebpack(webpackConfigFullPath, bootModuleNameFullPath, callback);
|
||||
} else {
|
||||
callback(null, require(bootModuleNameFullPath));
|
||||
}
|
||||
}
|
||||
|
||||
function findBootFunc(applicationBasePath: string, bootModule: BootModuleInfo, callback: (error: any, bootFunc: BootFunc) => void) {
|
||||
// First try to load the module (possibly via Webpack)
|
||||
findBootModule<any>(applicationBasePath, bootModule, (findBootModuleError, foundBootModule) => {
|
||||
if (findBootModuleError) {
|
||||
callback(findBootModuleError, null);
|
||||
return;
|
||||
}
|
||||
|
||||
// Now try to pick out the function they want us to invoke
|
||||
let bootFunc: BootFunc;
|
||||
if (bootModule.exportName) {
|
||||
// Explicitly-named export
|
||||
bootFunc = foundBootModule[bootModule.exportName];
|
||||
} else if (typeof foundBootModule !== 'function') {
|
||||
// TypeScript-style default export
|
||||
bootFunc = foundBootModule.default;
|
||||
} else {
|
||||
// Native default export
|
||||
bootFunc = foundBootModule;
|
||||
}
|
||||
|
||||
// Validate the result
|
||||
if (typeof bootFunc !== 'function') {
|
||||
if (bootModule.exportName) {
|
||||
callback(`The module at ${ bootModule.moduleName } has no function export named ${ bootModule.exportName }.`, null);
|
||||
} else {
|
||||
callback(`The module at ${ bootModule.moduleName } does not export a default function, and you have not specified which export to invoke.`, null);
|
||||
}
|
||||
} else {
|
||||
callback(null, bootFunc);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function bindPromiseContinuationsToDomain(promise: Promise<any>, domainInstance: domain.Domain) {
|
||||
const originalThen = promise.then;
|
||||
promise.then = function then(resolve, reject) {
|
||||
if (typeof resolve === 'function') {
|
||||
resolve = domainInstance.bind(resolve);
|
||||
}
|
||||
|
||||
if (typeof reject === 'function') {
|
||||
reject = domainInstance.bind(reject);
|
||||
}
|
||||
|
||||
return originalThen.call(this, resolve, reject);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './Prerendering';
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "node",
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"declaration": true,
|
||||
"outDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"version": "v4",
|
||||
"repo": "borisyankov/DefinitelyTyped",
|
||||
"ref": "master",
|
||||
"path": "typings",
|
||||
"bundle": "typings/tsd.d.ts",
|
||||
"installed": {
|
||||
"node/node.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"es6-promise/es6-promise.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"whatwg-fetch/whatwg-fetch.d.ts": {
|
||||
"commit": "dade4414712ce84e3c63393f1aae407e9e7e6af7"
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack-react/.gitignore
vendored
Normal file
4
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack-react/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/typings/
|
||||
/node_modules/
|
||||
/*.js
|
||||
/*.d.ts
|
||||
@@ -0,0 +1,3 @@
|
||||
!/*.js
|
||||
!/*.d.ts
|
||||
/typings/
|
||||
@@ -0,0 +1,12 @@
|
||||
Copyright (c) .NET Foundation. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
||||
these files except in compliance with the License. You may obtain a copy of the
|
||||
License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
@@ -0,0 +1,6 @@
|
||||
# Not for general use
|
||||
|
||||
This NPM package is an internal implementation detail of the `Microsoft.AspNet.SpaServices` NuGet package.
|
||||
|
||||
You should not use this package directly in your own applications, because it is not supported, and there are no
|
||||
guarantees about how its APIs will change in the future.
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "aspnet-webpack-react",
|
||||
"version": "1.0.1",
|
||||
"description": "Helpers for using Webpack with React in ASP.NET projects. Works in conjunction with the Microsoft.AspNet.SpaServices NuGet package.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"prepublish": "tsd update && tsc && echo 'Finished building NPM package \"aspnet-webpack-react\"'",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Microsoft",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"babel-core": "^6.7.2",
|
||||
"babel-loader": "^6.2.4",
|
||||
"babel-plugin-react-transform": "^2.0.2",
|
||||
"babel-preset-es2015": "^6.6.0",
|
||||
"babel-preset-react": "^6.5.0",
|
||||
"react": "^0.14.7",
|
||||
"react-transform-hmr": "^1.0.4",
|
||||
"webpack": "^1.12.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^1.8.10"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import * as webpack from 'webpack';
|
||||
|
||||
export function addReactHotModuleReplacementBabelTransform(webpackConfig: webpack.Configuration) {
|
||||
webpackConfig.module.loaders.forEach(loaderConfig => {
|
||||
if (loaderConfig.loader && loaderConfig.loader.match(/\bbabel-loader\b/)) {
|
||||
// Ensure the babel-loader options includes a 'query'
|
||||
const query = loaderConfig.query = loaderConfig.query || {};
|
||||
|
||||
// Ensure Babel plugins includes 'react-transform'
|
||||
const plugins = query['plugins'] = query['plugins'] || [];
|
||||
const hasReactTransform = plugins.some(p => p && p[0] === 'react-transform');
|
||||
if (!hasReactTransform) {
|
||||
plugins.push(['react-transform', {}]);
|
||||
}
|
||||
|
||||
// Ensure 'react-transform' plugin is configured to use 'react-transform-hmr'
|
||||
plugins.forEach(pluginConfig => {
|
||||
if (pluginConfig && pluginConfig[0] === 'react-transform') {
|
||||
const pluginOpts = pluginConfig[1] = pluginConfig[1] || {};
|
||||
const transforms = pluginOpts.transforms = pluginOpts.transforms || [];
|
||||
const hasReactTransformHmr = transforms.some(t => t.transform === 'react-transform-hmr');
|
||||
if (!hasReactTransformHmr) {
|
||||
transforms.push({
|
||||
transform: 'react-transform-hmr',
|
||||
imports: ['react'],
|
||||
locals: ['module'] // Important for Webpack HMR
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { addReactHotModuleReplacementBabelTransform } from './HotModuleReplacement';
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "node",
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"declaration": true,
|
||||
"outDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"version": "v4",
|
||||
"repo": "borisyankov/DefinitelyTyped",
|
||||
"ref": "master",
|
||||
"path": "typings",
|
||||
"bundle": "typings/tsd.d.ts",
|
||||
"installed": {
|
||||
"source-map/source-map.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"webpack/webpack.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"uglify-js/uglify-js.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/.gitignore
vendored
Normal file
4
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/typings/
|
||||
/node_modules/
|
||||
/*.js
|
||||
/*.d.ts
|
||||
@@ -0,0 +1,3 @@
|
||||
!/*.js
|
||||
!/*.d.ts
|
||||
/typings/
|
||||
@@ -0,0 +1,12 @@
|
||||
Copyright (c) .NET Foundation. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
||||
these files except in compliance with the License. You may obtain a copy of the
|
||||
License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
@@ -0,0 +1,6 @@
|
||||
# Not for general use
|
||||
|
||||
This NPM package is an internal implementation detail of the `Microsoft.AspNet.SpaServices` NuGet package.
|
||||
|
||||
You should not use this package directly in your own applications, because it is not supported, and there are no
|
||||
guarantees about how its APIs will change in the future.
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "aspnet-webpack",
|
||||
"version": "1.0.3",
|
||||
"description": "Helpers for using Webpack in ASP.NET projects. Works in conjunction with the Microsoft.AspNet.SpaServices NuGet package.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"prepublish": "rimraf *.d.ts && tsd update && tsc && echo 'Finished building NPM package \"aspnet-webpack\"'",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Microsoft",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"es6-promise": "^3.1.2",
|
||||
"connect": "^3.4.1",
|
||||
"memory-fs": "^0.3.0",
|
||||
"require-from-string": "^1.1.0",
|
||||
"webpack": "^1.12.14",
|
||||
"webpack-dev-middleware": "^1.5.1",
|
||||
"webpack-externals-plugin": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^1.8.10",
|
||||
"rimraf": "^2.5.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// When you're using Webpack, it's often convenient to be able to require modules from regular JavaScript
|
||||
// and have them transformed by Webpack. This is especially useful when doing ASP.NET server-side prerendering,
|
||||
// because it means your boot module can use whatever source language you like (e.g., TypeScript), and means
|
||||
// that your loader plugins (e.g., require('./mystyles.less')) work in exactly the same way on the server as
|
||||
// on the client.
|
||||
import 'es6-promise';
|
||||
import * as webpack from 'webpack';
|
||||
import { requireNewCopy } from './RequireNewCopy';
|
||||
|
||||
// Strange import syntax to work around https://github.com/Microsoft/TypeScript/issues/2719
|
||||
import { webpackexternals } from './typings/webpack-externals-plugin';
|
||||
import { requirefromstring } from './typings/require-from-string';
|
||||
import { memoryfs } from './typings/memory-fs';
|
||||
const ExternalsPlugin = require('webpack-externals-plugin') as typeof webpackexternals.ExternalsPlugin;
|
||||
const requireFromString = require('require-from-string') as typeof requirefromstring.requireFromString;
|
||||
const MemoryFS = require('memory-fs') as typeof memoryfs.MemoryFS;
|
||||
|
||||
// Ensure we only go through the compile process once per [config, module] pair
|
||||
const loadViaWebpackPromisesCache: { [key: string]: any } = {};
|
||||
|
||||
export interface LoadViaWebpackCallback<T> {
|
||||
(error: any, result: T): void;
|
||||
}
|
||||
|
||||
export function loadViaWebpack<T>(webpackConfigPath: string, modulePath: string, callback: LoadViaWebpackCallback<T>) {
|
||||
const cacheKey = JSON.stringify(webpackConfigPath) + JSON.stringify(modulePath);
|
||||
if (!(cacheKey in loadViaWebpackPromisesCache)) {
|
||||
loadViaWebpackPromisesCache[cacheKey] = loadViaWebpackNoCache(webpackConfigPath, modulePath);
|
||||
}
|
||||
loadViaWebpackPromisesCache[cacheKey].then(result => {
|
||||
callback(null, result);
|
||||
}, error => {
|
||||
callback(error, null);
|
||||
})
|
||||
}
|
||||
|
||||
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 = requireNewCopy(webpackConfigPath);
|
||||
webpackConfig.entry = modulePath;
|
||||
webpackConfig.target = 'node';
|
||||
webpackConfig.output = { path: '/', filename: 'webpack-output.js', libraryTarget: 'commonjs' };
|
||||
|
||||
// In Node, we want anything under /node_modules/ to be loaded natively and not bundled into the output
|
||||
// (partly because it's faster, but also because otherwise there'd be different instances of modules
|
||||
// depending on how they were loaded, which could lead to errors)
|
||||
webpackConfig.plugins = webpackConfig.plugins || [];
|
||||
webpackConfig.plugins.push(new ExternalsPlugin({ type: 'commonjs', include: /node_modules/ }));
|
||||
|
||||
// The CommonsChunkPlugin is not compatible with a CommonJS environment like Node, nor is it needed in that case
|
||||
webpackConfig.plugins = webpackConfig.plugins.filter(plugin => {
|
||||
return !(plugin instanceof webpack.optimize.CommonsChunkPlugin);
|
||||
});
|
||||
|
||||
// The typical use case for DllReferencePlugin is for referencing vendor modules. In a Node
|
||||
// environment, it doesn't make sense to load them from a DLL bundle, nor would that even
|
||||
// work, because then you'd get different module instances depending on whether a module
|
||||
// was referenced via a normal CommonJS 'require' or via Webpack. So just remove any
|
||||
// DllReferencePlugin from the config.
|
||||
// If someone wanted to load their own DLL modules (not an NPM module) via DllReferencePlugin,
|
||||
// that scenario is not supported today. We would have to add some extra option to the
|
||||
// asp-prerender tag helper to let you specify a list of DLL bundles that should be evaluated
|
||||
// in this context. But even then you'd need special DLL builds for the Node environment so that
|
||||
// external dependencies were fetched via CommonJS requires, so it's unclear how that could work.
|
||||
// The ultimate escape hatch here is just prebuilding your code as part of the application build
|
||||
// and *not* using asp-prerender-webpack-config at all, then you can do anything you want.
|
||||
webpackConfig.plugins = webpackConfig.plugins.filter(plugin => {
|
||||
// DllReferencePlugin is missing from webpack.d.ts for some reason, hence referencing it
|
||||
// as a key-value object property
|
||||
return !(plugin instanceof webpack['DllReferencePlugin']);
|
||||
});
|
||||
|
||||
// Create a compiler instance that stores its output in memory, then load its output
|
||||
const compiler = webpack(webpackConfig);
|
||||
compiler.outputFileSystem = new MemoryFS();
|
||||
compiler.run((err, stats) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
const fileContent = compiler.outputFileSystem.readFileSync('/webpack-output.js', 'utf8');
|
||||
const moduleInstance = requireFromString<T>(fileContent);
|
||||
resolve(moduleInstance);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import * as connect from 'connect';
|
||||
import * as webpack from 'webpack';
|
||||
import { requireNewCopy } from './RequireNewCopy';
|
||||
|
||||
export interface CreateDevServerCallback {
|
||||
(error: any, result: { Port: number, PublicPath: string }): void;
|
||||
}
|
||||
|
||||
// These are the options passed by WebpackDevMiddleware.cs
|
||||
interface CreateDevServerOptions {
|
||||
webpackConfigPath: string;
|
||||
suppliedOptions: DevServerOptions;
|
||||
}
|
||||
|
||||
// These are the options configured in C# and then JSON-serialized, hence the C#-style naming
|
||||
interface DevServerOptions {
|
||||
HotModuleReplacement: boolean;
|
||||
ReactHotModuleReplacement: boolean;
|
||||
}
|
||||
|
||||
export function createWebpackDevServer(callback: CreateDevServerCallback, optionsJson: string) {
|
||||
const options: CreateDevServerOptions = JSON.parse(optionsJson);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
const enableHotModuleReplacement = options.suppliedOptions.HotModuleReplacement;
|
||||
const enableReactHotModuleReplacement = options.suppliedOptions.ReactHotModuleReplacement;
|
||||
if (enableReactHotModuleReplacement && !enableHotModuleReplacement) {
|
||||
callback('To use ReactHotModuleReplacement, you must also enable the HotModuleReplacement option.', null);
|
||||
return;
|
||||
}
|
||||
|
||||
const app = connect();
|
||||
const defaultPort = 0; // 0 means 'choose randomly'. Could allow an explicit value to be supplied instead.
|
||||
const listener = app.listen(defaultPort, () => {
|
||||
// Build the final Webpack config based on supplied options
|
||||
if (enableHotModuleReplacement) {
|
||||
// TODO: Stop assuming there's an entry point called 'main'
|
||||
if (typeof webpackConfig.entry['main'] === 'string') {
|
||||
webpackConfig.entry['main'] = ['webpack-hot-middleware/client', webpackConfig.entry['main']];
|
||||
} else {
|
||||
webpackConfig.entry['main'].unshift('webpack-hot-middleware/client');
|
||||
}
|
||||
webpackConfig.plugins.push(
|
||||
new webpack.HotModuleReplacementPlugin()
|
||||
);
|
||||
|
||||
// Set up React HMR support if requested. This requires the 'aspnet-webpack-react' package.
|
||||
if (enableReactHotModuleReplacement) {
|
||||
let aspNetWebpackReactModule: any;
|
||||
try {
|
||||
aspNetWebpackReactModule = require('aspnet-webpack-react');
|
||||
} catch(ex) {
|
||||
callback('To use ReactHotModuleReplacement, you must install the NPM package \'aspnet-webpack-react\'.', null);
|
||||
return;
|
||||
}
|
||||
|
||||
aspNetWebpackReactModule.addReactHotModuleReplacementBabelTransform(webpackConfig);
|
||||
}
|
||||
}
|
||||
|
||||
// Attach Webpack dev middleware and optional 'hot' middleware
|
||||
const compiler = webpack(webpackConfig);
|
||||
app.use(require('webpack-dev-middleware')(compiler, {
|
||||
noInfo: true,
|
||||
publicPath: publicPath
|
||||
}));
|
||||
|
||||
if (enableHotModuleReplacement) {
|
||||
let webpackHotMiddlewareModule;
|
||||
try {
|
||||
webpackHotMiddlewareModule = require('webpack-hot-middleware');
|
||||
} catch (ex) {
|
||||
callback('To use HotModuleReplacement, you must install the NPM package \'webpack-hot-middleware\'.', null);
|
||||
return;
|
||||
}
|
||||
app.use(webpackHotMiddlewareModule(compiler));
|
||||
}
|
||||
|
||||
// Tell the ASP.NET app what addresses we're listening on, so that it can proxy requests here
|
||||
callback(null, {
|
||||
Port: listener.address().port,
|
||||
PublicPath: removeTrailingSlash(publicPath)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function removeTrailingSlash(str: string) {
|
||||
if (str.lastIndexOf('/') === str.length - 1) {
|
||||
str = str.substring(0, str.length - 1);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export { createWebpackDevServer } from './WebpackDevMiddleware';
|
||||
export { loadViaWebpack } from './LoadViaWebpack';
|
||||
3
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/typings/memory-fs.d.ts
vendored
Normal file
3
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/typings/memory-fs.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export namespace memoryfs {
|
||||
export class MemoryFS {}
|
||||
}
|
||||
3
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/typings/require-from-string.d.ts
vendored
Normal file
3
src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/typings/require-from-string.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export namespace requirefromstring {
|
||||
export function requireFromString<T>(fileContent: string): T;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import * as webpack from 'webpack';
|
||||
|
||||
export namespace webpackexternals {
|
||||
export interface ExternalsPluginOptions {
|
||||
type: string;
|
||||
include: webpack.LoaderCondition;
|
||||
}
|
||||
|
||||
export class ExternalsPlugin {
|
||||
constructor(options: ExternalsPluginOptions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "node",
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"declaration": true,
|
||||
"outDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"version": "v4",
|
||||
"repo": "borisyankov/DefinitelyTyped",
|
||||
"ref": "master",
|
||||
"path": "typings",
|
||||
"bundle": "typings/tsd.d.ts",
|
||||
"installed": {
|
||||
"webpack/webpack.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"node/node.d.ts": {
|
||||
"commit": "e937b3e64af586d19f2ea29fdf771e9dc4feecc8"
|
||||
},
|
||||
"source-map/source-map.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"uglify-js/uglify-js.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"es6-promise/es6-promise.d.ts": {
|
||||
"commit": "0144ad5a74053f2292424847259c4c8e1d0fecaa"
|
||||
},
|
||||
"connect/connect.d.ts": {
|
||||
"commit": "e937b3e64af586d19f2ea29fdf771e9dc4feecc8"
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/Microsoft.AspNetCore.SpaServices/npm/domain-task/.gitignore
vendored
Normal file
4
src/Microsoft.AspNetCore.SpaServices/npm/domain-task/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/typings/
|
||||
/node_modules/
|
||||
/*.js
|
||||
/*.d.ts
|
||||
@@ -0,0 +1,3 @@
|
||||
!/*.js
|
||||
!/*.d.ts
|
||||
/typings/
|
||||
@@ -0,0 +1,12 @@
|
||||
Copyright (c) .NET Foundation. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
||||
these files except in compliance with the License. You may obtain a copy of the
|
||||
License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed
|
||||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
@@ -0,0 +1 @@
|
||||
TODO
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "domain-task",
|
||||
"version": "1.0.1",
|
||||
"description": "Tracks outstanding operations for a logical thread of execution",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"prepublish": "tsd update && tsc && echo 'Finished building NPM package \"domain-task\"'",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Microsoft",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"domain-context": "^0.5.1",
|
||||
"isomorphic-fetch": "^2.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^1.8.10"
|
||||
}
|
||||
}
|
||||
9
src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/domain-context.d.ts
vendored
Normal file
9
src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/domain-context.d.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
declare module 'domain' {
|
||||
var active: Domain;
|
||||
}
|
||||
|
||||
declare module 'domain-context' {
|
||||
function get(key: string): any;
|
||||
function set(key: string, value: any): void;
|
||||
function runInNewDomain(code: () => void): void;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import * as url from 'url';
|
||||
import * as domain from 'domain';
|
||||
import * as domainContext from 'domain-context';
|
||||
import { addTask } from './main';
|
||||
const isomorphicFetch = require('isomorphic-fetch');
|
||||
const isBrowser: boolean = (new Function('try { return this === window; } catch (e) { return false; }'))();
|
||||
|
||||
// Not using a symbol, because this may need to run in a version of Node.js that doesn't support them
|
||||
const domainTaskStateKey = '__DOMAIN_TASK_INTERNAL_FETCH_BASEURL__DO_NOT_REFERENCE_THIS__';
|
||||
let noDomainBaseUrl: string;
|
||||
|
||||
function issueRequest(baseUrl: string, req: string | Request, init?: RequestInit): Promise<any> {
|
||||
// Resolve relative URLs
|
||||
if (baseUrl) {
|
||||
if (req instanceof Request) {
|
||||
const reqAsRequest = req as Request;
|
||||
reqAsRequest.url = url.resolve(baseUrl, reqAsRequest.url);
|
||||
} else {
|
||||
req = url.resolve(baseUrl, req as string);
|
||||
}
|
||||
} else if (!isBrowser) {
|
||||
// TODO: Consider only throwing if it's a relative URL, since absolute ones would work fine
|
||||
throw new Error(`
|
||||
When running outside the browser (e.g., in Node.js), you must specify a base URL
|
||||
before invoking domain-task's 'fetch' wrapper.
|
||||
Example:
|
||||
import { baseUrl } from 'domain-task/fetch';
|
||||
baseUrl('http://example.com'); // Relative URLs will be resolved against this
|
||||
`);
|
||||
}
|
||||
|
||||
// Currently, some part of ASP.NET (perhaps just Kestrel on Mac - unconfirmed) doesn't complete
|
||||
// its responses if we send 'Connection: close', which is the default. So if no 'Connection' header
|
||||
// has been specified explicitly, use 'Connection: keep-alive'.
|
||||
init = init || {};
|
||||
init.headers = init.headers || {};
|
||||
if (!init.headers['Connection']) {
|
||||
init.headers['Connection'] = 'keep-alive';
|
||||
}
|
||||
|
||||
return isomorphicFetch(req, init);
|
||||
}
|
||||
|
||||
export function fetch(url: string | Request, init?: RequestInit): Promise<any> {
|
||||
const promise = issueRequest(baseUrl(), url, init);
|
||||
addTask(promise);
|
||||
return promise;
|
||||
}
|
||||
|
||||
export function baseUrl(url?: string): string {
|
||||
if (url) {
|
||||
if (domain.active) {
|
||||
// There's an active domain (e.g., in Node.js), so associate the base URL with it
|
||||
domainContext.set(domainTaskStateKey, url);
|
||||
} else {
|
||||
// There's no active domain (e.g., in browser), so there's just one shared base URL
|
||||
noDomainBaseUrl = url;
|
||||
}
|
||||
}
|
||||
|
||||
return domain.active ? domainContext.get(domainTaskStateKey) : noDomainBaseUrl;
|
||||
}
|
||||
4
src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/isomorphic-fetch.d.ts
vendored
Normal file
4
src/Microsoft.AspNetCore.SpaServices/npm/domain-task/src/isomorphic-fetch.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
declare module 'isomorphic-fetch' {
|
||||
var fetch: (url: string | Request, init?: RequestInit) => Promise<any>;
|
||||
export default fetch;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import * as domain from 'domain';
|
||||
import * as domainContext from 'domain-context';
|
||||
const domainTasksStateKey = '__DOMAIN_TASKS';
|
||||
|
||||
export function addTask(task: PromiseLike<any>) {
|
||||
if (task && domain.active) {
|
||||
const state = domainContext.get(domainTasksStateKey) as DomainTasksState;
|
||||
if (state) {
|
||||
state.numRemainingTasks++;
|
||||
task.then(() => {
|
||||
// The application may have other listeners chained to this promise *after*
|
||||
// this listener, which may in turn register further tasks. Since we don't
|
||||
// want the combined task to complete until all the handlers for child tasks
|
||||
// have finished, delay the response to give time for more tasks to be added
|
||||
// synchronously.
|
||||
setTimeout(() => {
|
||||
state.numRemainingTasks--;
|
||||
if (state.numRemainingTasks === 0 && !state.hasIssuedSuccessCallback) {
|
||||
state.hasIssuedSuccessCallback = true;
|
||||
setTimeout(() => {
|
||||
state.completionCallback(/* error */ null);
|
||||
}, 0);
|
||||
}
|
||||
}, 0);
|
||||
}, (error) => {
|
||||
state.completionCallback(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function run<T>(codeToRun: () => T, completionCallback: (error: any) => void): T {
|
||||
let synchronousResult: T;
|
||||
domainContext.runInNewDomain(() => {
|
||||
const state: DomainTasksState = {
|
||||
numRemainingTasks: 0,
|
||||
hasIssuedSuccessCallback: false,
|
||||
completionCallback: domain.active.bind(completionCallback)
|
||||
};
|
||||
|
||||
try {
|
||||
domainContext.set(domainTasksStateKey, state);
|
||||
synchronousResult = codeToRun();
|
||||
|
||||
// If no tasks were registered synchronously, then we're done already
|
||||
if (state.numRemainingTasks === 0 && !state.hasIssuedSuccessCallback) {
|
||||
state.hasIssuedSuccessCallback = true;
|
||||
setTimeout(() => {
|
||||
state.completionCallback(/* error */ null);
|
||||
}, 0);
|
||||
}
|
||||
} catch(ex) {
|
||||
state.completionCallback(ex);
|
||||
}
|
||||
});
|
||||
|
||||
return synchronousResult;
|
||||
}
|
||||
|
||||
interface DomainTasksState {
|
||||
numRemainingTasks: number;
|
||||
hasIssuedSuccessCallback: boolean;
|
||||
completionCallback: (error: any) => void;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "node",
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"declaration": true,
|
||||
"outDir": "."
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"version": "v4",
|
||||
"repo": "borisyankov/DefinitelyTyped",
|
||||
"ref": "master",
|
||||
"path": "typings",
|
||||
"bundle": "typings/tsd.d.ts",
|
||||
"installed": {
|
||||
"node/node.d.ts": {
|
||||
"commit": "3030a4be536b6530c06b80081f1333dc0de4d703"
|
||||
},
|
||||
"es6-promise/es6-promise.d.ts": {
|
||||
"commit": "3030a4be536b6530c06b80081f1333dc0de4d703"
|
||||
},
|
||||
"whatwg-fetch/whatwg-fetch.d.ts": {
|
||||
"commit": "3030a4be536b6530c06b80081f1333dc0de4d703"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user