2019-01-26 01:30:59 +00:00
|
|
|
import * as plugins from './parcel-plugin-wrapper.plugins';
|
2019-01-26 13:05:00 +00:00
|
|
|
import '@gitzone/tsrun';
|
2019-01-26 01:09:36 +00:00
|
|
|
|
2019-01-26 01:30:59 +00:00
|
|
|
module.exports = bundler => {
|
2019-01-26 01:09:36 +00:00
|
|
|
const readAsset = pathArg => {
|
|
|
|
try {
|
2019-01-26 01:30:59 +00:00
|
|
|
return plugins.fs.readFileSync(pathArg, 'utf8');
|
2019-01-26 01:09:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error('file is invalid');
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const writeAsset = (name, { header = '', footer = '' }) => {
|
2019-01-26 01:30:59 +00:00
|
|
|
plugins.fs.writeFileSync(
|
2019-01-26 01:09:36 +00:00
|
|
|
name,
|
|
|
|
`${header}
|
|
|
|
${readAsset(name)}
|
|
|
|
${footer}`
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const processAsset = async (bundle, processFn) => {
|
|
|
|
const { name } = bundle;
|
|
|
|
const wrappingCode = await processFn({ name, bundler });
|
|
|
|
|
|
|
|
if (wrappingCode) {
|
|
|
|
writeAsset(name, wrappingCode);
|
|
|
|
}
|
|
|
|
|
2019-01-26 01:30:59 +00:00
|
|
|
bundle.childBundles.forEach((bundleArg) => {
|
|
|
|
processAsset(bundleArg, processFn);
|
2019-01-26 01:09:36 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-26 01:30:59 +00:00
|
|
|
bundler.on('bundled', async bundleArg => {
|
2019-01-26 01:09:36 +00:00
|
|
|
try {
|
|
|
|
const CWD = process.cwd();
|
2019-01-26 13:05:00 +00:00
|
|
|
const processFn = require(plugins.path.join(CWD, '.assetWrapper.ts'));
|
2019-01-26 01:09:36 +00:00
|
|
|
if (processFn && typeof processFn === 'function') {
|
2019-01-26 01:30:59 +00:00
|
|
|
await processAsset(bundleArg, processFn);
|
2019-01-26 01:09:36 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(
|
|
|
|
'parcel-plugin-wrapper cannot work without a .assetWrapper.js in the root of your project!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|