refactor(build): processAssets
This commit is contained in:
parent
de8774bc8e
commit
29130c04b6
|
@ -1,12 +1,17 @@
|
|||
const Build = require('./build');
|
||||
const configFn = require('./fleck.webpack.config');
|
||||
const {ProcessAssets} = require('./process-assets');
|
||||
const {ProcessAssets, processFleckAssets} = require('./process-assets');
|
||||
const {executable} = require('./webpack');
|
||||
|
||||
module.exports = async (env, argv) => {
|
||||
const flecks = await Build.from();
|
||||
const config = await configFn(env, argv, flecks);
|
||||
config.plugins.push(new ProcessAssets());
|
||||
config.plugins.push(new ProcessAssets('fleck', flecks));
|
||||
// Small hack because internals.
|
||||
flecks.hooks['@flecks/build.processAssets'] = [{
|
||||
hook: '@flecks/build',
|
||||
fn: (target, assets, compilation) => processFleckAssets(assets, compilation),
|
||||
}];
|
||||
config.plugins.push(executable());
|
||||
return config;
|
||||
};
|
||||
|
|
|
@ -3,6 +3,7 @@ const {join} = require('path');
|
|||
const webpack = require('webpack');
|
||||
|
||||
const {commands} = require('./commands');
|
||||
const {ProcessAssets} = require('./process-assets');
|
||||
|
||||
const {
|
||||
FLECKS_CORE_ROOT = process.cwd(),
|
||||
|
@ -22,6 +23,7 @@ exports.hooks = {
|
|||
if (Object.entries(flecks.compiled).length > 0) {
|
||||
config.resolve.symlinks = false;
|
||||
}
|
||||
config.plugins.push(new ProcessAssets(target, flecks));
|
||||
},
|
||||
'@flecks/build.files': () => [
|
||||
/**
|
||||
|
|
|
@ -69,22 +69,16 @@ export const hooks = {
|
|||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Process the `package.json` for a built fleck.
|
||||
* @param {Object} json The JSON.
|
||||
* @param {[Compilation](https://webpack.js.org/api/compilation-object/)} compilation The webpack compilation.
|
||||
*/
|
||||
'@flecks/build.packageJson': (json, compilation) => {
|
||||
json.files.push('something');
|
||||
},
|
||||
|
||||
/**
|
||||
* Process assets during a compilation.
|
||||
* @param {string} target The build target.
|
||||
* @param {Record<string, Source>} assets The assets.
|
||||
* @param {[Compilation](https://webpack.js.org/api/compilation-object/)} compilation The webpack compilation.
|
||||
*/
|
||||
'@flecks/build.processAssets': (assets, compilation) => {
|
||||
assets['my-file.js'] = new Source();
|
||||
'@flecks/build.processAssets': (target, assets, compilation) => {
|
||||
if (this.myTargets.includes(target)) {
|
||||
assets['my-file.js'] = new compilation.compiler.webpack.sources.RawSource('content');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,8 +8,9 @@ const {
|
|||
|
||||
exports.ProcessAssets = class ProcessAssets {
|
||||
|
||||
constructor(flecks) {
|
||||
constructor(target, flecks) {
|
||||
this.flecks = flecks;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
|
@ -20,16 +21,12 @@ exports.ProcessAssets = class ProcessAssets {
|
|||
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
|
||||
},
|
||||
async (assets, callback) => {
|
||||
if (this.flecks) {
|
||||
await this.flecks.invokeSequentialAsync(
|
||||
'@flecks/build.processAssets',
|
||||
assets,
|
||||
compilation,
|
||||
);
|
||||
}
|
||||
else {
|
||||
await exports.hook(assets, compilation);
|
||||
}
|
||||
await this.flecks.invokeSequentialAsync(
|
||||
'@flecks/build.processAssets',
|
||||
this.target,
|
||||
assets,
|
||||
compilation,
|
||||
);
|
||||
callback();
|
||||
},
|
||||
);
|
||||
|
@ -38,7 +35,7 @@ exports.ProcessAssets = class ProcessAssets {
|
|||
|
||||
};
|
||||
|
||||
exports.hook = async (assets, compilation, flecks) => {
|
||||
exports.processFleckAssets = async (assets, compilation, jsonCallback) => {
|
||||
const {RawSource} = compilation.compiler.webpack.sources;
|
||||
const packageJson = assets['package.json'];
|
||||
const json = JSON.parse(packageJson.source().toString());
|
||||
|
@ -55,8 +52,8 @@ exports.hook = async (assets, compilation, flecks) => {
|
|||
files.push('test');
|
||||
}
|
||||
// Let others have a say.
|
||||
if (flecks) {
|
||||
await flecks.invokeSequentialAsync('@flecks/build.packageJson', json, compilation);
|
||||
if (jsonCallback) {
|
||||
await jsonCallback(json, compilation);
|
||||
}
|
||||
// Add any sourcemaps.
|
||||
json.files = json.files
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
const Build = require('../../build/build/build');
|
||||
const configFn = require('../../build/build/fleck.webpack.config');
|
||||
const {ProcessAssets} = require('../../build/build/process-assets');
|
||||
const {ProcessAssets, processFleckAssets} = require('../../build/build/process-assets');
|
||||
|
||||
module.exports = async (env, argv) => {
|
||||
const flecks = await Build.from();
|
||||
const config = await configFn(env, argv, flecks);
|
||||
config.plugins.push(new ProcessAssets());
|
||||
config.plugins.push(new ProcessAssets('fleck', flecks));
|
||||
// Small hack because internals.
|
||||
flecks.hooks['@flecks/build.processAssets'] = [{
|
||||
hook: '@flecks/build',
|
||||
fn: (target, assets, compilation) => processFleckAssets(assets, compilation),
|
||||
}];
|
||||
return config;
|
||||
};
|
||||
|
|
|
@ -19,13 +19,8 @@
|
|||
"access": "public"
|
||||
},
|
||||
"files": [
|
||||
"build",
|
||||
"index.js",
|
||||
"index.js.map",
|
||||
"server.js",
|
||||
"server.js.map",
|
||||
"src",
|
||||
"test"
|
||||
"server.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"debug": "4.3.1",
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
const flecksConfigFn = require('@flecks/build/build/fleck.webpack.config');
|
||||
|
||||
const {ProcessAssets} = require('@flecks/build/build/process-assets');
|
||||
|
||||
module.exports = async (env, argv, flecks) => {
|
||||
const config = await flecksConfigFn(env, argv, flecks);
|
||||
config.plugins.push(new ProcessAssets(flecks));
|
||||
config.stats = flecks.get('@flecks/fleck.stats');
|
||||
return config;
|
||||
};
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
const {hook} = require('@flecks/build/build/process-assets');
|
||||
const {processFleckAssets} = require('@flecks/build/build/process-assets');
|
||||
|
||||
const commands = require('./commands');
|
||||
|
||||
|
@ -15,5 +14,11 @@ exports.hooks = {
|
|||
},
|
||||
}),
|
||||
'@flecks/build.targets': () => ['fleck'],
|
||||
'@flecks/build.processAssets': hook,
|
||||
'@flecks/build.processAssets': async (target, assets, compilation, flecks) => {
|
||||
if ('fleck' === target) {
|
||||
await processFleckAssets(assets, compilation, (json, compilation) => (
|
||||
flecks.invokeSequentialAsync('@flecks/fleck.packageJson', json, compilation)
|
||||
));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
12
packages/fleck/build/flecks.hooks.js
Normal file
12
packages/fleck/build/flecks.hooks.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
export const hooks = {
|
||||
|
||||
/**
|
||||
* Process the `package.json` for a built fleck.
|
||||
* @param {Object} json The JSON.
|
||||
* @param {[Compilation](https://webpack.js.org/api/compilation-object/)} compilation The webpack compilation.
|
||||
*/
|
||||
'@flecks/fleck.packageJson': (json, compilation) => {
|
||||
json.files.push('something');
|
||||
},
|
||||
|
||||
};
|
|
@ -292,11 +292,6 @@ exports.hooks = {
|
|||
*/
|
||||
trust: false,
|
||||
}),
|
||||
'@flecks/build.packageJson': (json, compilation) => {
|
||||
if (Object.keys(compilation.assets).some((filename) => filename.match(/^assets\//))) {
|
||||
json.files.push('assets');
|
||||
}
|
||||
},
|
||||
'@flecks/build.targets': (flecks) => [
|
||||
'web',
|
||||
...(flecks.get('@flecks/web.dll').length > 0 ? ['web-vendor'] : []),
|
||||
|
@ -307,6 +302,11 @@ exports.hooks = {
|
|||
targets.delete('web');
|
||||
}
|
||||
},
|
||||
'@flecks/fleck.packageJson': (json, compilation) => {
|
||||
if (Object.keys(compilation.assets).some((filename) => filename.match(/^assets\//))) {
|
||||
json.files.push('assets');
|
||||
}
|
||||
},
|
||||
'@flecks/server.runtime': async (flecks) => {
|
||||
const {config} = await Build.from({
|
||||
config: flecks.config,
|
||||
|
|
Loading…
Reference in New Issue
Block a user