Skip to content

Commit e9101b5

Browse files
committed
output library to lib folder and improve lib script
1 parent 8fc4f4f commit e9101b5

File tree

4 files changed

+14
-27
lines changed

4 files changed

+14
-27
lines changed

packages/react-scripts/config/paths.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function getServedPath(appPackageJson) {
5050
module.exports = {
5151
dotenv: resolveApp('.env'),
5252
appBuild: resolveApp('build'),
53+
appLib: resolveApp('lib'),
5354
appPublic: resolveApp('public'),
5455
appHtml: resolveApp('demo/index.html'),
5556
appIndexJs: resolveApp('src/index.jsx'),
@@ -72,6 +73,7 @@ module.exports = {
7273
dotenv: resolveApp('.env'),
7374
appPath: resolveApp('.'),
7475
appBuild: resolveApp('build'),
76+
appLib: resolveApp('lib'),
7577
appPublic: resolveApp('public'),
7678
appHtml: resolveApp('demo/index.html'),
7779
appIndexJs: resolveApp('src/index.jsx'),
@@ -104,6 +106,7 @@ if (
104106
dotenv: resolveOwn('template/.env'),
105107
appPath: resolveApp('.'),
106108
appBuild: resolveOwn('../../build'),
109+
appLib: resolveOwn('../../lib'),
107110
appPublic: resolveOwn('template/public'),
108111
appHtml: resolveOwn('template/demo/index.html'),
109112
appIndexJs: resolveOwn('template/src/index.jsx'),

packages/react-scripts/config/webpack.config.library.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const publicPath = paths.servedPath;
2323
// Some apps do not use client-side routing with pushState.
2424
// For these, "homepage" can be set to "." to enable relative asset paths.
2525
const shouldUseRelativeAssetPaths = publicPath === './';
26-
// Source maps are resource heavy and can cause out of memory issue for large source files.
27-
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
2826
// `publicUrl` is just like `publicPath`, but we will provide it to our app
2927
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
3028
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
@@ -39,7 +37,7 @@ if (env.stringified['process.env'].NODE_ENV !== '"production"') {
3937
}
4038

4139
// Note: defined here because it will be used more than once.
42-
const cssFilename = 'static/css/[name].[contenthash:8].css';
40+
const cssFilename = 'styles.css';
4341

4442
// ExtractTextPlugin expects the build output to be flat.
4543
// (See https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/27)
@@ -81,13 +79,13 @@ module.exports = {
8179
bail: true,
8280
// We generate sourcemaps in production. This is slow but gives good results.
8381
// You can exclude the *.map files from the build during deployment.
84-
devtool: shouldUseSourceMap ? 'source-map' : false,
82+
devtool: false,
8583
// In production, we only want to load the polyfills and the app code.
8684
entry: [paths.appIndexJs],
8785
target: 'node',
8886
output: {
8987
// The build folder.
90-
path: paths.appBuild,
88+
path: paths.appLib,
9189
// Generated JS file names (with nested folders).
9290
// There will be one main bundle, and one file per asynchronous chunk.
9391
// We don't currently advertise code splitting but Webpack supports it.
@@ -184,7 +182,7 @@ module.exports = {
184182
loader: require.resolve('url-loader'),
185183
options: {
186184
limit: 10000,
187-
name: 'static/media/[name].[hash:8].[ext]',
185+
name: 'media/[name].[hash:8].[ext]',
188186
},
189187
},
190188
// Process JS with Babel.
@@ -223,7 +221,6 @@ module.exports = {
223221
options: {
224222
importLoaders: 1,
225223
minimize: true,
226-
sourceMap: shouldUseSourceMap,
227224
},
228225
},
229226
{
@@ -275,7 +272,7 @@ module.exports = {
275272
// by webpacks internal loaders.
276273
exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
277274
options: {
278-
name: 'static/media/[name].[hash:8].[ext]',
275+
name: 'media/[name].[hash:8].[ext]',
279276
},
280277
},
281278
// ** STOP ** Are you adding a new loader?

packages/react-scripts/scripts/build-library.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ process.on('unhandledRejection', err => {
2222
// Ensure environment variables are read.
2323
require('../config/env');
2424

25-
const path = require('path');
2625
const chalk = require('chalk');
2726
const fs = require('fs-extra');
2827
const webpack = require('webpack');
2928
const config = require('../config/webpack.config.library');
3029
const paths = require('../config/paths');
3130
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
3231
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
33-
const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
3432
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
3533
const printBuildError = require('react-dev-utils/printBuildError');
3634

3735
const measureFileSizesBeforeBuild =
3836
FileSizeReporter.measureFileSizesBeforeBuild;
3937
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
40-
const useYarn = fs.existsSync(paths.yarnLockFile);
4138

4239
// These sizes are pretty large. We'll warn for bundles exceeding them.
4340
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
@@ -50,11 +47,11 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
5047

5148
// First, read the current file sizes in build directory.
5249
// This lets us display how much they changed later.
53-
measureFileSizesBeforeBuild(paths.appBuild)
50+
measureFileSizesBeforeBuild(paths.appLib)
5451
.then(previousFileSizes => {
5552
// Remove all content but keep the directory so that
5653
// if you're in it, you don't end up in Trash
57-
fs.emptyDirSync(paths.appBuild);
54+
fs.emptyDirSync(paths.appLib);
5855
// Start the webpack build
5956
return build(previousFileSizes);
6057
})
@@ -81,23 +78,10 @@ measureFileSizesBeforeBuild(paths.appBuild)
8178
printFileSizesAfterBuild(
8279
stats,
8380
previousFileSizes,
84-
paths.appBuild,
81+
paths.appLib,
8582
WARN_AFTER_BUNDLE_GZIP_SIZE,
8683
WARN_AFTER_CHUNK_GZIP_SIZE
8784
);
88-
console.log();
89-
90-
const appPackage = require(paths.appPackageJson);
91-
const publicUrl = paths.publicUrl;
92-
const publicPath = config.output.publicPath;
93-
const buildFolder = path.relative(process.cwd(), paths.appBuild);
94-
printHostingInstructions(
95-
appPackage,
96-
publicUrl,
97-
publicPath,
98-
buildFolder,
99-
useYarn
100-
);
10185
},
10286
err => {
10387
console.log(chalk.red('Failed to compile.\n'));

packages/react-scripts/scripts/init.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ module.exports = function(
4343
test: 'domain-react-scripts test --env=jsdom',
4444
};
4545

46+
// so the result of domain-react-scripts build:library can be required
47+
appPackage.main = 'lib';
48+
4649
fs.writeFileSync(
4750
path.join(appPath, 'package.json'),
4851
JSON.stringify(appPackage, null, 2)

0 commit comments

Comments
 (0)