-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm <3
- Loading branch information
Showing
15 changed files
with
130 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
#!/usr/bin/env node | ||
import '../dist/cli.mjs' |
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
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
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,45 @@ | ||
import type { Plugin } from 'rollup' | ||
import { findStaticImports } from 'mlly' | ||
import MagicString from 'magic-string' | ||
|
||
export function CJSBridgePlugin (_opts?: any): Plugin { | ||
return { | ||
name: 'cjs-bridge', | ||
renderChunk (code, _chunk, opts) { | ||
if (opts.format === 'es') { | ||
return CJSToESM(code) | ||
} | ||
return null | ||
} | ||
} as Plugin | ||
} | ||
|
||
const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/ | ||
|
||
const CJSShim = ` | ||
// -- Unbuild CommonJS Shims -- | ||
import __cjs_url__ from 'url'; | ||
import __cjs_path__ from 'path'; | ||
import __cjs_mod__ from 'module'; | ||
const __filename = __cjs_url__.fileURLToPath(import.meta.url); | ||
const __dirname = __cjs_path__.dirname(__filename); | ||
const require = __cjs_mod__.createRequire(import.meta.url); | ||
` | ||
|
||
// Shim __dirname, __filename and require | ||
function CJSToESM (code: string) { | ||
if (code.includes(CJSShim) || !CJSyntaxRe.test(code)) { | ||
return null | ||
} | ||
|
||
const lastESMImport = findStaticImports(code).pop() | ||
const indexToAppend = lastESMImport ? lastESMImport.end : 0 | ||
const s = new MagicString(code) | ||
s.appendRight(indexToAppend, CJSShim) | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap() | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1 +1,12 @@ | ||
import { arch } from 'os' | ||
|
||
console.log('__filename', __filename) | ||
console.log('__dirname', __dirname) | ||
console.log('import.meta.url', import.meta.url) | ||
|
||
console.log(arch()) | ||
console.log(require('os').arch()) | ||
console.log(require.resolve('rollup')) | ||
import('os').then(os => console.log(os.arch())) | ||
|
||
export const foo = 'bar' |
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