Skip to content

Commit ccc466a

Browse files
[example] Add preact-next example (#15401)
1 parent e9242f7 commit ccc466a

File tree

20 files changed

+1625
-2
lines changed

20 files changed

+1625
-2
lines changed

.eslintignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/coverage
44
/docs/export
55
/examples/create-react-app*/src/serviceWorker.js
6-
/examples/create-react-app-with-flow/flow
7-
/examples/create-react-app-with-flow/flow-typed
6+
/examples/preact-next/config
7+
/examples/preact-next/scripts
88
/examples/gatsby/public
99
/packages/babel-plugin-unwrap-createStyles/test/__fixtures__/
1010
/packages/material-ui-codemod/lib

examples/preact-next/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# IDEs and editors
15+
/.idea
16+
/.vscode
17+
18+
# misc
19+
.DS_Store
20+
.env.local
21+
.env.development.local
22+
.env.test.local
23+
.env.production.local
24+
25+
npm-debug.log*
26+
yarn-debug.log*
27+
yarn-error.log*

examples/preact-next/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Preact v4-alpha example
2+
3+
## How to use
4+
5+
Download the example [or clone the repo](https://github.com/mui-org/material-ui):
6+
7+
```sh
8+
curl https://codeload.github.com/mui-org/material-ui/tar.gz/next | tar -xz --strip=2 material-ui-next/examples/preact-next
9+
cd preact-next
10+
```
11+
12+
Install it and run:
13+
14+
```sh
15+
npm install
16+
npm run start
17+
```
18+
19+
## The idea behind the example
20+
21+
[Preact](https://github.com/developit/preact) is a fast 3kB alternative to React with the same modern API.
22+
23+
This example uses an ejected version of CRA.
24+
It's ejected to change the webpack configuration:
25+
26+
```js
27+
alias: {
28+
// Use Preact instead of React.
29+
'react': 'preact/compat',
30+
'react-dom': 'preact/compat',
31+
}
32+
```

examples/preact-next/config/env.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const paths = require('./paths');
6+
7+
// Make sure that including paths.js after env.js will read .env variables.
8+
delete require.cache[require.resolve('./paths')];
9+
10+
const NODE_ENV = process.env.NODE_ENV;
11+
if (!NODE_ENV) {
12+
throw new Error('The NODE_ENV environment variable is required but was not specified.');
13+
}
14+
15+
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
16+
var dotenvFiles = [
17+
`${paths.dotenv}.${NODE_ENV}.local`,
18+
`${paths.dotenv}.${NODE_ENV}`,
19+
// Don't include `.env.local` for `test` environment
20+
// since normally you expect tests to produce the same
21+
// results for everyone
22+
NODE_ENV !== 'test' && `${paths.dotenv}.local`,
23+
paths.dotenv,
24+
].filter(Boolean);
25+
26+
// Load environment variables from .env* files. Suppress warnings using silent
27+
// if this file is missing. dotenv will never modify any environment variables
28+
// that have already been set. Variable expansion is supported in .env files.
29+
// https://github.com/motdotla/dotenv
30+
// https://github.com/motdotla/dotenv-expand
31+
dotenvFiles.forEach(dotenvFile => {
32+
if (fs.existsSync(dotenvFile)) {
33+
require('dotenv-expand')(
34+
require('dotenv').config({
35+
path: dotenvFile,
36+
}),
37+
);
38+
}
39+
});
40+
41+
// We support resolving modules according to `NODE_PATH`.
42+
// This lets you use absolute paths in imports inside large monorepos:
43+
// https://github.com/facebook/create-react-app/issues/253.
44+
// It works similar to `NODE_PATH` in Node itself:
45+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
46+
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
47+
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
48+
// https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
49+
// We also resolve them to make sure all tools using them work consistently.
50+
const appDirectory = fs.realpathSync(process.cwd());
51+
process.env.NODE_PATH = (process.env.NODE_PATH || '')
52+
.split(path.delimiter)
53+
.filter(folder => folder && !path.isAbsolute(folder))
54+
.map(folder => path.resolve(appDirectory, folder))
55+
.join(path.delimiter);
56+
57+
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
58+
// injected into the application via DefinePlugin in Webpack configuration.
59+
const REACT_APP = /^REACT_APP_/i;
60+
61+
function getClientEnvironment(publicUrl) {
62+
const raw = Object.keys(process.env)
63+
.filter(key => REACT_APP.test(key))
64+
.reduce(
65+
(env, key) => {
66+
env[key] = process.env[key];
67+
return env;
68+
},
69+
{
70+
// Useful for determining whether we’re running in production mode.
71+
// Most importantly, it switches React into the correct mode.
72+
NODE_ENV: process.env.NODE_ENV || 'development',
73+
// Useful for resolving the correct path to static assets in `public`.
74+
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
75+
// This should only be used as an escape hatch. Normally you would put
76+
// images into the `src` and `import` them in code to get their paths.
77+
PUBLIC_URL: publicUrl,
78+
},
79+
);
80+
// Stringify all values so we can feed into Webpack DefinePlugin
81+
const stringified = {
82+
'process.env': Object.keys(raw).reduce((env, key) => {
83+
env[key] = JSON.stringify(raw[key]);
84+
return env;
85+
}, {}),
86+
};
87+
88+
return { raw, stringified };
89+
}
90+
91+
module.exports = getClientEnvironment;
Lines changed: 14 additions & 0 deletions
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/en/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+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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/en/webpack.html
7+
8+
module.exports = {
9+
process(src, filename) {
10+
const assetFilename = JSON.stringify(path.basename(filename));
11+
12+
if (filename.match(/\.svg$/)) {
13+
return `const React = require('react');
14+
module.exports = {
15+
__esModule: true,
16+
default: ${assetFilename},
17+
ReactComponent: React.forwardRef((props, ref) => ({
18+
$$typeof: Symbol.for('react.element'),
19+
type: 'svg',
20+
ref: ref,
21+
key: null,
22+
props: Object.assign({}, props, {
23+
children: ${assetFilename}
24+
})
25+
})),
26+
};`;
27+
}
28+
29+
return `module.exports = ${assetFilename};`;
30+
},
31+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const fs = require('fs');
5+
const url = require('url');
6+
7+
// Make sure any symlinks in the project folder are resolved:
8+
// https://github.com/facebook/create-react-app/issues/637
9+
const appDirectory = fs.realpathSync(process.cwd());
10+
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
11+
12+
const envPublicUrl = process.env.PUBLIC_URL;
13+
14+
function ensureSlash(inputPath, needsSlash) {
15+
const hasSlash = inputPath.endsWith('/');
16+
if (hasSlash && !needsSlash) {
17+
return inputPath.substr(0, inputPath.length - 1);
18+
} else if (!hasSlash && needsSlash) {
19+
return `${inputPath}/`;
20+
} else {
21+
return inputPath;
22+
}
23+
}
24+
25+
const getPublicUrl = appPackageJson => envPublicUrl || require(appPackageJson).homepage;
26+
27+
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
28+
// "public path" at which the app is served.
29+
// Webpack needs to know it to put the right <script> hrefs into HTML even in
30+
// single-page apps that may serve index.html for nested URLs like /todos/42.
31+
// We can't use a relative path in HTML because we don't want to load something
32+
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
33+
function getServedPath(appPackageJson) {
34+
const publicUrl = getPublicUrl(appPackageJson);
35+
const servedUrl = envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
36+
return ensureSlash(servedUrl, true);
37+
}
38+
39+
const moduleFileExtensions = [
40+
'web.mjs',
41+
'mjs',
42+
'web.js',
43+
'js',
44+
'web.ts',
45+
'ts',
46+
'web.tsx',
47+
'tsx',
48+
'json',
49+
'web.jsx',
50+
'jsx',
51+
];
52+
53+
// Resolve file paths in the same order as webpack
54+
const resolveModule = (resolveFn, filePath) => {
55+
const extension = moduleFileExtensions.find(extension =>
56+
fs.existsSync(resolveFn(`${filePath}.${extension}`)),
57+
);
58+
59+
if (extension) {
60+
return resolveFn(`${filePath}.${extension}`);
61+
}
62+
63+
return resolveFn(`${filePath}.js`);
64+
};
65+
66+
// config after eject: we're in ./config/
67+
module.exports = {
68+
dotenv: resolveApp('.env'),
69+
appPath: resolveApp('.'),
70+
appBuild: resolveApp('build'),
71+
appPublic: resolveApp('public'),
72+
appHtml: resolveApp('public/index.html'),
73+
appIndexJs: resolveModule(resolveApp, 'src/index'),
74+
appPackageJson: resolveApp('package.json'),
75+
appSrc: resolveApp('src'),
76+
appTsConfig: resolveApp('tsconfig.json'),
77+
yarnLockFile: resolveApp('yarn.lock'),
78+
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
79+
proxySetup: resolveApp('src/setupProxy.js'),
80+
appNodeModules: resolveApp('node_modules'),
81+
publicUrl: getPublicUrl(resolveApp('package.json')),
82+
servedPath: getServedPath(resolveApp('package.json')),
83+
};
84+
85+
module.exports.moduleFileExtensions = moduleFileExtensions;

0 commit comments

Comments
 (0)