Skip to content

Commit 43f7ea5

Browse files
author
chenghao
committed
初始化admin模板
1 parent 517a160 commit 43f7ea5

19 files changed

+1306
-0
lines changed

config/env.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
4+
// injected into the application via DefinePlugin in Webpack configuration.
5+
6+
var REACT_APP = /^REACT_APP_/i;
7+
8+
function getClientEnvironment(publicUrl) {
9+
var raw = Object
10+
.keys(process.env)
11+
.filter(key => REACT_APP.test(key))
12+
.reduce((env, key) => {
13+
env[key] = process.env[key];
14+
return env;
15+
}, {
16+
// Useful for determining whether we’re running in production mode.
17+
// Most importantly, it switches React into the correct mode.
18+
'NODE_ENV': process.env.NODE_ENV || 'development',
19+
// Useful for resolving the correct path to static assets in `public`.
20+
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
21+
// This should only be used as an escape hatch. Normally you would put
22+
// images into the `src` and `import` them in code to get their paths.
23+
'PUBLIC_URL': publicUrl
24+
});
25+
// Stringify all values so we can feed into Webpack DefinePlugin
26+
var stringified = {
27+
'process.env': Object
28+
.keys(raw)
29+
.reduce((env, key) => {
30+
env[key] = JSON.stringify(raw[key]);
31+
return env;
32+
}, {})
33+
};
34+
35+
return { raw, stringified };
36+
}
37+
38+
module.exports = getClientEnvironment;

config/jest/cssTransform.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
// This is a custom Jest transformer turning style imports into empty objects.
4+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
5+
6+
module.exports = {
7+
process() {
8+
return 'module.exports = {};';
9+
},
10+
getCacheKey() {
11+
// The output is always the same.
12+
return 'cssTransform';
13+
},
14+
};

config/jest/fileTransform.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
// This is a custom Jest transformer turning file imports into filenames.
6+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
7+
8+
module.exports = {
9+
process(src, filename) {
10+
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
11+
},
12+
};

config/paths.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var fs = require('fs');
5+
var url = require('url');
6+
7+
// Make sure any symlinks in the project folder are resolved:
8+
// https://github.com/facebookincubator/create-react-app/issues/637
9+
var appDirectory = fs.realpathSync(process.cwd());
10+
function resolveApp(relativePath) {
11+
return path.resolve(appDirectory, relativePath);
12+
}
13+
14+
// We support resolving modules according to `NODE_PATH`.
15+
// This lets you use absolute paths in imports inside large monorepos:
16+
// https://github.com/facebookincubator/create-react-app/issues/253.
17+
18+
// It works similar to `NODE_PATH` in Node itself:
19+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
20+
21+
// We will export `nodePaths` as an array of absolute paths.
22+
// It will then be used by Webpack configs.
23+
// Jest doesn’t need this because it already handles `NODE_PATH` out of the box.
24+
25+
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
26+
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
27+
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
28+
29+
var nodePaths = (process.env.NODE_PATH || '')
30+
.split(process.platform === 'win32' ? ';' : ':')
31+
.filter(Boolean)
32+
.filter(folder => !path.isAbsolute(folder))
33+
.map(resolveApp);
34+
35+
var envPublicUrl = process.env.PUBLIC_URL;
36+
37+
function ensureSlash(path, needsSlash) {
38+
var hasSlash = path.endsWith('/');
39+
if (hasSlash && !needsSlash) {
40+
return path.substr(path, path.length - 1);
41+
} else if (!hasSlash && needsSlash) {
42+
return path + '/';
43+
} else {
44+
return path;
45+
}
46+
}
47+
48+
function getPublicUrl(appPackageJson) {
49+
return envPublicUrl || require(appPackageJson).homepage;
50+
}
51+
52+
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
53+
// "public path" at which the app is served.
54+
// Webpack needs to know it to put the right <script> hrefs into HTML even in
55+
// single-page apps that may serve index.html for nested URLs like /todos/42.
56+
// We can't use a relative path in HTML because we don't want to load something
57+
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
58+
function getServedPath(appPackageJson) {
59+
var publicUrl = getPublicUrl(appPackageJson);
60+
var servedUrl = envPublicUrl || (
61+
publicUrl ? url.parse(publicUrl).pathname : '/'
62+
);
63+
return ensureSlash(servedUrl, true);
64+
}
65+
66+
// config after eject: we're in ./config/
67+
module.exports = {
68+
appBuild: resolveApp('build'),
69+
appPublic: resolveApp('public'),
70+
appHtml: resolveApp('public/index.html'),
71+
appIndexJs: resolveApp('src/index.js'),
72+
appPackageJson: resolveApp('package.json'),
73+
appSrc: resolveApp('src'),
74+
yarnLockFile: resolveApp('yarn.lock'),
75+
testsSetup: resolveApp('src/setupTests.js'),
76+
appNodeModules: resolveApp('node_modules'),
77+
nodePaths: nodePaths,
78+
publicUrl: getPublicUrl(resolveApp('package.json')),
79+
servedPath: getServedPath(resolveApp('package.json'))
80+
};

