Skip to content

Commit 2f60d9e

Browse files
authored
[Build] Fix SourceMaps for prod builds (codesandbox#2287)
* Enable sourcemaps once again * Enable babel caching * Upgrade babel and removed unused babel dependencies * Remove unused css in embed * Fix tests * Simplify babel lockfile * Deduplicate all yarn dependencies * Deduplicate test dependencies * Move sandpack and react-sandpack to standalone_packages * Fix ts-jest * Update circleci cache for a fresh start * Fix jest * Fix keys * Add more to the cache * Test caches * Upgrade caches
1 parent 16d0028 commit 2f60d9e

File tree

95 files changed

+1293
-8687
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1293
-8687
lines changed

.circleci/config.yml

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,75 @@ aliases:
2525

2626
- &restore_deps_cache
2727
keys:
28-
- v24-dependency-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
29-
- v24-dependency-cache-{{ .Branch }}
30-
- v24-dependency-cache
28+
- v26-dependency-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
29+
- v26-dependency-cache-{{ .Branch }}
30+
- v26-dependency-cache
3131

3232
- &save_deps_cache
33-
key: v24-dependency-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
33+
key: v26-dependency-cache-{{ .Branch }}-{{ checksum "yarn.lock" }}
3434
paths:
3535
- node_modules
3636
- packages/app/node_modules
3737
- packages/chrome-extension/node_modules
3838
- packages/codesandbox-api/node_modules
3939
- packages/common/node_modules
4040
- packages/dynamic-pages/node_modules
41+
- packages/deps/node_modules
42+
- packages/executors/node_modules
4143
- packages/homepage/node_modules
4244
- packages/node-services/node_modules
4345
- packages/notifications/node_modules
4446
- packages/react-embed/node_modules
45-
- packages/react-sandpack/node_modules
4647
- packages/sandbox-hooks/node_modules
47-
- packages/sandpack/node_modules
4848
- packages/sse-hooks/node_modules
4949

5050
- &restore_standalone_deps_cache
5151
keys:
52-
- v24-standalone-dependency-cache-{{ .Branch }}-{{ checksum
52+
- v26-standalone-dependency-cache-{{ .Branch }}-{{ checksum
5353
"standalone-packages/codesandbox-browserfs/yarn.lock" }}
54-
- v24-standalone-dependency-cache-{{ .Branch }}
55-
- v24-standalone-dependency-cache
54+
- v26-standalone-dependency-cache-{{ .Branch }}
55+
- v26-standalone-dependency-cache
5656

5757
- &save_standalone_deps_cache
5858
key:
59-
v24-standalone-dependency-cache-{{ .Branch }}-{{ checksum
59+
v26-standalone-dependency-cache-{{ .Branch }}-{{ checksum
6060
"standalone-packages/codesandbox-browserfs/yarn.lock" }}
6161
paths:
6262
- standalone-packages/codesandbox-browserfs/node_modules
6363

6464
- &restore_prod_homepage_cache
65-
key: v24-prod-homepage-build-cache-master
65+
key: v26-prod-homepage-build-cache-master
6666

67-
- &restore_prod_cache
67+
- &restore_prod_result
6868
key:
69-
v24-prod-build-cache-{{ .Environment.CIRCLE_BRANCH }}-{{
70-
.Environment.CIRCLE_SHA1 }}
69+
v26-prod-build-result-{{ .Environment.CIRCLE_BRANCH
70+
}}-{{.Environment.CIRCLE_SHA1 }}
71+
72+
- &save_prod_build_cache
73+
key:
74+
v26-prod-build-cache-{{ .Environment.CIRCLE_BRANCH
75+
}}-{{.Environment.CIRCLE_SHA1 }}
76+
paths:
77+
- packages/app/node_modules/.cache
78+
79+
- &restore_prod_build_cache
80+
keys:
81+
- v26-prod-build-cache-{{ .Environment.CIRCLE_BRANCH
82+
}}-{{.Environment.CIRCLE_SHA1 }}
83+
- v26-prod-build-cache-{{ .Environment.CIRCLE_BRANCH }}-
84+
- v26-prod-build-cache-master-
7185

7286
# To persist the images built by sharp
7387
- &save_prod_homepage_cache
7488
key:
75-
v24-prod-homepage-build-cache-{{ .Environment.CIRCLE_BRANCH }}-{{
89+
v26-prod-homepage-build-cache-{{ .Environment.CIRCLE_BRANCH }}-{{
7690
.Environment.CIRCLE_SHA1 }}
7791
paths:
7892
- ./packages/homepage/public
7993

80-
- &save_prod_cache
94+
- &save_prod_result
8195
key:
82-
v24-prod-build-cache-{{ .Environment.CIRCLE_BRANCH }}-{{
96+
v26-prod-build-result-{{ .Environment.CIRCLE_BRANCH }}-{{
8397
.Environment.CIRCLE_SHA1 }}
8498
paths:
8599
- ./www
@@ -134,6 +148,7 @@ commands:
134148
- restore_cache: *restore_deps_cache
135149
- attach_workspace: *attach_deps_workspace
136150
- restore_cache: *restore_prod_homepage_cache
151+
- restore_cache: *restore_prod_build_cache
137152
- run:
138153
name: Build Application
139154
command: yarn build:prod
@@ -143,7 +158,8 @@ commands:
143158
if [ "${CIRCLE_BRANCH}" == "master" ]; then
144159
node packages/app/scripts/sentry-create-release.js
145160
fi
146-
- save_cache: *save_prod_cache
161+
- save_cache: *save_prod_build_cache
162+
- save_cache: *save_prod_result
147163
- run:
148164
name: Remove non-images from public
149165
command: |
@@ -217,7 +233,7 @@ commands:
217233
type: string
218234
default: '$DOCKER_PWD'
219235
steps:
220-
- restore_cache: *restore_prod_cache
236+
- restore_cache: *restore_prod_result
221237
- run:
222238
name: Docker Sign In
223239
command:

packages/app/config/babel.dev.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module.exports = {
44
// This is a feature of `babel-loader` for webpack (not Babel itself).
55
// It enables caching results in OS temporary directory for faster rebuilds.
66
cacheDirectory: true,
7+
cacheCompression: false,
8+
compact: false,
79
presets: [
810
// Latest stable ECMAScript features
911
require.resolve('@babel/preset-flow'),

packages/app/config/babel.prod.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module.exports = {
22
// Don't try to find .babelrc because we want to force this configuration.
33
babelrc: false,
4+
cacheDirectory: true,
5+
cacheCompression: true,
6+
compact: true,
47
presets: [
58
require.resolve('@babel/preset-flow'),
69
// Latest stable ECMAScript features

packages/app/config/webpack.common.js

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ const path = require('path');
33
const fs = require('fs');
44
const paths = require('./paths');
55
const HtmlWebpackPlugin = require('html-webpack-plugin');
6+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
67
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
78
const threadLoader = require('thread-loader');
89
const WatchMissingNodeModulesPlugin = require('../scripts/utils/WatchMissingNodeModulesPlugin');
910
const env = require('@codesandbox/common/lib/config/env');
1011
const getHost = require('@codesandbox/common/lib/utils/host');
12+
const postcssNormalize = require('postcss-normalize');
1113

1214
const babelDev = require('./babel.dev');
1315
const babelProd = require('./babel.prod');
@@ -21,6 +23,63 @@ const babelConfig = __DEV__ && !SANDBOX_ONLY ? babelDev : babelProd;
2123
const publicPath = SANDBOX_ONLY || __DEV__ ? '/' : getHost.default() + '/';
2224
const isLint = 'LINT' in process.env;
2325

26+
// common function to get style loaders
27+
const getStyleLoaders = (cssOptions, preProcessor) => {
28+
const loaders = [
29+
__DEV__ && require.resolve('style-loader'),
30+
__PROD__ && {
31+
loader: MiniCssExtractPlugin.loader,
32+
options: {},
33+
},
34+
{
35+
loader: require.resolve('css-loader'),
36+
options: cssOptions,
37+
},
38+
{
39+
// Options for PostCSS as we reference these options twice
40+
// Adds vendor prefixing based on your specified browser support in
41+
// package.json
42+
loader: require.resolve('postcss-loader'),
43+
options: {
44+
// Necessary for external CSS imports to work
45+
// https://github.com/facebook/create-react-app/issues/2677
46+
ident: 'postcss',
47+
plugins: () => [
48+
require('postcss-flexbugs-fixes'),
49+
require('postcss-preset-env')({
50+
autoprefixer: {
51+
flexbox: 'no-2009',
52+
},
53+
stage: 3,
54+
}),
55+
// Adds PostCSS Normalize as the reset css with default options,
56+
// so that it honors browserslist config in package.json
57+
// which in turn let's users customize the target behavior as per their needs.
58+
postcssNormalize(),
59+
],
60+
sourceMap: __PROD__,
61+
},
62+
},
63+
].filter(Boolean);
64+
if (preProcessor) {
65+
loaders.push(
66+
{
67+
loader: require.resolve('resolve-url-loader'),
68+
options: {
69+
sourceMap: __PROD__,
70+
},
71+
},
72+
{
73+
loader: require.resolve(preProcessor),
74+
options: {
75+
sourceMap: true,
76+
},
77+
}
78+
);
79+
}
80+
return loaders;
81+
};
82+
2483
// Shim for `eslint-plugin-vue/lib/index.js`
2584
const ESLINT_PLUGIN_VUE_INDEX = `module.exports = {
2685
rules: {${fs
@@ -97,6 +156,7 @@ module.exports = {
97156
globalObject: 'this',
98157
jsonpFunction: 'csbJsonP', // So we don't conflict with webpack generated libraries in the sandbox
99158
pathinfo: false,
159+
futureEmitAssets: true,
100160
},
101161

102162
module: {
@@ -108,11 +168,18 @@ module.exports = {
108168
},
109169
{
110170
test: /\.scss$/,
111-
use: [
112-
'style-loader', // creates style nodes from JS strings
113-
'css-loader', // translates CSS into CommonJS
114-
'sass-loader', // compiles Sass to CSS, using Node Sass by default
115-
],
171+
use: getStyleLoaders(
172+
{
173+
importLoaders: 2,
174+
sourceMap: true,
175+
},
176+
'sass-loader'
177+
),
178+
// Don't consider CSS imports dead code even if the
179+
// containing package claims to have no side effects.
180+
// Remove this when webpack adds a warning or an error for this.
181+
// See https://github.com/webpack/webpack/issues/6571
182+
sideEffects: true,
116183
},
117184
// Transpile node dependencies, node deps are often not transpiled for IE11
118185
{
@@ -250,7 +317,15 @@ module.exports = {
250317
// in development "style" loader enables hot editing of CSS.
251318
{
252319
test: /\.css$/,
253-
loaders: ['style-loader', 'css-loader'],
320+
loaders: getStyleLoaders({
321+
importLoaders: 1,
322+
sourceMap: true,
323+
}),
324+
// Don't consider CSS imports dead code even if the
325+
// containing package claims to have no side effects.
326+
// Remove this when webpack adds a warning or an error for this.
327+
// See https://github.com/webpack/webpack/issues/6571
328+
sideEffects: true,
254329
},
255330
// For importing README.md
256331
{
@@ -454,5 +529,13 @@ module.exports = {
454529
// makes the discovery automatic so you don't have to restart.
455530
// See https://github.com/facebookincubator/create-react-app/issues/186
456531
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
532+
533+
__PROD__ &&
534+
new MiniCssExtractPlugin({
535+
// Options similar to the same options in webpackOptions.output
536+
// both options are optional
537+
filename: 'static/css/[name].[contenthash:8].css',
538+
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
539+
}),
457540
].filter(Boolean),
458541
};

packages/app/config/webpack.dev.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ module.exports = merge(
2020
commonConfig,
2121
{
2222
devtool: 'eval',
23+
mode: 'development',
2324
output: {
2425
filename: 'static/js/[name].js',
25-
pathinfo: false,
26+
pathinfo: true,
2627
},
2728
optimization: {
2829
removeAvailableModules: false,

packages/app/config/webpack.prod.js

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,62 @@ const normalize = normalizeName({ name: true, automaticNameDelimiter: '~' });
2525
module.exports = merge(commonConfig, {
2626
devtool: 'source-map',
2727
output: {
28-
filename: 'static/js/[name].[chunkhash:8].js',
29-
chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
30-
sourceMapFilename: '[file].map', // Default
28+
filename: 'static/js/[name].[contenthash:8].js',
29+
chunkFilename: 'static/js/[name].[contenthash:8].chunk.js',
3130
},
3231
mode: 'production',
3332
stats: 'verbose',
33+
// optimization: {
34+
// minimize: false,
35+
// },
3436

3537
optimization: {
36-
minimizer: [new TerserJSPlugin({ parallel: true })],
38+
minimize: true,
39+
minimizer: [
40+
new TerserJSPlugin({
41+
terserOptions: {
42+
parse: {
43+
// We want terser to parse ecma 8 code. However, we don't want it
44+
// to apply any minification steps that turns valid ecma 5 code
45+
// into invalid ecma 5 code. This is why the 'compress' and 'output'
46+
// sections only apply transformations that are ecma 5 safe
47+
// https://github.com/facebook/create-react-app/pull/4234
48+
ecma: 8,
49+
},
50+
compress: {
51+
ecma: 5,
52+
warnings: false,
53+
// Disabled because of an issue with Uglify breaking seemingly valid code:
54+
// https://github.com/facebook/create-react-app/issues/2376
55+
// Pending further investigation:
56+
// https://github.com/mishoo/UglifyJS2/issues/2011
57+
comparisons: false,
58+
// Disabled because of an issue with Terser breaking valid code:
59+
// https://github.com/facebook/create-react-app/issues/5250
60+
// Pending further investigation:
61+
// https://github.com/terser-js/terser/issues/120
62+
inline: 2,
63+
},
64+
mangle: {
65+
safari10: true,
66+
},
67+
output: {
68+
ecma: 5,
69+
comments: false,
70+
// Turned on because emoji and regex is not minified properly using default
71+
// https://github.com/facebook/create-react-app/issues/2488
72+
ascii_only: true,
73+
},
74+
},
75+
// Use multi-process parallel running to improve the build speed
76+
// Default number of concurrent runs: os.cpus().length - 1
77+
// Disabled on WSL (Windows Subsystem for Linux) due to an issue with Terser
78+
// https://github.com/webpack-contrib/terser-webpack-plugin/issues/21
79+
parallel: 2,
80+
cache: true,
81+
sourceMap: true,
82+
}),
83+
],
3784
concatenateModules: true, // ModuleConcatenationPlugin
3885
namedModules: true, // NamedModulesPlugin()
3986
noEmitOnErrors: true, // NoEmitOnErrorsPlugin
@@ -61,10 +108,6 @@ module.exports = merge(commonConfig, {
61108
plugins: [
62109
process.env.ANALYZE && new BundleAnalyzerPlugin(),
63110
new webpack.DefinePlugin({ VERSION: JSON.stringify(VERSION) }),
64-
new webpack.LoaderOptionsPlugin({
65-
minimize: true,
66-
debug: false,
67-
}),
68111
// Generate a service worker script that will precache, and keep up to date,
69112
// the HTML & assets that are part of the Webpack build.
70113
new SWPrecacheWebpackPlugin({
@@ -306,11 +349,11 @@ module.exports = merge(commonConfig, {
306349
to: 'public/sse-hooks',
307350
},
308351
]),
309-
isMaster &&
310-
new SentryWebpackPlugin({
311-
include: 'src',
312-
ignore: ['node_modules', 'webpack.config.js'],
313-
release: VERSION,
314-
}),
352+
// isMaster &&
353+
// new SentryWebpackPlugin({
354+
// include: 'src',
355+
// ignore: ['node_modules', 'webpack.config.js'],
356+
// release: VERSION,
357+
// }),
315358
].filter(Boolean),
316359
});

0 commit comments

Comments
 (0)