2019-01-06 00:50:05 +00:00
|
|
|
import babel from '@babel/core';
|
2018-09-04 22:58:52 +00:00
|
|
|
|
2019-07-10 00:21:25 +00:00
|
|
|
import unpkgRewrite from '../plugins/unpkgRewrite.js';
|
2018-09-04 22:58:52 +00:00
|
|
|
|
2019-01-26 23:20:15 +00:00
|
|
|
const origin = process.env.ORIGIN || 'https://unpkg.com';
|
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export default function rewriteBareModuleIdentifiers(code, packageConfig) {
|
2018-09-04 22:58:52 +00:00
|
|
|
const dependencies = Object.assign(
|
|
|
|
{},
|
|
|
|
packageConfig.peerDependencies,
|
|
|
|
packageConfig.dependencies
|
|
|
|
);
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
// Ignore .babelrc and package.json babel config
|
|
|
|
// because we haven't installed dependencies so
|
|
|
|
// we can't load plugins; see #84
|
|
|
|
babelrc: false,
|
2019-03-06 16:09:29 +00:00
|
|
|
// Make a reasonable attempt to preserve whitespace
|
|
|
|
// from the original file. This ensures minified
|
|
|
|
// .mjs stays minified; see #149
|
|
|
|
retainLines: true,
|
2020-12-31 20:58:45 +00:00
|
|
|
plugins: [unpkgRewrite(origin, dependencies), "@babel/plugin-proposal-optional-chaining"]
|
2018-09-04 22:58:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return babel.transform(code, options).code;
|
|
|
|
}
|