Skip to content

Commit 5ff429d

Browse files
authored
Merge pull request #45 from scssyworks/0.0.x
0.0.x
2 parents f1ba980 + dc18155 commit 5ff429d

File tree

21 files changed

+1465
-1425
lines changed

21 files changed

+1465
-1425
lines changed

bin/config/index.js

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,5 @@
1-
const { defineConfig } = require('rollup');
2-
const json = require('@rollup/plugin-json');
3-
const { nodeResolve } = require('@rollup/plugin-node-resolve');
4-
const commonjs = require('@rollup/plugin-commonjs');
5-
const { babel } = require('@rollup/plugin-babel');
6-
const terser = require('@rollup/plugin-terser');
7-
const replace = require('@rollup/plugin-replace');
8-
const babelConfig = require('../../templates/babel.config');
9-
const {
10-
getName,
11-
resolvePath,
12-
getOutputFileName,
13-
env,
14-
getInputProps,
15-
resolveOutputFields,
16-
externalize,
17-
opts,
18-
check,
19-
updateArgs,
20-
getLogger,
21-
getResource,
22-
} = require('../../utils');
23-
const { fileSize } = require('../../plugins');
24-
const { MSG_BABELRC, configTypes, DEV } = require('../../constants');
1+
const rollupConfig = require('../../templates/rollup.config');
252