config/polyfills.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
if (typeof Promise === 'undefined') {
4+
// Rejection tracking prevents a common issue where React gets into an
5+
// inconsistent state due to an error, but it gets swallowed by a Promise,
6+
// and the user has no idea what causes React's erratic future behavior.
7+
require('promise/lib/rejection-tracking').enable();
8+
window.Promise = require('promise/lib/es6-extensions.js');
9+
}
10+
11+
// fetch() polyfill for making API calls.
12+
require('whatwg-fetch');
13+
14+
// Object.assign() is commonly used with React.
15+
// It will use the native implementation if it's present and isn't buggy.
16+
Object.assign = require('object-assign');

config/webpack.config.dev.js

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
'use strict';
2+
3+
var autoprefixer = require('autoprefixer');
4+
var webpack = require('webpack');
5+
var HtmlWebpackPlugin = require('html-webpack-plugin');
6+
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
7+
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
8+
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
9+
var getClientEnvironment = require('./env');
10+
var paths = require('./paths');
11+
12+
13+
14+
// Webpack uses `publicPath` to determine where the app is being served from.
15+
// In development, we always serve from the root. This makes config easier.
16+
var publicPath = '/';
17+
// `publicUrl` is just like `publicPath`, but we will provide it to our app
18+
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
19+
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
20+
var publicUrl = '';
21+
// Get environment variables to inject into our app.
22+
var env = getClientEnvironment(publicUrl);
23+
24+
// This is the development configuration.
25+
// It is focused on developer experience and fast rebuilds.
26+
// The production configuration is different and lives in a separate file.
27+
module.exports = {
28+
// You may want 'eval' instead if you prefer to see the compiled output in DevTools.
29+
// See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
30+
devtool: 'cheap-module-source-map',
31+
// These are the "entry points" to our application.
32+
// This means they will be the "root" imports that are included in JS bundle.
33+
// The first two entry points enable "hot" CSS and auto-refreshes for JS.
34+
entry: [
35+
// Include an alternative client for WebpackDevServer. A client's job is to
36+
// connect to WebpackDevServer by a socket and get notified about changes.
37+
// When you save a file, the client will either apply hot updates (in case
38+
// of CSS changes), or refresh the page (in case of JS changes). When you
39+
// make a syntax error, this client will display a syntax error overlay.
40+
// Note: instead of the default WebpackDevServer client, we use a custom one
41+
// to bring better experience for Create React App users. You can replace
42+
// the line below with these two lines if you prefer the stock client:
43+
// require.resolve('webpack-dev-server/client') + '?/',
44+
// require.resolve('webpack/hot/dev-server'),
45+
require.resolve('react-dev-utils/webpackHotDevClient'),
46+
// We ship a few polyfills by default:
47+
require.resolve('./polyfills'),
48+
// Finally, this is your app's code:
49+
paths.appIndexJs
50+
// We include the app code last so that if there is a runtime error during
51+
// initialization, it doesn't blow up the WebpackDevServer client, and
52+
// changing JS code would still trigger a refresh.
53+
],
54+
output: {
55+
// Next line is not used in dev but WebpackDevServer crashes without it:
56+
path: paths.appBuild,
57+
// Add /* filename */ comments to generated require()s in the output.
58+
pathinfo: true,
59+
// This does not produce a real file. It's just the virtual path that is
60+
// served by WebpackDevServer in development. This is the JS bundle
61+
// containing code from all our entry points, and the Webpack runtime.
62+
filename: 'static/js/bundle.js',
63+
// This is the URL that app is served from. We use "/" in development.
64+
publicPath: publicPath
65+
},
66+
resolve: {
67+
// This allows you to set a fallback for where Webpack should look for modules.
68+
// We read `NODE_PATH` environment variable in `paths.js` and pass paths here.
69+
// We use `fallback` instead of `root` because we want `node_modules` to "win"
70+
// if there any conflicts. This matches Node resolution mechanism.
71+
// https://github.com/facebookincubator/create-react-app/issues/253
72+
fallback: paths.nodePaths,
73+
// These are the reasonable defaults supported by the Node ecosystem.
74+
// We also include JSX as a common component filename extension to support
75+
// some tools, although we do not recommend using it, see:
76+
// https://github.com/facebookincubator/create-react-app/issues/290
77+
extensions: ['.js', '.json', '.jsx', ''],
78+
alias: {
79+
// Support React Native Web
80+
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
81+
'react-native': 'react-native-web'
82+
}
83+
},
84+
85+
module: {
86+
// First, run the linter.
87+
// It's important to do this before Babel processes the JS.
88+
preLoaders: [
89+
{
90+
test: /\.(js|jsx)$/,
91+
loader: 'eslint',
92+
include: paths.appSrc,
93+
}
94+
],
95+
loaders: [
96+
// ** ADDING/UPDATING LOADERS **
97+
// The "url" loader handles all assets unless explicitly excluded.
98+
// The `exclude` list *must* be updated with every change to loader extensions.
99+
// When adding a new loader, you must add its `test`
100+
// as a new entry in the `exclude` list for "url" loader.
101+
102+
// "url" loader embeds assets smaller than specified size as data URLs to avoid requests.
103+
// Otherwise, it acts like the "file" loader.
104+
{
105+
exclude: [
106+
/\.html$/,
107+
// We have to write /\.(js|jsx)(\?.*)?$/ rather than just /\.(js|jsx)$/
108+
// because you might change the hot reloading server from the custom one
109+
// to Webpack's built-in webpack-dev-server/client?/, which would not
110+
// get properly excluded by /\.(js|jsx)$/ because of the query string.
111+
// Webpack 2 fixes this, but for now we include this hack.
112+
// https://github.com/facebookincubator/create-react-app/issues/1713
113+
/\.(js|jsx)(\?.*)?$/,
114+
/\.css$/,
115+
/\.json$/,
116+
/\.svg$/
117+
],
118+
loader: 'url',
119+
query: {
120+
limit: 10000,
121+
name: 'static/media/[name].[hash:8].[ext]'
122+
}
123+
},
124+
// Process JS with Babel.
125+
{
126+
test: /\.(js|jsx)$/,
127+
include: paths.appSrc,
128+
loader: 'babel',
129+
query: {
130+
131+
// This is a feature of `babel-loader` for webpack (not Babel itself).
132+
// It enables caching results in ./node_modules/.cache/babel-loader/
133+
// directory for faster rebuilds.
134+
cacheDirectory: true
135+
}
136+
},
137+
// "postcss" loader applies autoprefixer to our CSS.
138+
// "css" loader resolves paths in CSS and adds assets as dependencies.
139+
// "style" loader turns CSS into JS modules that inject <style> tags.
140+
// In production, we use a plugin to extract that CSS to a file, but
141+
// in development "style" loader enables hot editing of CSS.
142+
{
143+
test: /\.css$/,
144+
loader: 'style!css?importLoaders=1!postcss'
145+
},
146+
// JSON is not enabled by default in Webpack but both Node and Browserify
147+
// allow it implicitly so we also enable it.
148+
{
149+
test: /\.json$/,
150+
loader: 'json'
151+
},
152+
// "file" loader for svg
153+
{
154+
test: /\.svg$/,
155+
loader: 'file',
156+
query: {
157+
name: 'static/media/[name].[hash:8].[ext]'
158+
}
159+
}
160+
// ** STOP ** Are you adding a new loader?
161+
// Remember to add the new extension(s) to the "url" loader exclusion list.
162+
]
163+
},
164+
165+
// We use PostCSS for autoprefixing only.
166+
postcss: function() {
167+
return [
168+
autoprefixer({
169+
browsers: [
170+
'>1%',
171+
'last 4 versions',
172+
'Firefox ESR',
173+
'not ie < 9', // React doesn't support IE8 anyway
174+
]
175+
}),
176+
];
177+
},
178+
plugins: [
179+
// Makes some environment variables available in index.html.
180+
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
181+
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
182+
// In development, this will be an empty string.
183+
new InterpolateHtmlPlugin(env.raw),
184+
// Generates an `index.html` file with the <script> injected.
185+
new HtmlWebpackPlugin({
186+
inject: true,
187+
template: paths.appHtml,
188+
}),
189+
// Makes some environment variables available to the JS code, for example:
190+
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
191+
new webpack.DefinePlugin(env.stringified),
192+
// This is necessary to emit hot updates (currently CSS only):
193+
new webpack.HotModuleReplacementPlugin(),
194+
// Watcher doesn't work well if you mistype casing in a path so we use
195+
// a plugin that prints an error when you attempt to do this.
196+
// See https://github.com/facebookincubator/create-react-app/issues/240
197+
new CaseSensitivePathsPlugin(),
198+
// If you require a missing module and then `npm install` it, you still have
199+
// to restart the development server for Webpack to discover it. This plugin
200+
// makes the discovery automatic so you don't have to restart.
201+
// See https://github.com/facebookincubator/create-react-app/issues/186
202+
new WatchMissingNodeModulesPlugin(paths.appNodeModules)
203+
],
204+
// Some libraries import Node modules but don't use them in the browser.
205+
// Tell Webpack to provide empty mocks for them so importing them works.
206+
node: {
207+
fs: 'empty',
208+
net: 'empty',
209+
tls: 'empty'
210+
}
211+
};

0 commit comments

Comments
 (0)