From 7d4e749c579d3bfd06d604d5655ad29b28488416 Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 14 Nov 2019 09:35:19 -0600 Subject: [PATCH] Fix the watcher --- .gitignore | 1 + package-lock.json | 127 +++++++++++------------------------------- package.json | 18 +++--- rollup.config.js | 12 ++-- src/index.ts | 71 +++++++++++++++-------- test/rollup.config.js | 2 +- 6 files changed, 93 insertions(+), 138 deletions(-) diff --git a/.gitignore b/.gitignore index 5dfd45c..ffde63e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules dist typings +**globbed** diff --git a/package-lock.json b/package-lock.json index a8890cf..0ddda17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "rollup-plugin-glob", + "name": "rollup-plugin-glob-files", "version": "1.0.1", "lockfileVersion": 1, "requires": true, @@ -17,9 +17,9 @@ "dev": true }, "@types/micromatch": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/@types/micromatch/-/micromatch-3.1.0.tgz", - "integrity": "sha512-06uA9V7v68RTOzA3ky1Oi0HmCPa+YJ050vM+sTECwkxnHUQnO17TAcNCGX400QT6bldUiPb7ux5oKy0j8ccEDw==", + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@types/micromatch/-/micromatch-3.1.1.tgz", + "integrity": "sha512-Wr5y4uv3r7JP4jEUqv7rZeYiMBGRHcbojDVsl11wq6gw1v/ZZQvJexd9rtvVx3EIVqw8dwtcRjSs8m2DV9qHjQ==", "dev": true, "requires": { "@types/braces": "*" @@ -69,58 +69,6 @@ "integrity": "sha512-k0KL0aWZuBt2lrxrcASWDfwOLMnodeQjodT/1SxEQAXsHANgo6ZC/VEaSEHCXt7aSTZ4/4H5LKa+tBXmW7Vtvw==", "dev": true }, - "cross-env": { - "version": "6.0.3", - "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-6.0.3.tgz", - "integrity": "sha512-+KqxF6LCvfhWvADcDPqo64yVIB31gv/jQulX2NGzKS/g3GEVz6/pt4wjHFtFWsHMddebWD/sDthJemzM4MaAag==", - "dev": true, - "requires": { - "cross-spawn": "^7.0.0" - }, - "dependencies": { - "cross-spawn": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.1.tgz", - "integrity": "sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg==", - "dev": true, - "requires": { - "path-key": "^3.1.0", - "shebang-command": "^2.0.0", - "which": "^2.0.1" - } - }, - "path-key": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.0.tgz", - "integrity": "sha512-8cChqz0RP6SHJkMt48FW0A7+qUOn+OsnOsVtzI59tZ8m+5bCSk7hzwET0pulwOM2YMn9J1efb07KB9l9f30SGg==", - "dev": true - }, - "shebang-command": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", - "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", - "dev": true, - "requires": { - "shebang-regex": "^3.0.0" - } - }, - "shebang-regex": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", - "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", - "dev": true - }, - "which": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/which/-/which-2.0.1.tgz", - "integrity": "sha512-N7GBZOTswtB9lkQBZA4+zAXrjEIWAUOB93AvzUiudRzRxhUdLURQ7D/gAIMY1gatT/LTbmbcv8SiYazy3eYB7w==", - "dev": true, - "requires": { - "isexe": "^2.0.0" - } - } - } - }, "error-stack-parser": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/error-stack-parser/-/error-stack-parser-2.0.4.tgz", @@ -135,6 +83,21 @@ "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-0.6.1.tgz", "integrity": "sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==" }, + "flat": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.0.tgz", + "integrity": "sha512-6KSMM+cHHzXC/hpldXApL2S8Uz+QZv+tq5o/L0KQYleoG+GcwrnIJhTWC7tCOiKQp8D/fIvryINU1OZCCwevjA==", + "dev": true, + "requires": { + "is-buffer": "~2.0.4" + } + }, + "is-buffer": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz", + "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==", + "dev": true + }, "is-module": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz", @@ -150,12 +113,6 @@ "@types/estree": "0.0.39" } }, - "isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", - "dev": true - }, "magic-string": { "version": "0.25.4", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.4.tgz", @@ -171,16 +128,10 @@ "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", "dev": true }, - "plain-object-is-equal": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/plain-object-is-equal/-/plain-object-is-equal-1.0.0.tgz", - "integrity": "sha512-6WK64b9kBx5gGOy7vNojQDp+qKPheEnBtTFWq3Z6tNComkhLd0ippiAmARXyyC7wGZ65g0CfD67MpkDG9/XwOg==", - "dev": true - }, "prettier": { - "version": "1.18.2", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz", - "integrity": "sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==", + "version": "1.19.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz", + "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==", "dev": true }, "resolve": { @@ -193,30 +144,16 @@ } }, "rollup": { - "version": "1.26.3", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.26.3.tgz", - "integrity": "sha512-8MhY/M8gnv3Q/pQQSWYWzbeJ5J1C5anCNY5BK1kV8Yzw9RFS0FF4lbLt+uyPO3wLKWXSXrhAL5pWL85TZAh+Sw==", + "version": "1.27.0", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.27.0.tgz", + "integrity": "sha512-yaMna4MJ8LLEHhHl1ilgHakylf0LKeQctDxhngZLQ+W57GnXa5vtH7XKaK8zlAhNEhlWiH5YFVFt+QCDPUmNkw==", "dev": true, "requires": { "@types/estree": "*", "@types/node": "*", "acorn": "^7.1.0" - }, - "dependencies": { - "acorn": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.0.tgz", - "integrity": "sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==", - "dev": true - } } }, - "rollup-plugin-command": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/rollup-plugin-command/-/rollup-plugin-command-1.0.7.tgz", - "integrity": "sha512-co/CQyLjQGWf02hU6BNH1+UI6b+tT+5zaZ0qjAgxv+KETJIKGSRNp4Qyzw0KZIeqkXM2QplAtYL4xDdKW0ZIwg==", - "dev": true - }, "rollup-plugin-commonjs": { "version": "10.1.0", "resolved": "https://registry.npmjs.org/rollup-plugin-commonjs/-/rollup-plugin-commonjs-10.1.0.tgz", @@ -262,9 +199,9 @@ } }, "simply-get-files": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/simply-get-files/-/simply-get-files-1.0.1.tgz", - "integrity": "sha512-+NQjTidgtwJvs7NyIj7vK7HO73PuQhe4UmdFUi6Uvit5iiiINrE4YbSiB+zf7qC6pOkGBY5RbJfrvFFEXKw3pQ==" + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/simply-get-files/-/simply-get-files-1.0.2.tgz", + "integrity": "sha512-2Ye0XlqvhqoiPVaPk/95i2jR5gAs3wfUEdoKLuGnldjNzOFFpbm0cUSGmeLHkOi9sVB3ht3qxMMddj8vx2BsAQ==" }, "source-map": { "version": "0.5.6", @@ -340,12 +277,12 @@ } }, "zip-tap": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/zip-tap/-/zip-tap-1.0.2.tgz", - "integrity": "sha512-eHOC6xPoEnZGpZCymjP4/RFAlPqXu/w3ZcqsC7VPeb0VWvRRHV37myODoEUFpl8YSKTRb4Z+cN4vwXIha6EbGA==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/zip-tap/-/zip-tap-1.0.4.tgz", + "integrity": "sha512-+7uGsuiDhnT7s6BJGEf4dgU1RKPEpFC+od2ib1vYtpMYHGkHj3f/tntR30AKwhd3mqpcY3trZ6q3bCluWwCWLQ==", "dev": true, "requires": { - "plain-object-is-equal": "^1.0.0", + "flat": "^5.0.0", "stacktrace-js": "^2.0.1" } } diff --git a/package.json b/package.json index 6fb320f..2d057b6 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "module": "dist/build.esm.js", "typings": "typings/index.d.ts", "scripts": { - "build": "cross-env NODE_ENV=production rollup -c && tsc", - "test": "cross-env NODE_ENV=development rollup -c", + "build": "rollup -c && tsc", + "test": "rollup -c && cd test && rollup -cw", "lint": "prettier --write \"./**\"", "lint:test": "prettier --check \"./**\"", "preversion": "npm run test && npm run lint:test && npm run build", @@ -30,19 +30,17 @@ ], "license": "MIT", "devDependencies": { - "@types/micromatch": "^3.1.0", + "@types/micromatch": "^3.1.1", + "@types/write": "^2.0.0", "acorn": "^7.1.0", - "cross-env": "^6.0.3", - "prettier": "^1.18.2", - "rollup": "^1.26.3", - "rollup-plugin-command": "^1.0.7", + "prettier": "^1.19.1", + "rollup": "^1.27.0", "rollup-plugin-commonjs": "^10.1.0", "rollup-plugin-node-resolve": "^5.2.0", "rollup-plugin-typescript": "^1.0.1", "tslib": "^1.10.0", "typescript": "^3.7.2", - "zip-tap": "^1.0.2", - "@types/write": "^2.0.0" + "zip-tap": "^1.0.4" }, "homepage": "https://github.com/Vehmloewff/rollup-plugin-glob", "repository": { @@ -51,7 +49,7 @@ }, "dependencies": { "rollup-pluginutils": "^2.8.2", - "simply-get-files": "^1.0.1", + "simply-get-files": "^1.0.2", "write": "^2.0.0" }, "peerDependencies": { diff --git a/rollup.config.js b/rollup.config.js index e8b0661..2439167 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,23 +1,20 @@ import commonjs from 'rollup-plugin-commonjs'; import resolve from 'rollup-plugin-node-resolve'; import pkg from './package.json'; -import command from 'rollup-plugin-command'; import typescript from 'rollup-plugin-typescript'; const name = 'rollupPluginGlob'; const sourcemap = true; -const prod = process.env.NODE_ENV === 'production'; -const watching = process.env.ROLLUP_WATCH; - const sharedOutputOptions = { name, sourcemap, exports: 'named', }; -const output = [{ file: pkg.main, format: 'cjs', ...sharedOutputOptions }]; - -if (prod) output.push({ file: pkg.module, format: 'es', ...sharedOutputOptions }); +const output = [ + { file: pkg.main, format: 'cjs', ...sharedOutputOptions }, + { file: pkg.module, format: 'es', ...sharedOutputOptions }, +]; export default { input: 'src/index.ts', @@ -28,7 +25,6 @@ export default { preferBuiltins: true, }), commonjs(), - !prod && command(`cd test && rollup -c`, { exitOnFail: !watching }), typescript({ typescript: require('typescript'), }), diff --git a/src/index.ts b/src/index.ts index 8342207..16bce3a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,12 +26,53 @@ export type GlobOptions = { justImport?: boolean; }; +let watchFiles: string[] = []; + +const generateCode = async (options: GlobOptions) => { + const filter = createFilter(options.include, options.exclude); + const allFiles = await getFiles(process.cwd()); + const files = allFiles.filter(file => { + return filter(nodePath.resolve(file)) && file != options.file && file != `rollup.config.js`; + }); + const filesToGlob = files.map(name => nodePath.resolve(name)); + + let imports = ``; + let body = ``; + + if (!options.justImport) body += `export default [\n`; + + watchFiles = []; + + filesToGlob.forEach((file, index) => { + watchFiles.push(file); + + if (options.justImport) { + imports += `import '${file}';\n`; + } else { + imports += options.importStar + ? `import * as glob$file${index} from '${file}';\n` + : `import glob$file${index} from '${file}';\n`; + body += ` glob$file${index},\n`; + } + }); + + if (!options.justImport) body += `];`; + + return imports + `\n\n` + body; +}; + export default (options: GlobOptions[] | GlobOptions): Plugin => { let optionsArr: GlobOptions[] = []; if (Array.isArray(options)) optionsArr = options; else optionsArr[0] = options; + optionsArr.map(options => { + if (!options.include) options.include = `./**`; + if (!options.exclude) options.exclude = `./**/node_modules/**`; + return options; + }); + return { name: `glob`, resolveId: async function(id) { @@ -59,32 +100,14 @@ export default (options: GlobOptions[] | GlobOptions): Plugin => { const options = optionsArr.find(opt => nodePath.resolve(opt.file) === id); if (!options) return null; - const filter = createFilter(options.include || `./**`, options.exclude || `./**/node_modules/**`); - const allFiles = await getFiles(process.cwd()); - const files = allFiles.filter(file => { - return filter(nodePath.resolve(file)) && file != options.file && file != `rollup.config.js`; - }); - const filesToGlob = files.map(name => `./${name}`); + code = await generateCode(options); - let imports = ``; - let body = ``; + watchFiles.forEach(file => this.addWatchFile(file)); - if (!options.justImport) body += `export default [\n`; - - filesToGlob.forEach((file, index) => { - if (options.justImport) { - imports += `import '${file}';\n`; - } else { - imports += options.importStar - ? `import * as glob$file${index} from '${file}';\n` - : `import glob$file${index} from '${file}';\n`; - body += ` glob$file${index},\n`; - } - }); - - if (!options.justImport) body += `];`; - - return { code: imports + `\n\n` + body + `\n\n` + code }; + return { code }; + }, + generateBundle: () => { + watchFiles = []; }, }; }; diff --git a/test/rollup.config.js b/test/rollup.config.js index a0b13d7..84763f6 100644 --- a/test/rollup.config.js +++ b/test/rollup.config.js @@ -13,7 +13,7 @@ export default { file: `globbed.ts`, importStar: true, include: [`./fixture/**`], - exclude: [`./**/*ignore*.ts`], + exclude: [`./**/*ignore*.ts`, `./**/node_modules/**`], }, ]), typescript({