26-
const commonOutputConfig = {
27-
name: getName(),
28-
exports: 'named',
29-
sourcemap: true,
30-
};
31-
32-
const { main: pjMain, module: pjModule } = resolveOutputFields();
33-
34-
const defaultConfig = defineConfig({
35-
output: [
36-
{
37-
file: getOutputFileName(pjModule, true),
38-
format: 'es',
39-
},
40-
{
41-
file: getOutputFileName(pjMain, true),
42-
format: 'cjs',
43-
},
44-
{
45-
file: getOutputFileName(pjModule),
46-
format: 'es',
47-
},
48-
{
49-
file: getOutputFileName(pjMain),
50-
format: 'cjs',
51-
},
52-
],
53-
plugins: [
54-
replace({
55-
values: env(),
56-
preventAssignment: true,
57-
objectGuards: true,
58-
}),
59-
json(),
60-
nodeResolve(),
61-
commonjs({
62-
include: 'node_modules/**',
63-
extensions: ['.js', '.ts'],
64-
}),
65-
],
66-
external: externalize(),
67-
});
68-
69-
module.exports = async (args, lgr) => {
70-
const logger = getLogger(args, lgr);
71-
const { input, typescript, react, preact } = getInputProps(args, logger);
72-
const finalArgs = updateArgs(args, { typescript, react, preact });
73-
const babelFile = await check(configTypes.BABEL);
74-
const babelrc = !!babelFile;
75-
if (babelrc) {
76-
logger.log(MSG_BABELRC(babelFile));
77-
}
78-
const { configFile } = finalArgs;
79-
const confFunc = getResource(configFile);
80-
let finalConfig = Object.assign(defaultConfig, {
81-
input,
82-
});
83-
finalConfig.output = defaultConfig.output.map((outConf) => {
84-
const isDev = DEV.test(outConf.file);
85-
Object.assign(outConf, commonOutputConfig, {
86-
sourcemap: isDev,
87-
plugins: opts(!isDev, [terser()]),
88-
});
89-
return outConf;
90-
});
91-
finalConfig.plugins.push(
92-
babel({
93-
babelrc,
94-
exclude: 'node_modules/**',
95-
extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.es6', '.es'],
96-
babelHelpers: 'runtime',
97-
skipPreflightCheck: true,
98-
...(babelrc ? {} : babelConfig(finalArgs)),
99-
}),
100-
fileSize(args, logger)
101-
);
102-
try {
103-
if (typeof confFunc === 'function') {
104-
finalConfig = await Promise.resolve(confFunc(defaultConfig));
105-
}
106-
} catch (e) {
107-
logger.verbose(e);
108-
}
109-
return finalConfig;
3+
module.exports = async (args) => {
4+
return await rollupConfig(args);
1105
};

bin/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env node
2-
const { build, init } = require('./rollup-scripts');
2+
const { build, init, lint } = require('./rollup-scripts');
33
const yargs = require('yargs');
44
const { hideBin } = require('yargs/helpers');
55
const { CONFIG_FILE, SCRIPT_NAME } = require('../constants');
6-
const lint = require('./rollup-scripts/lint');
76

87
const boolConfig = {
98
default: false,

bin/rollup-scripts/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const build = require('./build');
22
const init = require('./init');
3+
const lint = require('./lint');
34

45
module.exports = {
56
build,
67
init,
8+
lint,
79
};

bin/rollup-scripts/init/index.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
const { existsSync } = require('node:fs');
2-
const fs = require('node:fs/promises');
1+
const fs = require('node:fs');
2+
const fsPromises = require('node:fs/promises');
33
const path = require('node:path');
4+
const rsConfig = require('../../../templates/rs.json');
45
const babelConfig = require('../../../templates/babel.config');
56
const eslintConfig = require('../../../templates/eslint.config');
67
const {
7-
SCRIPT_ROOT,
8-
CONFIG_FILE,
98
MSG_CONFIG,
109
MSG_CONFIGBABEL,
1110
DEFAULT_ENCODING,
12-
VAR_FILE_PATH,
1311
configTypes,
1412
configFiles,
1513
MSG_CONFIGESLINT,
@@ -42,12 +40,12 @@ function getMessage(configType) {
4240
}
4341
}
4442

45-
async function generateConfig(args, configType, configFile, lgr) {
46-
const logger = getLogger(args, lgr);
47-
const hasBabel = await check(configType);
48-
if (!hasBabel) {
43+
async function generateConfig(args, configType, configFile) {
44+
const logger = getLogger(args);
45+
const hasConfig = await check(configType);
46+
if (!hasConfig) {
4947
const conf = await getConfig(configType, args);
50-
await fs.writeFile(
48+
await fsPromises.writeFile(
5149
resolvePath(configFile),
5250
prettyJSON(conf),
5351
DEFAULT_ENCODING
@@ -57,26 +55,28 @@ async function generateConfig(args, configType, configFile, lgr) {
5755
}
5856

5957
module.exports = async function init(args) {
58+
const { configFile } = args;
6059
const logger = getLogger(args);
61-
const { src, typescript, react, preact } = getInputProps(args, logger);
62-
const finalArgs = updateArgs(args, { typescript, react, preact });
60+
const { src, sourceTypes } = getInputProps(args, logger);
61+
const finalArgs = updateArgs(args, sourceTypes);
6362
logger.log(MSG_INIT);
64-
const template = path.join(SCRIPT_ROOT, 'templates', CONFIG_FILE);
65-
const configFile = resolvePath(CONFIG_FILE);
66-
if (!existsSync(configFile)) {
67-
const configFileContent = await fs.readFile(template, DEFAULT_ENCODING);
68-
await fs.writeFile(
69-
configFile,
70-
configFileContent.replace(VAR_FILE_PATH, src),
71-
DEFAULT_ENCODING
72-
);
73-
logger.success(MSG_CONFIG(CONFIG_FILE));
63+
try {
64+
const targetFile = resolvePath(configFile);
65+
if (!fs.existsSync(targetFile)) {
66+
const template = JSON.parse(JSON.stringify(rsConfig));
67+
template.$schema =
68+
'./node_modules/rollup-scripts/templates/schemas/rs.schema.json';
69+
template.input = path.relative(rsConfig.srcRoot, src);
70+
await fsPromises.writeFile(
71+
targetFile,
72+
prettyJSON(template),
73+
DEFAULT_ENCODING
74+
);
75+
logger.success(MSG_CONFIG(configFile));
76+
}
77+
await generateConfig(finalArgs, configTypes.BABEL, configFiles.BABEL);
78+
await generateConfig(finalArgs, configTypes.ESLINT, configFiles.ESLINT);
79+
} catch (e) {
80+
logger.verbose(e);
7481
}
75-
await generateConfig(finalArgs, configTypes.BABEL, configFiles.BABEL, logger);
76-
await generateConfig(
77-
finalArgs,
78-
configTypes.ESLINT,
79-
configFiles.ESLINT,
80-
logger
81-
);
8282
};

bin/rollup-scripts/lint/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
getInputProps,
66
updateArgs,
77
getLogger,
8+
getRsConfig,
89
} = require('../../../utils');
910
const eslintConfig = require('../../../templates/eslint.config');
1011
const {
@@ -16,8 +17,9 @@ const {
1617

1718
module.exports = async function lint(args) {
1819
const logger = getLogger(args);
19-
const { typescript, react, preact } = getInputProps(args, logger);
20-
const finalArgs = updateArgs(args, { typescript, react, preact });
20+
const { srcRoot } = getRsConfig(args);
21+
const { sourceTypes } = getInputProps(args);
22+
const finalArgs = updateArgs(args, sourceTypes);
2123
logger.log(MSG_LINT);
2224
logger.timeStart(MSG_LINTED);
2325
const eslintConfigFile = await check(configTypes.ESLINT);
@@ -32,7 +34,7 @@ module.exports = async function lint(args) {
3234
overrideConfig,
3335
fix,
3436
});
35-
const results = await eslint.lintFiles(resolvePath('src/**/*'));
37+
const results = await eslint.lintFiles(resolvePath(`${srcRoot}/**/*`));
3638
totalFiles = results.length;
3739
results.forEach((result) => {
3840
errorCount += result.errorCount;

constants/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const JSX_MODULES = ['react', 'preact'];
2525

2626
const ROOT = fs.realpathSync(process.cwd());
2727
const SCRIPT_ROOT = path.resolve(__dirname, '../');
28-
const CONFIG_FILE = 'rsconfig.js';
28+
const CONFIG_FILE = 'rs.json';
2929
const OUT = 'dist/umd/output.js';
3030

3131
const EXT_REGEX = /\.(j|mj|cj|t|mt|ct)sx?$/;
@@ -57,15 +57,15 @@ const MSG_LINTED = 'Completed in';
5757
const MSG_LINTER = (totalFiles, errorCount, warningCount) =>
5858
`Checked ${totalFiles} files! Found ${errorCount} errors and ${warningCount} warnings.`;
5959
const MSG_EMITTED = 'Emitted:';
60-
const MSG_BABELRC = (babelrcFile) => `Using ${babelrcFile}`;
60+
const MSG_BABELRC = 'Using babel config from workspace';
6161
const MSG_CONFIG = (filename) => `${PREFIX}${filename}`;
6262
const MSG_CONFIGBABEL = `${PREFIX}.babelrc`;
6363
const MSG_CONFIGESLINT = `${PREFIX}.eslintrc.json`;
6464
const ERR_NOTFOUND = 'File not found!';
6565
const ERR_ENTRYFILE =
66-
'Warning: Entry file not detected automatically. Run the following command to configure entry file.';
66+
'Warning: Input file could not be resolved. Using "index.mjs" as default. Run the following command to configure "input" if you feel this is not right:';
6767
const ERR_SILENT_VERBOSE =
68-
'Warning: Verbose option is currently enabled. Silent option will be ignored!';
68+
'Warning: "--verbose" is currently enabled. "--silent" will be ignored!';
6969
const CMD_INIT = `npx ${SCRIPT_NAME} init`;
7070
const ERR_JSX_MODULE = (modules) =>
7171
`More than one JSX runtime detected ==> ${modules.join(', ')}`;

0 commit comments

Comments
 (0)