Skip to content

Commit

Permalink
Migrate server to ESM (#1003)
Browse files Browse the repository at this point in the history
* Add extensions to all imports

* Output ESM and CJS for dual publish

* Force extensions in ESLint

* Bump babel/runtime
  • Loading branch information
7rulnik committed Apr 13, 2024
1 parent 8f40762 commit c23df05
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 17 deletions.
15 changes: 14 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@ function getTargets() {
return undefined
}

function getModules() {
if (process.env.MODULE_TARGET === 'cjs') {
return 'cjs'
}
if (process.env.MODULE_TARGET === 'esm') {
return false
}
return 'auto'
}

module.exports = {
presets: [
['@babel/preset-react', { useBuiltIns: true }],
['@babel/preset-env', { loose: true, targets: getTargets() }],
[
'@babel/preset-env',
{ loose: true, targets: getTargets(), modules: getModules() },
],
],
plugins: ['@babel/plugin-proposal-class-properties'],
}
2 changes: 1 addition & 1 deletion packages/component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"react": "^16.3.0 || ^17.0.0 || ^18.0.0"
},
"dependencies": {
"@babel/runtime": "^7.7.7",
"@babel/runtime": "^7.12.18",
"hoist-non-react-statics": "^3.3.1",
"react-is": "^16.12.0"
}
Expand Down
5 changes: 5 additions & 0 deletions packages/server/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"import/extensions": ["error", "always", { "ignorePackages": true }]
}
}
17 changes: 15 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
"name": "@loadable/server",
"description": "Server utilities for loadable.",
"version": "5.16.2",
"main": "lib/index.js",
"type": "module",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"exports": {
".": {
"require": "./lib/cjs/index.js",
"import": "./lib/esm/index.js",
"default": "./lib/cjs/index.js"
}
},
"repository": "git@github.com:gregberge/loadable-components.git",
"author": "Greg Bergé <berge.greg@gmail.com>",
"publishConfig": {
Expand All @@ -21,7 +30,11 @@
"license": "MIT",
"scripts": {
"prebuild": "shx rm -rf lib",
"build": "BUILD_TARGET=node babel --config-file ../../babel.config.js -d lib --ignore \"**/*.test.js\" src",
"build:esm": "MODULE_TARGET=esm yarn run build:script -d lib/esm",
"build:cjs": "MODULE_TARGET=cjs yarn run build:script -d lib/cjs && yarn run create-cjs-package-json",
"build:script": "BUILD_TARGET=node babel --config-file ../../babel.config.js --ignore \"**/*.test.js\" src",
"build": "yarn build:esm && yarn build:cjs",
"create-cjs-package-json": "echo '{\"type\": \"commonjs\"}' > ./lib/cjs/package.json",
"prepublishOnly": "yarn run build",
"update-fixtures": "yarn --cwd ../../examples/__fixtures__ build:webpack && rm -rf ./__fixtures__ && cp -R ../../examples/__fixtures__/target ./__fixtures__ "
},
Expand Down
12 changes: 6 additions & 6 deletions packages/server/src/ChunkExtractor.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable react/no-danger */
import path from 'path'
import fs from 'fs'
import uniq from 'lodash/uniq'
import uniqBy from 'lodash/uniqBy'
import flatMap from 'lodash/flatMap'
import uniq from 'lodash/uniq.js'
import uniqBy from 'lodash/uniqBy.js'
import flatMap from 'lodash/flatMap.js'
import React from 'react'
import { invariant, getRequiredChunkKey } from './sharedInternals'
import ChunkExtractorManager from './ChunkExtractorManager'
import { smartRequire, joinURLPath, readJsonFileSync } from './util'
import { invariant, getRequiredChunkKey } from './sharedInternals.js'
import ChunkExtractorManager from './ChunkExtractorManager.js'
import { smartRequire, joinURLPath, readJsonFileSync } from './util.js'

const EXTENSION_SCRIPT_TYPES = {
'.js': 'script',
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/ChunkExtractor.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable import/no-extraneous-dependencies */
import 'regenerator-runtime/runtime'
import 'regenerator-runtime/runtime.js'
import path from 'path'
import stats from '../__fixtures__/stats.json'
import ChunkExtractor from './ChunkExtractor'
import ChunkExtractor from './ChunkExtractor.js'

const targetPath = path.resolve(
__dirname,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/ChunkExtractorManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Context } from './sharedInternals'
import { Context } from './sharedInternals.js'

const ChunkExtractorManager = ({ extractor, children }) => (
<Context.Provider value={extractor}>{children}</Context.Provider>
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as ChunkExtractorManager } from './ChunkExtractorManager'
export { default as ChunkExtractor } from './ChunkExtractor'
export { default as ChunkExtractorManager } from './ChunkExtractorManager.js'
export { default as ChunkExtractor } from './ChunkExtractor.js'
2 changes: 1 addition & 1 deletion packages/server/src/util.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { joinURLPath } from './util'
import { joinURLPath } from './util.js'

describe('util', () => {
describe('#joinURLPath', () => {
Expand Down
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,14 @@
core-js-pure "^3.0.0"
regenerator-runtime "^0.13.2"

"@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":
"@babel/runtime@^7.12.18":
version "7.24.4"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.24.4.tgz#de795accd698007a66ba44add6cc86542aff1edd"
integrity sha512-dkxf7+hn8mFBwKjs9bvBlArzLVxVbS8usaPUDd5p2a9JCL9tB8OaOVN1isD4+Xyk4ns89/xeOmbQvgdK7IIVdA==
dependencies:
regenerator-runtime "^0.14.0"

"@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":
version "7.7.7"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.7.7.tgz#194769ca8d6d7790ec23605af9ee3e42a0aa79cf"
integrity sha512-uCnC2JEVAu8AKB5do1WRIsvrdJ0flYx/A/9f/6chdacnEZ7LmavjdsDXr5ksYBegxtuTPR5Va9/+13QF/kFkCA==
Expand Down Expand Up @@ -8159,6 +8166,11 @@ regenerator-runtime@^0.13.2, regenerator-runtime@^0.13.3:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5"
integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==

regenerator-runtime@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==

regenerator-transform@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.1.tgz#3b2fce4e1ab7732c08f665dfdb314749c7ddd2fb"
Expand Down

0 comments on commit c23df05

Please sign in to comment.