st-bundle
Version:
CLI for watching and bundling SpringType projects.
159 lines (158 loc) • 6.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const env_1 = require("../env");
const Bundle_1 = require("./Bundle");
const bundleStrings_1 = require("./bundleStrings");
const utils_1 = require("../utils/utils");
/**
* Adding global settings like allowSyntheticDefaultImports and targets
* @param ctx
* @param bundle
*/
function injectSettingIntoDevBundle(ctx, bundle) {
if (ctx.config.allowSyntheticDefaultImports) {
bundle.addContent(bundleStrings_1.devStrings.allowSyntheticDefaultImports());
}
if (ctx.config.hmr.enabled && ctx.config.hmr.hmrProps.reloadEntryOnStylesheet) {
bundle.addContent(bundleStrings_1.devStrings.reloadEntryOnStylesheet(true));
}
bundle.addContent(bundleStrings_1.devStrings.processEnv(ctx.config.env));
bundle.addContent(bundleStrings_1.devStrings.target(ctx.config.target || 'server'));
}
exports.injectSettingIntoDevBundle = injectSettingIntoDevBundle;
/**
* Inject data into a default module
* @param ctx
* @param bundle
*/
function injectSettingsIntoDefaultBundle(ctx, bundle) {
const defaultPackage = bundle.packages[0];
if (defaultPackage.entry) {
bundle.addContent(bundleStrings_1.devStrings.setEntry(`default/${defaultPackage.entry.props.fuseBoxPath}`));
}
}
exports.injectSettingsIntoDefaultBundle = injectSettingsIntoDefaultBundle;
function inflatePackage(ctx, pkg) {
let packageName = pkg.getPublicName();
const customVersions = {};
for (const extPackage of pkg.externalPackages) {
if (!extPackage.isFlat) {
customVersions[extPackage.props.meta.name] = extPackage.props.meta.version;
}
}
const concat = utils_1.createConcat(true, '', '\n');
concat.add(null, bundleStrings_1.devStrings.openPackage(packageName, customVersions));
for (const _module of pkg.modules) {
if (_module.isCached) {
concat.add(null, _module.cache.contents, _module.isExecutable() ? _module.cache.sourceMap : undefined);
}
else {
if (_module.contents === undefined) {
ctx.log.error('$pkg/$path has not been processed by any plugins', {
pkg: _module.pkg.getPublicName(),
path: _module.props.fuseBoxPath,
});
}
else {
const fileConcat = utils_1.createConcat(true, '', '\n');
fileConcat.add(null, bundleStrings_1.devStrings.openFile(_module.props.fuseBoxPath));
const data = _module.generate();
fileConcat.add(null, data.contents, _module.isExecutable() ? data.sourceMap : undefined);
fileConcat.add(null, bundleStrings_1.devStrings.closeFile());
concat.add(null, fileConcat.content, fileConcat.sourceMap);
ctx.ict.sync('after_dev_module_inflate', {
concat: fileConcat,
ctx,
module: _module,
});
}
}
}
concat.add(null, bundleStrings_1.devStrings.closePackage(pkg.entry && pkg.entry.props.fuseBoxPath));
return concat;
}
exports.inflatePackage = inflatePackage;
function inflateBundle(ctx, bundle) {
for (const pkg of bundle.packages) {
if (pkg.isCached) {
bundle.addContent(pkg.cache.contents, pkg.cache.sourceMap);
}
else {
const concat = inflatePackage(ctx, pkg);
bundle.addContent(concat.content, concat.sourceMap);
ctx.ict.sync('after_dev_package_inflate', { ctx, concat, pkg: pkg });
}
}
// close developmentAPI for isolated bundles
if (bundle.isolated) {
const defaultProject = bundle.packages.find(pkg => pkg.isDefaultPackage);
if (defaultProject.entry) {
bundle.addContent(bundleStrings_1.devStrings.setEntry(`default/${defaultProject.entry.props.fuseBoxPath}`));
}
bundle.addContent(env_1.closeDevelopmentApi());
}
}
exports.inflateBundle = inflateBundle;
function inflateBundles(ctx, bundles) {
for (const key in bundles) {
const bundle = bundles[key];
inflateBundle(ctx, bundle);
}
}
exports.inflateBundles = inflateBundles;
function createDevBundles(ctx, packages) {
const useOneBundle = ctx.config.target === 'web-worker' || ctx.config.useSingleBundle;
const bundleSet = Bundle_1.createBundleSet(ctx);
let devBundle;
if (!useOneBundle) {
devBundle = bundleSet.getBundle(Bundle_1.BundleType.DEV);
devBundle.addContent(env_1.getDevelopmentApi());
injectSettingIntoDevBundle(ctx, devBundle);
}
else {
const defaultBundle = bundleSet.getBundle(Bundle_1.BundleType.PROJECT_JS);
defaultBundle.isolated = true;
defaultBundle.addContent(env_1.openDevelopmentApi());
injectSettingIntoDevBundle(ctx, defaultBundle);
}
packages.forEach(pkg => {
if (useOneBundle) {
let defaultBundle = bundleSet.getBundle(Bundle_1.BundleType.PROJECT_JS);
defaultBundle.addPackage(pkg);
}
else {
if (pkg.isDefaultPackage) {
let defaultBundle = bundleSet.getBundle(Bundle_1.BundleType.PROJECT_JS);
defaultBundle.addPackage(pkg);
}
else {
// dev packages here
if (pkg.props.meta.fusebox) {
if (pkg.props.meta.fusebox.dev) {
devBundle.addPackage(pkg);
}
if (pkg.props.meta.fusebox.system || pkg.props.meta.fusebox.polyfill) {
const bundle = bundleSet.getBundle(Bundle_1.BundleType.VENDOR_JS);
bundle.addPackage(pkg);
}
}
else {
const bundle = bundleSet.getBundle(Bundle_1.BundleType.VENDOR_JS);
bundle.addPackage(pkg);
}
}
}
});
inflateBundles(ctx, bundleSet.collection);
if (!useOneBundle) {
const defaultProject = packages.find(pkg => pkg.isDefaultPackage);
if (defaultProject.entry) {
const targetBundle = bundleSet.getBundle(Bundle_1.BundleType.PROJECT_ENTRY);
targetBundle.addContent(bundleStrings_1.devStrings.setEntry(`default/${defaultProject.entry.props.fuseBoxPath}`));
}
}
return {
bundles: bundleSet.collection,
};
}
exports.createDevBundles = createDevBundles;