Skip to content

Commit

Permalink
Update regexp in postbuild
Browse files Browse the repository at this point in the history
to include patterns like `import * as utils from ../utils.ts`
  • Loading branch information
ttsukagoshi committed Sep 18, 2023
1 parent 3bb3c81 commit 25b5d2f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions postbuild/postbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'path';

// The target strings to be modified.
// prettier-ignore
export const TARGET_REGEXP_STR = "^(import (\\S*, )?({[^}]*}|\\S*) from '\\.{1,2}\\/[^']*{{fileName}})(';)$";
export const TARGET_REGEXP_STR = "^(import (\\S*, )?({[^}]*}|\\S*( as \\S*)?) from '\\.{1,2}\\/[^']*{{fileName}})(';)$";

/**
* Find .js files in the given directory and its subfolders, and return an array of file objects,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function createRegexpFromFileNames(fileObjArr) {
*/
export function replaceFileContent(fileContent, regexp, extension) {
if (fileContent.match(regexp)) {
fileContent = fileContent.replace(regexp, `$1${extension}$4`); // insert '.js' before the last single quote
fileContent = fileContent.replace(regexp, `$1${extension}$5`); // insert '.js' before the last single quote
}
return fileContent;
}
Expand Down
4 changes: 2 additions & 2 deletions test/postbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ describe('createRegexpFromFileNames', () => {
describe('replaceFileContent', () => {
it('should replace the content of a file', () => {
const fileContent =
"import { test1 } from './test1';\nimport test2, { test2sub } from './test2';\nimport fs from 'fs';\nimport test2 from 'test2';\nconst test2 = () => {\n console.log('test2');\n};\n\nexport { test2 };\n";
"import { test1 } from './test1';\nimport test2, { test2sub } from './test2';\nimport fs from 'fs';\nimport test2 from 'test2';\nimport * as utils from './test2';\nconst test2 = () => {\n console.log('test2');\n};\n\nexport { test2 };\n";
const regexp = new RegExp(
postbuild.TARGET_REGEXP_STR.replace('{{fileName}}', 'test2'),
'gm',
);
const extension = '.js';
expect(fileContent.match(regexp)).not.toBeNull();
expect(postbuild.replaceFileContent(fileContent, regexp, extension)).toBe(
"import { test1 } from './test1';\nimport test2, { test2sub } from './test2.js';\nimport fs from 'fs';\nimport test2 from 'test2';\nconst test2 = () => {\n console.log('test2');\n};\n\nexport { test2 };\n",
"import { test1 } from './test1';\nimport test2, { test2sub } from './test2.js';\nimport fs from 'fs';\nimport test2 from 'test2';\nimport * as utils from './test2.js';\nconst test2 = () => {\n console.log('test2');\n};\n\nexport { test2 };\n",
);
});
});
Expand Down

0 comments on commit 25b5d2f

Please sign in to comment.