From e053ab9eb76eb96b21745790826a298f36216f05 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sat, 17 Feb 2024 14:02:18 +0200 Subject: [PATCH] feature: redlint: bundle: resolve-filenames: integrate --- lib/bundle/bundle.js | 20 ++++++++++++++----- .../resolve-filenames/fixture/esm-fix.js | 5 +++++ lib/bundle/resolve-filenames/fixture/esm.js | 5 +++++ .../fixture/resolve-filenames-fix.js | 4 ++-- lib/bundle/resolve-filenames/index.js | 11 +++++----- lib/bundle/resolve-filenames/index.spec.js | 15 ++++++++++++-- .../fixture/resolve-require-fix.js | 1 + .../fixture/resolve-require.js | 3 ++- .../resolve-require/index.js | 9 ++++++++- 9 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 lib/bundle/resolve-filenames/fixture/esm-fix.js create mode 100644 lib/bundle/resolve-filenames/fixture/esm.js diff --git a/lib/bundle/bundle.js b/lib/bundle/bundle.js index ba0dae7..bc8747f 100644 --- a/lib/bundle/bundle.js +++ b/lib/bundle/bundle.js @@ -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, @@ -45,8 +46,6 @@ export const bundle = (from, entry, filesystem, { }); const places = findPlaces(ast, filesystem, { - fixCount: 1, - progress, rules: { 'parse-filenames': ['on', { entry, @@ -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]); }; diff --git a/lib/bundle/resolve-filenames/fixture/esm-fix.js b/lib/bundle/resolve-filenames/fixture/esm-fix.js new file mode 100644 index 0000000..38ae4e7 --- /dev/null +++ b/lib/bundle/resolve-filenames/fixture/esm-fix.js @@ -0,0 +1,5 @@ +__putout_processor_filesystem([ + "/", + ["/index.js", "Y29uc3Qge3NlbmQ6IHNlbmR9ID0gcmVxdWlyZSgnL3NlbmQuanMnKTsK"], + ["/send.js", "Y29uc3Qgb25lID0gcmVxdWlyZSgnLzEuanMnKTsKCm1vZHVsZS5leHBvcnRzID0gJ2hlbGxvJzsK"] +]); diff --git a/lib/bundle/resolve-filenames/fixture/esm.js b/lib/bundle/resolve-filenames/fixture/esm.js new file mode 100644 index 0000000..bd589fc --- /dev/null +++ b/lib/bundle/resolve-filenames/fixture/esm.js @@ -0,0 +1,5 @@ +__putout_processor_filesystem([ + '/', + ['/index.js', "import {send} from './send.js';"], + ['/send.js', "import one from './1.js'; export default 'hello';"], +]); diff --git a/lib/bundle/resolve-filenames/fixture/resolve-filenames-fix.js b/lib/bundle/resolve-filenames/fixture/resolve-filenames-fix.js index 586e371..b655f8d 100644 --- a/lib/bundle/resolve-filenames/fixture/resolve-filenames-fix.js +++ b/lib/bundle/resolve-filenames/fixture/resolve-filenames-fix.js @@ -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'"] ]); diff --git a/lib/bundle/resolve-filenames/index.js b/lib/bundle/resolve-filenames/index.js index 8e4c794..a936358 100644 --- a/lib/bundle/resolve-filenames/index.js +++ b/lib/bundle/resolve-filenames/index.js @@ -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, @@ -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); } }; + diff --git a/lib/bundle/resolve-filenames/index.spec.js b/lib/bundle/resolve-filenames/index.spec.js index d57b860..e48fa13 100644 --- a/lib/bundle/resolve-filenames/index.spec.js +++ b/lib/bundle/resolve-filenames/index.spec.js @@ -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(); }); diff --git a/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require-fix.js b/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require-fix.js index dc34249..e71c78a 100644 --- a/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require-fix.js +++ b/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require-fix.js @@ -1 +1,2 @@ const a = require('/b.js'); +const c = require('/c.js'); diff --git a/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require.js b/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require.js index 869b8a8..1df738e 100644 --- a/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require.js +++ b/lib/bundle/resolve-filenames/resolve-require/fixture/resolve-require.js @@ -1 +1,2 @@ -const a = require('./b'); \ No newline at end of file +const a = require('./b'); +const c = require('./c.js'); diff --git a/lib/bundle/resolve-filenames/resolve-require/index.js b/lib/bundle/resolve-filenames/resolve-require/index.js index 5138ef5..286687c 100644 --- a/lib/bundle/resolve-filenames/resolve-require/index.js +++ b/lib/bundle/resolve-filenames/resolve-require/index.js @@ -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)';