refactor(v2): move unused generated files out from build folder (#2033)

This commit is contained in:
Endi 2019-11-24 09:16:04 +07:00 committed by Yangshun Tay
parent d1f84709e0
commit cea224b03d
5 changed files with 12 additions and 9 deletions

View file

@ -45,12 +45,12 @@ export default async function render(locals) {
]; ];
const metaAttributes = metaStrings.filter(Boolean); const metaAttributes = metaStrings.filter(Boolean);
const {outDir} = locals; const {generatedFilesDir} = locals;
const manifestPath = path.join(outDir, 'client-manifest.json'); const manifestPath = path.join(generatedFilesDir, 'client-manifest.json');
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8')); const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
// chunkName -> chunkAssets mapping. // chunkName -> chunkAssets mapping.
const chunkManifestPath = path.join(outDir, 'chunk-map.json'); const chunkManifestPath = path.join(generatedFilesDir, 'chunk-map.json');
const chunkManifest = JSON.parse( const chunkManifest = JSON.parse(
await fs.readFile(chunkManifestPath, 'utf8'), await fs.readFile(chunkManifestPath, 'utf8'),
); );

View file

@ -55,7 +55,7 @@ export async function build(
const props: Props = await load(siteDir); const props: Props = await load(siteDir);
// Apply user webpack config. // Apply user webpack config.
const {outDir, plugins} = props; const {outDir, generatedFilesDir, plugins} = props;
let clientConfig: Configuration = merge(createClientConfig(props), { let clientConfig: Configuration = merge(createClientConfig(props), {
plugins: [ plugins: [
@ -65,7 +65,7 @@ export async function build(
cliOptions.bundleAnalyzer && new BundleAnalyzerPlugin(), cliOptions.bundleAnalyzer && new BundleAnalyzerPlugin(),
// Generate client manifests file that will be used for server bundle // Generate client manifests file that will be used for server bundle
new ReactLoadableSSRAddon({ new ReactLoadableSSRAddon({
filename: 'client-manifest.json', filename: path.join(generatedFilesDir, 'client-manifest.json'),
}), }),
].filter(Boolean) as Plugin[], ].filter(Boolean) as Plugin[],
}); });

View file

@ -30,6 +30,7 @@ export function createClientConfig(props: Props): Configuration {
// Generate chunk-map.json (mapping of chunk names to their corresponding chunk assets) // Generate chunk-map.json (mapping of chunk names to their corresponding chunk assets)
new ChunkManifestPlugin({ new ChunkManifestPlugin({
filename: 'chunk-map.json', filename: 'chunk-map.json',
outputPath: props.generatedFilesDir,
manifestVariable: '__chunkMapping', manifestVariable: '__chunkMapping',
inlineManifest: !isProd, inlineManifest: !isProd,
}), }),

View file

@ -15,6 +15,7 @@ class ChunkManifestPlugin {
constructor(options) { constructor(options) {
this.options = { this.options = {
filename: 'manifest.json', filename: 'manifest.json',
outputPath: null,
manifestVariable: 'webpackManifest', manifestVariable: 'webpackManifest',
inlineManifest: false, inlineManifest: false,
...options, ...options,
@ -23,7 +24,7 @@ class ChunkManifestPlugin {
apply(compiler) { apply(compiler) {
let chunkManifest; let chunkManifest;
const {path: outputPath, publicPath} = compiler.options.output; const {path: defaultOutputPath, publicPath} = compiler.options.output;
// Build the chunk mapping // Build the chunk mapping
compiler.hooks.afterCompile.tapAsync(pluginName, (compilation, done) => { compiler.hooks.afterCompile.tapAsync(pluginName, (compilation, done) => {
@ -49,6 +50,7 @@ class ChunkManifestPlugin {
} }
chunkManifest = assetsMap; chunkManifest = assetsMap;
if (!this.options.inlineManifest) { if (!this.options.inlineManifest) {
const outputPath = this.options.outputPath || defaultOutputPath;
const finalPath = path.resolve(outputPath, this.options.filename); const finalPath = path.resolve(outputPath, this.options.filename);
fs.ensureDir(path.dirname(finalPath), () => { fs.ensureDir(path.dirname(finalPath), () => {
fs.writeFile(finalPath, JSON.stringify(chunkManifest, null, 2), done); fs.writeFile(finalPath, JSON.stringify(chunkManifest, null, 2), done);

View file

@ -16,7 +16,7 @@ import WaitPlugin from './plugins/WaitPlugin';
import LogPlugin from './plugins/LogPlugin'; import LogPlugin from './plugins/LogPlugin';
export function createServerConfig(props: Props): Configuration { export function createServerConfig(props: Props): Configuration {
const {baseUrl, routesPaths, outDir} = props; const {baseUrl, routesPaths, generatedFilesDir} = props;
const config = createBaseConfig(props, true); const config = createBaseConfig(props, true);
const routesLocation = {}; const routesLocation = {};
@ -41,7 +41,7 @@ export function createServerConfig(props: Props): Configuration {
plugins: [ plugins: [
// Wait until manifest from client bundle is generated // Wait until manifest from client bundle is generated
new WaitPlugin({ new WaitPlugin({
filepath: path.join(outDir, 'client-manifest.json'), filepath: path.join(generatedFilesDir, 'client-manifest.json'),
}), }),
// Static site generator webpack plugin. // Static site generator webpack plugin.
@ -49,7 +49,7 @@ export function createServerConfig(props: Props): Configuration {
entry: 'main', entry: 'main',
locals: { locals: {
baseUrl, baseUrl,
outDir, generatedFilesDir,
routesLocation, routesLocation,
}, },
paths: ssgPaths, paths: ssgPaths,