From 12fe3e156ee13ab555d14987dcfa801e2a425c6d Mon Sep 17 00:00:00 2001 From: epaew Date: Sat, 26 Nov 2022 09:09:57 +0900 Subject: [PATCH 1/4] Update package.json, tsconfig.json: Enable ESM feature --- package.json | 1 + tsconfig.json | 150 +++++++++++++++++++++++++++++++------------------- 2 files changed, 93 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index 222824de..7da960f8 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "repository": "git@github.com:epaew/react-liff", "license": "MIT", "author": "Ryo Maeda ", + "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ diff --git a/tsconfig.json b/tsconfig.json index 06854238..fe27fa38 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,74 +1,108 @@ { "compilerOptions": { - /* Visit https://aka.ms/tsconfig.json to read more about this file */ + /* Visit https://aka.ms/tsconfig to read more about this file */ - /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ - "target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ - "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ - "lib": [ /* Specify library files to be included in the compilation. */ + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2015", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "lib": [ /* Specify a set of bundled library declaration files that describe the target runtime environment. */ "dom", "dom.iterable", "esnext" ], - // "allowJs": true, /* Allow javascript files to be compiled. */ - // "checkJs": true, /* Report errors in .js files. */ - "jsx": "react-jsx", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ - "declaration": true, /* Generates corresponding '.d.ts' file. */ - // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ - // "sourceMap": true, /* Generates corresponding '.map' file. */ - // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./dist", /* Redirect output structure to the directory. */ - "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ - // "composite": true, /* Enable project compilation */ - // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ - // "removeComments": true, /* Do not emit comments to output. */ - // "noEmit": true, /* Do not emit outputs. */ - // "importHelpers": true, /* Import emit helpers from 'tslib'. */ - // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ - // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + "jsx": "react-jsx", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ - /* Strict Type-Checking Options */ - "strict": true, /* Enable all strict type-checking options. */ - // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ - // "strictNullChecks": true, /* Enable strict null checks. */ - // "strictFunctionTypes": true, /* Enable strict checking of function types. */ - // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ - // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ - // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ - // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + /* Modules */ + "module": "nodenext", /* Specify what module code is generated. */ + "rootDir": "./src", /* Specify the root folder within your source files. */ + // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ - /* Additional Checks */ - // "noUnusedLocals": true, /* Report errors on unused locals. */ - // "noUnusedParameters": true, /* Report errors on unused parameters. */ - // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ - // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ - /* Module Resolution Options */ - // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ - // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ - // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ - // "typeRoots": [], /* List of folders to include type definitions from. */ - // "types": [], /* Type declaration files to be included in compilation. */ - // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ - "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ - // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + /* Emit */ + "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./dist", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ - /* Source Map Options */ - // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ - // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ - /* Experimental Options */ - // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ - // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + /* Type Checking */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ - /* Advanced Options */ - "skipLibCheck": true, /* Skip type checking of declaration files. */ - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ }, "include": [ "src/index.ts" From 6ecd0753c8b5d5861a25bc826fdd03a71dccdc26 Mon Sep 17 00:00:00 2001 From: epaew Date: Sat, 26 Nov 2022 11:31:46 +0900 Subject: [PATCH 2/4] import with .js extension --- src/context/create-liff-context.ts | 4 ++-- src/context/create-liff-provider.ts | 6 +++--- src/context/index.ts | 2 +- src/context/types.ts | 2 +- src/hooks/index.ts | 2 +- src/index.ts | 2 +- src/lib/get-initialized-liff.ts | 6 +++++- src/lib/index.ts | 2 +- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/context/create-liff-context.ts b/src/context/create-liff-context.ts index 9c8c6b01..b19644d5 100644 --- a/src/context/create-liff-context.ts +++ b/src/context/create-liff-context.ts @@ -1,8 +1,8 @@ import { Liff } from '@line/liff'; import { createContext, useContext } from 'react'; -import { createLiffProvider } from './create-liff-provider'; -import { CreateLiffContext, LiffContextStates } from './types'; +import { createLiffProvider } from './create-liff-provider.js'; +import { CreateLiffContext, LiffContextStates } from './types.js'; export const createLiffContext: CreateLiffContext = () => { const context = createContext({ diff --git a/src/context/create-liff-provider.ts b/src/context/create-liff-provider.ts index a6da4d2c..f0379fa8 100644 --- a/src/context/create-liff-provider.ts +++ b/src/context/create-liff-provider.ts @@ -2,10 +2,10 @@ import { Liff } from '@line/liff'; import * as PropTypes from 'prop-types'; import { createElement, FC, useEffect, useState } from 'react'; -import { useLoginStateManager } from '../hooks'; -import { getInitializedLiff } from '../lib'; +import { useLoginStateManager } from '../hooks/index.js'; +import { getInitializedLiff } from '../lib/index.js'; -import { CreateLiffProvider, LiffProviderProps } from './types'; +import { CreateLiffProvider, LiffProviderProps } from './types.js'; const LiffProviderPropTypes = { children: PropTypes.element.isRequired, diff --git a/src/context/index.ts b/src/context/index.ts index 08393822..33b943b3 100644 --- a/src/context/index.ts +++ b/src/context/index.ts @@ -1,3 +1,3 @@ -import { createLiffContext } from './create-liff-context'; +import { createLiffContext } from './create-liff-context.js'; export const { LiffConsumer, LiffProvider, useLiff } = createLiffContext(); diff --git a/src/context/types.ts b/src/context/types.ts index e8353055..2c69b411 100644 --- a/src/context/types.ts +++ b/src/context/types.ts @@ -1,7 +1,7 @@ import type { Liff } from '@line/liff'; import type { Consumer, Context, FC, ReactNode } from 'react'; -import type { GetInitializedLiffProps } from '../lib'; +import type { GetInitializedLiffProps } from '../lib/index.js'; export interface CreateLiffContext { (): { diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 1bed10e3..c78d9ec8 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1 +1 @@ -export { useLoginStateManager } from './use-login-state-manager'; +export { useLoginStateManager } from './use-login-state-manager.js'; diff --git a/src/index.ts b/src/index.ts index c38e8e82..00c55032 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export * from './context'; +export * from './context/index.js'; diff --git a/src/lib/get-initialized-liff.ts b/src/lib/get-initialized-liff.ts index 8eb17f8e..8c94224f 100644 --- a/src/lib/get-initialized-liff.ts +++ b/src/lib/get-initialized-liff.ts @@ -19,7 +19,11 @@ interface GetInitializedLiffProps extends LiffConfig { type GetInitializedLiff = (props: GetInitializedLiffProps) => Promise; -const getLiff = async () => window.liff ?? (await import('@line/liff')).default; +const getLiff = async (): Promise => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore: This is an issue of @line/liff + return window.liff ?? ((await import('@line/liff')).default as Liff); +}; const registerLiffPlugin = (liff: Liff, plugin: Plugin) => { Array.isArray(plugin) ? liff.use(...plugin) : liff.use(plugin); diff --git a/src/lib/index.ts b/src/lib/index.ts index 98990d96..5bf19dc0 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1 +1 @@ -export * from './get-initialized-liff'; +export * from './get-initialized-liff.js'; From b868a3a14d82c4ce0788930865a2dbaa76224788 Mon Sep 17 00:00:00 2001 From: epaew Date: Sat, 26 Nov 2022 11:33:10 +0900 Subject: [PATCH 3/4] Update jest.config.js to be able to test native TS modules. --- jest.config.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/jest.config.js b/jest.config.js index 41a16bf3..6d67fc6f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,10 +1,12 @@ -module.exports = { +// eslint-disable-next-line import/no-default-export +export default { collectCoverageFrom: ['/src/**/*.ts{,x}', '!/src/**/*.test.ts{,x}'], - globals: { - 'ts-jest': { - tsconfig: 'tsconfig.test.json', - }, + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', }, - preset: 'ts-jest', + preset: 'ts-jest/presets/default-esm', testEnvironment: 'jsdom', + transform: { + '\\.tsx?$': ['ts-jest', { tsconfig: 'tsconfig.test.json', useESM: true }], + }, }; From 5c6c8fc6ac86bff1441f7cee6411c796ee0fe6c1 Mon Sep 17 00:00:00 2001 From: epaew Date: Sat, 26 Nov 2022 11:37:24 +0900 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35358c88..3c55eda1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ ## BREAKING CHANGES +- [#554](https://github.com/epaew/react-liff/pull/554) + - Publish as native ESM. + ## Others - [#492](https://github.com/epaew/react-liff/pull/492)