Skip to content

Commit

Permalink
feature: redlint: bundle: resolve-filenames: integrate
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Feb 17, 2024
1 parent d1c4579 commit e053ab9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 16 deletions.
20 changes: 15 additions & 5 deletions lib/bundle/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {createProgress} from '@putout/engine-runner/progress';
import * as pluginFilesystem from '@putout/plugin-filesystem';
import * as pluginParseFilenames from './parse-filenames/index.js';
import * as pluginResolveFilenames from './parse-filenames/index.js';
import {
branch as originalBranch,
merge as originalMerge,
Expand Down Expand Up @@ -45,8 +46,6 @@ export const bundle = (from, entry, filesystem, {
});

const places = findPlaces(ast, filesystem, {
fixCount: 1,
progress,
rules: {
'parse-filenames': ['on', {
entry,
Expand All @@ -57,11 +56,22 @@ export const bundle = (from, entry, filesystem, {
],
});

const filenames = places.map(getMessage);

console.log(filenames);
merge(filesystem, [code]);

const code = print(ast);
const filenames = places.map(getMessage);

transform(ast, filesystem, {
progress,
rules: {
'resolve-filenames': ['on', {
filenames,
}],
},
plugins: [
['resolve-filenames', pluginResolveFilenames],
],
});

return merge(filesystem, [code]);
};
5 changes: 5 additions & 0 deletions lib/bundle/resolve-filenames/fixture/esm-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__putout_processor_filesystem([
"/",
["/index.js", "Y29uc3Qge3NlbmQ6IHNlbmR9ID0gcmVxdWlyZSgnL3NlbmQuanMnKTsK"],
["/send.js", "Y29uc3Qgb25lID0gcmVxdWlyZSgnLzEuanMnKTsKCm1vZHVsZS5leHBvcnRzID0gJ2hlbGxvJzsK"]
]);
5 changes: 5 additions & 0 deletions lib/bundle/resolve-filenames/fixture/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__putout_processor_filesystem([
'/',
['/index.js', "import {send} from './send.js';"],
['/send.js', "import one from './1.js'; export default 'hello';"],
]);
4 changes: 2 additions & 2 deletions lib/bundle/resolve-filenames/fixture/resolve-filenames-fix.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__putout_processor_filesystem([
"/",
["/index.js", "cmVxdWlyZSgnL3NlbmQuanMuanMnKTsK"],
["/send.js", "cmVxdWlyZSgnLzEuanMuanMnKTsKbW9kdWxlLmV4cG9ydHMgPSAnaGVsbG8nOwo="],
["/index.js", "cmVxdWlyZSgnL3NlbmQuanMnKTsK"],
["/send.js", "cmVxdWlyZSgnLzEuanMnKTsKbW9kdWxlLmV4cG9ydHMgPSAnaGVsbG8nOwo="],
["/1.js", "require('./send.js');module.exports = '1'"]
]);
11 changes: 6 additions & 5 deletions lib/bundle/resolve-filenames/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import putout, {operator} from 'putout';
import {dirname} from 'node:path';
import * as resolveRequire from './resolve-require/index.js';
import * as convertEsmToCommonjs from '@putout/plugin-nodejs/convert-esm-to-commonjs';

const {
writeFileContent,
Expand All @@ -11,25 +12,25 @@ const {
export const report = (file) => getFilename(file);
export const fix = (file) => {
const {code} = putout(readFileContent(file), {
fixCount: 1,
rules: {
'resolve-require': ['on', {
dir: dirname(getFilename(file)),
}],
},
plugins: [
['convert-esm-to-commonjs', convertEsmToCommonjs],
['resolve-require', resolveRequire],
],
});

writeFileContent(file, code);
};
export const scan = (root, {push, trackFile}) => {
const filenames = [
'/index.js',
'/send.js',
];
export const scan = (root, {push, trackFile, options}) => {
const {filenames} = options;

for (const file of trackFile(root, filenames)) {
push(file);
}
};

15 changes: 13 additions & 2 deletions lib/bundle/resolve-filenames/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ const test = createTest(import.meta.url, {
});

test('redlint: bundle: resolve-filenames: report', (t) => {
t.report('resolve-filenames', `/index.js`);
t.reportWithOptions('resolve-filenames', `/index.js`, {
filenames: ['/index.js', '/send.js'],
});
t.end();
});

test('redlint: bundle: resolve-filenames: transform', (t) => {
t.transform('resolve-filenames');
t.transformWithOptions('resolve-filenames', {
filenames: ['/index.js', '/send.js'],
});
t.end();
});

test('redlint: bundle: resolve-filenames: transform: esm', (t) => {
t.transformWithOptions('esm', {
filenames: ['/index.js', '/send.js'],
});
t.end();
});
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
const a = require('/b.js');
const c = require('/c.js');
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
const a = require('./b');
const a = require('./b');
const c = require('./c.js');
9 changes: 8 additions & 1 deletion lib/bundle/resolve-filenames/resolve-require/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ const {
setLiteralValue,
} = operator;

const maybeAddJs = (a) => {
if (a.endsWith('.js'))
return a;

return `${a}.js`;
};

export const report = ({filename}) => filename;
export const fix = ({path, filename}) => {
const [arg] = path.node.arguments;
setLiteralValue(arg, `${filename}.js`);
setLiteralValue(arg, maybeAddJs(filename));
};

const REQUIRE = 'require(__a)';
Expand Down

0 comments on commit e053ab9

Please sign in to comment.