Skip to content

Commit

Permalink
feat: cache directory (#118)
Browse files Browse the repository at this point in the history
Co-authored-by: zhengyutay <zy>
  • Loading branch information
ZhengYuTay authored Dec 4, 2020
1 parent 2459440 commit 7e3d3ad
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 33 deletions.
18 changes: 10 additions & 8 deletions packages/shuvi/src/bundler/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,26 @@ export function createWepbackConfig(
const srcDirs = [paths.appDir, paths.srcDir, ...(opts.srcDirs || [])];
if (opts.node) {
chain = createNodeWebpackChain({
env: config.env,
srcDirs,
dev,
projectRoot: paths.rootDir,
buildManifestFilename: BUILD_MANIFEST_PATH,
dev,
env: config.env,
mediaFilename: BUILD_MEDIA_PATH,
name: opts.name,
projectRoot: paths.rootDir,
srcDirs,
webpackHelpers
});
chain.output.path(`${paths.buildDir}/${opts.outputDir}`);
} else {
chain = createBrowserWebpackChain({
env: config.env,
analyze: config.analyze,
srcDirs,
dev,
projectRoot: paths.rootDir,
buildManifestFilename: BUILD_MANIFEST_PATH,
dev,
env: config.env,
mediaFilename: BUILD_MEDIA_PATH,
name: opts.name,
projectRoot: paths.rootDir,
srcDirs,
publicPath: assetPublicPath,
webpackHelpers
});
Expand Down
82 changes: 57 additions & 25 deletions packages/toolpack/src/webpack/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ChunkNamePlugin from '../plugins/chunk-names-plugin';
import FixWatchingPlugin from '../plugins/fix-watching-plugin';
import RequireCacheHotReloaderPlugin from '../plugins/require-cache-hot-reloader-plugin';
import { AppSourceRegexs } from '../../constants';
import crypto from 'crypto';

const dumbRouteComponent = require.resolve('../../utils/emptyComponent');

Expand All @@ -24,6 +25,7 @@ const resolveLocalLoader = (name: string) =>

export interface BaseOptions {
dev: boolean;
name: string;
projectRoot: string;
srcDirs: string[];
mediaFilename: string;
Expand Down Expand Up @@ -63,6 +65,7 @@ export function baseWebpackChain({
projectRoot,
srcDirs,
mediaFilename,
name,
buildManifestFilename,
publicPath = '/',
env = {}
Expand All @@ -83,7 +86,8 @@ export function baseWebpackChain({
nodeEnv: false,
splitChunks: false,
runtimeChunk: undefined,
minimize: !dev
minimize: !dev,
realContentHash: false
});
if (!dev) {
config.optimization.minimizer('terser').use(TerserPlugin, [
Expand Down Expand Up @@ -162,7 +166,8 @@ export function baseWebpackChain({
.loader('@shuvi/shuvi-babel-loader')
.options({
isNode: false,
cacheDirectory: true
// webpack 5 have in-built cache.
cacheDirectory: false
});

mainRule
Expand All @@ -183,27 +188,31 @@ export function baseWebpackChain({
}
]);

const shuviPublicEnv = Object.keys(process.env).reduce(
(prev: { [key: string]: string }, key: string) => {
if (key.startsWith(PUBLIC_ENV_PREFIX)) {
prev[`process.env.${key}`] = JSON.stringify(process.env[key]);
}
return prev;
},
{}
);

const shuviConfigEnv = Object.keys(env).reduce((acc, key) => {
if (/^(?:NODE_.+)|^(?:__.+)$/i.test(key)) {
throw new Error(`The key "${key}" under "env" is not allowed.`);
}

return {
...acc,
[`process.env.${key}`]: JSON.stringify(env[key])
};
}, {} as { [key: string]: string });

config.plugin('define').use(webpack.DefinePlugin, [
{
...Object.keys(process.env).reduce(
(prev: { [key: string]: string }, key: string) => {
if (key.startsWith(PUBLIC_ENV_PREFIX)) {
prev[`process.env.${key}`] = JSON.stringify(process.env[key]);
}
return prev;
},
{}
),
...Object.keys(env).reduce((acc, key) => {
if (/^(?:NODE_.+)|^(?:__.+)$/i.test(key)) {
throw new Error(`The key "${key}" under "env" is not allowed.`);
}

return {
...acc,
[`process.env.${key}`]: JSON.stringify(env[key])
};
}, {}),
...shuviPublicEnv,
...shuviConfigEnv,
'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production')
}
]);
Expand Down Expand Up @@ -236,11 +245,34 @@ export function baseWebpackChain({
]);
}

if (dev) {
config.cache({
type: 'memory'
});
const getCacheConfig = () => {
const projectHash = crypto
.createHash('md5')
.update(projectRoot)
.digest('hex');

const stringifiedEnvs = Object.entries({
...shuviConfigEnv,
...shuviPublicEnv
}).reduce((prev: string, [key, value]) => {
return `${prev}|${key}=${value}`;
}, '');

const SHUVI_VERSION = require('shuvi/package.json').version;

return {
cacheDirectory: path.resolve(
`node_modules/.cache/webpack/${projectHash}`
),
type: 'filesystem',
name: `${name.replace(/\//, '-')}-${config.get('mode')}`,
version: `${SHUVI_VERSION}|${stringifiedEnvs}`
};
};

config.cache(getCacheConfig());

if (dev) {
// For future webpack-dev-server purpose
config.watchOptions({
ignored: ['**/.git/**', '**/node_modules/**']
Expand Down

0 comments on commit 7e3d3ad

Please sign in to comment.