Skip to content

Commit d234dac

Browse files
authored
chore: support esm imports (#3125)
1 parent 21581a4 commit d234dac

File tree

15 files changed

+343
-9
lines changed

15 files changed

+343
-9
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
if: ${{ always() }}
4747
with:
4848
name: ${{ matrix.browser }}-linux-jest-report
49-
path: jest-report.json
49+
path: jest-report.json
5050
- uses: actions/upload-artifact@v1
5151
if: failure()
5252
with:
@@ -82,7 +82,7 @@ jobs:
8282
if: ${{ always() }}
8383
with:
8484
name: ${{ matrix.browser }}-mac-jest-report
85-
path: jest-report.json
85+
path: jest-report.json
8686
- uses: actions/upload-artifact@v1
8787
if: failure()
8888
with:
@@ -134,6 +134,7 @@ jobs:
134134
test-package-installations:
135135
runs-on: ubuntu-latest
136136
strategy:
137+
fail-fast: false
137138
matrix:
138139
node_version:
139140
- "^10.17.0" # pre 10.17, --unhandled-rejections=strict was not an option (https://github.com/nodejs/node/pull/26599) which we need in our tests

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ lib/injected/
1414
!index.d.ts
1515

1616
!index.js
17+
!index.mjs
1718
!install.js
1819
!README.md
1920
!LICENSE

packages/build_package.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ if (args.some(arg => arg === '--help')) {
8383

8484
// 2. Setup cleanup if needed
8585
if (!args.some(arg => arg === '--no-cleanup')) {
86-
process.on('exit', () => {
86+
process.on('exit', () => {
8787
cleanupPaths.forEach(cleanupPath => rmSync(cleanupPath, {}));
8888
});
8989
process.on('SIGINT', () => process.exit(2));
@@ -123,6 +123,10 @@ if (!package) {
123123
engines: pwInternalJSON.engines,
124124
homepage: pwInternalJSON.homepage,
125125
main: 'index.js',
126+
exports: {
127+
import: './index.mjs',
128+
require: './index.js',
129+
},
126130
scripts: {
127131
install: 'node install.js',
128132
},
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { chromium, selectors, devices, errors } from 'playwright-chromium';
18+
import playwright from 'playwright-chromium';
19+
20+
if (playwright.chromium !== chromium)
21+
process.exit(1);
22+
23+
(async () => {
24+
for (const browserType of [chromium]) {
25+
const browser = await browserType.launch();
26+
const context = await browser.newContext();
27+
const page = await context.newPage();
28+
await page.evaluate(() => navigator.userAgent);
29+
await browser.close();
30+
}
31+
console.log(`esm SUCCESS`);
32+
})().catch(err => {
33+
console.error(err);
34+
process.exit(1);
35+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { firefox, selectors, devices, errors } from 'playwright-firefox';
18+
import playwright from 'playwright-firefox';
19+
20+
if (playwright.firefox !== firefox)
21+
process.exit(1);
22+
23+
(async () => {
24+
for (const browserType of [firefox]) {
25+
const browser = await browserType.launch();
26+
const context = await browser.newContext();
27+
const page = await context.newPage();
28+
await page.evaluate(() => navigator.userAgent);
29+
await browser.close();
30+
}
31+
console.log(`esm SUCCESS`);
32+
})().catch(err => {
33+
console.error(err);
34+
process.exit(1);
35+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { webkit, selectors, devices, errors } from 'playwright-webkit';
18+
import playwright from 'playwright-webkit';
19+
20+
if (playwright.webkit !== webkit)
21+
process.exit(1);
22+
23+
(async () => {
24+
for (const browserType of [webkit]) {
25+
const browser = await browserType.launch();
26+
const context = await browser.newContext();
27+
const page = await context.newPage();
28+
await page.evaluate(() => navigator.userAgent);
29+
await browser.close();
30+
}
31+
console.log(`esm SUCCESS`);
32+
})().catch(err => {
33+
console.error(err);
34+
process.exit(1);
35+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { chromium, firefox, webkit, selectors, devices, errors } from 'playwright';
18+
import playwright from 'playwright';
19+
20+
if (playwright.chromium !== chromium)
21+
process.exit(1);
22+
if (playwright.firefox !== firefox)
23+
process.exit(1);
24+
if (playwright.webkit !== webkit)
25+
process.exit(1);
26+
27+
(async () => {
28+
for (const browserType of [chromium, firefox, webkit]) {
29+
const browser = await browserType.launch();
30+
const context = await browser.newContext();
31+
const page = await context.newPage();
32+
await page.evaluate(() => navigator.userAgent);
33+
await browser.close();
34+
}
35+
console.log(`esm SUCCESS`);
36+
})().catch(err => {
37+
console.error(err);
38+
process.exit(1);
39+
});

packages/installation-tests/installation-tests.sh

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@ PLAYWRIGHT_CHROMIUM_TGZ="$(node ${PACKAGE_BUILDER} playwright-chromium ./playwri
2222
PLAYWRIGHT_WEBKIT_TGZ="$(node ${PACKAGE_BUILDER} playwright-webkit ./playwright-webkit.tgz)"
2323
PLAYWRIGHT_FIREFOX_TGZ="$(node ${PACKAGE_BUILDER} playwright-firefox ./playwright-firefox.tgz)"
2424

25-
SANITY_JS="$(pwd -P)/../sanity.js"
25+
SCRIPTS_PATH="$(pwd -P)/.."
2626
TEST_ROOT="$(pwd -P)"
27+
NODE_VERSION="$(node --version)"
28+
29+
function copy_test_scripts {
30+
cp "${SCRIPTS_PATH}/sanity.js" .
31+
cp "${SCRIPTS_PATH}/esm-playwright.mjs" .
32+
cp "${SCRIPTS_PATH}/esm-playwright-chromium.mjs" .
33+
cp "${SCRIPTS_PATH}/esm-playwright-firefox.mjs" .
34+
cp "${SCRIPTS_PATH}/esm-playwright-webkit.mjs" .
35+
}
2736

2837
function run_tests {
2938
test_typescript_types
@@ -67,7 +76,7 @@ function test_playwright_global_installation {
6776
echo "Directory for shared browsers was not created!"
6877
exit 1
6978
fi
70-
cp ${SANITY_JS} .
79+
copy_test_scripts
7180
if node sanity.js playwright chromium 2>/dev/null; then
7281
echo "Should not be able to launch chromium without PLAYWRIGHT_BROWSERS_PATH variable!"
7382
exit 1
@@ -111,28 +120,44 @@ function test_playwright_should_work {
111120
initialize_test "${FUNCNAME[0]}"
112121

113122
npm install ${PLAYWRIGHT_TGZ}
114-
cp ${SANITY_JS} . && node sanity.js playwright chromium firefox webkit
123+
copy_test_scripts
124+
node sanity.js playwright chromium firefox webkit
125+
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
126+
node esm-playwright.mjs
127+
fi
115128
}
116129

117130
function test_playwright_chromium_should_work {
118131
initialize_test "${FUNCNAME[0]}"
119132

120133
npm install ${PLAYWRIGHT_CHROMIUM_TGZ}
121-
cp ${SANITY_JS} . && node sanity.js playwright-chromium chromium
134+
copy_test_scripts
135+
node sanity.js playwright-chromium chromium
136+
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
137+
node esm-playwright-chromium.mjs
138+
fi
122139
}
123140

124141
function test_playwright_webkit_should_work {
125142
initialize_test "${FUNCNAME[0]}"
126143

127144
npm install ${PLAYWRIGHT_WEBKIT_TGZ}
128-
cp ${SANITY_JS} . && node sanity.js playwright-webkit webkit
145+
copy_test_scripts
146+
node sanity.js playwright-webkit webkit
147+
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
148+
node esm-playwright-webkit.mjs
149+
fi
129150
}
130151

131152
function test_playwright_firefox_should_work {
132153
initialize_test "${FUNCNAME[0]}"
133154

134155
npm install ${PLAYWRIGHT_FIREFOX_TGZ}
135-
cp ${SANITY_JS} . && node sanity.js playwright-firefox firefox
156+
copy_test_scripts
157+
node sanity.js playwright-firefox firefox
158+
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
159+
node esm-playwright-firefox.mjs
160+
fi
136161
}
137162

138163
function initialize_test {

packages/installation-tests/sanity.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
const requireName = process.argv[2];
218
const browsers = process.argv.slice(3);
319

@@ -11,6 +27,7 @@ const playwright = require(requireName);
1127
await page.evaluate(() => navigator.userAgent);
1228
await browser.close();
1329
}
30+
console.log(`require SUCCESS`);
1431
})().catch(err => {
1532
console.error(err);
1633
process.exit(1);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import playwright from './index.js';
18+
19+
export const chromium = playwright.chromium;
20+
export const selectors = playwright.selectors;
21+
export const devices = playwright.devices;
22+
export const errors = playwright.errors;
23+
export default playwright;

0 commit comments

Comments
 (0)