Skip to content

Commit

Permalink
Fix the watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Elijah committed Nov 14, 2019
1 parent 50ed2eb commit 7d4e749
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
dist
typings
**globbed**
127 changes: 32 additions & 95 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down
12 changes: 4 additions & 8 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -28,7 +25,6 @@ export default {
preferBuiltins: true,
}),
commonjs(),
!prod && command(`cd test && rollup -c`, { exitOnFail: !watching }),
typescript({
typescript: require('typescript'),
}),
Expand Down
71 changes: 47 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = [];
},
};
};
2 changes: 1 addition & 1 deletion test/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default {
file: `globbed.ts`,
importStar: true,
include: [`./fixture/**`],
exclude: [`./**/*ignore*.ts`],
exclude: [`./**/*ignore*.ts`, `./**/node_modules/**`],
},
]),
typescript({
Expand Down

0 comments on commit 7d4e749

Please sign in to comment.