-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proper handling of ES modules externals
Adding support for pure ESM packages ([see](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)). This is achieved by using proper external type (`commonjs` vs. `module`) in webpack's external config, so that proper code is generated to import ES modules in the server bundle. A few important facts: * Node.js requirement had to be bumped to >= 12.17, in order to use the dynamic `import()` * The `server-main.js` cjs entry becomes an "async module", so it returns a Promise that need to be awaited when using `require()` * Yarn PnP does not support ESM yet (see yarnpkg/berry#638); we'll bundle esm dependencies on the server to workaround this when project is using PnP (the `process.versions.pnp` check) Co-authored-by: Ryan Tsao <ryan.j.tsao@gmail.com>
- Loading branch information
1 parent
be2f809
commit aff9322
Showing
17 changed files
with
181 additions
and
29 deletions.
There are no files selected for viewing
File renamed without changes.
1 change: 1 addition & 0 deletions
1
fusion-cli-tests-fixtures/fixtures/node_modules/fixture-pure-esm-pkg/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
fusion-cli-tests-fixtures/fixtures/node_modules/fixture-pure-esm-pkg/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
9 changes: 9 additions & 0 deletions
9
fusion-cli-tests/test/e2e/pure-esm-package-import/fixture/src/main.js
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 @@ | ||
// @noflow | ||
import App from 'fusion-core'; | ||
import pureEsm from 'fixture-pure-esm-pkg'; | ||
|
||
export default async function () { | ||
return new App('element', () => { | ||
return pureEsm; | ||
}); | ||
}; |
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,46 @@ | ||
// @flow | ||
/* eslint-env node */ | ||
|
||
const t = require('assert'); | ||
const path = require('path'); | ||
const puppeteer = require('puppeteer'); | ||
|
||
const dev = require('../setup.js'); | ||
const {start, cmd} = require('../utils.js'); | ||
|
||
const dir = path.resolve(__dirname, './fixture'); | ||
|
||
test('`fusion dev` with pure esm package import', async () => { | ||
const app = dev(dir); | ||
const {browser, url} = await app.setup(); | ||
|
||
const page = await browser.newPage(); | ||
await page.goto(`${url}/`, {waitUntil: 'load'}); | ||
|
||
const content = await page.content(); | ||
t.ok(content.includes('FIXTURE_PURE_ESM_PACKAGE_CONTENT')); | ||
|
||
app.teardown(); | ||
}, 15000); | ||
|
||
test('`fusion build` with pure esm package import', async () => { | ||
await cmd(`build --dir=${dir} --production`); | ||
const {proc, port} = await start(`--dir=${dir}`, { | ||
env: { | ||
...process.env, | ||
NODE_ENV: 'production', | ||
}, | ||
}); | ||
|
||
const browser = await puppeteer.launch({ | ||
args: ['--no-sandbox', '--disable-setuid-sandbox'], | ||
}); | ||
const page = await browser.newPage(); | ||
await page.goto(`http://localhost:${port}/`, {waitUntil: 'load'}); | ||
|
||
const content = await page.content(); | ||
t.ok(content.includes('FIXTURE_PURE_ESM_PACKAGE_CONTENT')); | ||
|
||
browser.close(); | ||
proc.kill('SIGKILL'); | ||
}, 25000); |
3 changes: 0 additions & 3 deletions
3
fusion-cli-tests/test/e2e/transpile-node-modules-extended/fixture/.gitignore
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
fusion-cli-tests/test/e2e/transpile-node-modules/fixture/.gitignore
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
...li-tests/test/e2e/transpile-node-modules/fixture/node_modules/fixture-es2017-pkg/index.js
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
...ests/test/e2e/transpile-node-modules/fixture/node_modules/fixture-es2017-pkg/package.json
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
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,86 @@ | ||
// @flow | ||
/* eslint-env node */ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
function tryReadFile(filepath) { | ||
try { | ||
return fs.readFileSync(filepath, 'utf-8'); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
// Adopted from Node.js internal lib | ||
// @see: https://github.com/nodejs/node/blob/da0ede1ad55a502a25b4139f58aab3fb1ee3bf3f/lib/internal/modules/cjs/loader.js#L288-L336 | ||
const packageJsonCache = new Map(); | ||
function readPackage(requestPath) { | ||
const jsonPath = path.resolve(requestPath, 'package.json'); | ||
|
||
if (packageJsonCache.has(jsonPath)) { | ||
return packageJsonCache.get(jsonPath); | ||
} | ||
|
||
const json = tryReadFile(jsonPath); | ||
if (json === false) { | ||
return false; | ||
} | ||
|
||
try { | ||
const parsed = JSON.parse(json); | ||
const filtered = { | ||
name: parsed.name, | ||
main: parsed.main, | ||
exports: parsed.exports, | ||
imports: parsed.imports, | ||
type: parsed.type, | ||
}; | ||
packageJsonCache.set(jsonPath, filtered); | ||
return filtered; | ||
} catch (e) { | ||
e.path = jsonPath; | ||
e.message = 'Error parsing ' + jsonPath + ': ' + e.message; | ||
throw e; | ||
} | ||
} | ||
|
||
function readPackageScope(checkPath) { | ||
const rootSeparatorIndex = checkPath.indexOf(path.sep); | ||
let separatorIndex; | ||
do { | ||
separatorIndex = checkPath.lastIndexOf(path.sep); | ||
checkPath = checkPath.slice(0, separatorIndex); | ||
if (checkPath.endsWith(path.sep + 'node_modules')) return false; | ||
const pjson = readPackage(checkPath + path.sep); | ||
if (pjson) | ||
return { | ||
data: pjson, | ||
path: checkPath, | ||
}; | ||
} while (separatorIndex > rootSeparatorIndex); | ||
return false; | ||
} | ||
|
||
// @see: https://nodejs.org/api/packages.html#packages_determining_module_system | ||
function isEsModule(modulePath /*: string*/) { | ||
if (modulePath.endsWith('.cjs')) { | ||
return false; | ||
} | ||
|
||
if (modulePath.endsWith('.mjs')) { | ||
return true; | ||
} | ||
|
||
// @see: https://github.com/nodejs/node/blob/da0ede1ad55a502a25b4139f58aab3fb1ee3bf3f/lib/internal/modules/cjs/loader.js#L1120-L1123 | ||
if (modulePath.endsWith('.js')) { | ||
const pkg = readPackageScope(modulePath); | ||
|
||
if (pkg && pkg.data && pkg.data.type === 'module') { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
module.exports = isEsModule; |
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