Skip to content

Commit c23df05

Browse files
authored
Migrate server to ESM (#1003)
* Add extensions to all imports * Output ESM and CJS for dual publish * Force extensions in ESLint * Bump babel/runtime
1 parent 8f40762 commit c23df05

File tree

10 files changed

+60
-17
lines changed

10 files changed

+60
-17
lines changed

babel.config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@ function getTargets() {
55
return undefined
66
}
77

8+
function getModules() {
9+
if (process.env.MODULE_TARGET === 'cjs') {
10+
return 'cjs'
11+
}
12+
if (process.env.MODULE_TARGET === 'esm') {
13+
return false
14+
}
15+
return 'auto'
16+
}
17+
818
module.exports = {
919
presets: [
1020
['@babel/preset-react', { useBuiltIns: true }],
11-
['@babel/preset-env', { loose: true, targets: getTargets() }],
21+
[
22+
'@babel/preset-env',
23+
{ loose: true, targets: getTargets(), modules: getModules() },
24+
],
1225
],
1326
plugins: ['@babel/plugin-proposal-class-properties'],
1427
}

packages/component/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"react": "^16.3.0 || ^17.0.0 || ^18.0.0"
4545
},
4646
"dependencies": {
47-
"@babel/runtime": "^7.7.7",
47+
"@babel/runtime": "^7.12.18",
4848
"hoist-non-react-statics": "^3.3.1",
4949
"react-is": "^16.12.0"
5050
}

packages/server/.eslintrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"import/extensions": ["error", "always", { "ignorePackages": true }]
4+
}
5+
}

packages/server/package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
"name": "@loadable/server",
33
"description": "Server utilities for loadable.",
44
"version": "5.16.2",
5-
"main": "lib/index.js",
5+
"type": "module",
6+
"main": "./lib/cjs/index.js",
7+
"module": "./lib/esm/index.js",
8+
"exports": {
9+
".": {
10+
"require": "./lib/cjs/index.js",
11+
"import": "./lib/esm/index.js",
12+
"default": "./lib/cjs/index.js"
13+
}
14+
},
615
"repository": "[email protected]:gregberge/loadable-components.git",
716
"author": "Greg Bergé <[email protected]>",
817
"publishConfig": {
@@ -21,7 +30,11 @@
2130
"license": "MIT",
2231
"scripts": {
2332
"prebuild": "shx rm -rf lib",
24-
"build": "BUILD_TARGET=node babel --config-file ../../babel.config.js -d lib --ignore \"**/*.test.js\" src",
33+
"build:esm": "MODULE_TARGET=esm yarn run build:script -d lib/esm",
34+
"build:cjs": "MODULE_TARGET=cjs yarn run build:script -d lib/cjs && yarn run create-cjs-package-json",
35+
"build:script": "BUILD_TARGET=node babel --config-file ../../babel.config.js --ignore \"**/*.test.js\" src",
36+
"build": "yarn build:esm && yarn build:cjs",
37+
"create-cjs-package-json": "echo '{\"type\": \"commonjs\"}' > ./lib/cjs/package.json",
2538
"prepublishOnly": "yarn run build",
2639
"update-fixtures": "yarn --cwd ../../examples/__fixtures__ build:webpack && rm -rf ./__fixtures__ && cp -R ../../examples/__fixtures__/target ./__fixtures__ "
2740
},

packages/server/src/ChunkExtractor.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable react/no-danger */
22
import path from 'path'
33
import fs from 'fs'
4-
import uniq from 'lodash/uniq'
5-
import uniqBy from 'lodash/uniqBy'
6-
import flatMap from 'lodash/flatMap'
4+
import uniq from 'lodash/uniq.js'
5+
import uniqBy from 'lodash/uniqBy.js'
6+
import flatMap from 'lodash/flatMap.js'
77
import React from 'react'
8-
import { invariant, getRequiredChunkKey } from './sharedInternals'
9-
import ChunkExtractorManager from './ChunkExtractorManager'
10-
import { smartRequire, joinURLPath, readJsonFileSync } from './util'
8+
import { invariant, getRequiredChunkKey } from './sharedInternals.js'
9+
import ChunkExtractorManager from './ChunkExtractorManager.js'
10+
import { smartRequire, joinURLPath, readJsonFileSync } from './util.js'
1111

1212
const EXTENSION_SCRIPT_TYPES = {
1313
'.js': 'script',

packages/server/src/ChunkExtractor.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable import/no-extraneous-dependencies */
2-
import 'regenerator-runtime/runtime'
2+
import 'regenerator-runtime/runtime.js'
33
import path from 'path'
44
import stats from '../__fixtures__/stats.json'
5-
import ChunkExtractor from './ChunkExtractor'
5+
import ChunkExtractor from './ChunkExtractor.js'
66

77
const targetPath = path.resolve(
88
__dirname,

packages/server/src/ChunkExtractorManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import { Context } from './sharedInternals'
2+
import { Context } from './sharedInternals.js'
33

44
const ChunkExtractorManager = ({ extractor, children }) => (
55
<Context.Provider value={extractor}>{children}</Context.Provider>

packages/server/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default as ChunkExtractorManager } from './ChunkExtractorManager'
2-
export { default as ChunkExtractor } from './ChunkExtractor'
1+
export { default as ChunkExtractorManager } from './ChunkExtractorManager.js'
2+
export { default as ChunkExtractor } from './ChunkExtractor.js'

packages/server/src/util.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { joinURLPath } from './util'
1+
import { joinURLPath } from './util.js'
22

33
describe('util', () => {
44
describe('#joinURLPath', () => {

yarn.lock

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,14 @@
801801
core-js-pure "^3.0.0"
802802
regenerator-runtime "^0.13.2"
803803

804-
"@babel/runtime@^7.4.5", "@babel/runtime@^7.5.1", "@babel/runtime@^7.6.2", "@babel/runtime@^7.7.4", "@babel/runtime@^7.7.6", "@babel/runtime@^7.7.7":
804+
"@babel/runtime@^7.12.18":
805+
version "7.24.4"
806+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.24.4.tgz#de795accd698007a66ba44add6cc86542aff1edd"
807+
integrity sha512-dkxf7+hn8mFBwKjs9bvBlArzLVxVbS8usaPUDd5p2a9JCL9tB8OaOVN1isD4+Xyk4ns89/xeOmbQvgdK7IIVdA==
808+
dependencies:
809+
regenerator-runtime "^0.14.0"
810+
811+
"@babel/runtime@^7.4.5", "@babel/runtime@^7.5.1", "@babel/runtime@^7.6.2", "@babel/runtime@^7.7.4", "@babel/runtime@^7.7.6":
805812
version "7.7.7"
806813
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.7.7.tgz#194769ca8d6d7790ec23605af9ee3e42a0aa79cf"
807814
integrity sha512-uCnC2JEVAu8AKB5do1WRIsvrdJ0flYx/A/9f/6chdacnEZ7LmavjdsDXr5ksYBegxtuTPR5Va9/+13QF/kFkCA==
@@ -8159,6 +8166,11 @@ regenerator-runtime@^0.13.2, regenerator-runtime@^0.13.3:
81598166
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5"
81608167
integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==
81618168

8169+
regenerator-runtime@^0.14.0:
8170+
version "0.14.1"
8171+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
8172+
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==
8173+
81628174
regenerator-transform@^0.14.0:
81638175
version "0.14.1"
81648176
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.1.tgz#3b2fce4e1ab7732c08f665dfdb314749c7ddd2fb"

0 commit comments

Comments
 (0)