fix: support import assertions (#20009)

Fixes #19928

Co-authored-by: Max Schmitt <max@schmitt.mx>
This commit is contained in:
Andrey Lushnikov 2023-01-16 08:26:14 -08:00 committed by GitHub
parent b740aa04e7
commit 92375f6333
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 1 deletions

View file

@ -42,6 +42,7 @@ This project incorporates components from the projects listed below. The origina
- @babel/plugin-syntax-async-generators@7.8.4 (https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-async-generators)
- @babel/plugin-syntax-dynamic-import@7.8.3 (https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-dynamic-import)
- @babel/plugin-syntax-export-namespace-from@7.8.3 (https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-export-namespace-from)
- @babel/plugin-syntax-import-assertions@7.20.0 (https://github.com/babel/babel)
- @babel/plugin-syntax-json-strings@7.8.3 (https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-json-strings)
- @babel/plugin-syntax-jsx@7.18.6 (https://github.com/babel/babel)
- @babel/plugin-syntax-logical-assignment-operators@7.10.4 (https://github.com/babel/babel)
@ -1338,6 +1339,33 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=========================================
END OF @babel/plugin-syntax-export-namespace-from@7.8.3 AND INFORMATION
%% @babel/plugin-syntax-import-assertions@7.20.0 NOTICES AND INFORMATION BEGIN HERE
=========================================
MIT License
Copyright (c) 2014-present Sebastian McKenzie and other contributors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=========================================
END OF @babel/plugin-syntax-import-assertions@7.20.0 AND INFORMATION
%% @babel/plugin-syntax-json-strings@7.8.3 NOTICES AND INFORMATION BEGIN HERE
=========================================
MIT License
@ -3841,6 +3869,6 @@ END OF update-browserslist-db@1.0.10 AND INFORMATION
SUMMARY BEGIN HERE
=========================================
Total Packages: 131
Total Packages: 132
=========================================
END OF SUMMARY

View file

@ -22,6 +22,7 @@
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/plugin-proposal-private-property-in-object": "^7.18.6",
"@babel/plugin-syntax-async-generators": "^7.8.4",
"@babel/plugin-syntax-import-assertions": "^7.20.0",
"@babel/plugin-syntax-json-strings": "^7.8.3",
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",
"@babel/plugin-syntax-optional-catch-binding": "^7.8.3",
@ -540,6 +541,20 @@
"@babel/core": "^7.0.0-0"
}
},
"node_modules/@babel/plugin-syntax-import-assertions": {
"version": "7.20.0",
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-import-assertions/-/plugin-syntax-import-assertions-7.20.0.tgz",
"integrity": "sha512-IUh1vakzNoWalR8ch/areW7qFopR2AEw03JlG7BbrDqmQ4X3q9uuipQwSGrUn7oGiemKjtSLDhNtQHzMHr1JdQ==",
"dependencies": {
"@babel/helper-plugin-utils": "^7.19.0"
},
"engines": {
"node": ">=6.9.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
}
},
"node_modules/@babel/plugin-syntax-json-strings": {
"version": "7.8.3",
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.8.3.tgz",
@ -1467,6 +1482,14 @@
"@babel/helper-plugin-utils": "^7.8.3"
}
},
"@babel/plugin-syntax-import-assertions": {
"version": "7.20.0",
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-import-assertions/-/plugin-syntax-import-assertions-7.20.0.tgz",
"integrity": "sha512-IUh1vakzNoWalR8ch/areW7qFopR2AEw03JlG7BbrDqmQ4X3q9uuipQwSGrUn7oGiemKjtSLDhNtQHzMHr1JdQ==",
"requires": {
"@babel/helper-plugin-utils": "^7.19.0"
}
},
"@babel/plugin-syntax-json-strings": {
"version": "7.8.3",
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.8.3.tgz",

View file

@ -23,6 +23,7 @@
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/plugin-proposal-private-property-in-object": "^7.18.6",
"@babel/plugin-syntax-async-generators": "^7.8.4",
"@babel/plugin-syntax-import-assertions": "^7.20.0",
"@babel/plugin-syntax-json-strings": "^7.8.3",
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",
"@babel/plugin-syntax-optional-catch-binding": "^7.8.3",

View file

@ -66,6 +66,8 @@ export function babelTransform(filename: string, isTypeScript: boolean, isModule
if (!isModule) {
plugins.push([require('@babel/plugin-transform-modules-commonjs')]);
plugins.push([require('@babel/plugin-proposal-dynamic-import')]);
} else {
plugins.push([require('@babel/plugin-syntax-import-assertions')]);
}
plugins.unshift(additionalPlugin);

View file

@ -36,6 +36,28 @@ test('should load nested as esm when package.json has type module', async ({ run
expect(result.passed).toBe(1);
});
test('should support import assertions', async ({ runInlineTest, nodeVersion }) => {
// We only support experimental esm mode on Node 16+
test.skip(nodeVersion.major < 16);
const result = await runInlineTest({
'playwright.config.ts': `
import packageJSON from './package.json' assert { type: 'json' };
export default { };
`,
'package.json': JSON.stringify({ type: 'module' }),
'a.esm.test.ts': `
const { test } = pwt;
test('check project name', ({}, testInfo) => {
expect(1).toBe(1);
});
`
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});
test('should import esm from ts when package.json has type module in experimental mode', async ({ runInlineTest, nodeVersion }) => {
// We only support experimental esm mode on Node 16+
test.skip(nodeVersion.major < 16);