@omni-door/cli
Version:
A tool set for set up the standard JS project
16 lines (15 loc) • 7.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function default_1(config) {
var ts = config.ts, multiOutput = config.multiOutput, _a = config.srcDir, srcDir = _a === void 0 ? 'src' : _a, _b = config.outDir, outDir = _b === void 0 ? 'lib' : _b, esmDir = config.esmDir, configurationPath = config.configurationPath, _c = config.pkjFieldName, pkjFieldName = _c === void 0 ? 'omni' : _c, _d = config.configFileName, configFileName = _d === void 0 ? 'omni.config.js' : _d;
return "'use strict';\n\nconst fs = require('fs');\nconst path = require('path');\nconst { requireCwd, logErr } = require('@omni-door/utils');\nconst { nodeResolve: resolve } = requireCwd('@rollup/plugin-node-resolve', true) || {};\nconst commonjs = requireCwd('@rollup/plugin-commonjs', true);\nconst { babel } = requireCwd('@rollup/plugin-babel', true) || {};\nconst json = requireCwd('@rollup/plugin-json', true);\n" + (ts ? "const typescript = requireCwd('@rollup/plugin-typescript', true);\n" : '') + "\nconst pkj = requireCwd('./package.json');\nconst configFilePath = (pkj && pkj." + pkjFieldName + " && pkj." + pkjFieldName + ".filePath) || './" + configFileName + "';\nconst configs = requireCwd(configFilePath);\n" + (configurationPath ? "const customConfig = require('" + configurationPath + "')\n" : '') + "\nconst { build } = configs || {};\nconst { configuration = getConfig => getConfig(true) } = build || {};\n\nconst extensions = ['.ts', '.js'];\nconst tsExcludes = ['**/__test__/*'];\nconst babelConfig = {\n exclude: 'node_modules/**',\n plugins: [['@babel/plugin-transform-runtime', { useESModules: false, corejs: 3 }]],\n babelHelpers: 'runtime',\n extensions\n};\nconst resolveConfig = {\n extensions,\n preferBuiltins: true,\n browser: true\n};\nconst tsconfig = {\n cjs: (dir = '', emit = false) => ({\n compilerOptions: {\n target: 'es5',\n module: 'esnext',\n declaration: emit,\n outDir: path.resolve('" + outDir + "', dir),\n emitDeclarationOnly: emit\n },\n exclude: tsExcludes\n }),\n esm: (dir = '', emit = false) => ({\n compilerOptions: {\n target: 'esnext',\n module: 'esnext',\n declaration: emit,\n outDir: path.resolve('" + esmDir + "', dir),\n emitDeclarationOnly: emit\n },\n exclude: tsExcludes\n })\n};\n\nlet indexPath = '';\nconst exts = ['ts', 'tsx', 'jsx', 'js'];\nfor (let i = 0, len = exts.length; i < len; i++) {\n indexPath = path.resolve('" + srcDir + "', `index.${exts[i]}`);\n if (fs.existsSync(indexPath)) break;\n if (i === len - 1) {\n logErr('\u8BF7\u4EE5 index \u4E3A\u540D\u79F0\u6307\u5B9A\u6B63\u786E\u7684\u5165\u53E3\u6587\u4EF6\uFF01(Please specify the correct entry file with name of index)');\n process.exit(1);\n }\n}\n\nfunction flatten (arr) {\n return arr.reduce(function(prev, next){\n return prev.concat(Array.isArray(next) ? flatten(next) : next);\n }, []);\n}\n\nfunction createConfig (bundle = true) {\n const filesPaths = [];\n " + (multiOutput ? "const getEntryPath = (files, preDir) => {\n const len = files.length;\n for (let i = 0; i < len; i++) {\n const file = files[i];\n if (file.includes('test')) continue;\n const preDirPath = path.join(preDir, file);\n const filePath = path.resolve('" + srcDir + "', preDirPath);\n const stats = fs.statSync(filePath);\n if (stats.isDirectory()) {\n getEntryPath(fs.readdirSync(filePath), preDirPath);\n } else {\n let entryPath = '';\n const extArr = path.extname(filePath).split('.');\n entryPath = (extArr.length && !!~exts.indexOf(extArr[1])) ? filePath : '';\n if (!entryPath || !fs.existsSync(entryPath) || fs.existsSync(path.resolve(filePath, '.buildignore'))) {\n tsExcludes.push(`${filePath}/*`);\n continue;\n }\n filesPaths.push({\n entry: entryPath,\n file: path.join(file, 'index.js'),\n dir: preDir\n });\n }\n }\n };\n getEntryPath(fs.readdirSync('" + srcDir + "'), '');\n " : '') + "\n return bundle ? [\n {\n input: indexPath,\n output: {\n file: '" + outDir + "/index.js',\n format: 'cjs',\n exports: 'named',\n compact: true\n },\n plugins: [\n resolve(resolveConfig),\n commonjs(),\n " + (ts ? 'typescript(tsconfig.cjs(\'\', true)),' : '') + "\n babel(babelConfig),\n json()\n ]\n },\n" + (esmDir
? "\n {\n input: indexPath,\n output: {\n file: '" + esmDir + "/index.js',\n format: 'esm',\n compact: true\n },\n plugins: [\n resolve(resolveConfig),\n commonjs({ transformMixedEsModules: true, esmExternals: true }),\n " + (ts ? 'typescript(tsconfig.esm(\'\', true)),' : '') + "\n json()\n ]\n },"
: '') + "\n\n ...flatten(filesPaths.map(fileParams => {\n const { entry, file, dir } = fileParams;\n return [\n {\n input: entry,\n output: {\n dir: path.resolve('" + outDir + "', dir),\n format: 'cjs',\n exports: 'named',\n compact: true\n },\n plugins: [\n resolve(resolveConfig),\n commonjs(),\n " + (ts ? 'typescript(tsconfig.cjs(dir)),' : '') + "\n json()\n ]\n },\n" + (esmDir
? "\n {\n input: entry,\n output: {\n dir: path.resolve('" + esmDir + "', dir),\n format: 'esm',\n compact: true\n },\n plugins: [\n resolve(resolveConfig),\n commonjs({ transformMixedEsModules: true, esmExternals: true }),\n " + (ts ? 'typescript(tsconfig.esm(dir)),' : '') + "\n json()\n ]\n }"
: '') + "\n ];\n }))\n ] : [\n {\n input: indexPath,\n output: {\n dir: '" + outDir + "',\n format: 'cjs',\n exports: 'named',\n compact: true,\n preserveModules: true,\n preserveModulesRoot: '" + srcDir + "'\n },\n plugins: [\n resolve(resolveConfig),\n commonjs(),\n " + (ts ? 'typescript(tsconfig.cjs(\'\', true)),' : '') + "\n babel(babelConfig),\n json()\n ]\n },\n" + (esmDir
? "\n {\n input: indexPath,\n output: {\n dir: '" + esmDir + "',\n format: 'esm',\n compact: true,\n preserveModules: true,\n preserveModulesRoot: '" + srcDir + "'\n },\n plugins: [\n resolve(resolveConfig),\n commonjs({ transformMixedEsModules: true, esmExternals: true }),\n " + (ts ? 'typescript(tsconfig.esm(\'\', true)),' : '') + "\n json()\n ]\n },"
: '') + "\n ]\n};\n\nmodule.exports = " + (configurationPath
? 'typeof customConfig === \'function\' ? customConfig((bundle = true) => createConfig(bundle)) : customConfig'
: 'configuration((bundle = true) => createConfig(bundle));') + "\n";
}
exports.default = default_1;