diff --git a/.gitignore b/.gitignore index 8bc5a868..a77f54d9 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,6 @@ node_modules # Extras temp file /tools/owl.js -release-notes.md \ No newline at end of file +release-notes.md + +.rpt2_cache \ No newline at end of file diff --git a/package.json b/package.json index c0056277..b0c0781f 100644 --- a/package.json +++ b/package.json @@ -2,22 +2,19 @@ "name": "@odoo/owl", "version": "1.0.11", "description": "Odoo Web Library (OWL)", - "main": "dist/owl.js", + "main": "dist/owl.cjs.js", + "browser": "dist/owl.iife.js", + "module": "dist/owl.es.js", "types": "dist/types/index.d.ts", "files": [ - "dist/types/", - "dist/owl.js", - "dist/owl-iife.js" + "dist" ], "engines": { "node": ">=10.15.3" }, "scripts": { - "build:js": "tsc --target esnext --module es6 --outDir dist/owl", "build:bundle": "rollup -c", - "build": "npm run build:js && npm run build:bundle", - "buildcommonjs": "npm run build:js && npm run build:bundle -- -f cjs", - "minify": "uglifyjs dist/owl.js -o dist/owl.min.js --compress --mangle", + "build": "npm run build:bundle", "test": "jest", "test:watch": "jest --watch", "tools:serve": "python3 tools/server.py || python tools/server.py", @@ -25,7 +22,7 @@ "pretools:watch": "npm run build", "tools:watch": "npm-run-all --parallel tools:serve \"build:* -- --watch\"", "prettier": "prettier {src/*.ts,src/**/*.ts,tests/*.ts,tests/**/*.ts,doc/*.md,doc/**/*.md} --write", - "publish": "npm run build && mv dist/owl.js dist/owl-iife.js && npm run buildcommonjs && npm publish", + "publish": "npm run build && npm publish", "release": "node tools/release.js" }, "repository": { @@ -38,9 +35,9 @@ "url": "https://github.com/odoo/owl/issues" }, "homepage": "https://github.com/odoo/owl#readme", - "dependencies": {}, "devDependencies": { - "@types/jest": "^23.3.12", + "@types/jest": "^23.3.14", + "@types/node": "^14.11.8", "chalk": "^3.0.0", "cpx": "^1.5.0", "git-rev-sync": "^1.12.0", @@ -51,7 +48,8 @@ "npm-run-all": "^4.1.5", "prettier": "^2.0.4", "rollup": "^1.6.0", - "rollup-plugin-typescript2": "^0.20.1", + "rollup-plugin-terser": "^7.0.2", + "rollup-plugin-typescript2": "^0.27.3", "sass": "^1.16.1", "source-map-support": "^0.5.10", "ts-jest": "^23.10.5", diff --git a/rollup.config.js b/rollup.config.js index 59eae6d1..40499abb 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,14 +1,69 @@ -import { version } from "./package.json"; +import pkg from "./package.json"; import git from "git-rev-sync"; +import typescript from 'rollup-plugin-typescript2'; +import { terser } from "rollup-plugin-terser"; -// rollup.config.js -export default { - input: "dist/owl/index.js", - output: { - file: "dist/owl.js", - format: "iife", - name: "owl", - extend: true, - outro: `exports.__info__.version = '${version}';\nexports.__info__.date = '${new Date().toISOString()}';\nexports.__info__.hash = '${git.short()}';\nexports.__info__.url = 'https://github.com/odoo/owl';` +const name = "owl" +const extend = true + +/** + * Meta data to be added on the __info__ object. + * Used to let external tools know the current owl version. + */ +const outro = ` +__info__.version = '${pkg.version}'; +__info__.date = '${new Date().toISOString()}'; +__info__.hash = '${git.short()}'; +__info__.url = 'https://github.com/odoo/owl'; +` + +/** + * Generate from a string depicting a path a new path for the minified version. + * @param {string} pkgFileName file name + */ +function generateMinifiedNameFromPkgName(pkgFileName) { + const parts = pkgFileName.split('.') + parts.splice(parts.length - 1, 0, "min"); + return parts.join('.'); +} + +/** + * Get the rollup config based on the arguments + * @param {string} format format of the bundle + * @param {string} generatedFileName generated file name + * @param {boolean} minified should it be minified + */ +function getConfigForFormat(format, generatedFileName, minified = false) { + return { + file: minified ? generateMinifiedNameFromPkgName(generatedFileName) : generatedFileName, + format: format, + name: name, + extend: extend, + outro: outro, + plugins: minified ? [terser()] : [] } +} + +export default { + input: "src/index.ts", + output: [ + + /** + * Read about module formats: + * https://auth0.com/blog/javascript-module-systems-showdown/ + * https://medium.com/@kelin2025/so-you-wanna-use-es6-modules-714f48b3a953 + */ + + getConfigForFormat('esm', pkg.module), + getConfigForFormat('esm', pkg.module, true), + getConfigForFormat('cjs', pkg.main), + getConfigForFormat('cjs', pkg.main, true), + getConfigForFormat('iife', pkg.browser), + getConfigForFormat('iife', pkg.browser, true), + ], + plugins: [ + typescript({ + useTsconfigDeclarationDir: true + }), + ] }; diff --git a/tools/server.py b/tools/server.py index ce7f1399..47495a67 100644 --- a/tools/server.py +++ b/tools/server.py @@ -14,9 +14,9 @@ URL = 'http://{0}:{1}/tools'.format(HOST, PORT) # With this, we can simply copy the playground folder as is in the gh-page when # we want to update the playground. class OWLHandler(SimpleHTTPRequestHandler): - def do_GET(self): + def do_GET(self): if self.path == '/tools/owl.js': - self.path = '/dist/owl.js' + self.path = '/dist/owl.iife.js' return SimpleHTTPRequestHandler.do_GET(self) def end_headers(self): diff --git a/tsconfig.json b/tsconfig.json index 03876f95..337d2b59 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,20 +1,107 @@ { + /** + ** Commented-out options have their default values. + **/ + "include": [ + "src/**/*.ts", + "src/*.ts" + ], +                                                               // "exclude": [], + // "files": [],                   // A list of relative or absolute file paths to include. + // "extends": "",                   // A string containing a path to another configuration file to inherit from. + // "references": [],                   // An array of objects `{"path": "./to/dirOrConfig"}` that specifies projects to reference. + // "compileOnSave": false,                   // Signals to the IDE to generate all files for a given tsconfig.json upon saving. "compilerOptions": { - "module": "commonjs", - "preserveConstEnums": true, - "noImplicitThis": true, - "removeComments": false, - "declaration": true, - "target": "esnext", - "outDir": "dist", - "alwaysStrict": true, - "noUnusedLocals": true, - "noUnusedParameters": false, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true, - "strictPropertyInitialization": true, - "strictNullChecks": true, - "declarationDir": "dist/types" - }, - "include": ["src/**/*.ts","src/*.ts"] +                                                             // Main options + "target": "esnext",                                         // Specify ECMAScript target version: 'es3' (default), 'es5', 'es2015', 'es2016', 'es2017','es2018' or 'esnext'. + "module": "esnext",                                         // Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. + // "lib": ["esnext", "dom"],                 // Specify library files to be included in the compilation. + // "allowJs": false,                 // Allow javascript files to be compiled. + // "checkJs": false,                 // Report errors in .js files. + // "outFile": "./",                 // Concatenate and emit output to single file. + "outDir": "dist",                                           // Redirect output structure to the directory. + // "rootDir": "./",                 // Specify the root directory of input files. Use to control the output directory structure with `--outDir`. + // "project": "",                 // Compile a project given a valid configuration file. +                                                             // Compilation options + // "composite": true,                 // Enable project compilation + // "diagnostics": false,                 // Show diagnostic information. + // "incremental": true,                 // Enable incremental compilation by reading/writing information from prior compilations to a file on disk. + // "isolatedModules": false,                 // Transpile each file as a separate module (similar to 'ts.transpileModule'). + // "listEmittedFiles": false,                 // Print names of generated files part of the compilation. + // "listFiles": true,                 // Print names of files part of the compilation. + // "noErrorTruncation": false,                 // Do not truncate error messages. + // "preserveWatchOutput": false,                 // Keep outdated console output in watch mode instead of clearing the screen. + // "traceResolution": false,                 // Enable tracing of the name resolution process. + // "tsBuildInfoFile": ".tsbuildinfo",                 // Specify file to store incremental compilation information. +                                                             // Strict typechecking options + // "strict": false,                                         // Enable all strict type-checking options. + // "noImplicitAny": true,                 // Raise error on expressions and declarations with an implied 'any' type. + // "noImplicitThis": true,                 // Raise error on 'this' expressions with an implied 'any' type. + // "strictBindCallApply": true,                 // Enable stricter checking of of the `bind`, `call`, and `apply` methods on functions. + // "strictFunctionTypes": true,                 // Disable bivariant parameter checking for function types. + // "strictNullChecks": true,                 // In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any. + // "strictPropertyInitialization": true,                 // Ensure non-undefined class properties are initialized in the constructor. This option requires `--strictNullChecks` be enabled in order to take effect. + // "alwaysStrict": true,                 // Parse in strict mode and emit "use strict" for each source file. +                                                             // Additional checks + // "allowUnreachableCode": false,                 // Do not report errors on unreachable code. + // "allowUnusedLabels": false,                 // Do not report errors on unused labels. + "forceConsistentCasingInFileNames": true,                   // Disallow inconsistently-cased references to the same file. + // "noStrictGenericChecks": false,                 // Disable strict checking of generic signatures in function types. + "noUnusedLocals": true,                                     // Report errors on unused locals. + "noUnusedParameters": false,                                // 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. + // "skipLibCheck": false,                 // Skip type checking of all declaration files (*.d.ts). + // "suppressExcessPropertyErrors": false,                 // Suppress excess property checks for object literals. + // "suppressImplicitAnyIndexErrors": false,                 // Suppress noImplicitAny errors for indexing objects lacking index signatures. +                                                             // 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": [ + "jest", + "node" + ],                                                // Type declaration files to be included in compilation. + // "allowSyntheticDefaultImports": false                    // Allow default imports from modules with no default export. This does not affect code emit, just typechecking. + "esModuleInterop": true,                  // Emit '__importStar' and '__importDefault' helpers for runtime babel ecosystem compatibility and enable '--allowSyntheticDefaultImports' for typesystem compatibility. + // "maxNodeModuleJsDepth": 0,                 // The maximum dependency depth to search under node_modules and load JavaScript files. Only applicable with --allowJs. + // "preserveSymlinks": false,                 // Do not resolve the real path of symlinks. + "resolveJsonModule": true,                                  // Include modules imported with '.json' extension. +                                                             // Emit options + "declaration": true,                                        // Generates corresponding '.d.ts' file. + "declarationDir": "dist/types",                             // Output directory for generated declaration files. + // "declarationMap": false,                 // Generates a sourcemap for each corresponding '.d.ts' file. + // "emitBOM": false,                 // Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. + // "emitDeclarationOnly": false,                 // Only emit ‘.d.ts’ declaration files. + // "importHelpers": false,                 // Import emit helpers from 'tslib'. + // "newLine": "LF",                 // Use the specified end of line sequence to be used when emitting files: "crlf" (windows) or "lf" (unix).” + // "noEmit": true,                 // Do not emit outputs. + // "noEmitHelpers": false,                 // Do not generate custom helper functions like __extends in compiled output. + // "noEmitOnError": false,                 // Do not emit outputs if any errors were reported. + // "noImplicitUseStrict": false,                 // Do not emit "use strict" directives in module output. + // "noResolve": false,                 // Do not add triple-slash references or module import targets to the list of compiled files. + "preserveConstEnums": true,                                 // Do not erase const enum declarations in generated code. + // "removeComments": false,                 // Remove all comments except copy-right header comments beginning with + // "experimentalDecorators": true,                 // Enables experimental support for ES7 decorators. + // "emitDecoratorMetadata": true,                 // Enables experimental support for emitting type metadata for decorators. +                                                             // Source map options + // "sourceMap": false,                 // Generates corresponding '.map' file. + // "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. +                                                             // JSX options + // "jsx": "preserve",                 // Specify JSX code generation: 'preserve', 'react-native', or 'react'. + // "jsxFactory": "React.createElement",                 // Specify the JSX factory function to use when targeting react JSX emit, e.g. 'React.createElement' or 'h'. +                                                             // Other options + // "allowUmdGlobalAccess": true,                 // Allow accessing UMD globals from modules. + // "charset": "utf8",                 // The character set of the input files. + // "downlevelIteration": false,                 // Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. + // "disableSizeLimit": false,                 // Disable size limitation on JavaScript project. + // "keyofStringsOnly": false,                 // Resolve 'keyof' to string valued property names only (no numbers or symbols). + // "noLib": false,                 // Do not include the default library file (lib.d.ts). + // "pretty": true,                 // Stylize errors and messages using color and context. + } }