-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add .js file extension to built output + unwrap folder import/e…
…xport to support native ESM within browser (#30770)
- Loading branch information
Showing
9 changed files
with
217 additions
and
94 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-tokens-7b6f098e-f611-4292-adb9-4c0351d335cb.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "feat: enable .js extension addition and directory import/export unwrapping in build output", | ||
"packageName": "@fluentui/tokens", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
"/**/*.test.tsx" | ||
], | ||
"jsc": { | ||
"baseUrl": ".", | ||
"parser": { | ||
"syntax": "typescript", | ||
"tsx": true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { swc } from './swc'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { type Options as SwcOptions } from '@swc/core'; | ||
|
||
declare module '@swc/core' { | ||
interface BaseModuleConfig { | ||
resolveFully?: boolean; | ||
} | ||
} | ||
|
||
export type Options = SwcOptions & Required<Pick<SwcOptions, 'module' | 'outputPath'>> & { root?: string }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { stripIndents } from '@nx/devkit'; | ||
|
||
import { addJsExtensionToImports } from './utils'; | ||
|
||
describe(`utils`, () => { | ||
describe(`#addJsExtensionToImports`, () => { | ||
it(`should not transform anything if non supported module type is specified`, () => { | ||
const code = stripIndents` | ||
export { themeToTokensObject } from './themeToTokensObject'; | ||
`; | ||
|
||
let actual = addJsExtensionToImports(code, 'umd'); | ||
|
||
expect(actual).toEqual(code); | ||
|
||
actual = addJsExtensionToImports(code, 'amd'); | ||
|
||
expect(actual).toEqual(code); | ||
|
||
actual = addJsExtensionToImports(code, 'systemjs'); | ||
|
||
expect(actual).toEqual(code); | ||
}); | ||
|
||
it(`should add .js extensions for esm`, () => { | ||
const code = stripIndents` | ||
export { themeToTokensObject } from './themeToTokensObject'; | ||
export { tokens } from './tokens'; | ||
export { typographyStyles } from './global/index.js'; | ||
`; | ||
|
||
const actual = addJsExtensionToImports(code, 'es6'); | ||
const expected = stripIndents` | ||
export { themeToTokensObject } from './themeToTokensObject.js'; | ||
export { tokens } from './tokens.js'; | ||
export { typographyStyles } from './global/index.js'; | ||
`; | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it(`should add .js extensions for commonjs`, () => { | ||
const code = stripIndents` | ||
const _themeToTokensObject = require("./themeToTokensObject"); | ||
const _tokens = require("./tokens"); | ||
const _index2 = require("./global/index.js"); | ||
`; | ||
|
||
const actual = addJsExtensionToImports(code, 'commonjs'); | ||
const expected = stripIndents` | ||
const _themeToTokensObject = require("./themeToTokensObject.js"); | ||
const _tokens = require("./tokens.js"); | ||
const _index2 = require("./global/index.js"); | ||
`; | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { type Options } from './types'; | ||
|
||
const importPaths = { | ||
es6: /from\s+["']([^"']+)["']/g, | ||
commonjs: /require\(["']([^"']+)["']\)/g, | ||
}; | ||
export function addJsExtensionToImports(code: string, type: Options['module']['type'] = 'es6') { | ||
if (!(type === 'es6' || type === 'commonjs')) { | ||
return code; | ||
} | ||
|
||
const regex = importPaths[type]; | ||
|
||
return code.replace(regex, (match, importPath) => { | ||
if (importPath.endsWith('.js')) { | ||
return match; | ||
} | ||
return match.replace(importPath, importPath + '.js'); | ||
}); | ||
} | ||
|
||
export function postprocessOutput( | ||
code: string, | ||
options: { moduleType: Options['module']['type']; addExplicitJsExtensionToImports: boolean }, | ||
) { | ||
// Strip @jsx comments, see https://github.com/microsoft/fluentui/issues/29126 | ||
let resultCode = code | ||
.replace('/** @jsxRuntime automatic */', '') | ||
.replace('/** @jsxImportSource @fluentui/react-jsx-runtime */', ''); | ||
|
||
// TODO: Remove after swc implement proper js extension addition https://github.com/microsoft/fluentui/issues/30634 | ||
resultCode = options.addExplicitJsExtensionToImports | ||
? addJsExtensionToImports(resultCode, options.moduleType) | ||
: resultCode; | ||
|
||
return resultCode; | ||
} |
Oops, something went wrong.