Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
envName: program.envName,
rootMode: program.rootMode,
// Commander will default the "--no-" arguments to true, but we want to
// leave them undefined so that @babel/core can handle the
// default-assignment logic on its own.
babelrc: program.babelrc === true ? undefined : program.babelrc,
};
for (const key of Object.keys(babelOptions)) {
if (babelOptions[key] === undefined) {
delete babelOptions[key];
}
}
register(babelOptions);
const replPlugin = ({ types: t }) => ({
visitor: {
ModuleDeclaration(path) {
throw path.buildCodeFrameError("Modules aren't supported in the REPL");
},
VariableDeclaration(path) {
if (path.node.kind !== "var") {
throw path.buildCodeFrameError(
"Only `var` variables are supported in the REPL",
);
}
},
Program(path) {
export default function babelRequire(path) {
register({ presets: [preset] })
// eslint-disable-next-line global-require, import/no-dynamic-require
const mod = require(path)
revert()
return mod
}
export default function getDevServer(config = {}) {
const staticPath = getStaticBasePath(config)
const app = getBaseServer(config)
const webpackConfig = config.webpack || getWebpackDevConfig(config)
// Serve an empty CSS file for the main stylesheet,
// as they are injected dynamically in development mode
app.get(`${staticPath}/css/main.css`, (req, res) => {
res.set('Content-Type', 'text/css')
res.send()
})
// Use babel-register in order to be able to load things like
// the document component, which can contain JSX etc
registerBabel()
// Apply the dev and hot middlewares to build/serve bundles on the fly
const compiler = webpack(webpackConfig)
app.use(
webpackDevMiddleware(compiler, {
logLevel: 'silent',
watchOptions: {
ignored: /node_modules/
},
publicPath: webpackConfig.output.publicPath
})
)
app.use(webpackHotMiddleware(compiler))
// Expose webpack compiler on server instance
import mhyConfig from '@/configs/mhy'
import babelConfig from '@/configs/babel'
import register from '@babel/register'
babelConfig.presets.find(p => p[0].includes('preset-env'))[1] = {
modules: 'commonjs',
targets: {
node: true,
browsers: false,
esmodules: true
}
}
babelConfig.extensions = ['.es6', '.es', '.jsx', '.js', '.mjs', '.ts', '.tsx']
babelConfig.cache = false
register(babelConfig)
addPath(path.resolve(process.cwd(), 'node_modules'))
addPath(path.resolve(__dirname, '../../node_modules'))
const alias = { ...mhyConfig.defaultAliases }
for (const [key, entry] of Object.entries(alias)) {
if (!fs.existsSync(entry)) {
alias[key] = path.resolve(process.cwd(), entry)
} else {
// Make sure it's a resolved path indeed
alias[key] = path.resolve(entry)
}
}
addAliases(alias)
const scriptKey = '--mhy-script'
const scriptIndex = process.argv.findIndex(v => v.includes(scriptKey))
export function initPlugins(options) {
register();
options.plugins.forEach(async plugin => {
let [, pluginPath, pluginOptions] = plugin;
const pluginPackage = path.join(pluginPath, 'package.json');
let initFile;
if (!pluginOptions) {
pluginOptions = {};
plugin[2] = pluginOptions;
}
try {
initFile = path.join(pluginPath, require(pluginPackage).init);
} catch (error) {
initFile = path.resolve(path.join(pluginPath, 'init.js'));
}
require.resolve('@babel/preset-react'),
[
require.resolve('@babel/preset-env'),
{
targets: {
node: 'current'
}
}
]
],
plugins: [require.resolve('@babel/plugin-proposal-class-properties')]
}
}
}
registerBabel(getConfig())