From 7c6755d3ecca774a332390c1e271bac5c0f363a9 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 17 Jul 2018 14:48:06 -0600 Subject: [PATCH] Scripts for generating the package and legacy build --- .gitignore | 2 -- config/rollup.js | 2 +- package.json | 23 ++++++++--------------- tasks/generate-index.js | 16 +++++----------- tasks/prepare-package.js | 28 ++++++++++------------------ tasks/transform-types.js | 31 ------------------------------- tasks/typecheck.js | 28 ---------------------------- 7 files changed, 24 insertions(+), 106 deletions(-) delete mode 100644 tasks/transform-types.js delete mode 100644 tasks/typecheck.js diff --git a/.gitignore b/.gitignore index d914ce0fc6..6df267a88f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,3 @@ /coverage/ /dist/ node_modules/ -src/index.js -src/ol/package.json diff --git a/config/rollup.js b/config/rollup.js index 18297d1ea0..4d19d05ffd 100644 --- a/config/rollup.js +++ b/config/rollup.js @@ -7,7 +7,7 @@ import buble from 'rollup-plugin-buble'; import sourcemaps from 'rollup-plugin-sourcemaps'; export default { - input: 'src/index.js', + input: 'build/index.js', output: [ {file: 'build/ol.js', format: 'iife', sourcemap: true} ], diff --git a/package.json b/package.json index 0f6d53058b..c014623699 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "openlayers", + "name": "ol", "version": "5.0.3", "description": "OpenLayers mapping library", "keywords": [ @@ -7,6 +7,7 @@ "mapping", "ol" ], + "private": true, "homepage": "https://openlayers.org/", "scripts": { "lint": "eslint tasks test src/ol examples config", @@ -15,18 +16,13 @@ "karma": "karma start test/karma.config.js", "serve-examples": "webpack-dev-server --config examples/webpack/config.js --mode development --watch", "build-examples": "webpack --config examples/webpack/config.js --mode production", - "build-index": "node tasks/generate-index", - "prepare-package": "node tasks/prepare-package", - "prebuild": "npm run prepare-package && npm run build-index", - "prepare": "npm run prepare-package", - "build": "rollup --config config/rollup.js && cleancss --source-map src/ol/ol.css -o build/ol.css", - "presrc-closure": "npm run prebuild", - "src-closure": "node tasks/transform-types", - "pretypecheck": "npm run src-closure", - "typecheck": "node tasks/typecheck", + "build-package": "npm run transpile && node tasks/prepare-package", + "build-index": "npm run build-package && node tasks/generate-index", + "build-legacy": "rm -rf build && npm run build-index && rollup --config config/rollup.js && cleancss --source-map src/ol/ol.css -o build/ol.css", + "transpile": "rm -rf build/ol && mkdir -p build && buble --input src/ol --output build/ol --no modules --sourcemap", "apidoc": "jsdoc config/jsdoc/api/index.md -c config/jsdoc/api/conf.json -P package.json -d build/apidoc" }, - "main": "src/ol/index.js", + "main": "index.js", "repository": { "type": "git", "url": "git://github.com/openlayers/openlayers.git" @@ -41,8 +37,7 @@ "rbush": "2.0.2" }, "devDependencies": { - "babel-core": "^6.26.3", - "babel-plugin-jsdoc-closure": "1.5.1", + "buble": "^0.19.3", "buble-loader": "^0.5.1", "chaikin-smooth": "^1.0.4", "clean-css-cli": "4.1.11", @@ -54,7 +49,6 @@ "front-matter": "^2.1.2", "fs-extra": "^6.0.0", "glob": "^7.1.2", - "google-closure-compiler": "20180610.0.2", "handlebars": "4.0.11", "istanbul": "0.4.5", "jquery": "3.3.1", @@ -72,7 +66,6 @@ "mustache": "^2.3.0", "pixelmatch": "^4.0.2", "proj4": "2.4.4", - "recast": "0.15.2", "rollup": "0.62.0", "rollup-plugin-buble": "0.19.2", "rollup-plugin-commonjs": "9.1.3", diff --git a/tasks/generate-index.js b/tasks/generate-index.js index 5bd07243dd..c94638428e 100644 --- a/tasks/generate-index.js +++ b/tasks/generate-index.js @@ -12,12 +12,6 @@ async function getSymbols() { return info.symbols.filter(symbol => symbol.kind != 'member'); } -const srcPath = path.posix.resolve(__dirname, '../src').replace(/\\/g, '/'); -function getPath(name) { - const fullPath = require.resolve(path.resolve('src', name)); - return './' + path.posix.relative(srcPath, fullPath.replace(/\\/g, '/')); -} - /** * Generate a list of imports. * @param {Array.} symbols List of symbols. @@ -31,12 +25,12 @@ function getImports(symbols) { if (defaultExport.length > 1) { const from = defaultExport[0].replace(/^module\:/, './'); const importName = from.replace(/[.\/]+/g, '$'); - const defaultImport = `import ${importName} from '${getPath(from)}';`; + const defaultImport = `import ${importName} from '${from}';`; imports[defaultImport] = true; } else if (namedExport.length > 1) { const from = namedExport[0].replace(/^module\:/, './'); const importName = from.replace(/[.\/]+/g, '_'); - const namedImport = `import * as ${importName} from '${getPath(from)}';`; + const namedImport = `import * as ${importName} from '${from}';`; imports[namedImport] = true; } }); @@ -86,14 +80,14 @@ function generateExports(symbols, namespaces, imports) { } } }); - const nsdefs = ['const ol = window[\'ol\'] = {};']; + const nsdefs = []; const ns = Object.keys(namespaces).sort(); for (let i = 0, ii = ns.length; i < ii; ++i) { if (namespaces[ns[i]]) { nsdefs.push(`${ns[i]} = {};`); } } - blocks = imports.concat(nsdefs.sort()).concat(blocks.sort()); + blocks = imports.concat('\nvar ol = window[\'ol\'] = {};\n', nsdefs.sort()).concat(blocks.sort()); blocks.push(''); return blocks.join('\n'); } @@ -116,7 +110,7 @@ async function main() { */ if (require.main === module) { main().then(async code => { - const filepath = path.join(__dirname, '..', 'src', 'index.js'); + const filepath = path.join(__dirname, '..', 'build', 'index.js'); await fse.outputFile(filepath, code); }).catch(err => { process.stderr.write(`${err.message}\n`, () => process.exit(1)); diff --git a/tasks/prepare-package.js b/tasks/prepare-package.js index 5397b5dc5a..0234f5e830 100644 --- a/tasks/prepare-package.js +++ b/tasks/prepare-package.js @@ -2,26 +2,18 @@ const fs = require('fs'); const path = require('path'); const pkg = require('../package.json'); -const util = require.resolve('../src/ol/util'); -const lines = fs.readFileSync(util, 'utf-8').split('\n'); -const versionRegEx = /const VERSION = '(.*)';$/; -for (let i = 0, ii = lines.length; i < ii; ++i) { - const line = lines[i]; - if (versionRegEx.test(line)) { - lines[i] = line.replace(versionRegEx, `const VERSION = '${pkg.version}';`); - break; - } -} -fs.writeFileSync(util, lines.join('\n'), 'utf-8'); +const buildDir = path.resolve(__dirname, '../build/ol'); -const src = path.join('src', 'ol'); -const packageJson = path.resolve(__dirname, path.join('..', src, 'package.json')); +// update the version number in util.js +const utilPath = path.join(buildDir, 'util.js'); +const versionRegEx = /const VERSION = '(.*)';/g; +const utilSrc = fs.readFileSync(utilPath, 'utf-8').replace(versionRegEx, `const VERSION = '${pkg.version}';`); +fs.writeFileSync(utilPath, utilSrc, 'utf-8'); + +// write out simplified package.json delete pkg.scripts; delete pkg.devDependencies; delete pkg.style; delete pkg.eslintConfig; -const main = path.posix.relative(src, require.resolve(path.join('..', pkg.main))); -pkg.main = pkg.module = main; -pkg.name = 'ol'; - -fs.writeFileSync(packageJson, JSON.stringify(pkg, null, 2), 'utf-8'); +delete pkg.private; +fs.writeFileSync(path.join(buildDir, 'package.json'), JSON.stringify(pkg, null, 2), 'utf-8'); diff --git a/tasks/transform-types.js b/tasks/transform-types.js deleted file mode 100644 index 90ffb7a6e8..0000000000 --- a/tasks/transform-types.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * @filedesc - * Transforms type comments in all source files to types that Closure Compiler - * understands. - */ - -const glob = require('glob'); -const mkdirp = require('mkdirp').sync; -const fs = require('fs'); -const path = require('path'); -const transform = require('babel-core').transformFileSync; - -const options = { - plugins: 'jsdoc-closure', - parserOpts: { - parser: 'recast' - }, - generatorOpts: { - generator: 'recast' - } -}; - -const outDir = path.join('build', 'src-closure'); - -glob('src/**/*.js', (err, matches) => { - matches.forEach(match => { - const out = path.join(outDir, path.relative('src', match)); - mkdirp(path.dirname(out)); - fs.writeFileSync(out, transform(match, options).code, 'utf-8'); - }); -}); diff --git a/tasks/typecheck.js b/tasks/typecheck.js deleted file mode 100644 index 0a7f667f37..0000000000 --- a/tasks/typecheck.js +++ /dev/null @@ -1,28 +0,0 @@ -const Compiler = require('google-closure-compiler').compiler; - -const compiler = new Compiler({ - js: [ - './build/src-closure/**.js', - // Resolve dependencies - './node_modules/pbf/package.json', './node_modules/pbf/**.js', './node_modules/ieee754/**.js', - './node_modules/pixelworks/package.json', './node_modules/pixelworks/**.js', - './node_modules/rbush/package.json', './node_modules/rbush/**.js', 'node_modules/quickselect/**.js' - ], - entry_point: './build/src-closure/index.js', - module_resolution: 'NODE', - dependency_mode: 'STRICT', - checks_only: true, - jscomp_error: ['newCheckTypes'], - // Options to make dependencies work - process_common_js_modules: true, - hide_warnings_for: 'node_modules' -}); - -compiler.run((exit, out, err) => { - if (exit) { - process.stderr.write(err, () => process.exit(exit)); - } else { - process.stderr.write(err); - process.stdout.write(out); - } -});